2019-05-27 16:34:55 +03:00
|
|
|
// Copyright (c) 2019 Google LLC
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#ifndef SOURCE_FUZZ_FUZZER_CONTEXT_H_
|
|
|
|
#define SOURCE_FUZZ_FUZZER_CONTEXT_H_
|
|
|
|
|
2019-06-18 20:41:08 +03:00
|
|
|
#include <functional>
|
2019-09-23 18:29:19 +03:00
|
|
|
#include <utility>
|
2019-06-18 20:41:08 +03:00
|
|
|
|
2020-07-12 11:59:08 +03:00
|
|
|
#include "source/fuzz/protobufs/spirvfuzz_protobufs.h"
|
2019-05-27 16:34:55 +03:00
|
|
|
#include "source/fuzz/random_generator.h"
|
|
|
|
#include "source/opt/function.h"
|
2021-03-03 18:34:53 +03:00
|
|
|
#include "source/opt/ir_context.h"
|
2019-05-27 16:34:55 +03:00
|
|
|
|
|
|
|
namespace spvtools {
|
|
|
|
namespace fuzz {
|
|
|
|
|
|
|
|
// Encapsulates all parameters that control the fuzzing process, such as the
|
|
|
|
// source of randomness and the probabilities with which transformations are
|
|
|
|
// applied.
|
|
|
|
class FuzzerContext {
|
|
|
|
public:
|
|
|
|
// Constructs a fuzzer context with a given random generator and the minimum
|
|
|
|
// value that can be used for fresh ids.
|
2021-03-03 18:34:53 +03:00
|
|
|
FuzzerContext(std::unique_ptr<RandomGenerator> random_generator,
|
|
|
|
uint32_t min_fresh_id);
|
2019-05-27 16:34:55 +03:00
|
|
|
|
|
|
|
~FuzzerContext();
|
|
|
|
|
2019-09-10 17:02:25 +03:00
|
|
|
// Returns a random boolean.
|
|
|
|
bool ChooseEven();
|
|
|
|
|
|
|
|
// Returns true if and only if a randomly-chosen integer in the range [0, 100]
|
|
|
|
// is less than |percentage_chance|.
|
|
|
|
bool ChoosePercentage(uint32_t percentage_chance);
|
|
|
|
|
|
|
|
// Returns a random index into |sequence|, which is expected to have a 'size'
|
|
|
|
// method, and which must be non-empty. Typically 'HasSizeMethod' will be an
|
|
|
|
// std::vector.
|
|
|
|
template <typename HasSizeMethod>
|
2020-02-11 02:22:34 +03:00
|
|
|
uint32_t RandomIndex(const HasSizeMethod& sequence) const {
|
2019-09-10 17:02:25 +03:00
|
|
|
assert(sequence.size() > 0);
|
|
|
|
return random_generator_->RandomUint32(
|
|
|
|
static_cast<uint32_t>(sequence.size()));
|
|
|
|
}
|
2019-05-27 16:34:55 +03:00
|
|
|
|
2020-02-11 02:22:34 +03:00
|
|
|
// Selects a random index into |sequence|, removes the element at that index
|
|
|
|
// and returns it.
|
|
|
|
template <typename T>
|
|
|
|
T RemoveAtRandomIndex(std::vector<T>* sequence) const {
|
|
|
|
uint32_t index = RandomIndex(*sequence);
|
|
|
|
T result = sequence->at(index);
|
|
|
|
sequence->erase(sequence->begin() + index);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-03-08 17:27:05 +03:00
|
|
|
// Randomly shuffles a |sequence| between |lo| and |hi| indices inclusively.
|
|
|
|
// |lo| and |hi| must be valid indices to the |sequence|
|
|
|
|
template <typename T>
|
|
|
|
void Shuffle(std::vector<T>* sequence, size_t lo, size_t hi) const {
|
|
|
|
auto& array = *sequence;
|
|
|
|
|
|
|
|
if (array.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(lo <= hi && hi < array.size() && "lo and/or hi indices are invalid");
|
|
|
|
|
|
|
|
// i > lo to account for potential infinite loop when lo == 0
|
|
|
|
for (size_t i = hi; i > lo; --i) {
|
|
|
|
auto index =
|
|
|
|
random_generator_->RandomUint32(static_cast<uint32_t>(i - lo + 1));
|
|
|
|
|
|
|
|
if (lo + index != i) {
|
|
|
|
// Introduce std::swap to the scope but don't use it
|
|
|
|
// directly since there might be a better overload
|
|
|
|
using std::swap;
|
|
|
|
swap(array[lo + index], array[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ramdomly shuffles a |sequence|
|
|
|
|
template <typename T>
|
|
|
|
void Shuffle(std::vector<T>* sequence) const {
|
|
|
|
if (!sequence->empty()) {
|
|
|
|
Shuffle(sequence, 0, sequence->size() - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-27 16:34:55 +03:00
|
|
|
// Yields an id that is guaranteed not to be used in the module being fuzzed,
|
|
|
|
// or to have been issued before.
|
|
|
|
uint32_t GetFreshId();
|
|
|
|
|
2020-06-16 13:20:51 +03:00
|
|
|
// Returns a vector of |count| fresh ids.
|
|
|
|
std::vector<uint32_t> GetFreshIds(const uint32_t count);
|
|
|
|
|
2020-11-03 19:51:10 +03:00
|
|
|
// A suggested limit on the id bound for the module being fuzzed. This is
|
|
|
|
// useful for deciding when to stop the overall fuzzing process. Furthermore,
|
|
|
|
// fuzzer passes that run the risk of spiralling out of control can
|
|
|
|
// periodically check this limit and terminate early if it has been reached.
|
|
|
|
uint32_t GetIdBoundLimit() const;
|
|
|
|
|
|
|
|
// A suggested limit on the number of transformations that should be applied.
|
|
|
|
// Also useful to control the overall fuzzing process and rein in individual
|
|
|
|
// fuzzer passes.
|
|
|
|
uint32_t GetTransformationLimit() const;
|
|
|
|
|
2021-03-03 18:34:53 +03:00
|
|
|
// Returns the minimum fresh id that can be used given the |ir_context|.
|
|
|
|
static uint32_t GetMinFreshId(opt::IRContext* ir_context);
|
|
|
|
|
2019-05-29 18:42:46 +03:00
|
|
|
// Probabilities associated with applying various transformations.
|
|
|
|
// Keep them in alphabetical order.
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfAcceptingRepeatedPassRecommendation() const {
|
2020-09-18 17:51:35 +03:00
|
|
|
return chance_of_accepting_repeated_pass_recommendation_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfAddingAccessChain() const {
|
2020-02-12 02:10:57 +03:00
|
|
|
return chance_of_adding_access_chain_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfAddingAnotherPassToPassLoop() const {
|
2020-09-18 17:51:35 +03:00
|
|
|
return chance_of_adding_another_pass_to_pass_loop_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfAddingAnotherStructField() const {
|
2020-02-04 17:00:19 +03:00
|
|
|
return chance_of_adding_another_struct_field_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfAddingArrayOrStructType() const {
|
2020-02-04 17:00:19 +03:00
|
|
|
return chance_of_adding_array_or_struct_type_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfAddingBitInstructionSynonym() const {
|
2020-09-16 01:36:23 +03:00
|
|
|
return chance_of_adding_bit_instruction_synonym_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfAddingBothBranchesWhenReplacingOpSelect() const {
|
2020-09-03 12:19:02 +03:00
|
|
|
return chance_of_adding_both_branches_when_replacing_opselect_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfAddingCompositeExtract() const {
|
2020-10-23 16:49:50 +03:00
|
|
|
return chance_of_adding_composite_extract_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfAddingCompositeInsert() const {
|
2020-08-19 15:56:03 +03:00
|
|
|
return chance_of_adding_composite_insert_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfAddingCopyMemory() const {
|
2020-06-30 23:13:05 +03:00
|
|
|
return chance_of_adding_copy_memory_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfAddingDeadBlock() const {
|
|
|
|
return chance_of_adding_dead_block_;
|
|
|
|
}
|
|
|
|
uint32_t GetChanceOfAddingDeadBreak() const {
|
|
|
|
return chance_of_adding_dead_break_;
|
|
|
|
}
|
|
|
|
uint32_t GetChanceOfAddingDeadContinue() const {
|
2019-07-25 15:50:33 +03:00
|
|
|
return chance_of_adding_dead_continue_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfAddingEquationInstruction() const {
|
2020-03-04 17:54:08 +03:00
|
|
|
return chance_of_adding_equation_instruction_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfAddingGlobalVariable() const {
|
2020-02-06 00:07:44 +03:00
|
|
|
return chance_of_adding_global_variable_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfAddingImageSampleUnusedComponents() const {
|
2020-07-08 19:07:04 +03:00
|
|
|
return chance_of_adding_image_sample_unused_components_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfAddingLoad() const { return chance_of_adding_load_; }
|
|
|
|
uint32_t GetChanceOfAddingLocalVariable() const {
|
2020-02-06 00:07:44 +03:00
|
|
|
return chance_of_adding_local_variable_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfAddingLoopPreheader() const {
|
2020-08-14 14:44:28 +03:00
|
|
|
return chance_of_adding_loop_preheader_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfAddingMatrixType() const {
|
2020-02-04 17:00:19 +03:00
|
|
|
return chance_of_adding_matrix_type_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfAddingNoContractionDecoration() const {
|
2019-10-11 11:15:47 +03:00
|
|
|
return chance_of_adding_no_contraction_decoration_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfAddingOpPhiSynonym() const {
|
2020-08-27 17:59:54 +03:00
|
|
|
return chance_of_adding_opphi_synonym_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfAddingParameters() const {
|
|
|
|
return chance_of_adding_parameters;
|
|
|
|
}
|
|
|
|
uint32_t GetChanceOfAddingRelaxedDecoration() const {
|
2020-07-20 15:13:07 +03:00
|
|
|
return chance_of_adding_relaxed_decoration_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfAddingStore() const { return chance_of_adding_store_; }
|
|
|
|
uint32_t GetChanceOfAddingSynonyms() const {
|
|
|
|
return chance_of_adding_synonyms_;
|
|
|
|
}
|
|
|
|
uint32_t GetChanceOfAddingTrueBranchWhenReplacingOpSelect() const {
|
2020-09-03 12:19:02 +03:00
|
|
|
return chance_of_adding_true_branch_when_replacing_opselect_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfAddingVectorShuffle() const {
|
2020-06-16 13:21:31 +03:00
|
|
|
return chance_of_adding_vector_shuffle_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfAddingVectorType() const {
|
2020-02-04 17:00:19 +03:00
|
|
|
return chance_of_adding_vector_type_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfAdjustingBranchWeights() const {
|
2020-05-14 13:38:34 +03:00
|
|
|
return chance_of_adjusting_branch_weights_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfAdjustingFunctionControl() const {
|
2019-10-11 09:10:47 +03:00
|
|
|
return chance_of_adjusting_function_control_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfAdjustingLoopControl() const {
|
2019-10-10 15:34:38 +03:00
|
|
|
return chance_of_adjusting_loop_control_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfAdjustingMemoryOperandsMask() const {
|
2019-10-22 20:05:35 +03:00
|
|
|
return chance_of_adjusting_memory_operands_mask_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfAdjustingSelectionControl() const {
|
2019-10-08 13:25:34 +03:00
|
|
|
return chance_of_adjusting_selection_control_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfCallingFunction() const {
|
|
|
|
return chance_of_calling_function_;
|
|
|
|
}
|
|
|
|
uint32_t GetChanceOfChoosingStructTypeVsArrayType() const {
|
2020-02-04 17:00:19 +03:00
|
|
|
return chance_of_choosing_struct_type_vs_array_type_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfChoosingWorkgroupStorageClass() const {
|
2020-07-08 01:46:47 +03:00
|
|
|
return chance_of_choosing_workgroup_storage_class_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfConstructingComposite() const {
|
2019-10-08 16:04:10 +03:00
|
|
|
return chance_of_constructing_composite_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfCopyingObject() const {
|
|
|
|
return chance_of_copying_object_;
|
|
|
|
}
|
|
|
|
uint32_t GetChanceOfCreatingIntSynonymsUsingLoops() const {
|
2020-09-23 16:10:02 +03:00
|
|
|
return chance_of_creating_int_synonyms_using_loops_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfDonatingAdditionalModule() const {
|
2020-01-07 11:39:55 +03:00
|
|
|
return chance_of_donating_additional_module_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfDuplicatingRegionWithSelection() const {
|
2020-09-11 13:48:19 +03:00
|
|
|
return chance_of_duplicating_region_with_selection_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfExpandingVectorReduction() const {
|
2020-10-23 16:59:08 +03:00
|
|
|
return chance_of_expanding_vector_reduction_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfFlatteningConditionalBranch() const {
|
2020-09-15 12:31:01 +03:00
|
|
|
return chance_of_flattening_conditional_branch_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfGoingDeeperToExtractComposite() const {
|
2020-10-23 16:49:50 +03:00
|
|
|
return chance_of_going_deeper_to_extract_composite_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfGoingDeeperToInsertInComposite() const {
|
2020-08-19 15:56:03 +03:00
|
|
|
return chance_of_going_deeper_to_insert_in_composite_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfGoingDeeperWhenMakingAccessChain() const {
|
2020-02-12 02:10:57 +03:00
|
|
|
return chance_of_going_deeper_when_making_access_chain_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfHavingTwoBlocksInLoopToCreateIntSynonym() const {
|
2020-09-23 16:10:02 +03:00
|
|
|
return chance_of_having_two_blocks_in_loop_to_create_int_synonym_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfInliningFunction() const {
|
2020-08-25 19:28:23 +03:00
|
|
|
return chance_of_inlining_function_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfInterchangingSignednessOfIntegerOperands() const {
|
2020-07-30 21:48:29 +03:00
|
|
|
return chance_of_interchanging_signedness_of_integer_operands_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfInterchangingZeroLikeConstants() const {
|
2020-07-15 14:58:29 +03:00
|
|
|
return chance_of_interchanging_zero_like_constants_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfInvertingComparisonOperators() const {
|
2020-07-03 19:37:32 +03:00
|
|
|
return chance_of_inverting_comparison_operators_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t ChanceOfMakingDonorLivesafe() const {
|
2020-01-29 18:52:31 +03:00
|
|
|
return chance_of_making_donor_livesafe_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfMakingVectorOperationDynamic() const {
|
2020-08-06 17:50:18 +03:00
|
|
|
return chance_of_making_vector_operation_dynamic_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfMergingBlocks() const {
|
|
|
|
return chance_of_merging_blocks_;
|
|
|
|
}
|
|
|
|
uint32_t GetChanceOfMergingFunctionReturns() const {
|
2020-10-02 06:45:44 +03:00
|
|
|
return chance_of_merging_function_returns_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfMovingBlockDown() const {
|
|
|
|
return chance_of_moving_block_down_;
|
|
|
|
}
|
|
|
|
uint32_t GetChanceOfMutatingPointer() const {
|
|
|
|
return chance_of_mutating_pointer_;
|
|
|
|
}
|
|
|
|
uint32_t GetChanceOfObfuscatingConstant() const {
|
2019-06-18 20:41:08 +03:00
|
|
|
return chance_of_obfuscating_constant_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfOutliningFunction() const {
|
2019-12-10 17:47:42 +03:00
|
|
|
return chance_of_outlining_function_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfPermutingInstructions() const {
|
2020-08-03 18:45:24 +03:00
|
|
|
return chance_of_permuting_instructions_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfPermutingParameters() const {
|
2020-03-08 17:27:05 +03:00
|
|
|
return chance_of_permuting_parameters_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfPermutingPhiOperands() const {
|
2020-06-23 17:00:28 +03:00
|
|
|
return chance_of_permuting_phi_operands_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfPropagatingInstructionsDown() const {
|
2020-10-06 15:38:19 +03:00
|
|
|
return chance_of_propagating_instructions_down_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfPropagatingInstructionsUp() const {
|
2020-08-11 12:24:32 +03:00
|
|
|
return chance_of_propagating_instructions_up_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfPushingIdThroughVariable() const {
|
2020-05-29 18:43:38 +03:00
|
|
|
return chance_of_pushing_id_through_variable_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfReplacingAddSubMulWithCarryingExtended() const {
|
2020-08-06 19:30:34 +03:00
|
|
|
return chance_of_replacing_add_sub_mul_with_carrying_extended_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfReplacingBranchFromDeadBlockWithExit() const {
|
2020-10-06 17:59:05 +03:00
|
|
|
return chance_of_replacing_branch_from_dead_block_with_exit_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfReplacingCopyMemoryWithLoadStore() const {
|
2020-07-23 17:14:20 +03:00
|
|
|
return chance_of_replacing_copy_memory_with_load_store_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfReplacingCopyObjectWithStoreLoad() const {
|
2020-07-23 10:17:45 +03:00
|
|
|
return chance_of_replacing_copyobject_with_store_load_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfReplacingIdWithSynonym() const {
|
2019-09-18 22:47:08 +03:00
|
|
|
return chance_of_replacing_id_with_synonym_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfReplacingIrrelevantId() const {
|
2020-09-01 18:28:04 +03:00
|
|
|
return chance_of_replacing_irrelevant_id_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfReplacingLinearAlgebraInstructions() const {
|
2020-06-16 13:20:51 +03:00
|
|
|
return chance_of_replacing_linear_algebra_instructions_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfReplacingLoadStoreWithCopyMemory() const {
|
2020-07-27 17:17:04 +03:00
|
|
|
return chance_of_replacing_load_store_with_copy_memory_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfReplacingOpPhiIdFromDeadPredecessor() const {
|
2020-09-02 03:06:38 +03:00
|
|
|
return chance_of_replacing_opphi_id_from_dead_predecessor_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfReplacingOpselectWithConditionalBranch() const {
|
2020-09-03 12:19:02 +03:00
|
|
|
return chance_of_replacing_opselect_with_conditional_branch_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfReplacingParametersWithGlobals() const {
|
2020-07-09 13:03:49 +03:00
|
|
|
return chance_of_replacing_parameters_with_globals_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfReplacingParametersWithStruct() const {
|
2020-07-21 23:02:32 +03:00
|
|
|
return chance_of_replacing_parameters_with_struct_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfSplittingBlock() const {
|
|
|
|
return chance_of_splitting_block_;
|
|
|
|
}
|
|
|
|
uint32_t GetChanceOfSwappingConditionalBranchOperands() const {
|
2020-06-19 18:38:52 +03:00
|
|
|
return chance_of_swapping_conditional_branch_operands_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfTogglingAccessChainInstruction() const {
|
2020-03-09 01:33:24 +03:00
|
|
|
return chance_of_toggling_access_chain_instruction_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetChanceOfWrappingRegionInSelection() const {
|
2020-10-01 11:54:10 +03:00
|
|
|
return chance_of_wrapping_region_in_selection_;
|
|
|
|
}
|
2020-04-20 21:02:49 +03:00
|
|
|
|
|
|
|
// Other functions to control transformations. Keep them in alphabetical
|
|
|
|
// order.
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetMaximumEquivalenceClassSizeForDataSynonymFactClosure() const {
|
2020-04-20 21:02:49 +03:00
|
|
|
return max_equivalence_class_size_for_data_synonym_fact_closure_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetMaximumNumberOfFunctionParameters() const {
|
2020-06-23 19:40:44 +03:00
|
|
|
return max_number_of_function_parameters_;
|
|
|
|
}
|
2020-11-03 19:51:10 +03:00
|
|
|
uint32_t GetMaximumNumberOfParametersReplacedWithStruct() const {
|
2020-07-21 23:02:32 +03:00
|
|
|
return max_number_of_parameters_replaced_with_struct_;
|
|
|
|
}
|
2020-05-14 13:38:34 +03:00
|
|
|
std::pair<uint32_t, uint32_t> GetRandomBranchWeights() {
|
|
|
|
std::pair<uint32_t, uint32_t> branch_weights = {0, 0};
|
|
|
|
|
|
|
|
while (branch_weights.first == 0 && branch_weights.second == 0) {
|
|
|
|
// Using INT32_MAX to do not overflow UINT32_MAX when the branch weights
|
|
|
|
// are added together.
|
|
|
|
branch_weights.first = random_generator_->RandomUint32(INT32_MAX);
|
|
|
|
branch_weights.second = random_generator_->RandomUint32(INT32_MAX);
|
|
|
|
}
|
|
|
|
|
|
|
|
return branch_weights;
|
|
|
|
}
|
2020-06-16 13:21:31 +03:00
|
|
|
std::vector<uint32_t> GetRandomComponentsForVectorShuffle(
|
|
|
|
uint32_t max_component_index) {
|
|
|
|
// Component count must be in range [2, 4].
|
|
|
|
std::vector<uint32_t> components(random_generator_->RandomUint32(2) + 2);
|
|
|
|
|
|
|
|
for (uint32_t& component : components) {
|
|
|
|
component = random_generator_->RandomUint32(max_component_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
return components;
|
|
|
|
}
|
2020-10-23 16:49:50 +03:00
|
|
|
uint32_t GetRandomCompositeExtractIndex(uint32_t number_of_members) {
|
|
|
|
assert(number_of_members > 0 && "Composite object must have some members");
|
|
|
|
return ChooseBetweenMinAndMax({0, number_of_members - 1});
|
|
|
|
}
|
2020-06-16 13:21:31 +03:00
|
|
|
uint32_t GetRandomIndexForAccessChain(uint32_t composite_size_bound) {
|
|
|
|
return random_generator_->RandomUint32(composite_size_bound);
|
|
|
|
}
|
2020-08-19 15:56:03 +03:00
|
|
|
uint32_t GetRandomIndexForCompositeInsert(uint32_t number_of_components) {
|
|
|
|
return random_generator_->RandomUint32(number_of_components);
|
|
|
|
}
|
2020-09-23 16:10:02 +03:00
|
|
|
int64_t GetRandomValueForStepConstantInLoop() {
|
|
|
|
return random_generator_->RandomUint64(UINT64_MAX);
|
|
|
|
}
|
2020-06-16 13:21:31 +03:00
|
|
|
uint32_t GetRandomLoopControlPartialCount() {
|
|
|
|
return random_generator_->RandomUint32(max_loop_control_partial_count_);
|
|
|
|
}
|
|
|
|
uint32_t GetRandomLoopControlPeelCount() {
|
|
|
|
return random_generator_->RandomUint32(max_loop_control_peel_count_);
|
|
|
|
}
|
|
|
|
uint32_t GetRandomLoopLimit() {
|
|
|
|
return random_generator_->RandomUint32(max_loop_limit_);
|
|
|
|
}
|
2020-09-23 16:10:02 +03:00
|
|
|
uint32_t GetRandomNumberOfLoopIterations(uint32_t max_num_iterations) {
|
|
|
|
return ChooseBetweenMinAndMax({1, max_num_iterations});
|
|
|
|
}
|
2020-06-23 19:40:44 +03:00
|
|
|
uint32_t GetRandomNumberOfNewParameters(uint32_t num_of_params) {
|
|
|
|
assert(num_of_params < GetMaximumNumberOfFunctionParameters());
|
|
|
|
return ChooseBetweenMinAndMax(
|
|
|
|
{1, std::min(max_number_of_new_parameters_,
|
|
|
|
GetMaximumNumberOfFunctionParameters() - num_of_params)});
|
|
|
|
}
|
2020-07-21 23:02:32 +03:00
|
|
|
uint32_t GetRandomNumberOfParametersReplacedWithStruct(uint32_t num_params) {
|
|
|
|
assert(num_params != 0 && "A function must have parameters to replace");
|
|
|
|
return ChooseBetweenMinAndMax(
|
|
|
|
{1, std::min(num_params,
|
|
|
|
GetMaximumNumberOfParametersReplacedWithStruct())});
|
|
|
|
}
|
2020-02-04 17:00:19 +03:00
|
|
|
uint32_t GetRandomSizeForNewArray() {
|
|
|
|
// Ensure that the array size is non-zero.
|
|
|
|
return random_generator_->RandomUint32(max_new_array_size_limit_ - 1) + 1;
|
|
|
|
}
|
2020-07-12 11:59:08 +03:00
|
|
|
protobufs::TransformationAddSynonym::SynonymType GetRandomSynonymType();
|
2020-07-08 19:07:04 +03:00
|
|
|
uint32_t GetRandomUnusedComponentCountForImageSample(
|
|
|
|
uint32_t max_unused_component_count) {
|
|
|
|
// Ensure that the number of unused components is non-zero.
|
|
|
|
return random_generator_->RandomUint32(max_unused_component_count) + 1;
|
|
|
|
}
|
2019-09-10 17:02:25 +03:00
|
|
|
bool GoDeeperInConstantObfuscation(uint32_t depth) {
|
2021-03-03 18:34:53 +03:00
|
|
|
return go_deeper_in_constant_obfuscation_(depth, random_generator_.get());
|
2019-06-18 20:41:08 +03:00
|
|
|
}
|
|
|
|
|
2019-05-27 16:34:55 +03:00
|
|
|
private:
|
|
|
|
// The source of randomness.
|
2021-03-03 18:34:53 +03:00
|
|
|
std::unique_ptr<RandomGenerator> random_generator_;
|
2019-05-27 16:34:55 +03:00
|
|
|
// The next fresh id to be issued.
|
|
|
|
uint32_t next_fresh_id_;
|
2019-05-29 18:42:46 +03:00
|
|
|
|
|
|
|
// Probabilities associated with applying various transformations.
|
|
|
|
// Keep them in alphabetical order.
|
2020-09-18 17:51:35 +03:00
|
|
|
uint32_t chance_of_accepting_repeated_pass_recommendation_;
|
2020-02-12 02:10:57 +03:00
|
|
|
uint32_t chance_of_adding_access_chain_;
|
2020-09-18 17:51:35 +03:00
|
|
|
uint32_t chance_of_adding_another_pass_to_pass_loop_;
|
2020-02-04 17:00:19 +03:00
|
|
|
uint32_t chance_of_adding_another_struct_field_;
|
|
|
|
uint32_t chance_of_adding_array_or_struct_type_;
|
2020-09-16 01:36:23 +03:00
|
|
|
uint32_t chance_of_adding_bit_instruction_synonym_;
|
2020-09-03 12:19:02 +03:00
|
|
|
uint32_t chance_of_adding_both_branches_when_replacing_opselect_;
|
2020-10-23 16:49:50 +03:00
|
|
|
uint32_t chance_of_adding_composite_extract_;
|
2020-08-19 15:56:03 +03:00
|
|
|
uint32_t chance_of_adding_composite_insert_;
|
2020-06-30 23:13:05 +03:00
|
|
|
uint32_t chance_of_adding_copy_memory_;
|
2020-01-14 01:04:01 +03:00
|
|
|
uint32_t chance_of_adding_dead_block_;
|
2019-06-05 10:02:16 +03:00
|
|
|
uint32_t chance_of_adding_dead_break_;
|
2019-07-25 15:50:33 +03:00
|
|
|
uint32_t chance_of_adding_dead_continue_;
|
2020-03-04 17:54:08 +03:00
|
|
|
uint32_t chance_of_adding_equation_instruction_;
|
2020-02-06 00:07:44 +03:00
|
|
|
uint32_t chance_of_adding_global_variable_;
|
2020-07-08 19:07:04 +03:00
|
|
|
uint32_t chance_of_adding_image_sample_unused_components_;
|
2020-02-06 19:54:34 +03:00
|
|
|
uint32_t chance_of_adding_load_;
|
2020-02-06 00:07:44 +03:00
|
|
|
uint32_t chance_of_adding_local_variable_;
|
2020-08-14 14:44:28 +03:00
|
|
|
uint32_t chance_of_adding_loop_preheader_;
|
2020-02-04 17:00:19 +03:00
|
|
|
uint32_t chance_of_adding_matrix_type_;
|
2019-10-11 11:15:47 +03:00
|
|
|
uint32_t chance_of_adding_no_contraction_decoration_;
|
2020-08-27 17:59:54 +03:00
|
|
|
uint32_t chance_of_adding_opphi_synonym_;
|
2020-06-23 19:40:44 +03:00
|
|
|
uint32_t chance_of_adding_parameters;
|
2020-07-20 15:13:07 +03:00
|
|
|
uint32_t chance_of_adding_relaxed_decoration_;
|
2020-02-06 19:54:34 +03:00
|
|
|
uint32_t chance_of_adding_store_;
|
2020-07-12 11:59:08 +03:00
|
|
|
uint32_t chance_of_adding_synonyms_;
|
2020-09-03 12:19:02 +03:00
|
|
|
uint32_t chance_of_adding_true_branch_when_replacing_opselect_;
|
2020-06-16 13:21:31 +03:00
|
|
|
uint32_t chance_of_adding_vector_shuffle_;
|
2020-02-04 17:00:19 +03:00
|
|
|
uint32_t chance_of_adding_vector_type_;
|
2020-05-14 13:38:34 +03:00
|
|
|
uint32_t chance_of_adjusting_branch_weights_;
|
2019-10-11 09:10:47 +03:00
|
|
|
uint32_t chance_of_adjusting_function_control_;
|
2019-10-10 15:34:38 +03:00
|
|
|
uint32_t chance_of_adjusting_loop_control_;
|
2019-10-22 20:05:35 +03:00
|
|
|
uint32_t chance_of_adjusting_memory_operands_mask_;
|
2019-10-08 13:25:34 +03:00
|
|
|
uint32_t chance_of_adjusting_selection_control_;
|
2020-02-11 02:22:34 +03:00
|
|
|
uint32_t chance_of_calling_function_;
|
2020-02-04 17:00:19 +03:00
|
|
|
uint32_t chance_of_choosing_struct_type_vs_array_type_;
|
2020-07-08 01:46:47 +03:00
|
|
|
uint32_t chance_of_choosing_workgroup_storage_class_;
|
2019-10-08 16:04:10 +03:00
|
|
|
uint32_t chance_of_constructing_composite_;
|
2019-09-12 01:45:20 +03:00
|
|
|
uint32_t chance_of_copying_object_;
|
2020-09-23 16:10:02 +03:00
|
|
|
uint32_t chance_of_creating_int_synonyms_using_loops_;
|
2020-01-07 11:39:55 +03:00
|
|
|
uint32_t chance_of_donating_additional_module_;
|
2020-09-11 13:48:19 +03:00
|
|
|
uint32_t chance_of_duplicating_region_with_selection_;
|
2020-10-23 16:59:08 +03:00
|
|
|
uint32_t chance_of_expanding_vector_reduction_;
|
2020-09-15 12:31:01 +03:00
|
|
|
uint32_t chance_of_flattening_conditional_branch_;
|
2020-10-23 16:49:50 +03:00
|
|
|
uint32_t chance_of_going_deeper_to_extract_composite_;
|
2020-08-19 15:56:03 +03:00
|
|
|
uint32_t chance_of_going_deeper_to_insert_in_composite_;
|
2020-02-12 02:10:57 +03:00
|
|
|
uint32_t chance_of_going_deeper_when_making_access_chain_;
|
2020-09-23 16:10:02 +03:00
|
|
|
uint32_t chance_of_having_two_blocks_in_loop_to_create_int_synonym_;
|
2020-08-25 19:28:23 +03:00
|
|
|
uint32_t chance_of_inlining_function_;
|
2020-07-30 21:48:29 +03:00
|
|
|
uint32_t chance_of_interchanging_signedness_of_integer_operands_;
|
2020-07-15 14:58:29 +03:00
|
|
|
uint32_t chance_of_interchanging_zero_like_constants_;
|
2020-07-03 19:37:32 +03:00
|
|
|
uint32_t chance_of_inverting_comparison_operators_;
|
2020-01-29 18:52:31 +03:00
|
|
|
uint32_t chance_of_making_donor_livesafe_;
|
2020-08-06 17:50:18 +03:00
|
|
|
uint32_t chance_of_making_vector_operation_dynamic_;
|
2019-12-12 18:27:40 +03:00
|
|
|
uint32_t chance_of_merging_blocks_;
|
2020-10-02 06:45:44 +03:00
|
|
|
uint32_t chance_of_merging_function_returns_;
|
2019-05-31 11:59:06 +03:00
|
|
|
uint32_t chance_of_moving_block_down_;
|
2020-09-01 14:45:13 +03:00
|
|
|
uint32_t chance_of_mutating_pointer_;
|
2019-06-18 20:41:08 +03:00
|
|
|
uint32_t chance_of_obfuscating_constant_;
|
2019-12-10 17:47:42 +03:00
|
|
|
uint32_t chance_of_outlining_function_;
|
2020-08-03 18:45:24 +03:00
|
|
|
uint32_t chance_of_permuting_instructions_;
|
2020-03-08 17:27:05 +03:00
|
|
|
uint32_t chance_of_permuting_parameters_;
|
2020-06-23 17:00:28 +03:00
|
|
|
uint32_t chance_of_permuting_phi_operands_;
|
2020-10-06 15:38:19 +03:00
|
|
|
uint32_t chance_of_propagating_instructions_down_;
|
2020-08-11 12:24:32 +03:00
|
|
|
uint32_t chance_of_propagating_instructions_up_;
|
2020-05-29 18:43:38 +03:00
|
|
|
uint32_t chance_of_pushing_id_through_variable_;
|
2020-08-06 19:30:34 +03:00
|
|
|
uint32_t chance_of_replacing_add_sub_mul_with_carrying_extended_;
|
2020-10-06 17:59:05 +03:00
|
|
|
uint32_t chance_of_replacing_branch_from_dead_block_with_exit_;
|
2020-07-23 17:14:20 +03:00
|
|
|
uint32_t chance_of_replacing_copy_memory_with_load_store_;
|
2020-07-23 10:17:45 +03:00
|
|
|
uint32_t chance_of_replacing_copyobject_with_store_load_;
|
2019-09-18 22:47:08 +03:00
|
|
|
uint32_t chance_of_replacing_id_with_synonym_;
|
2020-09-01 18:28:04 +03:00
|
|
|
uint32_t chance_of_replacing_irrelevant_id_;
|
2020-06-16 13:20:51 +03:00
|
|
|
uint32_t chance_of_replacing_linear_algebra_instructions_;
|
2020-07-27 17:17:04 +03:00
|
|
|
uint32_t chance_of_replacing_load_store_with_copy_memory_;
|
2020-09-02 03:06:38 +03:00
|
|
|
uint32_t chance_of_replacing_opphi_id_from_dead_predecessor_;
|
2020-09-03 12:19:02 +03:00
|
|
|
uint32_t chance_of_replacing_opselect_with_conditional_branch_;
|
2020-07-09 13:03:49 +03:00
|
|
|
uint32_t chance_of_replacing_parameters_with_globals_;
|
2020-07-21 23:02:32 +03:00
|
|
|
uint32_t chance_of_replacing_parameters_with_struct_;
|
2019-05-29 18:42:46 +03:00
|
|
|
uint32_t chance_of_splitting_block_;
|
2020-06-19 18:38:52 +03:00
|
|
|
uint32_t chance_of_swapping_conditional_branch_operands_;
|
2020-03-09 01:33:24 +03:00
|
|
|
uint32_t chance_of_toggling_access_chain_instruction_;
|
2020-10-01 11:54:10 +03:00
|
|
|
uint32_t chance_of_wrapping_region_in_selection_;
|
2019-06-18 20:41:08 +03:00
|
|
|
|
2019-10-10 15:34:38 +03:00
|
|
|
// Limits associated with various quantities for which random values are
|
|
|
|
// chosen during fuzzing.
|
|
|
|
// Keep them in alphabetical order.
|
2020-04-20 21:02:49 +03:00
|
|
|
uint32_t max_equivalence_class_size_for_data_synonym_fact_closure_;
|
2019-10-10 15:34:38 +03:00
|
|
|
uint32_t max_loop_control_partial_count_;
|
|
|
|
uint32_t max_loop_control_peel_count_;
|
2020-01-29 18:52:31 +03:00
|
|
|
uint32_t max_loop_limit_;
|
2020-02-04 17:00:19 +03:00
|
|
|
uint32_t max_new_array_size_limit_;
|
2020-06-23 19:40:44 +03:00
|
|
|
uint32_t max_number_of_function_parameters_;
|
|
|
|
uint32_t max_number_of_new_parameters_;
|
2020-07-21 23:02:32 +03:00
|
|
|
uint32_t max_number_of_parameters_replaced_with_struct_;
|
2019-10-10 15:34:38 +03:00
|
|
|
|
2019-06-18 20:41:08 +03:00
|
|
|
// Functions to determine with what probability to go deeper when generating
|
|
|
|
// or mutating constructs recursively.
|
|
|
|
const std::function<bool(uint32_t, RandomGenerator*)>&
|
|
|
|
go_deeper_in_constant_obfuscation_;
|
2019-09-23 18:29:19 +03:00
|
|
|
|
|
|
|
// Requires |min_max.first| <= |min_max.second|, and returns a value in the
|
|
|
|
// range [ |min_max.first|, |min_max.second| ]
|
|
|
|
uint32_t ChooseBetweenMinAndMax(const std::pair<uint32_t, uint32_t>& min_max);
|
2019-05-27 16:34:55 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace fuzz
|
|
|
|
} // namespace spvtools
|
|
|
|
|
|
|
|
#endif // SOURCE_FUZZ_FUZZER_CONTEXT_H_
|