fuzzer_context.cpp 13 KB

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