2019-05-27 13:34:55 +00: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.
|
|
|
|
|
|
|
|
#include "source/fuzz/fuzzer.h"
|
|
|
|
|
|
|
|
#include <cassert>
|
2019-09-23 15:29:19 +00:00
|
|
|
#include <memory>
|
2020-09-18 14:51:35 +00:00
|
|
|
#include <numeric>
|
2020-10-13 18:48:18 +00:00
|
|
|
#include <sstream>
|
2019-05-27 13:34:55 +00:00
|
|
|
|
2020-08-25 17:11:37 +00:00
|
|
|
#include "source/fuzz/fact_manager/fact_manager.h"
|
2019-05-27 13:34:55 +00:00
|
|
|
#include "source/fuzz/fuzzer_context.h"
|
2020-02-11 23:10:57 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_add_access_chains.h"
|
2020-09-15 22:36:23 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_add_bit_instruction_synonyms.h"
|
2020-08-19 12:56:03 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_add_composite_inserts.h"
|
2020-02-04 14:00:19 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_add_composite_types.h"
|
2020-06-30 20:13:05 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_add_copy_memory.h"
|
2020-01-13 22:04:01 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_add_dead_blocks.h"
|
2019-06-05 07:02:16 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_add_dead_breaks.h"
|
2019-07-25 12:50:33 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_add_dead_continues.h"
|
2020-03-04 14:54:08 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_add_equation_instructions.h"
|
2020-02-10 23:22:34 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_add_function_calls.h"
|
2020-02-05 21:07:44 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_add_global_variables.h"
|
2020-07-08 16:07:04 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_add_image_sample_unused_components.h"
|
2020-02-06 16:54:34 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_add_loads.h"
|
2020-02-05 21:07:44 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_add_local_variables.h"
|
2020-08-14 11:44:28 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_add_loop_preheaders.h"
|
2020-09-23 13:10:02 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_add_loops_to_create_int_constant_synonyms.h"
|
2019-10-11 08:15:47 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_add_no_contraction_decorations.h"
|
2020-08-27 14:59:54 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_add_opphi_synonyms.h"
|
2020-06-23 16:40:44 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_add_parameters.h"
|
2020-08-19 12:56:03 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_add_relaxed_decorations.h"
|
2020-02-06 16:54:34 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_add_stores.h"
|
2020-07-12 08:59:08 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_add_synonyms.h"
|
2020-06-16 10:21:31 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_add_vector_shuffle_instructions.h"
|
2020-05-14 10:38:34 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_adjust_branch_weights.h"
|
2019-10-11 06:10:47 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_adjust_function_controls.h"
|
2019-10-10 12:34:38 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_adjust_loop_controls.h"
|
2020-07-15 11:58:29 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_adjust_memory_operands_masks.h"
|
2019-10-08 10:25:34 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_adjust_selection_controls.h"
|
2019-09-18 19:47:08 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_apply_id_synonyms.h"
|
2019-10-08 13:04:10 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_construct_composites.h"
|
2019-09-11 22:45:20 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_copy_objects.h"
|
2020-01-07 08:39:55 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_donate_modules.h"
|
2020-09-11 10:48:19 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_duplicate_regions_with_selections.h"
|
2020-09-15 09:31:01 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_flatten_conditional_branches.h"
|
2020-08-25 16:28:23 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_inline_functions.h"
|
2020-07-30 18:48:29 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_interchange_signedness_of_integer_operands.h"
|
2020-07-15 11:58:29 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_interchange_zero_like_constants.h"
|
2020-07-03 16:37:32 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_invert_comparison_operators.h"
|
2020-08-06 14:50:18 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_make_vector_operations_dynamic.h"
|
2019-12-12 15:27:40 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_merge_blocks.h"
|
2020-10-02 03:45:44 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_merge_function_returns.h"
|
2020-09-01 11:45:13 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_mutate_pointers.h"
|
2019-06-18 17:41:08 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_obfuscate_constants.h"
|
2019-12-10 14:47:42 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_outline_functions.h"
|
2019-05-31 08:59:06 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_permute_blocks.h"
|
2020-03-08 14:27:05 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_permute_function_parameters.h"
|
2020-08-03 15:45:24 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_permute_instructions.h"
|
2020-06-23 14:00:28 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_permute_phi_operands.h"
|
2020-10-06 12:38:19 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_propagate_instructions_down.h"
|
2020-08-11 09:24:32 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_propagate_instructions_up.h"
|
2020-05-29 15:43:38 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_push_ids_through_variables.h"
|
2020-08-19 12:56:03 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_replace_adds_subs_muls_with_carrying_extended.h"
|
2020-10-06 14:59:05 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_replace_branches_from_dead_blocks_with_exits.h"
|
2020-08-19 12:56:03 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_replace_copy_memories_with_loads_stores.h"
|
|
|
|
#include "source/fuzz/fuzzer_pass_replace_copy_objects_with_stores_loads.h"
|
2020-09-01 15:28:04 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_replace_irrelevant_ids.h"
|
2020-06-16 10:20:51 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_replace_linear_algebra_instructions.h"
|
2020-08-19 12:56:03 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_replace_loads_stores_with_copy_memories.h"
|
2020-09-02 00:06:38 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_replace_opphi_ids_from_dead_predecessors.h"
|
2020-09-03 09:19:02 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_replace_opselects_with_conditional_branches.h"
|
2020-07-09 10:03:49 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_replace_parameter_with_global.h"
|
2020-07-21 20:02:32 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_replace_params_with_struct.h"
|
2019-05-29 15:42:46 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_split_blocks.h"
|
2020-03-05 08:18:39 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_swap_commutable_operands.h"
|
2020-06-19 15:38:52 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_swap_conditional_branch_operands.h"
|
2020-03-08 22:33:24 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_toggle_access_chain_instruction.h"
|
2020-10-01 08:54:10 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_wrap_regions_in_selections.h"
|
2020-09-18 14:51:35 +00:00
|
|
|
#include "source/fuzz/pass_management/repeated_pass_manager.h"
|
|
|
|
#include "source/fuzz/pass_management/repeated_pass_manager_looped_with_recommendations.h"
|
|
|
|
#include "source/fuzz/pass_management/repeated_pass_manager_random_with_recommendations.h"
|
|
|
|
#include "source/fuzz/pass_management/repeated_pass_manager_simple.h"
|
|
|
|
#include "source/fuzz/pass_management/repeated_pass_recommender_standard.h"
|
2019-05-27 13:34:55 +00:00
|
|
|
#include "source/fuzz/protobufs/spirvfuzz_protobufs.h"
|
2020-04-02 14:54:46 +00:00
|
|
|
#include "source/fuzz/transformation_context.h"
|
2019-05-27 13:34:55 +00:00
|
|
|
#include "source/opt/build_module.h"
|
|
|
|
#include "source/spirv_fuzzer_options.h"
|
|
|
|
#include "source/util/make_unique.h"
|
|
|
|
|
|
|
|
namespace spvtools {
|
|
|
|
namespace fuzz {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
const uint32_t kIdBoundGap = 100;
|
2019-09-23 15:29:19 +00:00
|
|
|
|
2020-09-18 14:51:35 +00:00
|
|
|
const uint32_t kTransformationLimit = 2000;
|
2019-05-27 13:34:55 +00:00
|
|
|
|
2019-09-23 15:29:19 +00:00
|
|
|
} // namespace
|
|
|
|
|
2020-09-22 09:07:58 +00:00
|
|
|
Fuzzer::Fuzzer(spv_target_env target_env, MessageConsumer consumer,
|
|
|
|
const std::vector<uint32_t>& binary_in,
|
|
|
|
const protobufs::FactSequence& initial_facts,
|
|
|
|
const std::vector<fuzzerutil::ModuleSupplier>& donor_suppliers,
|
|
|
|
std::unique_ptr<RandomGenerator> random_generator,
|
|
|
|
bool enable_all_passes,
|
2020-09-18 14:51:35 +00:00
|
|
|
RepeatedPassStrategy repeated_pass_strategy,
|
2020-04-02 13:37:59 +00:00
|
|
|
bool validate_after_each_fuzzer_pass,
|
|
|
|
spv_validator_options validator_options)
|
2020-08-28 12:30:06 +00:00
|
|
|
: target_env_(target_env),
|
2020-09-22 09:07:58 +00:00
|
|
|
consumer_(std::move(consumer)),
|
|
|
|
binary_in_(binary_in),
|
|
|
|
initial_facts_(initial_facts),
|
|
|
|
donor_suppliers_(donor_suppliers),
|
|
|
|
random_generator_(std::move(random_generator)),
|
2020-09-18 14:51:35 +00:00
|
|
|
enable_all_passes_(enable_all_passes),
|
|
|
|
repeated_pass_strategy_(repeated_pass_strategy),
|
2020-08-28 12:30:06 +00:00
|
|
|
validate_after_each_fuzzer_pass_(validate_after_each_fuzzer_pass),
|
2020-09-18 14:51:35 +00:00
|
|
|
validator_options_(validator_options),
|
2020-09-22 09:07:58 +00:00
|
|
|
num_repeated_passes_applied_(0),
|
|
|
|
ir_context_(nullptr),
|
|
|
|
fuzzer_context_(nullptr),
|
|
|
|
transformation_context_(nullptr),
|
|
|
|
transformation_sequence_out_() {}
|
2019-05-27 13:34:55 +00:00
|
|
|
|
|
|
|
Fuzzer::~Fuzzer() = default;
|
|
|
|
|
2020-09-18 14:51:35 +00:00
|
|
|
template <typename FuzzerPassT, typename... Args>
|
2020-10-01 08:07:06 +00:00
|
|
|
void Fuzzer::MaybeAddRepeatedPass(uint32_t percentage_chance_of_adding_pass,
|
|
|
|
RepeatedPassInstances* pass_instances,
|
2020-09-22 09:07:58 +00:00
|
|
|
Args&&... extra_args) {
|
2020-10-01 08:07:06 +00:00
|
|
|
if (enable_all_passes_ ||
|
|
|
|
fuzzer_context_->ChoosePercentage(percentage_chance_of_adding_pass)) {
|
2020-09-18 14:51:35 +00:00
|
|
|
pass_instances->SetPass(MakeUnique<FuzzerPassT>(
|
2020-09-22 09:07:58 +00:00
|
|
|
ir_context_.get(), transformation_context_.get(), fuzzer_context_.get(),
|
|
|
|
&transformation_sequence_out_, std::forward<Args>(extra_args)...));
|
2020-09-18 14:51:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename FuzzerPassT, typename... Args>
|
2020-09-22 09:07:58 +00:00
|
|
|
void Fuzzer::MaybeAddFinalPass(std::vector<std::unique_ptr<FuzzerPass>>* passes,
|
|
|
|
Args&&... extra_args) {
|
|
|
|
if (enable_all_passes_ || fuzzer_context_->ChooseEven()) {
|
2020-09-18 14:51:35 +00:00
|
|
|
passes->push_back(MakeUnique<FuzzerPassT>(
|
2020-09-22 09:07:58 +00:00
|
|
|
ir_context_.get(), transformation_context_.get(), fuzzer_context_.get(),
|
|
|
|
&transformation_sequence_out_, std::forward<Args>(extra_args)...));
|
2020-09-18 14:51:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-16 21:58:09 +00:00
|
|
|
bool Fuzzer::ApplyPassAndCheckValidity(FuzzerPass* pass) const {
|
2019-11-27 18:05:56 +00:00
|
|
|
pass->Apply();
|
2020-10-16 21:58:09 +00:00
|
|
|
return !validate_after_each_fuzzer_pass_ ||
|
|
|
|
fuzzerutil::IsValidAndWellFormed(ir_context_.get(), validator_options_,
|
|
|
|
consumer_);
|
2019-11-27 18:05:56 +00:00
|
|
|
}
|
|
|
|
|
2020-09-22 09:07:58 +00:00
|
|
|
Fuzzer::FuzzerResult Fuzzer::Run() {
|
2019-05-27 13:34:55 +00:00
|
|
|
// Check compatibility between the library version being linked with and the
|
|
|
|
// header files being used.
|
|
|
|
GOOGLE_PROTOBUF_VERIFY_VERSION;
|
|
|
|
|
2020-09-22 09:07:58 +00:00
|
|
|
assert(ir_context_ == nullptr && fuzzer_context_ == nullptr &&
|
|
|
|
transformation_context_ == nullptr &&
|
|
|
|
transformation_sequence_out_.transformation_size() == 0 &&
|
|
|
|
"'Run' must not be invoked more than once.");
|
|
|
|
|
2020-08-28 12:30:06 +00:00
|
|
|
spvtools::SpirvTools tools(target_env_);
|
|
|
|
tools.SetMessageConsumer(consumer_);
|
2019-05-27 13:34:55 +00:00
|
|
|
if (!tools.IsValid()) {
|
2020-08-28 12:30:06 +00:00
|
|
|
consumer_(SPV_MSG_ERROR, nullptr, {},
|
|
|
|
"Failed to create SPIRV-Tools interface; stopping.");
|
2020-09-22 09:07:58 +00:00
|
|
|
return {Fuzzer::FuzzerResultStatus::kFailedToCreateSpirvToolsInterface,
|
|
|
|
std::vector<uint32_t>(), protobufs::TransformationSequence()};
|
2019-05-27 13:34:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initial binary should be valid.
|
2020-09-22 09:07:58 +00:00
|
|
|
if (!tools.Validate(&binary_in_[0], binary_in_.size(), validator_options_)) {
|
2020-08-28 12:30:06 +00:00
|
|
|
consumer_(SPV_MSG_ERROR, nullptr, {},
|
|
|
|
"Initial binary is invalid; stopping.");
|
2020-09-22 09:07:58 +00:00
|
|
|
return {Fuzzer::FuzzerResultStatus::kInitialBinaryInvalid,
|
|
|
|
std::vector<uint32_t>(), protobufs::TransformationSequence()};
|
2019-05-27 13:34:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build the module from the input binary.
|
2020-09-22 09:07:58 +00:00
|
|
|
ir_context_ =
|
|
|
|
BuildModule(target_env_, consumer_, binary_in_.data(), binary_in_.size());
|
|
|
|
assert(ir_context_);
|
2019-05-27 13:34:55 +00:00
|
|
|
|
|
|
|
// The fuzzer will introduce new ids into the module. The module's id bound
|
|
|
|
// gives the smallest id that can be used for this purpose. We add an offset
|
|
|
|
// to this so that there is a sizeable gap between the ids used in the
|
|
|
|
// original module and the ids used for fuzzing, as a readability aid.
|
|
|
|
//
|
|
|
|
// TODO(https://github.com/KhronosGroup/SPIRV-Tools/issues/2541) consider the
|
|
|
|
// case where the maximum id bound is reached.
|
2020-09-22 09:07:58 +00:00
|
|
|
auto minimum_fresh_id = ir_context_->module()->id_bound() + kIdBoundGap;
|
|
|
|
fuzzer_context_ =
|
|
|
|
MakeUnique<FuzzerContext>(random_generator_.get(), minimum_fresh_id);
|
2019-05-27 13:34:55 +00:00
|
|
|
|
2020-09-24 16:28:49 +00:00
|
|
|
transformation_context_ = MakeUnique<TransformationContext>(
|
|
|
|
MakeUnique<FactManager>(ir_context_.get()), validator_options_);
|
2020-10-01 10:48:47 +00:00
|
|
|
transformation_context_->GetFactManager()->AddInitialFacts(consumer_,
|
|
|
|
initial_facts_);
|
2019-05-27 13:34:55 +00:00
|
|
|
|
2020-09-18 14:51:35 +00:00
|
|
|
RepeatedPassInstances pass_instances{};
|
2020-10-01 08:07:06 +00:00
|
|
|
|
|
|
|
// The following passes are likely to be very useful: many other passes
|
|
|
|
// introduce synonyms, irrelevant ids and constants that these passes can work
|
|
|
|
// with. We thus enable them with high probability.
|
|
|
|
MaybeAddRepeatedPass<FuzzerPassObfuscateConstants>(90, &pass_instances);
|
|
|
|
MaybeAddRepeatedPass<FuzzerPassApplyIdSynonyms>(90, &pass_instances);
|
|
|
|
MaybeAddRepeatedPass<FuzzerPassReplaceIrrelevantIds>(90, &pass_instances);
|
|
|
|
|
2020-09-18 14:51:35 +00:00
|
|
|
do {
|
|
|
|
// Each call to MaybeAddRepeatedPass randomly decides whether the given pass
|
|
|
|
// should be enabled, and adds an instance of the pass to |pass_instances|
|
|
|
|
// if it is enabled.
|
2020-09-22 09:07:58 +00:00
|
|
|
MaybeAddRepeatedPass<FuzzerPassAddAccessChains>(&pass_instances);
|
|
|
|
MaybeAddRepeatedPass<FuzzerPassAddBitInstructionSynonyms>(&pass_instances);
|
|
|
|
MaybeAddRepeatedPass<FuzzerPassAddCompositeInserts>(&pass_instances);
|
|
|
|
MaybeAddRepeatedPass<FuzzerPassAddCompositeTypes>(&pass_instances);
|
|
|
|
MaybeAddRepeatedPass<FuzzerPassAddCopyMemory>(&pass_instances);
|
|
|
|
MaybeAddRepeatedPass<FuzzerPassAddDeadBlocks>(&pass_instances);
|
|
|
|
MaybeAddRepeatedPass<FuzzerPassAddDeadBreaks>(&pass_instances);
|
|
|
|
MaybeAddRepeatedPass<FuzzerPassAddDeadContinues>(&pass_instances);
|
|
|
|
MaybeAddRepeatedPass<FuzzerPassAddEquationInstructions>(&pass_instances);
|
|
|
|
MaybeAddRepeatedPass<FuzzerPassAddFunctionCalls>(&pass_instances);
|
|
|
|
MaybeAddRepeatedPass<FuzzerPassAddGlobalVariables>(&pass_instances);
|
2020-09-18 14:51:35 +00:00
|
|
|
MaybeAddRepeatedPass<FuzzerPassAddImageSampleUnusedComponents>(
|
2020-09-22 09:07:58 +00:00
|
|
|
&pass_instances);
|
|
|
|
MaybeAddRepeatedPass<FuzzerPassAddLoads>(&pass_instances);
|
|
|
|
MaybeAddRepeatedPass<FuzzerPassAddLocalVariables>(&pass_instances);
|
|
|
|
MaybeAddRepeatedPass<FuzzerPassAddLoopPreheaders>(&pass_instances);
|
2020-09-23 13:10:02 +00:00
|
|
|
MaybeAddRepeatedPass<FuzzerPassAddLoopsToCreateIntConstantSynonyms>(
|
|
|
|
&pass_instances);
|
2020-09-22 09:07:58 +00:00
|
|
|
MaybeAddRepeatedPass<FuzzerPassAddOpPhiSynonyms>(&pass_instances);
|
|
|
|
MaybeAddRepeatedPass<FuzzerPassAddParameters>(&pass_instances);
|
|
|
|
MaybeAddRepeatedPass<FuzzerPassAddRelaxedDecorations>(&pass_instances);
|
|
|
|
MaybeAddRepeatedPass<FuzzerPassAddStores>(&pass_instances);
|
|
|
|
MaybeAddRepeatedPass<FuzzerPassAddSynonyms>(&pass_instances);
|
2020-09-18 14:51:35 +00:00
|
|
|
MaybeAddRepeatedPass<FuzzerPassAddVectorShuffleInstructions>(
|
2020-09-22 09:07:58 +00:00
|
|
|
&pass_instances);
|
|
|
|
MaybeAddRepeatedPass<FuzzerPassConstructComposites>(&pass_instances);
|
|
|
|
MaybeAddRepeatedPass<FuzzerPassCopyObjects>(&pass_instances);
|
|
|
|
MaybeAddRepeatedPass<FuzzerPassDonateModules>(&pass_instances,
|
|
|
|
donor_suppliers_);
|
2020-09-18 14:51:35 +00:00
|
|
|
MaybeAddRepeatedPass<FuzzerPassDuplicateRegionsWithSelections>(
|
2020-09-22 09:07:58 +00:00
|
|
|
&pass_instances);
|
|
|
|
MaybeAddRepeatedPass<FuzzerPassFlattenConditionalBranches>(&pass_instances);
|
|
|
|
MaybeAddRepeatedPass<FuzzerPassInlineFunctions>(&pass_instances);
|
|
|
|
MaybeAddRepeatedPass<FuzzerPassInvertComparisonOperators>(&pass_instances);
|
2020-09-18 14:51:35 +00:00
|
|
|
MaybeAddRepeatedPass<FuzzerPassMakeVectorOperationsDynamic>(
|
2020-09-22 09:07:58 +00:00
|
|
|
&pass_instances);
|
|
|
|
MaybeAddRepeatedPass<FuzzerPassMergeBlocks>(&pass_instances);
|
2020-10-02 03:45:44 +00:00
|
|
|
MaybeAddRepeatedPass<FuzzerPassMergeFunctionReturns>(&pass_instances);
|
2020-09-22 09:07:58 +00:00
|
|
|
MaybeAddRepeatedPass<FuzzerPassMutatePointers>(&pass_instances);
|
|
|
|
MaybeAddRepeatedPass<FuzzerPassOutlineFunctions>(&pass_instances);
|
|
|
|
MaybeAddRepeatedPass<FuzzerPassPermuteBlocks>(&pass_instances);
|
|
|
|
MaybeAddRepeatedPass<FuzzerPassPermuteFunctionParameters>(&pass_instances);
|
|
|
|
MaybeAddRepeatedPass<FuzzerPassPermuteInstructions>(&pass_instances);
|
2020-10-06 12:38:19 +00:00
|
|
|
MaybeAddRepeatedPass<FuzzerPassPropagateInstructionsDown>(&pass_instances);
|
2020-09-22 09:07:58 +00:00
|
|
|
MaybeAddRepeatedPass<FuzzerPassPropagateInstructionsUp>(&pass_instances);
|
|
|
|
MaybeAddRepeatedPass<FuzzerPassPushIdsThroughVariables>(&pass_instances);
|
2020-09-18 14:51:35 +00:00
|
|
|
MaybeAddRepeatedPass<FuzzerPassReplaceAddsSubsMulsWithCarryingExtended>(
|
2020-09-22 09:07:58 +00:00
|
|
|
&pass_instances);
|
2020-10-06 14:59:05 +00:00
|
|
|
MaybeAddRepeatedPass<FuzzerPassReplaceBranchesFromDeadBlocksWithExits>(
|
|
|
|
&pass_instances);
|
2020-09-18 14:51:35 +00:00
|
|
|
MaybeAddRepeatedPass<FuzzerPassReplaceCopyMemoriesWithLoadsStores>(
|
2020-09-22 09:07:58 +00:00
|
|
|
&pass_instances);
|
2020-09-18 14:51:35 +00:00
|
|
|
MaybeAddRepeatedPass<FuzzerPassReplaceCopyObjectsWithStoresLoads>(
|
2020-09-22 09:07:58 +00:00
|
|
|
&pass_instances);
|
2020-09-18 14:51:35 +00:00
|
|
|
MaybeAddRepeatedPass<FuzzerPassReplaceLoadsStoresWithCopyMemories>(
|
2020-09-22 09:07:58 +00:00
|
|
|
&pass_instances);
|
|
|
|
MaybeAddRepeatedPass<FuzzerPassReplaceParameterWithGlobal>(&pass_instances);
|
2020-09-18 14:51:35 +00:00
|
|
|
MaybeAddRepeatedPass<FuzzerPassReplaceLinearAlgebraInstructions>(
|
2020-09-22 09:07:58 +00:00
|
|
|
&pass_instances);
|
2020-09-18 14:51:35 +00:00
|
|
|
MaybeAddRepeatedPass<FuzzerPassReplaceOpPhiIdsFromDeadPredecessors>(
|
2020-09-22 09:07:58 +00:00
|
|
|
&pass_instances);
|
2020-09-18 14:51:35 +00:00
|
|
|
MaybeAddRepeatedPass<FuzzerPassReplaceOpSelectsWithConditionalBranches>(
|
2020-09-22 09:07:58 +00:00
|
|
|
&pass_instances);
|
|
|
|
MaybeAddRepeatedPass<FuzzerPassReplaceParamsWithStruct>(&pass_instances);
|
|
|
|
MaybeAddRepeatedPass<FuzzerPassSplitBlocks>(&pass_instances);
|
2020-09-18 14:51:35 +00:00
|
|
|
MaybeAddRepeatedPass<FuzzerPassSwapBranchConditionalOperands>(
|
2020-09-22 09:07:58 +00:00
|
|
|
&pass_instances);
|
2020-10-01 08:54:10 +00:00
|
|
|
MaybeAddRepeatedPass<FuzzerPassWrapRegionsInSelections>(&pass_instances);
|
2020-09-18 14:51:35 +00:00
|
|
|
// There is a theoretical possibility that no pass instances were created
|
|
|
|
// until now; loop again if so.
|
|
|
|
} while (pass_instances.GetPasses().empty());
|
|
|
|
|
|
|
|
RepeatedPassRecommenderStandard pass_recommender(&pass_instances,
|
2020-09-22 09:07:58 +00:00
|
|
|
fuzzer_context_.get());
|
2020-09-18 14:51:35 +00:00
|
|
|
|
|
|
|
std::unique_ptr<RepeatedPassManager> repeated_pass_manager = nullptr;
|
|
|
|
switch (repeated_pass_strategy_) {
|
|
|
|
case RepeatedPassStrategy::kSimple:
|
|
|
|
repeated_pass_manager = MakeUnique<RepeatedPassManagerSimple>(
|
2020-09-22 09:07:58 +00:00
|
|
|
fuzzer_context_.get(), &pass_instances);
|
2020-09-18 14:51:35 +00:00
|
|
|
break;
|
|
|
|
case RepeatedPassStrategy::kLoopedWithRecommendations:
|
|
|
|
repeated_pass_manager =
|
|
|
|
MakeUnique<RepeatedPassManagerLoopedWithRecommendations>(
|
2020-09-22 09:07:58 +00:00
|
|
|
fuzzer_context_.get(), &pass_instances, &pass_recommender);
|
2020-09-18 14:51:35 +00:00
|
|
|
break;
|
|
|
|
case RepeatedPassStrategy::kRandomWithRecommendations:
|
|
|
|
repeated_pass_manager =
|
|
|
|
MakeUnique<RepeatedPassManagerRandomWithRecommendations>(
|
2020-09-22 09:07:58 +00:00
|
|
|
fuzzer_context_.get(), &pass_instances, &pass_recommender);
|
2020-09-18 14:51:35 +00:00
|
|
|
break;
|
2019-09-23 15:29:19 +00:00
|
|
|
}
|
2019-05-31 08:59:06 +00:00
|
|
|
|
2020-09-18 14:51:35 +00:00
|
|
|
do {
|
2020-10-02 03:48:39 +00:00
|
|
|
if (!ApplyPassAndCheckValidity(
|
2020-10-16 21:58:09 +00:00
|
|
|
repeated_pass_manager->ChoosePass(transformation_sequence_out_))) {
|
2020-09-22 09:07:58 +00:00
|
|
|
return {Fuzzer::FuzzerResultStatus::kFuzzerPassLedToInvalidModule,
|
|
|
|
std::vector<uint32_t>(), protobufs::TransformationSequence()};
|
2019-11-27 18:05:56 +00:00
|
|
|
}
|
2020-09-22 09:07:58 +00:00
|
|
|
} while (ShouldContinueFuzzing());
|
2019-05-31 08:59:06 +00:00
|
|
|
|
2019-10-08 10:25:34 +00:00
|
|
|
// Now apply some passes that it does not make sense to apply repeatedly,
|
|
|
|
// as they do not unlock other passes.
|
2019-10-11 06:10:47 +00:00
|
|
|
std::vector<std::unique_ptr<FuzzerPass>> final_passes;
|
2020-09-22 09:07:58 +00:00
|
|
|
MaybeAddFinalPass<FuzzerPassAdjustBranchWeights>(&final_passes);
|
|
|
|
MaybeAddFinalPass<FuzzerPassAdjustFunctionControls>(&final_passes);
|
|
|
|
MaybeAddFinalPass<FuzzerPassAdjustLoopControls>(&final_passes);
|
|
|
|
MaybeAddFinalPass<FuzzerPassAdjustMemoryOperandsMasks>(&final_passes);
|
|
|
|
MaybeAddFinalPass<FuzzerPassAdjustSelectionControls>(&final_passes);
|
|
|
|
MaybeAddFinalPass<FuzzerPassAddNoContractionDecorations>(&final_passes);
|
2020-09-18 14:51:35 +00:00
|
|
|
MaybeAddFinalPass<FuzzerPassInterchangeSignednessOfIntegerOperands>(
|
2020-09-22 09:07:58 +00:00
|
|
|
&final_passes);
|
|
|
|
MaybeAddFinalPass<FuzzerPassInterchangeZeroLikeConstants>(&final_passes);
|
|
|
|
MaybeAddFinalPass<FuzzerPassPermutePhiOperands>(&final_passes);
|
|
|
|
MaybeAddFinalPass<FuzzerPassSwapCommutableOperands>(&final_passes);
|
|
|
|
MaybeAddFinalPass<FuzzerPassToggleAccessChainInstruction>(&final_passes);
|
2019-10-11 06:10:47 +00:00
|
|
|
for (auto& pass : final_passes) {
|
2020-10-16 21:58:09 +00:00
|
|
|
if (!ApplyPassAndCheckValidity(pass.get())) {
|
2020-09-22 09:07:58 +00:00
|
|
|
return {Fuzzer::FuzzerResultStatus::kFuzzerPassLedToInvalidModule,
|
|
|
|
std::vector<uint32_t>(), protobufs::TransformationSequence()};
|
2019-11-27 18:05:56 +00:00
|
|
|
}
|
2019-10-11 08:15:47 +00:00
|
|
|
}
|
2019-05-27 13:34:55 +00:00
|
|
|
// Encode the module as a binary.
|
2020-09-22 09:07:58 +00:00
|
|
|
std::vector<uint32_t> binary_out;
|
|
|
|
ir_context_->module()->ToBinary(&binary_out, false);
|
2019-05-27 13:34:55 +00:00
|
|
|
|
2020-09-22 09:07:58 +00:00
|
|
|
return {Fuzzer::FuzzerResultStatus::kComplete, std::move(binary_out),
|
|
|
|
std::move(transformation_sequence_out_)};
|
2019-05-27 13:34:55 +00:00
|
|
|
}
|
|
|
|
|
2020-09-22 09:07:58 +00:00
|
|
|
bool Fuzzer::ShouldContinueFuzzing() {
|
2020-09-18 14:51:35 +00:00
|
|
|
// There's a risk that fuzzing could get stuck, if none of the enabled fuzzer
|
|
|
|
// passes are able to apply any transformations. To guard against this we
|
|
|
|
// count the number of times some repeated pass has been applied and ensure
|
|
|
|
// that fuzzing stops if the number of repeated passes hits the limit on the
|
|
|
|
// number of transformations that can be applied.
|
|
|
|
assert(
|
|
|
|
num_repeated_passes_applied_ <= kTransformationLimit &&
|
|
|
|
"The number of repeated passes applied must not exceed its upper limit.");
|
|
|
|
if (num_repeated_passes_applied_ == kTransformationLimit) {
|
|
|
|
// Stop because fuzzing has got stuck.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto transformations_applied_so_far =
|
2020-09-22 09:07:58 +00:00
|
|
|
static_cast<uint32_t>(transformation_sequence_out_.transformation_size());
|
2020-09-18 14:51:35 +00:00
|
|
|
if (transformations_applied_so_far >= kTransformationLimit) {
|
|
|
|
// Stop because we have reached the transformation limit.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto chance_of_continuing = static_cast<uint32_t>(
|
|
|
|
100.0 * (1.0 - (static_cast<double>(transformations_applied_so_far) /
|
|
|
|
static_cast<double>(kTransformationLimit))));
|
2020-09-22 09:07:58 +00:00
|
|
|
if (!fuzzer_context_->ChoosePercentage(chance_of_continuing)) {
|
2020-09-18 14:51:35 +00:00
|
|
|
// We have probabilistically decided to stop.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Continue fuzzing!
|
|
|
|
num_repeated_passes_applied_++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-27 13:34:55 +00:00
|
|
|
} // namespace fuzz
|
|
|
|
} // namespace spvtools
|