fuzzer_context.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. // Copyright (c) 2019 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "source/fuzz/fuzzer_context.h"
  15. #include <cmath>
  16. namespace spvtools {
  17. namespace fuzz {
  18. namespace {
  19. // Default <minimum, maximum> pairs of probabilities for applying various
  20. // transformations. All values are percentages. Keep them in alphabetical order.
  21. const std::pair<uint32_t, uint32_t> kChanceOfAddingAccessChain = {5, 50};
  22. const std::pair<uint32_t, uint32_t> kChanceOfAddingAnotherStructField = {20,
  23. 90};
  24. const std::pair<uint32_t, uint32_t> kChanceOfAddingArrayOrStructType = {20, 90};
  25. const std::pair<uint32_t, uint32_t> kChanceOfAddingCopyMemory = {20, 50};
  26. const std::pair<uint32_t, uint32_t> kChanceOfAddingDeadBlock = {20, 90};
  27. const std::pair<uint32_t, uint32_t> kChanceOfAddingDeadBreak = {5, 80};
  28. const std::pair<uint32_t, uint32_t> kChanceOfAddingDeadContinue = {5, 80};
  29. const std::pair<uint32_t, uint32_t> kChanceOfAddingEquationInstruction = {5,
  30. 90};
  31. const std::pair<uint32_t, uint32_t> kChanceOfAddingGlobalVariable = {20, 90};
  32. const std::pair<uint32_t, uint32_t> kChanceOfAddingImageSampleUnusedComponents =
  33. {20, 90};
  34. const std::pair<uint32_t, uint32_t> kChanceOfAddingLoad = {5, 50};
  35. const std::pair<uint32_t, uint32_t> kChanceOfAddingLocalVariable = {20, 90};
  36. const std::pair<uint32_t, uint32_t> kChanceOfAddingLoopPreheader = {20, 90};
  37. const std::pair<uint32_t, uint32_t> kChanceOfAddingMatrixType = {20, 70};
  38. const std::pair<uint32_t, uint32_t> kChanceOfAddingNoContractionDecoration = {
  39. 5, 70};
  40. const std::pair<uint32_t, uint32_t> kChanceOfAddingParameters = {5, 70};
  41. const std::pair<uint32_t, uint32_t> kChanceOfAddingRelaxedDecoration = {20, 90};
  42. const std::pair<uint32_t, uint32_t> kChanceOfAddingStore = {5, 50};
  43. const std::pair<uint32_t, uint32_t> kChanceOfAddingSynonyms = {20, 50};
  44. const std::pair<uint32_t, uint32_t> kChanceOfAddingVectorType = {20, 70};
  45. const std::pair<uint32_t, uint32_t> kChanceOfAddingVectorShuffle = {20, 70};
  46. const std::pair<uint32_t, uint32_t> kChanceOfAdjustingBranchWeights = {20, 90};
  47. const std::pair<uint32_t, uint32_t> kChanceOfAdjustingFunctionControl = {20,
  48. 70};
  49. const std::pair<uint32_t, uint32_t> kChanceOfAdjustingLoopControl = {20, 90};
  50. const std::pair<uint32_t, uint32_t> kChanceOfAdjustingMemoryOperandsMask = {20,
  51. 90};
  52. const std::pair<uint32_t, uint32_t> kChanceOfAdjustingSelectionControl = {20,
  53. 90};
  54. const std::pair<uint32_t, uint32_t> kChanceOfCallingFunction = {1, 10};
  55. const std::pair<uint32_t, uint32_t> kChanceOfChoosingStructTypeVsArrayType = {
  56. 20, 80};
  57. const std::pair<uint32_t, uint32_t> kChanceOfChoosingWorkgroupStorageClass = {
  58. 50, 50};
  59. const std::pair<uint32_t, uint32_t> kChanceOfConstructingComposite = {20, 50};
  60. const std::pair<uint32_t, uint32_t> kChanceOfCopyingObject = {20, 50};
  61. const std::pair<uint32_t, uint32_t> kChanceOfDonatingAdditionalModule = {5, 50};
  62. const std::pair<uint32_t, uint32_t> kChanceOfGoingDeeperWhenMakingAccessChain =
  63. {50, 95};
  64. const std::pair<uint32_t, uint32_t> kChanceOfInterchangingZeroLikeConstants = {
  65. 10, 90};
  66. const std::pair<uint32_t, uint32_t>
  67. kChanceOfInterchangingSignednessOfIntegerOperands = {10, 90};
  68. const std::pair<uint32_t, uint32_t> kChanceOfInvertingComparisonOperators = {
  69. 20, 50};
  70. const std::pair<uint32_t, uint32_t> kChanceOfMakingDonorLivesafe = {40, 60};
  71. const std::pair<uint32_t, uint32_t> kChanceOfMakingVectorOperationDynamic = {
  72. 20, 90};
  73. const std::pair<uint32_t, uint32_t> kChanceOfMergingBlocks = {20, 95};
  74. const std::pair<uint32_t, uint32_t> kChanceOfMovingBlockDown = {20, 50};
  75. const std::pair<uint32_t, uint32_t> kChanceOfObfuscatingConstant = {10, 90};
  76. const std::pair<uint32_t, uint32_t> kChanceOfOutliningFunction = {10, 90};
  77. const std::pair<uint32_t, uint32_t> kChanceOfPermutingInstructions = {20, 70};
  78. const std::pair<uint32_t, uint32_t> kChanceOfPermutingParameters = {30, 90};
  79. const std::pair<uint32_t, uint32_t> kChanceOfPermutingPhiOperands = {30, 90};
  80. const std::pair<uint32_t, uint32_t> kChanceOfPropagatingInstructionsUp = {20,
  81. 70};
  82. const std::pair<uint32_t, uint32_t> kChanceOfPushingIdThroughVariable = {5, 50};
  83. const std::pair<uint32_t, uint32_t>
  84. kChanceOfReplacingAddSubMulWithCarryingExtended = {20, 90};
  85. const std::pair<uint32_t, uint32_t> kChanceOfReplacingCopyMemoryWithLoadStore =
  86. {20, 90};
  87. const std::pair<uint32_t, uint32_t> kChanceOfReplacingCopyObjectWithStoreLoad =
  88. {20, 90};
  89. const std::pair<uint32_t, uint32_t> kChanceOfReplacingIdWithSynonym = {10, 90};
  90. const std::pair<uint32_t, uint32_t>
  91. kChanceOfReplacingLinearAlgebraInstructions = {10, 90};
  92. const std::pair<uint32_t, uint32_t> kChanceOfReplacingLoadStoreWithCopyMemory =
  93. {20, 90};
  94. const std::pair<uint32_t, uint32_t> kChanceOfReplacingParametersWithGlobals = {
  95. 30, 70};
  96. const std::pair<uint32_t, uint32_t> kChanceOfReplacingParametersWithStruct = {
  97. 20, 40};
  98. const std::pair<uint32_t, uint32_t> kChanceOfSplittingBlock = {40, 95};
  99. const std::pair<uint32_t, uint32_t> kChanceOfSwappingConditionalBranchOperands =
  100. {10, 70};
  101. const std::pair<uint32_t, uint32_t> kChanceOfTogglingAccessChainInstruction = {
  102. 20, 90};
  103. // Default limits for various quantities that are chosen during fuzzing.
  104. // Keep them in alphabetical order.
  105. const uint32_t kDefaultMaxEquivalenceClassSizeForDataSynonymFactClosure = 1000;
  106. const uint32_t kDefaultMaxLoopControlPartialCount = 100;
  107. const uint32_t kDefaultMaxLoopControlPeelCount = 100;
  108. const uint32_t kDefaultMaxLoopLimit = 20;
  109. const uint32_t kDefaultMaxNewArraySizeLimit = 100;
  110. // TODO(https://github.com/KhronosGroup/SPIRV-Tools/issues/3424):
  111. // think whether there is a better limit on the maximum number of parameters.
  112. const uint32_t kDefaultMaxNumberOfFunctionParameters = 128;
  113. const uint32_t kDefaultMaxNumberOfNewParameters = 15;
  114. const uint32_t kGetDefaultMaxNumberOfParametersReplacedWithStruct = 5;
  115. // Default functions for controlling how deep to go during recursive
  116. // generation/transformation. Keep them in alphabetical order.
  117. const std::function<bool(uint32_t, RandomGenerator*)>
  118. kDefaultGoDeeperInConstantObfuscation =
  119. [](uint32_t current_depth, RandomGenerator* random_generator) -> bool {
  120. double chance = 1.0 / std::pow(3.0, static_cast<float>(current_depth + 1));
  121. return random_generator->RandomDouble() < chance;
  122. };
  123. } // namespace
  124. FuzzerContext::FuzzerContext(RandomGenerator* random_generator,
  125. uint32_t min_fresh_id)
  126. : random_generator_(random_generator),
  127. next_fresh_id_(min_fresh_id),
  128. max_equivalence_class_size_for_data_synonym_fact_closure_(
  129. kDefaultMaxEquivalenceClassSizeForDataSynonymFactClosure),
  130. max_loop_control_partial_count_(kDefaultMaxLoopControlPartialCount),
  131. max_loop_control_peel_count_(kDefaultMaxLoopControlPeelCount),
  132. max_loop_limit_(kDefaultMaxLoopLimit),
  133. max_new_array_size_limit_(kDefaultMaxNewArraySizeLimit),
  134. max_number_of_function_parameters_(kDefaultMaxNumberOfFunctionParameters),
  135. max_number_of_new_parameters_(kDefaultMaxNumberOfNewParameters),
  136. max_number_of_parameters_replaced_with_struct_(
  137. kGetDefaultMaxNumberOfParametersReplacedWithStruct),
  138. go_deeper_in_constant_obfuscation_(
  139. kDefaultGoDeeperInConstantObfuscation) {
  140. chance_of_adding_access_chain_ =
  141. ChooseBetweenMinAndMax(kChanceOfAddingAccessChain);
  142. chance_of_adding_another_struct_field_ =
  143. ChooseBetweenMinAndMax(kChanceOfAddingAnotherStructField);
  144. chance_of_adding_array_or_struct_type_ =
  145. ChooseBetweenMinAndMax(kChanceOfAddingArrayOrStructType);
  146. chance_of_adding_copy_memory_ =
  147. ChooseBetweenMinAndMax(kChanceOfAddingCopyMemory);
  148. chance_of_adding_dead_block_ =
  149. ChooseBetweenMinAndMax(kChanceOfAddingDeadBlock);
  150. chance_of_adding_dead_break_ =
  151. ChooseBetweenMinAndMax(kChanceOfAddingDeadBreak);
  152. chance_of_adding_dead_continue_ =
  153. ChooseBetweenMinAndMax(kChanceOfAddingDeadContinue);
  154. chance_of_adding_equation_instruction_ =
  155. ChooseBetweenMinAndMax(kChanceOfAddingEquationInstruction);
  156. chance_of_adding_global_variable_ =
  157. ChooseBetweenMinAndMax(kChanceOfAddingGlobalVariable);
  158. chance_of_adding_load_ = ChooseBetweenMinAndMax(kChanceOfAddingLoad);
  159. chance_of_adding_loop_preheader_ =
  160. ChooseBetweenMinAndMax(kChanceOfAddingLoopPreheader);
  161. chance_of_adding_image_sample_unused_components_ =
  162. ChooseBetweenMinAndMax(kChanceOfAddingImageSampleUnusedComponents);
  163. chance_of_adding_local_variable_ =
  164. ChooseBetweenMinAndMax(kChanceOfAddingLocalVariable);
  165. chance_of_adding_matrix_type_ =
  166. ChooseBetweenMinAndMax(kChanceOfAddingMatrixType);
  167. chance_of_adding_no_contraction_decoration_ =
  168. ChooseBetweenMinAndMax(kChanceOfAddingNoContractionDecoration);
  169. chance_of_adding_parameters =
  170. ChooseBetweenMinAndMax(kChanceOfAddingParameters);
  171. chance_of_adding_relaxed_decoration_ =
  172. ChooseBetweenMinAndMax(kChanceOfAddingRelaxedDecoration);
  173. chance_of_adding_store_ = ChooseBetweenMinAndMax(kChanceOfAddingStore);
  174. chance_of_adding_vector_shuffle_ =
  175. ChooseBetweenMinAndMax(kChanceOfAddingVectorShuffle);
  176. chance_of_adding_vector_type_ =
  177. ChooseBetweenMinAndMax(kChanceOfAddingVectorType);
  178. chance_of_adjusting_branch_weights_ =
  179. ChooseBetweenMinAndMax(kChanceOfAdjustingBranchWeights);
  180. chance_of_adjusting_function_control_ =
  181. ChooseBetweenMinAndMax(kChanceOfAdjustingFunctionControl);
  182. chance_of_adding_synonyms_ = ChooseBetweenMinAndMax(kChanceOfAddingSynonyms);
  183. chance_of_adjusting_loop_control_ =
  184. ChooseBetweenMinAndMax(kChanceOfAdjustingLoopControl);
  185. chance_of_adjusting_memory_operands_mask_ =
  186. ChooseBetweenMinAndMax(kChanceOfAdjustingMemoryOperandsMask);
  187. chance_of_adjusting_selection_control_ =
  188. ChooseBetweenMinAndMax(kChanceOfAdjustingSelectionControl);
  189. chance_of_calling_function_ =
  190. ChooseBetweenMinAndMax(kChanceOfCallingFunction);
  191. chance_of_choosing_struct_type_vs_array_type_ =
  192. ChooseBetweenMinAndMax(kChanceOfChoosingStructTypeVsArrayType);
  193. chance_of_choosing_workgroup_storage_class_ =
  194. ChooseBetweenMinAndMax(kChanceOfChoosingWorkgroupStorageClass);
  195. chance_of_constructing_composite_ =
  196. ChooseBetweenMinAndMax(kChanceOfConstructingComposite);
  197. chance_of_copying_object_ = ChooseBetweenMinAndMax(kChanceOfCopyingObject);
  198. chance_of_donating_additional_module_ =
  199. ChooseBetweenMinAndMax(kChanceOfDonatingAdditionalModule);
  200. chance_of_going_deeper_when_making_access_chain_ =
  201. ChooseBetweenMinAndMax(kChanceOfGoingDeeperWhenMakingAccessChain);
  202. chance_of_interchanging_signedness_of_integer_operands_ =
  203. ChooseBetweenMinAndMax(kChanceOfInterchangingSignednessOfIntegerOperands);
  204. chance_of_interchanging_zero_like_constants_ =
  205. ChooseBetweenMinAndMax(kChanceOfInterchangingZeroLikeConstants);
  206. chance_of_inverting_comparison_operators_ =
  207. ChooseBetweenMinAndMax(kChanceOfInvertingComparisonOperators);
  208. chance_of_making_donor_livesafe_ =
  209. ChooseBetweenMinAndMax(kChanceOfMakingDonorLivesafe);
  210. chance_of_making_vector_operation_dynamic_ =
  211. ChooseBetweenMinAndMax(kChanceOfMakingVectorOperationDynamic);
  212. chance_of_merging_blocks_ = ChooseBetweenMinAndMax(kChanceOfMergingBlocks);
  213. chance_of_moving_block_down_ =
  214. ChooseBetweenMinAndMax(kChanceOfMovingBlockDown);
  215. chance_of_obfuscating_constant_ =
  216. ChooseBetweenMinAndMax(kChanceOfObfuscatingConstant);
  217. chance_of_outlining_function_ =
  218. ChooseBetweenMinAndMax(kChanceOfOutliningFunction);
  219. chance_of_permuting_instructions_ =
  220. ChooseBetweenMinAndMax(kChanceOfPermutingInstructions);
  221. chance_of_permuting_parameters_ =
  222. ChooseBetweenMinAndMax(kChanceOfPermutingParameters);
  223. chance_of_permuting_phi_operands_ =
  224. ChooseBetweenMinAndMax(kChanceOfPermutingPhiOperands);
  225. chance_of_propagating_instructions_up_ =
  226. ChooseBetweenMinAndMax(kChanceOfPropagatingInstructionsUp);
  227. chance_of_pushing_id_through_variable_ =
  228. ChooseBetweenMinAndMax(kChanceOfPushingIdThroughVariable);
  229. chance_of_replacing_add_sub_mul_with_carrying_extended_ =
  230. ChooseBetweenMinAndMax(kChanceOfReplacingAddSubMulWithCarryingExtended);
  231. chance_of_replacing_copy_memory_with_load_store_ =
  232. ChooseBetweenMinAndMax(kChanceOfReplacingCopyMemoryWithLoadStore);
  233. chance_of_replacing_copyobject_with_store_load_ =
  234. ChooseBetweenMinAndMax(kChanceOfReplacingCopyObjectWithStoreLoad);
  235. chance_of_replacing_id_with_synonym_ =
  236. ChooseBetweenMinAndMax(kChanceOfReplacingIdWithSynonym);
  237. chance_of_replacing_linear_algebra_instructions_ =
  238. ChooseBetweenMinAndMax(kChanceOfReplacingLinearAlgebraInstructions);
  239. chance_of_replacing_load_store_with_copy_memory_ =
  240. ChooseBetweenMinAndMax(kChanceOfReplacingLoadStoreWithCopyMemory);
  241. chance_of_replacing_parameters_with_globals_ =
  242. ChooseBetweenMinAndMax(kChanceOfReplacingParametersWithGlobals);
  243. chance_of_replacing_parameters_with_struct_ =
  244. ChooseBetweenMinAndMax(kChanceOfReplacingParametersWithStruct);
  245. chance_of_splitting_block_ = ChooseBetweenMinAndMax(kChanceOfSplittingBlock);
  246. chance_of_swapping_conditional_branch_operands_ =
  247. ChooseBetweenMinAndMax(kChanceOfSwappingConditionalBranchOperands);
  248. chance_of_toggling_access_chain_instruction_ =
  249. ChooseBetweenMinAndMax(kChanceOfTogglingAccessChainInstruction);
  250. }
  251. FuzzerContext::~FuzzerContext() = default;
  252. uint32_t FuzzerContext::GetFreshId() { return next_fresh_id_++; }
  253. std::vector<uint32_t> FuzzerContext::GetFreshIds(const uint32_t count) {
  254. std::vector<uint32_t> fresh_ids(count);
  255. for (uint32_t& fresh_id : fresh_ids) {
  256. fresh_id = next_fresh_id_++;
  257. }
  258. return fresh_ids;
  259. }
  260. bool FuzzerContext::ChooseEven() { return random_generator_->RandomBool(); }
  261. bool FuzzerContext::ChoosePercentage(uint32_t percentage_chance) {
  262. assert(percentage_chance <= 100);
  263. return random_generator_->RandomPercentage() < percentage_chance;
  264. }
  265. uint32_t FuzzerContext::ChooseBetweenMinAndMax(
  266. const std::pair<uint32_t, uint32_t>& min_max) {
  267. assert(min_max.first <= min_max.second);
  268. return min_max.first +
  269. random_generator_->RandomUint32(min_max.second - min_max.first + 1);
  270. }
  271. protobufs::TransformationAddSynonym::SynonymType
  272. FuzzerContext::GetRandomSynonymType() {
  273. // value_count method is guaranteed to return a value greater than 0.
  274. auto result_index = ChooseBetweenMinAndMax(
  275. {0, static_cast<uint32_t>(
  276. protobufs::TransformationAddSynonym::SynonymType_descriptor()
  277. ->value_count() -
  278. 1)});
  279. auto result = protobufs::TransformationAddSynonym::SynonymType_descriptor()
  280. ->value(result_index)
  281. ->number();
  282. assert(protobufs::TransformationAddSynonym::SynonymType_IsValid(result) &&
  283. "|result| is not a value of SynonymType");
  284. return static_cast<protobufs::TransformationAddSynonym::SynonymType>(result);
  285. }
  286. } // namespace fuzz
  287. } // namespace spvtools