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