fuzzer_context.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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> kChanceOfAddingMatrixType = {20, 70};
  37. const std::pair<uint32_t, uint32_t> kChanceOfAddingNoContractionDecoration = {
  38. 5, 70};
  39. const std::pair<uint32_t, uint32_t> kChanceOfAddingParameters = {5, 70};
  40. const std::pair<uint32_t, uint32_t> kChanceOfAddingStore = {5, 50};
  41. const std::pair<uint32_t, uint32_t> kChanceOfAddingVectorType = {20, 70};
  42. const std::pair<uint32_t, uint32_t> kChanceOfAddingVectorShuffle = {20, 70};
  43. const std::pair<uint32_t, uint32_t> kChanceOfAdjustingBranchWeights = {20, 90};
  44. const std::pair<uint32_t, uint32_t> kChanceOfAdjustingFunctionControl = {20,
  45. 70};
  46. const std::pair<uint32_t, uint32_t> kChanceOfAdjustingLoopControl = {20, 90};
  47. const std::pair<uint32_t, uint32_t> kChanceOfAdjustingMemoryOperandsMask = {20,
  48. 90};
  49. const std::pair<uint32_t, uint32_t> kChanceOfAdjustingSelectionControl = {20,
  50. 90};
  51. const std::pair<uint32_t, uint32_t> kChanceOfCallingFunction = {1, 10};
  52. const std::pair<uint32_t, uint32_t> kChanceOfChoosingStructTypeVsArrayType = {
  53. 20, 80};
  54. const std::pair<uint32_t, uint32_t> kChanceOfChoosingWorkgroupStorageClass = {
  55. 50, 50};
  56. const std::pair<uint32_t, uint32_t> kChanceOfConstructingComposite = {20, 50};
  57. const std::pair<uint32_t, uint32_t> kChanceOfCopyingObject = {20, 50};
  58. const std::pair<uint32_t, uint32_t> kChanceOfDonatingAdditionalModule = {5, 50};
  59. const std::pair<uint32_t, uint32_t> kChanceOfGoingDeeperWhenMakingAccessChain =
  60. {50, 95};
  61. const std::pair<uint32_t, uint32_t> kChanceOfInvertingComparisonOperators = {
  62. 20, 50};
  63. const std::pair<uint32_t, uint32_t> kChanceOfMakingDonorLivesafe = {40, 60};
  64. const std::pair<uint32_t, uint32_t> kChanceOfMergingBlocks = {20, 95};
  65. const std::pair<uint32_t, uint32_t> kChanceOfMovingBlockDown = {20, 50};
  66. const std::pair<uint32_t, uint32_t> kChanceOfObfuscatingConstant = {10, 90};
  67. const std::pair<uint32_t, uint32_t> kChanceOfOutliningFunction = {10, 90};
  68. const std::pair<uint32_t, uint32_t> kChanceOfPermutingParameters = {30, 90};
  69. const std::pair<uint32_t, uint32_t> kChanceOfPermutingPhiOperands = {30, 90};
  70. const std::pair<uint32_t, uint32_t> kChanceOfPushingIdThroughVariable = {5, 50};
  71. const std::pair<uint32_t, uint32_t> kChanceOfReplacingIdWithSynonym = {10, 90};
  72. const std::pair<uint32_t, uint32_t>
  73. kChanceOfReplacingLinearAlgebraInstructions = {10, 90};
  74. const std::pair<uint32_t, uint32_t> kChanceOfReplacingParametersWithGlobals = {
  75. 30, 70};
  76. const std::pair<uint32_t, uint32_t> kChanceOfSplittingBlock = {40, 95};
  77. const std::pair<uint32_t, uint32_t> kChanceOfSwappingConditionalBranchOperands =
  78. {10, 70};
  79. const std::pair<uint32_t, uint32_t> kChanceOfTogglingAccessChainInstruction = {
  80. 20, 90};
  81. // Default limits for various quantities that are chosen during fuzzing.
  82. // Keep them in alphabetical order.
  83. const uint32_t kDefaultMaxEquivalenceClassSizeForDataSynonymFactClosure = 1000;
  84. const uint32_t kDefaultMaxLoopControlPartialCount = 100;
  85. const uint32_t kDefaultMaxLoopControlPeelCount = 100;
  86. const uint32_t kDefaultMaxLoopLimit = 20;
  87. const uint32_t kDefaultMaxNewArraySizeLimit = 100;
  88. // TODO(https://github.com/KhronosGroup/SPIRV-Tools/issues/3424):
  89. // think whether there is a better limit on the maximum number of parameters.
  90. const uint32_t kDefaultMaxNumberOfFunctionParameters = 128;
  91. const uint32_t kDefaultMaxNumberOfNewParameters = 15;
  92. // Default functions for controlling how deep to go during recursive
  93. // generation/transformation. Keep them in alphabetical order.
  94. const std::function<bool(uint32_t, RandomGenerator*)>
  95. kDefaultGoDeeperInConstantObfuscation =
  96. [](uint32_t current_depth, RandomGenerator* random_generator) -> bool {
  97. double chance = 1.0 / std::pow(3.0, static_cast<float>(current_depth + 1));
  98. return random_generator->RandomDouble() < chance;
  99. };
  100. } // namespace
  101. FuzzerContext::FuzzerContext(RandomGenerator* random_generator,
  102. uint32_t min_fresh_id)
  103. : random_generator_(random_generator),
  104. next_fresh_id_(min_fresh_id),
  105. max_equivalence_class_size_for_data_synonym_fact_closure_(
  106. kDefaultMaxEquivalenceClassSizeForDataSynonymFactClosure),
  107. max_loop_control_partial_count_(kDefaultMaxLoopControlPartialCount),
  108. max_loop_control_peel_count_(kDefaultMaxLoopControlPeelCount),
  109. max_loop_limit_(kDefaultMaxLoopLimit),
  110. max_new_array_size_limit_(kDefaultMaxNewArraySizeLimit),
  111. max_number_of_function_parameters_(kDefaultMaxNumberOfFunctionParameters),
  112. max_number_of_new_parameters_(kDefaultMaxNumberOfNewParameters),
  113. go_deeper_in_constant_obfuscation_(
  114. kDefaultGoDeeperInConstantObfuscation) {
  115. chance_of_adding_access_chain_ =
  116. ChooseBetweenMinAndMax(kChanceOfAddingAccessChain);
  117. chance_of_adding_another_struct_field_ =
  118. ChooseBetweenMinAndMax(kChanceOfAddingAnotherStructField);
  119. chance_of_adding_array_or_struct_type_ =
  120. ChooseBetweenMinAndMax(kChanceOfAddingArrayOrStructType);
  121. chance_of_adding_copy_memory_ =
  122. ChooseBetweenMinAndMax(kChanceOfAddingCopyMemory);
  123. chance_of_adding_dead_block_ =
  124. ChooseBetweenMinAndMax(kChanceOfAddingDeadBlock);
  125. chance_of_adding_dead_break_ =
  126. ChooseBetweenMinAndMax(kChanceOfAddingDeadBreak);
  127. chance_of_adding_dead_continue_ =
  128. ChooseBetweenMinAndMax(kChanceOfAddingDeadContinue);
  129. chance_of_adding_equation_instruction_ =
  130. ChooseBetweenMinAndMax(kChanceOfAddingEquationInstruction);
  131. chance_of_adding_global_variable_ =
  132. ChooseBetweenMinAndMax(kChanceOfAddingGlobalVariable);
  133. chance_of_adding_load_ = ChooseBetweenMinAndMax(kChanceOfAddingLoad);
  134. chance_of_adding_image_sample_unused_components_ =
  135. ChooseBetweenMinAndMax(kChanceOfAddingImageSampleUnusedComponents);
  136. chance_of_adding_local_variable_ =
  137. ChooseBetweenMinAndMax(kChanceOfAddingLocalVariable);
  138. chance_of_adding_matrix_type_ =
  139. ChooseBetweenMinAndMax(kChanceOfAddingMatrixType);
  140. chance_of_adding_no_contraction_decoration_ =
  141. ChooseBetweenMinAndMax(kChanceOfAddingNoContractionDecoration);
  142. chance_of_adding_parameters =
  143. ChooseBetweenMinAndMax(kChanceOfAddingParameters);
  144. chance_of_adding_store_ = ChooseBetweenMinAndMax(kChanceOfAddingStore);
  145. chance_of_adding_vector_shuffle_ =
  146. ChooseBetweenMinAndMax(kChanceOfAddingVectorShuffle);
  147. chance_of_adding_vector_type_ =
  148. ChooseBetweenMinAndMax(kChanceOfAddingVectorType);
  149. chance_of_adjusting_branch_weights_ =
  150. ChooseBetweenMinAndMax(kChanceOfAdjustingBranchWeights);
  151. chance_of_adjusting_function_control_ =
  152. ChooseBetweenMinAndMax(kChanceOfAdjustingFunctionControl);
  153. chance_of_adjusting_loop_control_ =
  154. ChooseBetweenMinAndMax(kChanceOfAdjustingLoopControl);
  155. chance_of_adjusting_memory_operands_mask_ =
  156. ChooseBetweenMinAndMax(kChanceOfAdjustingMemoryOperandsMask);
  157. chance_of_adjusting_selection_control_ =
  158. ChooseBetweenMinAndMax(kChanceOfAdjustingSelectionControl);
  159. chance_of_calling_function_ =
  160. ChooseBetweenMinAndMax(kChanceOfCallingFunction);
  161. chance_of_choosing_struct_type_vs_array_type_ =
  162. ChooseBetweenMinAndMax(kChanceOfChoosingStructTypeVsArrayType);
  163. chance_of_choosing_workgroup_storage_class_ =
  164. ChooseBetweenMinAndMax(kChanceOfChoosingWorkgroupStorageClass);
  165. chance_of_constructing_composite_ =
  166. ChooseBetweenMinAndMax(kChanceOfConstructingComposite);
  167. chance_of_copying_object_ = ChooseBetweenMinAndMax(kChanceOfCopyingObject);
  168. chance_of_donating_additional_module_ =
  169. ChooseBetweenMinAndMax(kChanceOfDonatingAdditionalModule);
  170. chance_of_going_deeper_when_making_access_chain_ =
  171. ChooseBetweenMinAndMax(kChanceOfGoingDeeperWhenMakingAccessChain);
  172. chance_of_inverting_comparison_operators_ =
  173. ChooseBetweenMinAndMax(kChanceOfInvertingComparisonOperators);
  174. chance_of_making_donor_livesafe_ =
  175. ChooseBetweenMinAndMax(kChanceOfMakingDonorLivesafe);
  176. chance_of_merging_blocks_ = ChooseBetweenMinAndMax(kChanceOfMergingBlocks);
  177. chance_of_moving_block_down_ =
  178. ChooseBetweenMinAndMax(kChanceOfMovingBlockDown);
  179. chance_of_obfuscating_constant_ =
  180. ChooseBetweenMinAndMax(kChanceOfObfuscatingConstant);
  181. chance_of_outlining_function_ =
  182. ChooseBetweenMinAndMax(kChanceOfOutliningFunction);
  183. chance_of_permuting_parameters_ =
  184. ChooseBetweenMinAndMax(kChanceOfPermutingParameters);
  185. chance_of_permuting_phi_operands_ =
  186. ChooseBetweenMinAndMax(kChanceOfPermutingPhiOperands);
  187. chance_of_pushing_id_through_variable_ =
  188. ChooseBetweenMinAndMax(kChanceOfPushingIdThroughVariable);
  189. chance_of_replacing_id_with_synonym_ =
  190. ChooseBetweenMinAndMax(kChanceOfReplacingIdWithSynonym);
  191. chance_of_replacing_linear_algebra_instructions_ =
  192. ChooseBetweenMinAndMax(kChanceOfReplacingLinearAlgebraInstructions);
  193. chance_of_replacing_parameters_with_globals_ =
  194. ChooseBetweenMinAndMax(kChanceOfReplacingParametersWithGlobals);
  195. chance_of_splitting_block_ = ChooseBetweenMinAndMax(kChanceOfSplittingBlock);
  196. chance_of_swapping_conditional_branch_operands_ =
  197. ChooseBetweenMinAndMax(kChanceOfSwappingConditionalBranchOperands);
  198. chance_of_toggling_access_chain_instruction_ =
  199. ChooseBetweenMinAndMax(kChanceOfTogglingAccessChainInstruction);
  200. }
  201. FuzzerContext::~FuzzerContext() = default;
  202. uint32_t FuzzerContext::GetFreshId() { return next_fresh_id_++; }
  203. std::vector<uint32_t> FuzzerContext::GetFreshIds(const uint32_t count) {
  204. std::vector<uint32_t> fresh_ids(count);
  205. for (uint32_t& fresh_id : fresh_ids) {
  206. fresh_id = next_fresh_id_++;
  207. }
  208. return fresh_ids;
  209. }
  210. bool FuzzerContext::ChooseEven() { return random_generator_->RandomBool(); }
  211. bool FuzzerContext::ChoosePercentage(uint32_t percentage_chance) {
  212. assert(percentage_chance <= 100);
  213. return random_generator_->RandomPercentage() < percentage_chance;
  214. }
  215. uint32_t FuzzerContext::ChooseBetweenMinAndMax(
  216. const std::pair<uint32_t, uint32_t>& min_max) {
  217. assert(min_max.first <= min_max.second);
  218. return min_max.first +
  219. random_generator_->RandomUint32(min_max.second - min_max.first + 1);
  220. }
  221. } // namespace fuzz
  222. } // namespace spvtools