| 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 |
#ifndef SOURCE_FUZZ_FUZZER_CONTEXT_H_ |
| 16 |
#define SOURCE_FUZZ_FUZZER_CONTEXT_H_ |
| 17 |
|
| 18 |
#include <functional> |
| 19 |
#include <utility> |
| 20 |
|
| 21 |
#include "source/fuzz/protobufs/spirvfuzz_protobufs.h" |
| 22 |
#include "source/fuzz/random_generator.h" |
| 23 |
#include "source/opt/function.h" |
| 24 |
|
| 25 |
namespace spvtools { |
| 26 |
namespace fuzz { |
| 27 |
|
| 28 |
// Encapsulates all parameters that control the fuzzing process, such as the |
| 29 |
// source of randomness and the probabilities with which transformations are |
| 30 |
// applied. |
| 31 |
class FuzzerContext { |
| 32 |
public: |
| 33 |
// Constructs a fuzzer context with a given random generator and the minimum |
| 34 |
// value that can be used for fresh ids. |
| 35 |
FuzzerContext(RandomGenerator* random_generator, uint32_t min_fresh_id); |
| 36 |
|
| 37 |
~FuzzerContext(); |
| 38 |
|
| 39 |
// Returns a random boolean. |
| 40 |
bool ChooseEven(); |
| 41 |
|
| 42 |
// Returns true if and only if a randomly-chosen integer in the range [0, 100] |
| 43 |
// is less than |percentage_chance|. |
| 44 |
bool ChoosePercentage(uint32_t percentage_chance); |
| 45 |
|
| 46 |
// Returns a random index into |sequence|, which is expected to have a 'size' |
| 47 |
// method, and which must be non-empty. Typically 'HasSizeMethod' will be an |
| 48 |
// std::vector. |
| 49 |
template <typename HasSizeMethod> |
| 50 |
uint32_t RandomIndex(const HasSizeMethod& sequence) const { |
| 51 |
assert(sequence.size() > 0); |
| 52 |
return random_generator_->RandomUint32( |
| 53 |
static_cast<uint32_t>(sequence.size())); |
| 54 |
} |
| 55 |
|
| 56 |
// Selects a random index into |sequence|, removes the element at that index |
| 57 |
// and returns it. |
| 58 |
template <typename T> |
| 59 |
T RemoveAtRandomIndex(std::vector<T>* sequence) const { |
| 60 |
uint32_t index = RandomIndex(*sequence); |
| 61 |
T result = sequence->at(index); |
| 62 |
sequence->erase(sequence->begin() + index); |
| 63 |
return result; |
| 64 |
} |
| 65 |
|
| 66 |
// Randomly shuffles a |sequence| between |lo| and |hi| indices inclusively. |
| 67 |
// |lo| and |hi| must be valid indices to the |sequence| |
| 68 |
template <typename T> |
| 69 |
void Shuffle(std::vector<T>* sequence, size_t lo, size_t hi) const { |
| 70 |
auto& array = *sequence; |
| 71 |
|
| 72 |
if (array.empty()) { |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
assert(lo <= hi && hi < array.size() && "lo and/or hi indices are invalid"); |
| 77 |
|
| 78 |
// i > lo to account for potential infinite loop when lo == 0 |
| 79 |
for (size_t i = hi; i > lo; --i) { |
| 80 |
auto index = |
| 81 |
random_generator_->RandomUint32(static_cast<uint32_t>(i - lo + 1)); |
| 82 |
|
| 83 |
if (lo + index != i) { |
| 84 |
// Introduce std::swap to the scope but don't use it |
| 85 |
// directly since there might be a better overload |
| 86 |
using std::swap; |
| 87 |
swap(array[lo + index], array[i]); |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
// Ramdomly shuffles a |sequence| |
| 93 |
template <typename T> |
| 94 |
void Shuffle(std::vector<T>* sequence) const { |
| 95 |
if (!sequence->empty()) { |
| 96 |
Shuffle(sequence, 0, sequence->size() - 1); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
// Yields an id that is guaranteed not to be used in the module being fuzzed, |
| 101 |
// or to have been issued before. |
| 102 |
uint32_t GetFreshId(); |
| 103 |
|
| 104 |
// Returns a vector of |count| fresh ids. |
| 105 |
std::vector<uint32_t> GetFreshIds(const uint32_t count); |
| 106 |
|
| 107 |
// A suggested limit on the id bound for the module being fuzzed. This is |
| 108 |
// useful for deciding when to stop the overall fuzzing process. Furthermore, |
| 109 |
// fuzzer passes that run the risk of spiralling out of control can |
| 110 |
// periodically check this limit and terminate early if it has been reached. |
| 111 |
uint32_t GetIdBoundLimit() const; |
| 112 |
|
| 113 |
// A suggested limit on the number of transformations that should be applied. |
| 114 |
// Also useful to control the overall fuzzing process and rein in individual |
| 115 |
// fuzzer passes. |
| 116 |
uint32_t GetTransformationLimit() const; |
| 117 |
|
| 118 |
// Probabilities associated with applying various transformations. |
| 119 |
// Keep them in alphabetical order. |
| 120 |
uint32_t GetChanceOfAcceptingRepeatedPassRecommendation() const { |
| 121 |
return chance_of_accepting_repeated_pass_recommendation_; |
| 122 |
} |
| 123 |
uint32_t GetChanceOfAddingAccessChain() const { |
| 124 |
return chance_of_adding_access_chain_; |
| 125 |
} |
| 126 |
uint32_t GetChanceOfAddingAnotherPassToPassLoop() const { |
| 127 |
return chance_of_adding_another_pass_to_pass_loop_; |
| 128 |
} |
| 129 |
uint32_t GetChanceOfAddingAnotherStructField() const { |
| 130 |
return chance_of_adding_another_struct_field_; |
| 131 |
} |
| 132 |
uint32_t GetChanceOfAddingArrayOrStructType() const { |
| 133 |
return chance_of_adding_array_or_struct_type_; |
| 134 |
} |
| 135 |
uint32_t GetChanceOfAddingBitInstructionSynonym() const { |
| 136 |
return chance_of_adding_bit_instruction_synonym_; |
| 137 |
} |
| 138 |
uint32_t GetChanceOfAddingBothBranchesWhenReplacingOpSelect() const { |
| 139 |
return chance_of_adding_both_branches_when_replacing_opselect_; |
| 140 |
} |
| 141 |
uint32_t GetChanceOfAddingCompositeExtract() const { |
| 142 |
return chance_of_adding_composite_extract_; |
| 143 |
} |
| 144 |
uint32_t GetChanceOfAddingCompositeInsert() const { |
| 145 |
return chance_of_adding_composite_insert_; |
| 146 |
} |
| 147 |
uint32_t GetChanceOfAddingCopyMemory() const { |
| 148 |
return chance_of_adding_copy_memory_; |
| 149 |
} |
| 150 |
uint32_t GetChanceOfAddingDeadBlock() const { |
| 151 |
return chance_of_adding_dead_block_; |
| 152 |
} |
| 153 |
uint32_t GetChanceOfAddingDeadBreak() const { |
| 154 |
return chance_of_adding_dead_break_; |
| 155 |
} |
| 156 |
uint32_t GetChanceOfAddingDeadContinue() const { |
| 157 |
return chance_of_adding_dead_continue_; |
| 158 |
} |
| 159 |
uint32_t GetChanceOfAddingEquationInstruction() const { |
| 160 |
return chance_of_adding_equation_instruction_; |
| 161 |
} |
| 162 |
uint32_t GetChanceOfAddingGlobalVariable() const { |
| 163 |
return chance_of_adding_global_variable_; |
| 164 |
} |
| 165 |
uint32_t GetChanceOfAddingImageSampleUnusedComponents() const { |
| 166 |
return chance_of_adding_image_sample_unused_components_; |
| 167 |
} |
| 168 |
uint32_t GetChanceOfAddingLoad() const { return chance_of_adding_load_; } |
| 169 |
uint32_t GetChanceOfAddingLocalVariable() const { |
| 170 |
return chance_of_adding_local_variable_; |
| 171 |
} |
| 172 |
uint32_t GetChanceOfAddingLoopPreheader() const { |
| 173 |
return chance_of_adding_loop_preheader_; |
| 174 |
} |
| 175 |
uint32_t GetChanceOfAddingMatrixType() const { |
| 176 |
return chance_of_adding_matrix_type_; |
| 177 |
} |
| 178 |
uint32_t GetChanceOfAddingNoContractionDecoration() const { |
| 179 |
return chance_of_adding_no_contraction_decoration_; |
| 180 |
} |
| 181 |
uint32_t GetChanceOfAddingOpPhiSynonym() const { |
| 182 |
return chance_of_adding_opphi_synonym_; |
| 183 |
} |
| 184 |
uint32_t GetChanceOfAddingParameters() const { |
| 185 |
return chance_of_adding_parameters; |
| 186 |
} |
| 187 |
uint32_t GetChanceOfAddingRelaxedDecoration() const { |
| 188 |
return chance_of_adding_relaxed_decoration_; |
| 189 |
} |
| 190 |
uint32_t GetChanceOfAddingStore() const { return chance_of_adding_store_; } |
| 191 |
uint32_t GetChanceOfAddingSynonyms() const { |
| 192 |
return chance_of_adding_synonyms_; |
| 193 |
} |
| 194 |
uint32_t GetChanceOfAddingTrueBranchWhenReplacingOpSelect() const { |
| 195 |
return chance_of_adding_true_branch_when_replacing_opselect_; |
| 196 |
} |
| 197 |
uint32_t GetChanceOfAddingVectorShuffle() const { |
| 198 |
return chance_of_adding_vector_shuffle_; |
| 199 |
} |
| 200 |
uint32_t GetChanceOfAddingVectorType() const { |
| 201 |
return chance_of_adding_vector_type_; |
| 202 |
} |
| 203 |
uint32_t GetChanceOfAdjustingBranchWeights() const { |
| 204 |
return chance_of_adjusting_branch_weights_; |
| 205 |
} |
| 206 |
uint32_t GetChanceOfAdjustingFunctionControl() const { |
| 207 |
return chance_of_adjusting_function_control_; |
| 208 |
} |
| 209 |
uint32_t GetChanceOfAdjustingLoopControl() const { |
| 210 |
return chance_of_adjusting_loop_control_; |
| 211 |
} |
| 212 |
uint32_t GetChanceOfAdjustingMemoryOperandsMask() const { |
| 213 |
return chance_of_adjusting_memory_operands_mask_; |
| 214 |
} |
| 215 |
uint32_t GetChanceOfAdjustingSelectionControl() const { |
| 216 |
return chance_of_adjusting_selection_control_; |
| 217 |
} |
| 218 |
uint32_t GetChanceOfCallingFunction() const { |
| 219 |
return chance_of_calling_function_; |
| 220 |
} |
| 221 |
uint32_t GetChanceOfChoosingStructTypeVsArrayType() const { |
| 222 |
return chance_of_choosing_struct_type_vs_array_type_; |
| 223 |
} |
| 224 |
uint32_t GetChanceOfChoosingWorkgroupStorageClass() const { |
| 225 |
return chance_of_choosing_workgroup_storage_class_; |
| 226 |
} |
| 227 |
uint32_t GetChanceOfConstructingComposite() const { |
| 228 |
return chance_of_constructing_composite_; |
| 229 |
} |
| 230 |
uint32_t GetChanceOfCopyingObject() const { |
| 231 |
return chance_of_copying_object_; |
| 232 |
} |
| 233 |
uint32_t GetChanceOfCreatingIntSynonymsUsingLoops() const { |
| 234 |
return chance_of_creating_int_synonyms_using_loops_; |
| 235 |
} |
| 236 |
uint32_t GetChanceOfDonatingAdditionalModule() const { |
| 237 |
return chance_of_donating_additional_module_; |
| 238 |
} |
| 239 |
uint32_t GetChanceOfDuplicatingRegionWithSelection() const { |
| 240 |
return chance_of_duplicating_region_with_selection_; |
| 241 |
} |
| 242 |
uint32_t GetChanceOfExpandingVectorReduction() const { |
| 243 |
return chance_of_expanding_vector_reduction_; |
| 244 |
} |
| 245 |
uint32_t GetChanceOfFlatteningConditionalBranch() const { |
| 246 |
return chance_of_flattening_conditional_branch_; |
| 247 |
} |
| 248 |
uint32_t GetChanceOfGoingDeeperToExtractComposite() const { |
| 249 |
return chance_of_going_deeper_to_extract_composite_; |
| 250 |
} |
| 251 |
uint32_t GetChanceOfGoingDeeperToInsertInComposite() const { |
| 252 |
return chance_of_going_deeper_to_insert_in_composite_; |
| 253 |
} |
| 254 |
uint32_t GetChanceOfGoingDeeperWhenMakingAccessChain() const { |
| 255 |
return chance_of_going_deeper_when_making_access_chain_; |
| 256 |
} |
| 257 |
uint32_t GetChanceOfHavingTwoBlocksInLoopToCreateIntSynonym() const { |
| 258 |
return chance_of_having_two_blocks_in_loop_to_create_int_synonym_; |
| 259 |
} |
| 260 |
uint32_t GetChanceOfInliningFunction() const { |
| 261 |
return chance_of_inlining_function_; |
| 262 |
} |
| 263 |
uint32_t GetChanceOfInterchangingSignednessOfIntegerOperands() const { |
| 264 |
return chance_of_interchanging_signedness_of_integer_operands_; |
| 265 |
} |
| 266 |
uint32_t GetChanceOfInterchangingZeroLikeConstants() const { |
| 267 |
return chance_of_interchanging_zero_like_constants_; |
| 268 |
} |
| 269 |
uint32_t GetChanceOfInvertingComparisonOperators() const { |
| 270 |
return chance_of_inverting_comparison_operators_; |
| 271 |
} |
| 272 |
uint32_t ChanceOfMakingDonorLivesafe() const { |
| 273 |
return chance_of_making_donor_livesafe_; |
| 274 |
} |
| 275 |
uint32_t GetChanceOfMakingVectorOperationDynamic() const { |
| 276 |
return chance_of_making_vector_operation_dynamic_; |
| 277 |
} |
| 278 |
uint32_t GetChanceOfMergingBlocks() const { |
| 279 |
return chance_of_merging_blocks_; |
| 280 |
} |
| 281 |
uint32_t GetChanceOfMergingFunctionReturns() const { |
| 282 |
return chance_of_merging_function_returns_; |
| 283 |
} |
| 284 |
uint32_t GetChanceOfMovingBlockDown() const { |
| 285 |
return chance_of_moving_block_down_; |
| 286 |
} |
| 287 |
uint32_t GetChanceOfMutatingPointer() const { |
| 288 |
return chance_of_mutating_pointer_; |
| 289 |
} |
| 290 |
uint32_t GetChanceOfObfuscatingConstant() const { |
| 291 |
return chance_of_obfuscating_constant_; |
| 292 |
} |
| 293 |
uint32_t GetChanceOfOutliningFunction() const { |
| 294 |
return chance_of_outlining_function_; |
| 295 |
} |
| 296 |
uint32_t GetChanceOfPermutingInstructions() const { |
| 297 |
return chance_of_permuting_instructions_; |
| 298 |
} |
| 299 |
uint32_t GetChanceOfPermutingParameters() const { |
| 300 |
return chance_of_permuting_parameters_; |
| 301 |
} |
| 302 |
uint32_t GetChanceOfPermutingPhiOperands() const { |
| 303 |
return chance_of_permuting_phi_operands_; |
| 304 |
} |
| 305 |
uint32_t GetChanceOfPropagatingInstructionsDown() const { |
| 306 |
return chance_of_propagating_instructions_down_; |
| 307 |
} |
| 308 |
uint32_t GetChanceOfPropagatingInstructionsUp() const { |
| 309 |
return chance_of_propagating_instructions_up_; |
| 310 |
} |
| 311 |
uint32_t GetChanceOfPushingIdThroughVariable() const { |
| 312 |
return chance_of_pushing_id_through_variable_; |
| 313 |
} |
| 314 |
uint32_t GetChanceOfReplacingAddSubMulWithCarryingExtended() const { |
| 315 |
return chance_of_replacing_add_sub_mul_with_carrying_extended_; |
| 316 |
} |
| 317 |
uint32_t GetChanceOfReplacingBranchFromDeadBlockWithExit() const { |
| 318 |
return chance_of_replacing_branch_from_dead_block_with_exit_; |
| 319 |
} |
| 320 |
uint32_t GetChanceOfReplacingCopyMemoryWithLoadStore() const { |
| 321 |
return chance_of_replacing_copy_memory_with_load_store_; |
| 322 |
} |
| 323 |
uint32_t GetChanceOfReplacingCopyObjectWithStoreLoad() const { |
| 324 |
return chance_of_replacing_copyobject_with_store_load_; |
| 325 |
} |
| 326 |
uint32_t GetChanceOfReplacingIdWithSynonym() const { |
| 327 |
return chance_of_replacing_id_with_synonym_; |
| 328 |
} |
| 329 |
uint32_t GetChanceOfReplacingIrrelevantId() const { |
| 330 |
return chance_of_replacing_irrelevant_id_; |
| 331 |
} |
| 332 |
uint32_t GetChanceOfReplacingLinearAlgebraInstructions() const { |
| 333 |
return chance_of_replacing_linear_algebra_instructions_; |
| 334 |
} |
| 335 |
uint32_t GetChanceOfReplacingLoadStoreWithCopyMemory() const { |
| 336 |
return chance_of_replacing_load_store_with_copy_memory_; |
| 337 |
} |
| 338 |
uint32_t GetChanceOfReplacingOpPhiIdFromDeadPredecessor() const { |
| 339 |
return chance_of_replacing_opphi_id_from_dead_predecessor_; |
| 340 |
} |
| 341 |
uint32_t GetChanceOfReplacingOpselectWithConditionalBranch() const { |
| 342 |
return chance_of_replacing_opselect_with_conditional_branch_; |
| 343 |
} |
| 344 |
uint32_t GetChanceOfReplacingParametersWithGlobals() const { |
| 345 |
return chance_of_replacing_parameters_with_globals_; |
| 346 |
} |
| 347 |
uint32_t GetChanceOfReplacingParametersWithStruct() const { |
| 348 |
return chance_of_replacing_parameters_with_struct_; |
| 349 |
} |
| 350 |
uint32_t GetChanceOfSplittingBlock() const { |
| 351 |
return chance_of_splitting_block_; |
| 352 |
} |
| 353 |
uint32_t GetChanceOfSwappingConditionalBranchOperands() const { |
| 354 |
return chance_of_swapping_conditional_branch_operands_; |
| 355 |
} |
| 356 |
uint32_t GetChanceOfTogglingAccessChainInstruction() const { |
| 357 |
return chance_of_toggling_access_chain_instruction_; |
| 358 |
} |
| 359 |
uint32_t GetChanceOfWrappingRegionInSelection() const { |
| 360 |
return chance_of_wrapping_region_in_selection_; |
| 361 |
} |
| 362 |
|
| 363 |
// Other functions to control transformations. Keep them in alphabetical |
| 364 |
// order. |
| 365 |
uint32_t GetMaximumEquivalenceClassSizeForDataSynonymFactClosure() const { |
| 366 |
return max_equivalence_class_size_for_data_synonym_fact_closure_; |
| 367 |
} |
| 368 |
uint32_t GetMaximumNumberOfFunctionParameters() const { |
| 369 |
return max_number_of_function_parameters_; |
| 370 |
} |
| 371 |
uint32_t GetMaximumNumberOfParametersReplacedWithStruct() const { |
| 372 |
return max_number_of_parameters_replaced_with_struct_; |
| 373 |
} |
| 374 |
std::pair<uint32_t, uint32_t> GetRandomBranchWeights() { |
| 375 |
std::pair<uint32_t, uint32_t> branch_weights = {0, 0}; |
| 376 |
|
| 377 |
while (branch_weights.first == 0 && branch_weights.second == 0) { |
| 378 |
// Using INT32_MAX to do not overflow UINT32_MAX when the branch weights |
| 379 |
// are added together. |
| 380 |
branch_weights.first = random_generator_->RandomUint32(INT32_MAX); |
| 381 |
branch_weights.second = random_generator_->RandomUint32(INT32_MAX); |
| 382 |
} |
| 383 |
|
| 384 |
return branch_weights; |
| 385 |
} |
| 386 |
std::vector<uint32_t> GetRandomComponentsForVectorShuffle( |
| 387 |
uint32_t max_component_index) { |
| 388 |
// Component count must be in range [2, 4]. |
| 389 |
std::vector<uint32_t> components(random_generator_->RandomUint32(2) + 2); |
| 390 |
|
| 391 |
for (uint32_t& component : components) { |
| 392 |
component = random_generator_->RandomUint32(max_component_index); |
| 393 |
} |
| 394 |
|
| 395 |
return components; |
| 396 |
} |
| 397 |
uint32_t GetRandomCompositeExtractIndex(uint32_t number_of_members) { |
| 398 |
assert(number_of_members > 0 && "Composite object must have some members"); |
| 399 |
return ChooseBetweenMinAndMax({0, number_of_members - 1}); |
| 400 |
} |
| 401 |
uint32_t GetRandomIndexForAccessChain(uint32_t composite_size_bound) { |
| 402 |
return random_generator_->RandomUint32(composite_size_bound); |
| 403 |
} |
| 404 |
uint32_t GetRandomIndexForCompositeInsert(uint32_t number_of_components) { |
| 405 |
return random_generator_->RandomUint32(number_of_components); |
| 406 |
} |
| 407 |
int64_t GetRandomValueForStepConstantInLoop() { |
| 408 |
return random_generator_->RandomUint64(UINT64_MAX); |
| 409 |
} |
| 410 |
uint32_t GetRandomLoopControlPartialCount() { |
| 411 |
return random_generator_->RandomUint32(max_loop_control_partial_count_); |
| 412 |
} |
| 413 |
uint32_t GetRandomLoopControlPeelCount() { |
| 414 |
return random_generator_->RandomUint32(max_loop_control_peel_count_); |
| 415 |
} |
| 416 |
uint32_t GetRandomLoopLimit() { |
| 417 |
return random_generator_->RandomUint32(max_loop_limit_); |
| 418 |
} |
| 419 |
uint32_t GetRandomNumberOfLoopIterations(uint32_t max_num_iterations) { |
| 420 |
return ChooseBetweenMinAndMax({1, max_num_iterations}); |
| 421 |
} |
| 422 |
uint32_t GetRandomNumberOfNewParameters(uint32_t num_of_params) { |
| 423 |
assert(num_of_params < GetMaximumNumberOfFunctionParameters()); |
| 424 |
return ChooseBetweenMinAndMax( |
| 425 |
{1, std::min(max_number_of_new_parameters_, |
| 426 |
GetMaximumNumberOfFunctionParameters() - num_of_params)}); |
| 427 |
} |
| 428 |
uint32_t GetRandomNumberOfParametersReplacedWithStruct(uint32_t num_params) { |
| 429 |
assert(num_params != 0 && "A function must have parameters to replace"); |
| 430 |
return ChooseBetweenMinAndMax( |
| 431 |
{1, std::min(num_params, |
| 432 |
GetMaximumNumberOfParametersReplacedWithStruct())}); |
| 433 |
} |
| 434 |
uint32_t GetRandomSizeForNewArray() { |
| 435 |
// Ensure that the array size is non-zero. |
| 436 |
return random_generator_->RandomUint32(max_new_array_size_limit_ - 1) + 1; |
| 437 |
} |
| 438 |
protobufs::TransformationAddSynonym::SynonymType GetRandomSynonymType(); |
| 439 |
uint32_t GetRandomUnusedComponentCountForImageSample( |
| 440 |
uint32_t max_unused_component_count) { |
| 441 |
// Ensure that the number of unused components is non-zero. |
| 442 |
return random_generator_->RandomUint32(max_unused_component_count) + 1; |
| 443 |
} |
| 444 |
bool GoDeeperInConstantObfuscation(uint32_t depth) { |
| 445 |
return go_deeper_in_constant_obfuscation_(depth, random_generator_); |
| 446 |
} |
| 447 |
|
| 448 |
private: |
| 449 |
// The source of randomness. |
| 450 |
RandomGenerator* random_generator_; |
| 451 |
// The next fresh id to be issued. |
| 452 |
uint32_t next_fresh_id_; |
| 453 |
|
| 454 |
// Probabilities associated with applying various transformations. |
| 455 |
// Keep them in alphabetical order. |
| 456 |
uint32_t chance_of_accepting_repeated_pass_recommendation_; |
| 457 |
uint32_t chance_of_adding_access_chain_; |
| 458 |
uint32_t chance_of_adding_another_pass_to_pass_loop_; |
| 459 |
uint32_t chance_of_adding_another_struct_field_; |
| 460 |
uint32_t chance_of_adding_array_or_struct_type_; |
| 461 |
uint32_t chance_of_adding_bit_instruction_synonym_; |
| 462 |
uint32_t chance_of_adding_both_branches_when_replacing_opselect_; |
| 463 |
uint32_t chance_of_adding_composite_extract_; |
| 464 |
uint32_t chance_of_adding_composite_insert_; |
| 465 |
uint32_t chance_of_adding_copy_memory_; |
| 466 |
uint32_t chance_of_adding_dead_block_; |
| 467 |
uint32_t chance_of_adding_dead_break_; |
| 468 |
uint32_t chance_of_adding_dead_continue_; |
| 469 |
uint32_t chance_of_adding_equation_instruction_; |
| 470 |
uint32_t chance_of_adding_global_variable_; |
| 471 |
uint32_t chance_of_adding_image_sample_unused_components_; |
| 472 |
uint32_t chance_of_adding_load_; |
| 473 |
uint32_t chance_of_adding_local_variable_; |
| 474 |
uint32_t chance_of_adding_loop_preheader_; |
| 475 |
uint32_t chance_of_adding_matrix_type_; |
| 476 |
uint32_t chance_of_adding_no_contraction_decoration_; |
| 477 |
uint32_t chance_of_adding_opphi_synonym_; |
| 478 |
uint32_t chance_of_adding_parameters; |
| 479 |
uint32_t chance_of_adding_relaxed_decoration_; |
| 480 |
uint32_t chance_of_adding_store_; |
| 481 |
uint32_t chance_of_adding_synonyms_; |
| 482 |
uint32_t chance_of_adding_true_branch_when_replacing_opselect_; |
| 483 |
uint32_t chance_of_adding_vector_shuffle_; |
| 484 |
uint32_t chance_of_adding_vector_type_; |
| 485 |
uint32_t chance_of_adjusting_branch_weights_; |
| 486 |
uint32_t chance_of_adjusting_function_control_; |
| 487 |
uint32_t chance_of_adjusting_loop_control_; |
| 488 |
uint32_t chance_of_adjusting_memory_operands_mask_; |
| 489 |
uint32_t chance_of_adjusting_selection_control_; |
| 490 |
uint32_t chance_of_calling_function_; |
| 491 |
uint32_t chance_of_choosing_struct_type_vs_array_type_; |
| 492 |
uint32_t chance_of_choosing_workgroup_storage_class_; |
| 493 |
uint32_t chance_of_constructing_composite_; |
| 494 |
uint32_t chance_of_copying_object_; |
| 495 |
uint32_t chance_of_creating_int_synonyms_using_loops_; |
| 496 |
uint32_t chance_of_donating_additional_module_; |
| 497 |
uint32_t chance_of_duplicating_region_with_selection_; |
| 498 |
uint32_t chance_of_expanding_vector_reduction_; |
| 499 |
uint32_t chance_of_flattening_conditional_branch_; |
| 500 |
uint32_t chance_of_going_deeper_to_extract_composite_; |
| 501 |
uint32_t chance_of_going_deeper_to_insert_in_composite_; |
| 502 |
uint32_t chance_of_going_deeper_when_making_access_chain_; |
| 503 |
uint32_t chance_of_having_two_blocks_in_loop_to_create_int_synonym_; |
| 504 |
uint32_t chance_of_inlining_function_; |
| 505 |
uint32_t chance_of_interchanging_signedness_of_integer_operands_; |
| 506 |
uint32_t chance_of_interchanging_zero_like_constants_; |
| 507 |
uint32_t chance_of_inverting_comparison_operators_; |
| 508 |
uint32_t chance_of_making_donor_livesafe_; |
| 509 |
uint32_t chance_of_making_vector_operation_dynamic_; |
| 510 |
uint32_t chance_of_merging_blocks_; |
| 511 |
uint32_t chance_of_merging_function_returns_; |
| 512 |
uint32_t chance_of_moving_block_down_; |
| 513 |
uint32_t chance_of_mutating_pointer_; |
| 514 |
uint32_t chance_of_obfuscating_constant_; |
| 515 |
uint32_t chance_of_outlining_function_; |
| 516 |
uint32_t chance_of_permuting_instructions_; |
| 517 |
uint32_t chance_of_permuting_parameters_; |
| 518 |
uint32_t chance_of_permuting_phi_operands_; |
| 519 |
uint32_t chance_of_propagating_instructions_down_; |
| 520 |
uint32_t chance_of_propagating_instructions_up_; |
| 521 |
uint32_t chance_of_pushing_id_through_variable_; |
| 522 |
uint32_t chance_of_replacing_add_sub_mul_with_carrying_extended_; |
| 523 |
uint32_t chance_of_replacing_branch_from_dead_block_with_exit_; |
| 524 |
uint32_t chance_of_replacing_copy_memory_with_load_store_; |
| 525 |
uint32_t chance_of_replacing_copyobject_with_store_load_; |
| 526 |
uint32_t chance_of_replacing_id_with_synonym_; |
| 527 |
uint32_t chance_of_replacing_irrelevant_id_; |
| 528 |
uint32_t chance_of_replacing_linear_algebra_instructions_; |
| 529 |
uint32_t chance_of_replacing_load_store_with_copy_memory_; |
| 530 |
uint32_t chance_of_replacing_opphi_id_from_dead_predecessor_; |
| 531 |
uint32_t chance_of_replacing_opselect_with_conditional_branch_; |
| 532 |
uint32_t chance_of_replacing_parameters_with_globals_; |
| 533 |
uint32_t chance_of_replacing_parameters_with_struct_; |
| 534 |
uint32_t chance_of_splitting_block_; |
| 535 |
uint32_t chance_of_swapping_conditional_branch_operands_; |
| 536 |
uint32_t chance_of_toggling_access_chain_instruction_; |
| 537 |
uint32_t chance_of_wrapping_region_in_selection_; |
| 538 |
|
| 539 |
// Limits associated with various quantities for which random values are |
| 540 |
// chosen during fuzzing. |
| 541 |
// Keep them in alphabetical order. |
| 542 |
uint32_t max_equivalence_class_size_for_data_synonym_fact_closure_; |
| 543 |
uint32_t max_loop_control_partial_count_; |
| 544 |
uint32_t max_loop_control_peel_count_; |
| 545 |
uint32_t max_loop_limit_; |
| 546 |
uint32_t max_new_array_size_limit_; |
| 547 |
uint32_t max_number_of_function_parameters_; |
| 548 |
uint32_t max_number_of_new_parameters_; |
| 549 |
uint32_t max_number_of_parameters_replaced_with_struct_; |
| 550 |
|
| 551 |
// Functions to determine with what probability to go deeper when generating |
| 552 |
// or mutating constructs recursively. |
| 553 |
const std::function<bool(uint32_t, RandomGenerator*)>& |
| 554 |
go_deeper_in_constant_obfuscation_; |
| 555 |
|
| 556 |
// Requires |min_max.first| <= |min_max.second|, and returns a value in the |
| 557 |
// range [ |min_max.first|, |min_max.second| ] |
| 558 |
uint32_t ChooseBetweenMinAndMax(const std::pair<uint32_t, uint32_t>& min_max); |
| 559 |
}; |
| 560 |
|
| 561 |
} // namespace fuzz |
| 562 |
} // namespace spvtools |
| 563 |
|
| 564 |
#endif // SOURCE_FUZZ_FUZZER_CONTEXT_H_ |