fuzzer_context.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. // Limits to help control the overall fuzzing process and rein in individual
  20. // fuzzer passes.
  21. const uint32_t kIdBoundLimit = 50000;
  22. const uint32_t kTransformationLimit = 2000;
  23. // Default <minimum, maximum> pairs of probabilities for applying various
  24. // transformations. All values are percentages. Keep them in alphabetical order.
  25. const std::pair<uint32_t, uint32_t>
  26. kChanceOfAcceptingRepeatedPassRecommendation = {50, 80};
  27. const std::pair<uint32_t, uint32_t> kChanceOfAddingAccessChain = {5, 50};
  28. const std::pair<uint32_t, uint32_t> kChanceOfAddingAnotherPassToPassLoop = {50,
  29. 90};
  30. const std::pair<uint32_t, uint32_t> kChanceOfAddingAnotherStructField = {20,
  31. 90};
  32. const std::pair<uint32_t, uint32_t> kChanceOfAddingArrayOrStructType = {20, 90};
  33. const std::pair<uint32_t, uint32_t> kChanceOfAddingBitInstructionSynonym = {5,
  34. 20};
  35. const std::pair<uint32_t, uint32_t>
  36. kChanceOfAddingBothBranchesWhenReplacingOpSelect = {40, 60};
  37. const std::pair<uint32_t, uint32_t> kChanceOfAddingCompositeExtract = {20, 50};
  38. const std::pair<uint32_t, uint32_t> kChanceOfAddingCompositeInsert = {20, 50};
  39. const std::pair<uint32_t, uint32_t> kChanceOfAddingCopyMemory = {20, 50};
  40. const std::pair<uint32_t, uint32_t> kChanceOfAddingDeadBlock = {20, 90};
  41. const std::pair<uint32_t, uint32_t> kChanceOfAddingDeadBreak = {5, 80};
  42. const std::pair<uint32_t, uint32_t> kChanceOfAddingDeadContinue = {5, 80};
  43. const std::pair<uint32_t, uint32_t> kChanceOfAddingEquationInstruction = {5,
  44. 90};
  45. const std::pair<uint32_t, uint32_t> kChanceOfAddingGlobalVariable = {20, 90};
  46. const std::pair<uint32_t, uint32_t> kChanceOfAddingImageSampleUnusedComponents =
  47. {20, 90};
  48. const std::pair<uint32_t, uint32_t> kChanceOfAddingLoad = {5, 50};
  49. const std::pair<uint32_t, uint32_t> kChanceOfAddingLocalVariable = {20, 90};
  50. const std::pair<uint32_t, uint32_t> kChanceOfAddingLoopPreheader = {20, 90};
  51. const std::pair<uint32_t, uint32_t> kChanceOfAddingMatrixType = {20, 70};
  52. const std::pair<uint32_t, uint32_t> kChanceOfAddingNoContractionDecoration = {
  53. 5, 70};
  54. const std::pair<uint32_t, uint32_t> kChanceOfAddingOpPhiSynonym = {5, 70};
  55. const std::pair<uint32_t, uint32_t> kChanceOfAddingParameters = {5, 70};
  56. const std::pair<uint32_t, uint32_t> kChanceOfAddingRelaxedDecoration = {20, 90};
  57. const std::pair<uint32_t, uint32_t> kChanceOfAddingStore = {5, 50};
  58. const std::pair<uint32_t, uint32_t> kChanceOfAddingSynonyms = {20, 50};
  59. const std::pair<uint32_t, uint32_t>
  60. kChanceOfAddingTrueBranchWhenReplacingOpSelect = {40, 60};
  61. const std::pair<uint32_t, uint32_t> kChanceOfAddingVectorType = {20, 70};
  62. const std::pair<uint32_t, uint32_t> kChanceOfAddingVectorShuffle = {20, 70};
  63. const std::pair<uint32_t, uint32_t> kChanceOfAdjustingBranchWeights = {20, 90};
  64. const std::pair<uint32_t, uint32_t> kChanceOfAdjustingFunctionControl = {20,
  65. 70};
  66. const std::pair<uint32_t, uint32_t> kChanceOfAdjustingLoopControl = {20, 90};
  67. const std::pair<uint32_t, uint32_t> kChanceOfAdjustingMemoryOperandsMask = {20,
  68. 90};
  69. const std::pair<uint32_t, uint32_t> kChanceOfAdjustingSelectionControl = {20,
  70. 90};
  71. const std::pair<uint32_t, uint32_t> kChanceOfCallingFunction = {1, 10};
  72. const std::pair<uint32_t, uint32_t> kChanceOfChoosingStructTypeVsArrayType = {
  73. 20, 80};
  74. const std::pair<uint32_t, uint32_t> kChanceOfChoosingWorkgroupStorageClass = {
  75. 50, 50};
  76. const std::pair<uint32_t, uint32_t> kChanceOfConstructingComposite = {20, 50};
  77. const std::pair<uint32_t, uint32_t> kChanceOfCopyingObject = {20, 50};
  78. const std::pair<uint32_t, uint32_t> kChanceOfCreatingIntSynonymsUsingLoops = {
  79. 5, 10};
  80. const std::pair<uint32_t, uint32_t> kChanceOfDonatingAdditionalModule = {5, 50};
  81. const std::pair<uint32_t, uint32_t> kChanceOfDuplicatingRegionWithSelection = {
  82. 20, 50};
  83. const std::pair<uint32_t, uint32_t> kChanceOfExpandingVectorReduction = {20,
  84. 90};
  85. const std::pair<uint32_t, uint32_t> kChanceOfFlatteningConditionalBranch = {45,
  86. 95};
  87. const std::pair<uint32_t, uint32_t> kChanceOfGoingDeeperToExtractComposite = {
  88. 30, 70};
  89. const std::pair<uint32_t, uint32_t> kChanceOfGoingDeeperToInsertInComposite = {
  90. 30, 70};
  91. const std::pair<uint32_t, uint32_t> kChanceOfGoingDeeperWhenMakingAccessChain =
  92. {50, 95};
  93. const std::pair<uint32_t, uint32_t>
  94. kChanceOfHavingTwoBlocksInLoopToCreateIntSynonym = {50, 80};
  95. const std::pair<uint32_t, uint32_t> kChanceOfInliningFunction = {10, 90};
  96. const std::pair<uint32_t, uint32_t> kChanceOfInterchangingZeroLikeConstants = {
  97. 10, 90};
  98. const std::pair<uint32_t, uint32_t>
  99. kChanceOfInterchangingSignednessOfIntegerOperands = {10, 90};
  100. const std::pair<uint32_t, uint32_t> kChanceOfInvertingComparisonOperators = {
  101. 20, 50};
  102. const std::pair<uint32_t, uint32_t> kChanceOfMakingDonorLivesafe = {40, 60};
  103. const std::pair<uint32_t, uint32_t> kChanceOfMakingVectorOperationDynamic = {
  104. 20, 90};
  105. const std::pair<uint32_t, uint32_t> kChanceOfMergingBlocks = {20, 95};
  106. const std::pair<uint32_t, uint32_t> kChanceOfMergingFunctionReturns = {20, 90};
  107. const std::pair<uint32_t, uint32_t> kChanceOfMovingBlockDown = {20, 50};
  108. const std::pair<uint32_t, uint32_t> kChanceOfMutatingPointer = {20, 90};
  109. const std::pair<uint32_t, uint32_t> kChanceOfObfuscatingConstant = {10, 90};
  110. const std::pair<uint32_t, uint32_t> kChanceOfOutliningFunction = {10, 90};
  111. const std::pair<uint32_t, uint32_t> kChanceOfPermutingInstructions = {20, 70};
  112. const std::pair<uint32_t, uint32_t> kChanceOfPermutingParameters = {30, 90};
  113. const std::pair<uint32_t, uint32_t> kChanceOfPermutingPhiOperands = {30, 90};
  114. const std::pair<uint32_t, uint32_t> kChanceOfPropagatingInstructionsDown = {20,
  115. 70};
  116. const std::pair<uint32_t, uint32_t> kChanceOfPropagatingInstructionsUp = {20,
  117. 70};
  118. const std::pair<uint32_t, uint32_t> kChanceOfPushingIdThroughVariable = {5, 50};
  119. const std::pair<uint32_t, uint32_t>
  120. kChanceOfReplacingAddSubMulWithCarryingExtended = {20, 70};
  121. const std::pair<uint32_t, uint32_t>
  122. kChanceOfReplacingBranchFromDeadBlockWithExit = {10, 65};
  123. const std::pair<uint32_t, uint32_t> kChanceOfReplacingCopyMemoryWithLoadStore =
  124. {20, 90};
  125. const std::pair<uint32_t, uint32_t> kChanceOfReplacingCopyObjectWithStoreLoad =
  126. {20, 90};
  127. const std::pair<uint32_t, uint32_t> kChanceOfReplacingIdWithSynonym = {10, 90};
  128. const std::pair<uint32_t, uint32_t> kChanceOfReplacingIrrelevantId = {35, 95};
  129. const std::pair<uint32_t, uint32_t>
  130. kChanceOfReplacingLinearAlgebraInstructions = {10, 90};
  131. const std::pair<uint32_t, uint32_t> kChanceOfReplacingLoadStoreWithCopyMemory =
  132. {20, 90};
  133. const std::pair<uint32_t, uint32_t>
  134. kChanceOfReplacingOpPhiIdFromDeadPredecessor = {20, 90};
  135. const std::pair<uint32_t, uint32_t>
  136. kChanceOfReplacingOpSelectWithConditionalBranch = {20, 90};
  137. const std::pair<uint32_t, uint32_t> kChanceOfReplacingParametersWithGlobals = {
  138. 30, 70};
  139. const std::pair<uint32_t, uint32_t> kChanceOfReplacingParametersWithStruct = {
  140. 20, 40};
  141. const std::pair<uint32_t, uint32_t> kChanceOfSplittingBlock = {40, 95};
  142. const std::pair<uint32_t, uint32_t> kChanceOfSwappingConditionalBranchOperands =
  143. {10, 70};
  144. const std::pair<uint32_t, uint32_t> kChanceOfTogglingAccessChainInstruction = {
  145. 20, 90};
  146. const std::pair<uint32_t, uint32_t> kChanceOfWrappingRegionInSelection = {70,
  147. 90};
  148. // Default limits for various quantities that are chosen during fuzzing.
  149. // Keep them in alphabetical order.
  150. const uint32_t kDefaultMaxEquivalenceClassSizeForDataSynonymFactClosure = 1000;
  151. const uint32_t kDefaultMaxLoopControlPartialCount = 100;
  152. const uint32_t kDefaultMaxLoopControlPeelCount = 100;
  153. const uint32_t kDefaultMaxLoopLimit = 20;
  154. const uint32_t kDefaultMaxNewArraySizeLimit = 100;
  155. // TODO(https://github.com/KhronosGroup/SPIRV-Tools/issues/3424):
  156. // think whether there is a better limit on the maximum number of parameters.
  157. const uint32_t kDefaultMaxNumberOfFunctionParameters = 128;
  158. const uint32_t kDefaultMaxNumberOfNewParameters = 15;
  159. const uint32_t kGetDefaultMaxNumberOfParametersReplacedWithStruct = 5;
  160. // Default functions for controlling how deep to go during recursive
  161. // generation/transformation. Keep them in alphabetical order.
  162. const std::function<bool(uint32_t, RandomGenerator*)>
  163. kDefaultGoDeeperInConstantObfuscation =
  164. [](uint32_t current_depth, RandomGenerator* random_generator) -> bool {
  165. double chance = 1.0 / std::pow(3.0, static_cast<float>(current_depth + 1));
  166. return random_generator->RandomDouble() < chance;
  167. };
  168. } // namespace
  169. FuzzerContext::FuzzerContext(RandomGenerator* random_generator,
  170. uint32_t min_fresh_id)
  171. : random_generator_(random_generator),
  172. next_fresh_id_(min_fresh_id),
  173. max_equivalence_class_size_for_data_synonym_fact_closure_(
  174. kDefaultMaxEquivalenceClassSizeForDataSynonymFactClosure),
  175. max_loop_control_partial_count_(kDefaultMaxLoopControlPartialCount),
  176. max_loop_control_peel_count_(kDefaultMaxLoopControlPeelCount),
  177. max_loop_limit_(kDefaultMaxLoopLimit),
  178. max_new_array_size_limit_(kDefaultMaxNewArraySizeLimit),
  179. max_number_of_function_parameters_(kDefaultMaxNumberOfFunctionParameters),
  180. max_number_of_new_parameters_(kDefaultMaxNumberOfNewParameters),
  181. max_number_of_parameters_replaced_with_struct_(
  182. kGetDefaultMaxNumberOfParametersReplacedWithStruct),
  183. go_deeper_in_constant_obfuscation_(
  184. kDefaultGoDeeperInConstantObfuscation) {
  185. chance_of_accepting_repeated_pass_recommendation_ =
  186. ChooseBetweenMinAndMax(kChanceOfAcceptingRepeatedPassRecommendation);
  187. chance_of_adding_access_chain_ =
  188. ChooseBetweenMinAndMax(kChanceOfAddingAccessChain);
  189. chance_of_adding_another_pass_to_pass_loop_ =
  190. ChooseBetweenMinAndMax(kChanceOfAddingAnotherPassToPassLoop);
  191. chance_of_adding_another_struct_field_ =
  192. ChooseBetweenMinAndMax(kChanceOfAddingAnotherStructField);
  193. chance_of_adding_array_or_struct_type_ =
  194. ChooseBetweenMinAndMax(kChanceOfAddingArrayOrStructType);
  195. chance_of_adding_bit_instruction_synonym_ =
  196. ChooseBetweenMinAndMax(kChanceOfAddingBitInstructionSynonym);
  197. chance_of_adding_both_branches_when_replacing_opselect_ =
  198. ChooseBetweenMinAndMax(kChanceOfAddingBothBranchesWhenReplacingOpSelect);
  199. chance_of_adding_composite_extract_ =
  200. ChooseBetweenMinAndMax(kChanceOfAddingCompositeExtract);
  201. chance_of_adding_composite_insert_ =
  202. ChooseBetweenMinAndMax(kChanceOfAddingCompositeInsert);
  203. chance_of_adding_copy_memory_ =
  204. ChooseBetweenMinAndMax(kChanceOfAddingCopyMemory);
  205. chance_of_adding_dead_block_ =
  206. ChooseBetweenMinAndMax(kChanceOfAddingDeadBlock);
  207. chance_of_adding_dead_break_ =
  208. ChooseBetweenMinAndMax(kChanceOfAddingDeadBreak);
  209. chance_of_adding_dead_continue_ =
  210. ChooseBetweenMinAndMax(kChanceOfAddingDeadContinue);
  211. chance_of_adding_equation_instruction_ =
  212. ChooseBetweenMinAndMax(kChanceOfAddingEquationInstruction);
  213. chance_of_adding_global_variable_ =
  214. ChooseBetweenMinAndMax(kChanceOfAddingGlobalVariable);
  215. chance_of_adding_load_ = ChooseBetweenMinAndMax(kChanceOfAddingLoad);
  216. chance_of_adding_loop_preheader_ =
  217. ChooseBetweenMinAndMax(kChanceOfAddingLoopPreheader);
  218. chance_of_adding_image_sample_unused_components_ =
  219. ChooseBetweenMinAndMax(kChanceOfAddingImageSampleUnusedComponents);
  220. chance_of_adding_local_variable_ =
  221. ChooseBetweenMinAndMax(kChanceOfAddingLocalVariable);
  222. chance_of_adding_matrix_type_ =
  223. ChooseBetweenMinAndMax(kChanceOfAddingMatrixType);
  224. chance_of_adding_no_contraction_decoration_ =
  225. ChooseBetweenMinAndMax(kChanceOfAddingNoContractionDecoration);
  226. chance_of_adding_opphi_synonym_ =
  227. ChooseBetweenMinAndMax(kChanceOfAddingOpPhiSynonym);
  228. chance_of_adding_parameters =
  229. ChooseBetweenMinAndMax(kChanceOfAddingParameters);
  230. chance_of_adding_relaxed_decoration_ =
  231. ChooseBetweenMinAndMax(kChanceOfAddingRelaxedDecoration);
  232. chance_of_adding_store_ = ChooseBetweenMinAndMax(kChanceOfAddingStore);
  233. chance_of_adding_true_branch_when_replacing_opselect_ =
  234. ChooseBetweenMinAndMax(kChanceOfAddingTrueBranchWhenReplacingOpSelect);
  235. chance_of_adding_vector_shuffle_ =
  236. ChooseBetweenMinAndMax(kChanceOfAddingVectorShuffle);
  237. chance_of_adding_vector_type_ =
  238. ChooseBetweenMinAndMax(kChanceOfAddingVectorType);
  239. chance_of_adjusting_branch_weights_ =
  240. ChooseBetweenMinAndMax(kChanceOfAdjustingBranchWeights);
  241. chance_of_adjusting_function_control_ =
  242. ChooseBetweenMinAndMax(kChanceOfAdjustingFunctionControl);
  243. chance_of_adding_synonyms_ = ChooseBetweenMinAndMax(kChanceOfAddingSynonyms);
  244. chance_of_adjusting_loop_control_ =
  245. ChooseBetweenMinAndMax(kChanceOfAdjustingLoopControl);
  246. chance_of_adjusting_memory_operands_mask_ =
  247. ChooseBetweenMinAndMax(kChanceOfAdjustingMemoryOperandsMask);
  248. chance_of_adjusting_selection_control_ =
  249. ChooseBetweenMinAndMax(kChanceOfAdjustingSelectionControl);
  250. chance_of_calling_function_ =
  251. ChooseBetweenMinAndMax(kChanceOfCallingFunction);
  252. chance_of_choosing_struct_type_vs_array_type_ =
  253. ChooseBetweenMinAndMax(kChanceOfChoosingStructTypeVsArrayType);
  254. chance_of_choosing_workgroup_storage_class_ =
  255. ChooseBetweenMinAndMax(kChanceOfChoosingWorkgroupStorageClass);
  256. chance_of_constructing_composite_ =
  257. ChooseBetweenMinAndMax(kChanceOfConstructingComposite);
  258. chance_of_copying_object_ = ChooseBetweenMinAndMax(kChanceOfCopyingObject);
  259. chance_of_creating_int_synonyms_using_loops_ =
  260. ChooseBetweenMinAndMax(kChanceOfCreatingIntSynonymsUsingLoops);
  261. chance_of_donating_additional_module_ =
  262. ChooseBetweenMinAndMax(kChanceOfDonatingAdditionalModule);
  263. chance_of_duplicating_region_with_selection_ =
  264. ChooseBetweenMinAndMax(kChanceOfDuplicatingRegionWithSelection);
  265. chance_of_expanding_vector_reduction_ =
  266. ChooseBetweenMinAndMax(kChanceOfExpandingVectorReduction);
  267. chance_of_flattening_conditional_branch_ =
  268. ChooseBetweenMinAndMax(kChanceOfFlatteningConditionalBranch);
  269. chance_of_going_deeper_to_extract_composite_ =
  270. ChooseBetweenMinAndMax(kChanceOfGoingDeeperToExtractComposite);
  271. chance_of_going_deeper_to_insert_in_composite_ =
  272. ChooseBetweenMinAndMax(kChanceOfGoingDeeperToInsertInComposite);
  273. chance_of_going_deeper_when_making_access_chain_ =
  274. ChooseBetweenMinAndMax(kChanceOfGoingDeeperWhenMakingAccessChain);
  275. chance_of_having_two_blocks_in_loop_to_create_int_synonym_ =
  276. ChooseBetweenMinAndMax(kChanceOfHavingTwoBlocksInLoopToCreateIntSynonym);
  277. chance_of_inlining_function_ =
  278. ChooseBetweenMinAndMax(kChanceOfInliningFunction);
  279. chance_of_interchanging_signedness_of_integer_operands_ =
  280. ChooseBetweenMinAndMax(kChanceOfInterchangingSignednessOfIntegerOperands);
  281. chance_of_interchanging_zero_like_constants_ =
  282. ChooseBetweenMinAndMax(kChanceOfInterchangingZeroLikeConstants);
  283. chance_of_inverting_comparison_operators_ =
  284. ChooseBetweenMinAndMax(kChanceOfInvertingComparisonOperators);
  285. chance_of_making_donor_livesafe_ =
  286. ChooseBetweenMinAndMax(kChanceOfMakingDonorLivesafe);
  287. chance_of_making_vector_operation_dynamic_ =
  288. ChooseBetweenMinAndMax(kChanceOfMakingVectorOperationDynamic);
  289. chance_of_merging_blocks_ = ChooseBetweenMinAndMax(kChanceOfMergingBlocks);
  290. chance_of_merging_function_returns_ =
  291. ChooseBetweenMinAndMax(kChanceOfMergingFunctionReturns);
  292. chance_of_moving_block_down_ =
  293. ChooseBetweenMinAndMax(kChanceOfMovingBlockDown);
  294. chance_of_mutating_pointer_ =
  295. ChooseBetweenMinAndMax(kChanceOfMutatingPointer);
  296. chance_of_obfuscating_constant_ =
  297. ChooseBetweenMinAndMax(kChanceOfObfuscatingConstant);
  298. chance_of_outlining_function_ =
  299. ChooseBetweenMinAndMax(kChanceOfOutliningFunction);
  300. chance_of_permuting_instructions_ =
  301. ChooseBetweenMinAndMax(kChanceOfPermutingInstructions);
  302. chance_of_permuting_parameters_ =
  303. ChooseBetweenMinAndMax(kChanceOfPermutingParameters);
  304. chance_of_permuting_phi_operands_ =
  305. ChooseBetweenMinAndMax(kChanceOfPermutingPhiOperands);
  306. chance_of_propagating_instructions_down_ =
  307. ChooseBetweenMinAndMax(kChanceOfPropagatingInstructionsDown);
  308. chance_of_propagating_instructions_up_ =
  309. ChooseBetweenMinAndMax(kChanceOfPropagatingInstructionsUp);
  310. chance_of_pushing_id_through_variable_ =
  311. ChooseBetweenMinAndMax(kChanceOfPushingIdThroughVariable);
  312. chance_of_replacing_add_sub_mul_with_carrying_extended_ =
  313. ChooseBetweenMinAndMax(kChanceOfReplacingAddSubMulWithCarryingExtended);
  314. chance_of_replacing_branch_from_dead_block_with_exit_ =
  315. ChooseBetweenMinAndMax(kChanceOfReplacingBranchFromDeadBlockWithExit);
  316. chance_of_replacing_copy_memory_with_load_store_ =
  317. ChooseBetweenMinAndMax(kChanceOfReplacingCopyMemoryWithLoadStore);
  318. chance_of_replacing_copyobject_with_store_load_ =
  319. ChooseBetweenMinAndMax(kChanceOfReplacingCopyObjectWithStoreLoad);
  320. chance_of_replacing_id_with_synonym_ =
  321. ChooseBetweenMinAndMax(kChanceOfReplacingIdWithSynonym);
  322. chance_of_replacing_irrelevant_id_ =
  323. ChooseBetweenMinAndMax(kChanceOfReplacingIrrelevantId);
  324. chance_of_replacing_linear_algebra_instructions_ =
  325. ChooseBetweenMinAndMax(kChanceOfReplacingLinearAlgebraInstructions);
  326. chance_of_replacing_load_store_with_copy_memory_ =
  327. ChooseBetweenMinAndMax(kChanceOfReplacingLoadStoreWithCopyMemory);
  328. chance_of_replacing_opphi_id_from_dead_predecessor_ =
  329. ChooseBetweenMinAndMax(kChanceOfReplacingOpPhiIdFromDeadPredecessor);
  330. chance_of_replacing_opselect_with_conditional_branch_ =
  331. ChooseBetweenMinAndMax(kChanceOfReplacingOpSelectWithConditionalBranch);
  332. chance_of_replacing_parameters_with_globals_ =
  333. ChooseBetweenMinAndMax(kChanceOfReplacingParametersWithGlobals);
  334. chance_of_replacing_parameters_with_struct_ =
  335. ChooseBetweenMinAndMax(kChanceOfReplacingParametersWithStruct);
  336. chance_of_splitting_block_ = ChooseBetweenMinAndMax(kChanceOfSplittingBlock);
  337. chance_of_swapping_conditional_branch_operands_ =
  338. ChooseBetweenMinAndMax(kChanceOfSwappingConditionalBranchOperands);
  339. chance_of_toggling_access_chain_instruction_ =
  340. ChooseBetweenMinAndMax(kChanceOfTogglingAccessChainInstruction);
  341. chance_of_wrapping_region_in_selection_ =
  342. ChooseBetweenMinAndMax(kChanceOfWrappingRegionInSelection);
  343. }
  344. FuzzerContext::~FuzzerContext() = default;
  345. uint32_t FuzzerContext::GetFreshId() { return next_fresh_id_++; }
  346. std::vector<uint32_t> FuzzerContext::GetFreshIds(const uint32_t count) {
  347. std::vector<uint32_t> fresh_ids(count);
  348. for (uint32_t& fresh_id : fresh_ids) {
  349. fresh_id = next_fresh_id_++;
  350. }
  351. return fresh_ids;
  352. }
  353. bool FuzzerContext::ChooseEven() { return random_generator_->RandomBool(); }
  354. bool FuzzerContext::ChoosePercentage(uint32_t percentage_chance) {
  355. assert(percentage_chance <= 100);
  356. return random_generator_->RandomPercentage() < percentage_chance;
  357. }
  358. uint32_t FuzzerContext::ChooseBetweenMinAndMax(
  359. const std::pair<uint32_t, uint32_t>& min_max) {
  360. assert(min_max.first <= min_max.second);
  361. return min_max.first +
  362. random_generator_->RandomUint32(min_max.second - min_max.first + 1);
  363. }
  364. protobufs::TransformationAddSynonym::SynonymType
  365. FuzzerContext::GetRandomSynonymType() {
  366. // value_count method is guaranteed to return a value greater than 0.
  367. auto result_index = ChooseBetweenMinAndMax(
  368. {0, static_cast<uint32_t>(
  369. protobufs::TransformationAddSynonym::SynonymType_descriptor()
  370. ->value_count() -
  371. 1)});
  372. auto result = protobufs::TransformationAddSynonym::SynonymType_descriptor()
  373. ->value(result_index)
  374. ->number();
  375. assert(protobufs::TransformationAddSynonym::SynonymType_IsValid(result) &&
  376. "|result| is not a value of SynonymType");
  377. return static_cast<protobufs::TransformationAddSynonym::SynonymType>(result);
  378. }
  379. uint32_t FuzzerContext::GetIdBoundLimit() const { return kIdBoundLimit; }
  380. uint32_t FuzzerContext::GetTransformationLimit() const {
  381. return kTransformationLimit;
  382. }
  383. } // namespace fuzz
  384. } // namespace spvtools