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