fuzzer_context.cpp 14 KB

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