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