fuzzer_context.cpp 11 KB

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