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>
|
2019-05-27 13:34:55 +00:00
|
|
|
#include <sstream>
|
|
|
|
|
2019-10-22 17:05:35 +00:00
|
|
|
#include "fuzzer_pass_adjust_memory_operands_masks.h"
|
2019-05-27 13:34:55 +00:00
|
|
|
#include "source/fuzz/fact_manager.h"
|
|
|
|
#include "source/fuzz/fuzzer_context.h"
|
2020-02-11 23:10:57 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_add_access_chains.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"
|
2019-10-11 08:15:47 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_add_no_contraction_decorations.h"
|
2020-06-23 16:40:44 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_add_parameters.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"
|
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-07-03 16:37:32 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_invert_comparison_operators.h"
|
2019-12-12 15:27:40 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_merge_blocks.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-06-23 14:00:28 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_permute_phi_operands.h"
|
2020-05-29 15:43:38 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_push_ids_through_variables.h"
|
2020-06-16 10:20:51 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_replace_linear_algebra_instructions.h"
|
2020-07-09 10:03:49 +00:00
|
|
|
#include "source/fuzz/fuzzer_pass_replace_parameter_with_global.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"
|
2019-05-27 13:34:55 +00:00
|
|
|
#include "source/fuzz/protobufs/spirvfuzz_protobufs.h"
|
|
|
|
#include "source/fuzz/pseudo_random_generator.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
|
|
|
|
|
|
|
const uint32_t kTransformationLimit = 500;
|
|
|
|
|
|
|
|
const uint32_t kChanceOfApplyingAnotherPass = 85;
|
|
|
|
|
2020-01-07 08:39:55 +00:00
|
|
|
// A convenience method to add a fuzzer pass to |passes| with probability 0.5.
|
2020-04-02 14:54:46 +00:00
|
|
|
// All fuzzer passes take |ir_context|, |transformation_context|,
|
|
|
|
// |fuzzer_context| and |transformation_sequence_out| as parameters. Extra
|
|
|
|
// arguments can be provided via |extra_args|.
|
2020-01-07 08:39:55 +00:00
|
|
|
template <typename T, typename... Args>
|
2019-09-23 15:29:19 +00:00
|
|
|
void MaybeAddPass(
|
|
|
|
std::vector<std::unique_ptr<FuzzerPass>>* passes,
|
2020-04-02 14:54:46 +00:00
|
|
|
opt::IRContext* ir_context, TransformationContext* transformation_context,
|
2019-09-23 15:29:19 +00:00
|
|
|
FuzzerContext* fuzzer_context,
|
2020-01-07 08:39:55 +00:00
|
|
|
protobufs::TransformationSequence* transformation_sequence_out,
|
|
|
|
Args&&... extra_args) {
|
2019-09-23 15:29:19 +00:00
|
|
|
if (fuzzer_context->ChooseEven()) {
|
2020-04-02 14:54:46 +00:00
|
|
|
passes->push_back(MakeUnique<T>(ir_context, transformation_context,
|
|
|
|
fuzzer_context, transformation_sequence_out,
|
2020-01-07 08:39:55 +00:00
|
|
|
std::forward<Args>(extra_args)...));
|
2019-09-23 15:29:19 +00:00
|
|
|
}
|
2019-05-27 13:34:55 +00:00
|
|
|
}
|
|
|
|
|
2019-09-23 15:29:19 +00:00
|
|
|
} // namespace
|
|
|
|
|
2019-05-27 13:34:55 +00:00
|
|
|
struct Fuzzer::Impl {
|
2020-04-02 13:37:59 +00:00
|
|
|
Impl(spv_target_env env, uint32_t random_seed, bool validate_after_each_pass,
|
|
|
|
spv_validator_options options)
|
2019-11-27 18:05:56 +00:00
|
|
|
: target_env(env),
|
|
|
|
seed(random_seed),
|
2020-04-02 13:37:59 +00:00
|
|
|
validate_after_each_fuzzer_pass(validate_after_each_pass),
|
|
|
|
validator_options(options) {}
|
2019-11-27 18:05:56 +00:00
|
|
|
|
|
|
|
bool ApplyPassAndCheckValidity(FuzzerPass* pass,
|
|
|
|
const opt::IRContext& ir_context,
|
|
|
|
const spvtools::SpirvTools& tools) const;
|
|
|
|
|
|
|
|
const spv_target_env target_env; // Target environment.
|
2020-04-02 13:37:59 +00:00
|
|
|
MessageConsumer consumer; // Message consumer.
|
2019-11-27 18:05:56 +00:00
|
|
|
const uint32_t seed; // Seed for random number generator.
|
|
|
|
bool validate_after_each_fuzzer_pass; // Determines whether the validator
|
2020-04-02 13:37:59 +00:00
|
|
|
// should be invoked after every fuzzer
|
|
|
|
// pass.
|
|
|
|
spv_validator_options validator_options; // Options to control validation.
|
2019-05-27 13:34:55 +00:00
|
|
|
};
|
|
|
|
|
2019-11-27 18:05:56 +00:00
|
|
|
Fuzzer::Fuzzer(spv_target_env env, uint32_t seed,
|
2020-04-02 13:37:59 +00:00
|
|
|
bool validate_after_each_fuzzer_pass,
|
|
|
|
spv_validator_options validator_options)
|
|
|
|
: impl_(MakeUnique<Impl>(env, seed, validate_after_each_fuzzer_pass,
|
|
|
|
validator_options)) {}
|
2019-05-27 13:34:55 +00:00
|
|
|
|
|
|
|
Fuzzer::~Fuzzer() = default;
|
|
|
|
|
|
|
|
void Fuzzer::SetMessageConsumer(MessageConsumer c) {
|
|
|
|
impl_->consumer = std::move(c);
|
|
|
|
}
|
|
|
|
|
2019-11-27 18:05:56 +00:00
|
|
|
bool Fuzzer::Impl::ApplyPassAndCheckValidity(
|
|
|
|
FuzzerPass* pass, const opt::IRContext& ir_context,
|
|
|
|
const spvtools::SpirvTools& tools) const {
|
|
|
|
pass->Apply();
|
|
|
|
if (validate_after_each_fuzzer_pass) {
|
|
|
|
std::vector<uint32_t> binary_to_validate;
|
|
|
|
ir_context.module()->ToBinary(&binary_to_validate, false);
|
2020-04-02 13:37:59 +00:00
|
|
|
if (!tools.Validate(&binary_to_validate[0], binary_to_validate.size(),
|
|
|
|
validator_options)) {
|
2019-11-27 18:05:56 +00:00
|
|
|
consumer(SPV_MSG_INFO, nullptr, {},
|
|
|
|
"Binary became invalid during fuzzing (set a breakpoint to "
|
|
|
|
"inspect); stopping.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-27 13:34:55 +00:00
|
|
|
Fuzzer::FuzzerResultStatus Fuzzer::Run(
|
|
|
|
const std::vector<uint32_t>& binary_in,
|
|
|
|
const protobufs::FactSequence& initial_facts,
|
2020-01-07 08:39:55 +00:00
|
|
|
const std::vector<fuzzerutil::ModuleSupplier>& donor_suppliers,
|
2019-11-27 18:05:56 +00:00
|
|
|
std::vector<uint32_t>* binary_out,
|
2019-07-07 07:55:30 +00:00
|
|
|
protobufs::TransformationSequence* transformation_sequence_out) const {
|
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;
|
|
|
|
|
|
|
|
spvtools::SpirvTools tools(impl_->target_env);
|
2019-07-07 07:55:30 +00:00
|
|
|
tools.SetMessageConsumer(impl_->consumer);
|
2019-05-27 13:34:55 +00:00
|
|
|
if (!tools.IsValid()) {
|
|
|
|
impl_->consumer(SPV_MSG_ERROR, nullptr, {},
|
|
|
|
"Failed to create SPIRV-Tools interface; stopping.");
|
|
|
|
return Fuzzer::FuzzerResultStatus::kFailedToCreateSpirvToolsInterface;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initial binary should be valid.
|
2020-04-02 13:37:59 +00:00
|
|
|
if (!tools.Validate(&binary_in[0], binary_in.size(),
|
|
|
|
impl_->validator_options)) {
|
2019-05-27 13:34:55 +00:00
|
|
|
impl_->consumer(SPV_MSG_ERROR, nullptr, {},
|
|
|
|
"Initial binary is invalid; stopping.");
|
|
|
|
return Fuzzer::FuzzerResultStatus::kInitialBinaryInvalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the module from the input binary.
|
|
|
|
std::unique_ptr<opt::IRContext> ir_context = BuildModule(
|
|
|
|
impl_->target_env, impl_->consumer, binary_in.data(), binary_in.size());
|
|
|
|
assert(ir_context);
|
|
|
|
|
2019-11-27 18:05:56 +00:00
|
|
|
// Make a PRNG from the seed passed to the fuzzer on creation.
|
|
|
|
PseudoRandomGenerator random_generator(impl_->seed);
|
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.
|
|
|
|
auto minimum_fresh_id = ir_context->module()->id_bound() + kIdBoundGap;
|
|
|
|
FuzzerContext fuzzer_context(&random_generator, minimum_fresh_id);
|
|
|
|
|
|
|
|
FactManager fact_manager;
|
2019-06-26 15:40:19 +00:00
|
|
|
fact_manager.AddFacts(impl_->consumer, initial_facts, ir_context.get());
|
2020-04-02 14:54:46 +00:00
|
|
|
TransformationContext transformation_context(&fact_manager,
|
|
|
|
impl_->validator_options);
|
2019-05-27 13:34:55 +00:00
|
|
|
|
2019-05-29 15:42:46 +00:00
|
|
|
// Apply some semantics-preserving passes.
|
2019-09-23 15:29:19 +00:00
|
|
|
std::vector<std::unique_ptr<FuzzerPass>> passes;
|
|
|
|
while (passes.empty()) {
|
2020-04-02 14:54:46 +00:00
|
|
|
MaybeAddPass<FuzzerPassAddAccessChains>(
|
|
|
|
&passes, ir_context.get(), &transformation_context, &fuzzer_context,
|
|
|
|
transformation_sequence_out);
|
|
|
|
MaybeAddPass<FuzzerPassAddCompositeTypes>(
|
|
|
|
&passes, ir_context.get(), &transformation_context, &fuzzer_context,
|
|
|
|
transformation_sequence_out);
|
2020-06-30 20:13:05 +00:00
|
|
|
MaybeAddPass<FuzzerPassAddCopyMemory>(
|
|
|
|
&passes, ir_context.get(), &transformation_context, &fuzzer_context,
|
|
|
|
transformation_sequence_out);
|
2020-04-02 14:54:46 +00:00
|
|
|
MaybeAddPass<FuzzerPassAddDeadBlocks>(
|
|
|
|
&passes, ir_context.get(), &transformation_context, &fuzzer_context,
|
|
|
|
transformation_sequence_out);
|
|
|
|
MaybeAddPass<FuzzerPassAddDeadBreaks>(
|
|
|
|
&passes, ir_context.get(), &transformation_context, &fuzzer_context,
|
|
|
|
transformation_sequence_out);
|
|
|
|
MaybeAddPass<FuzzerPassAddDeadContinues>(
|
|
|
|
&passes, ir_context.get(), &transformation_context, &fuzzer_context,
|
|
|
|
transformation_sequence_out);
|
2020-03-04 14:54:08 +00:00
|
|
|
MaybeAddPass<FuzzerPassAddEquationInstructions>(
|
2020-04-02 14:54:46 +00:00
|
|
|
&passes, ir_context.get(), &transformation_context, &fuzzer_context,
|
2020-03-04 14:54:08 +00:00
|
|
|
transformation_sequence_out);
|
2020-04-02 14:54:46 +00:00
|
|
|
MaybeAddPass<FuzzerPassAddFunctionCalls>(
|
|
|
|
&passes, ir_context.get(), &transformation_context, &fuzzer_context,
|
|
|
|
transformation_sequence_out);
|
|
|
|
MaybeAddPass<FuzzerPassAddGlobalVariables>(
|
|
|
|
&passes, ir_context.get(), &transformation_context, &fuzzer_context,
|
|
|
|
transformation_sequence_out);
|
2020-07-08 16:07:04 +00:00
|
|
|
MaybeAddPass<FuzzerPassAddImageSampleUnusedComponents>(
|
|
|
|
&passes, ir_context.get(), &transformation_context, &fuzzer_context,
|
|
|
|
transformation_sequence_out);
|
2020-04-02 14:54:46 +00:00
|
|
|
MaybeAddPass<FuzzerPassAddLoads>(&passes, ir_context.get(),
|
|
|
|
&transformation_context, &fuzzer_context,
|
2020-02-06 16:54:34 +00:00
|
|
|
transformation_sequence_out);
|
2020-04-02 14:54:46 +00:00
|
|
|
MaybeAddPass<FuzzerPassAddLocalVariables>(
|
|
|
|
&passes, ir_context.get(), &transformation_context, &fuzzer_context,
|
|
|
|
transformation_sequence_out);
|
2020-06-23 16:40:44 +00:00
|
|
|
MaybeAddPass<FuzzerPassAddParameters>(
|
|
|
|
&passes, ir_context.get(), &transformation_context, &fuzzer_context,
|
|
|
|
transformation_sequence_out);
|
2020-04-02 14:54:46 +00:00
|
|
|
MaybeAddPass<FuzzerPassAddStores>(&passes, ir_context.get(),
|
|
|
|
&transformation_context, &fuzzer_context,
|
2020-02-06 16:54:34 +00:00
|
|
|
transformation_sequence_out);
|
2020-07-12 08:59:08 +00:00
|
|
|
MaybeAddPass<FuzzerPassAddSynonyms>(
|
|
|
|
&passes, ir_context.get(), &transformation_context, &fuzzer_context,
|
|
|
|
transformation_sequence_out);
|
2020-06-16 10:21:31 +00:00
|
|
|
MaybeAddPass<FuzzerPassAddVectorShuffleInstructions>(
|
|
|
|
&passes, ir_context.get(), &transformation_context, &fuzzer_context,
|
|
|
|
transformation_sequence_out);
|
2020-04-02 14:54:46 +00:00
|
|
|
MaybeAddPass<FuzzerPassApplyIdSynonyms>(
|
|
|
|
&passes, ir_context.get(), &transformation_context, &fuzzer_context,
|
|
|
|
transformation_sequence_out);
|
|
|
|
MaybeAddPass<FuzzerPassConstructComposites>(
|
|
|
|
&passes, ir_context.get(), &transformation_context, &fuzzer_context,
|
|
|
|
transformation_sequence_out);
|
|
|
|
MaybeAddPass<FuzzerPassCopyObjects>(
|
|
|
|
&passes, ir_context.get(), &transformation_context, &fuzzer_context,
|
|
|
|
transformation_sequence_out);
|
2020-01-07 08:39:55 +00:00
|
|
|
MaybeAddPass<FuzzerPassDonateModules>(
|
2020-04-02 14:54:46 +00:00
|
|
|
&passes, ir_context.get(), &transformation_context, &fuzzer_context,
|
2020-01-07 08:39:55 +00:00
|
|
|
transformation_sequence_out, donor_suppliers);
|
2020-07-03 16:37:32 +00:00
|
|
|
MaybeAddPass<FuzzerPassInvertComparisonOperators>(
|
|
|
|
&passes, ir_context.get(), &transformation_context, &fuzzer_context,
|
|
|
|
transformation_sequence_out);
|
2020-04-02 14:54:46 +00:00
|
|
|
MaybeAddPass<FuzzerPassMergeBlocks>(
|
|
|
|
&passes, ir_context.get(), &transformation_context, &fuzzer_context,
|
|
|
|
transformation_sequence_out);
|
|
|
|
MaybeAddPass<FuzzerPassObfuscateConstants>(
|
|
|
|
&passes, ir_context.get(), &transformation_context, &fuzzer_context,
|
|
|
|
transformation_sequence_out);
|
|
|
|
MaybeAddPass<FuzzerPassOutlineFunctions>(
|
|
|
|
&passes, ir_context.get(), &transformation_context, &fuzzer_context,
|
|
|
|
transformation_sequence_out);
|
|
|
|
MaybeAddPass<FuzzerPassPermuteBlocks>(
|
|
|
|
&passes, ir_context.get(), &transformation_context, &fuzzer_context,
|
|
|
|
transformation_sequence_out);
|
2020-03-08 14:27:05 +00:00
|
|
|
MaybeAddPass<FuzzerPassPermuteFunctionParameters>(
|
2020-04-02 14:54:46 +00:00
|
|
|
&passes, ir_context.get(), &transformation_context, &fuzzer_context,
|
|
|
|
transformation_sequence_out);
|
2020-05-29 15:43:38 +00:00
|
|
|
MaybeAddPass<FuzzerPassPushIdsThroughVariables>(
|
|
|
|
&passes, ir_context.get(), &transformation_context, &fuzzer_context,
|
|
|
|
transformation_sequence_out);
|
2020-07-09 10:03:49 +00:00
|
|
|
MaybeAddPass<FuzzerPassReplaceParameterWithGlobal>(
|
|
|
|
&passes, ir_context.get(), &transformation_context, &fuzzer_context,
|
|
|
|
transformation_sequence_out);
|
2020-06-16 10:20:51 +00:00
|
|
|
MaybeAddPass<FuzzerPassReplaceLinearAlgebraInstructions>(
|
|
|
|
&passes, ir_context.get(), &transformation_context, &fuzzer_context,
|
|
|
|
transformation_sequence_out);
|
2020-04-02 14:54:46 +00:00
|
|
|
MaybeAddPass<FuzzerPassSplitBlocks>(
|
|
|
|
&passes, ir_context.get(), &transformation_context, &fuzzer_context,
|
2020-03-08 14:27:05 +00:00
|
|
|
transformation_sequence_out);
|
2020-06-19 15:38:52 +00:00
|
|
|
MaybeAddPass<FuzzerPassSwapBranchConditionalOperands>(
|
|
|
|
&passes, ir_context.get(), &transformation_context, &fuzzer_context,
|
|
|
|
transformation_sequence_out);
|
2019-09-23 15:29:19 +00:00
|
|
|
}
|
2019-05-31 08:59:06 +00:00
|
|
|
|
2019-09-23 15:29:19 +00:00
|
|
|
bool is_first = true;
|
|
|
|
while (static_cast<uint32_t>(
|
|
|
|
transformation_sequence_out->transformation_size()) <
|
|
|
|
kTransformationLimit &&
|
|
|
|
(is_first ||
|
|
|
|
fuzzer_context.ChoosePercentage(kChanceOfApplyingAnotherPass))) {
|
|
|
|
is_first = false;
|
2019-11-27 18:05:56 +00:00
|
|
|
if (!impl_->ApplyPassAndCheckValidity(
|
|
|
|
passes[fuzzer_context.RandomIndex(passes)].get(), *ir_context,
|
|
|
|
tools)) {
|
|
|
|
return Fuzzer::FuzzerResultStatus::kFuzzerPassLedToInvalidModule;
|
|
|
|
}
|
2019-09-23 15:29:19 +00:00
|
|
|
}
|
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-05-14 10:38:34 +00:00
|
|
|
MaybeAddPass<FuzzerPassAdjustBranchWeights>(
|
|
|
|
&final_passes, ir_context.get(), &transformation_context, &fuzzer_context,
|
|
|
|
transformation_sequence_out);
|
2019-10-22 17:05:35 +00:00
|
|
|
MaybeAddPass<FuzzerPassAdjustFunctionControls>(
|
2020-04-02 14:54:46 +00:00
|
|
|
&final_passes, ir_context.get(), &transformation_context, &fuzzer_context,
|
|
|
|
transformation_sequence_out);
|
|
|
|
MaybeAddPass<FuzzerPassAdjustLoopControls>(
|
|
|
|
&final_passes, ir_context.get(), &transformation_context, &fuzzer_context,
|
2019-10-22 17:05:35 +00:00
|
|
|
transformation_sequence_out);
|
|
|
|
MaybeAddPass<FuzzerPassAdjustMemoryOperandsMasks>(
|
2020-04-02 14:54:46 +00:00
|
|
|
&final_passes, ir_context.get(), &transformation_context, &fuzzer_context,
|
2019-10-22 17:05:35 +00:00
|
|
|
transformation_sequence_out);
|
2019-10-11 06:10:47 +00:00
|
|
|
MaybeAddPass<FuzzerPassAdjustSelectionControls>(
|
2020-04-02 14:54:46 +00:00
|
|
|
&final_passes, ir_context.get(), &transformation_context, &fuzzer_context,
|
2019-10-11 06:10:47 +00:00
|
|
|
transformation_sequence_out);
|
2019-11-27 18:05:56 +00:00
|
|
|
MaybeAddPass<FuzzerPassAddNoContractionDecorations>(
|
2020-04-02 14:54:46 +00:00
|
|
|
&final_passes, ir_context.get(), &transformation_context, &fuzzer_context,
|
2019-11-27 18:05:56 +00:00
|
|
|
transformation_sequence_out);
|
2020-06-23 14:00:28 +00:00
|
|
|
MaybeAddPass<FuzzerPassPermutePhiOperands>(
|
|
|
|
&final_passes, ir_context.get(), &transformation_context, &fuzzer_context,
|
|
|
|
transformation_sequence_out);
|
2020-03-05 08:18:39 +00:00
|
|
|
MaybeAddPass<FuzzerPassSwapCommutableOperands>(
|
2020-04-02 14:54:46 +00:00
|
|
|
&final_passes, ir_context.get(), &transformation_context, &fuzzer_context,
|
2020-03-05 08:18:39 +00:00
|
|
|
transformation_sequence_out);
|
2020-03-08 22:33:24 +00:00
|
|
|
MaybeAddPass<FuzzerPassToggleAccessChainInstruction>(
|
2020-04-02 14:54:46 +00:00
|
|
|
&final_passes, ir_context.get(), &transformation_context, &fuzzer_context,
|
2020-03-08 22:33:24 +00:00
|
|
|
transformation_sequence_out);
|
2019-10-11 06:10:47 +00:00
|
|
|
for (auto& pass : final_passes) {
|
2019-11-27 18:05:56 +00:00
|
|
|
if (!impl_->ApplyPassAndCheckValidity(pass.get(), *ir_context, tools)) {
|
|
|
|
return Fuzzer::FuzzerResultStatus::kFuzzerPassLedToInvalidModule;
|
|
|
|
}
|
2019-10-11 08:15:47 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 13:34:55 +00:00
|
|
|
// Encode the module as a binary.
|
|
|
|
ir_context->module()->ToBinary(binary_out, false);
|
|
|
|
|
|
|
|
return Fuzzer::FuzzerResultStatus::kComplete;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace fuzz
|
|
|
|
} // namespace spvtools
|