| 1 |
// Copyright (c) 2020 Vasyl Teliman |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
#include "source/fuzz/fuzzer_pass_add_parameters.h" |
| 16 |
|
| 17 |
#include "source/fuzz/fuzzer_context.h" |
| 18 |
#include "source/fuzz/fuzzer_util.h" |
| 19 |
#include "source/fuzz/instruction_descriptor.h" |
| 20 |
#include "source/fuzz/transformation_add_parameter.h" |
| 21 |
|
| 22 |
namespace spvtools { |
| 23 |
namespace fuzz { |
| 24 |
|
| 25 |
FuzzerPassAddParameters::FuzzerPassAddParameters( |
| 26 |
opt::IRContext* ir_context, TransformationContext* transformation_context, |
| 27 |
FuzzerContext* fuzzer_context, |
| 28 |
protobufs::TransformationSequence* transformations) |
| 29 |
: FuzzerPass(ir_context, transformation_context, fuzzer_context, |
| 30 |
transformations) {} |
| 31 |
|
| 32 |
FuzzerPassAddParameters::~FuzzerPassAddParameters() = default; |
| 33 |
|
| 34 |
void FuzzerPassAddParameters::Apply() { |
| 35 |
// Compute type candidates for the new parameter. |
| 36 |
std::vector<uint32_t> type_candidates; |
| 37 |
for (const auto& type_inst : GetIRContext()->module()->GetTypes()) { |
| 38 |
if (TransformationAddParameter::IsParameterTypeSupported( |
| 39 |
GetIRContext(), type_inst->result_id())) { |
| 40 |
type_candidates.push_back(type_inst->result_id()); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
if (type_candidates.empty()) { |
| 45 |
// The module contains no suitable types to use in new parameters. |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
// Iterate over all functions in the module. |
| 50 |
for (const auto& function : *GetIRContext()->module()) { |
| 51 |
// Skip all entry-point functions - we don't want to change those. |
| 52 |
if (fuzzerutil::FunctionIsEntryPoint(GetIRContext(), |
| 53 |
function.result_id())) { |
| 54 |
continue; |
| 55 |
} |
| 56 |
|
| 57 |
if (GetNumberOfParameters(function) >= |
| 58 |
GetFuzzerContext()->GetMaximumNumberOfFunctionParameters()) { |
| 59 |
continue; |
| 60 |
} |
| 61 |
|
| 62 |
if (!GetFuzzerContext()->ChoosePercentage( |
| 63 |
GetFuzzerContext()->GetChanceOfAddingParameters())) { |
| 64 |
continue; |
| 65 |
} |
| 66 |
|
| 67 |
auto num_new_parameters = |
| 68 |
GetFuzzerContext()->GetRandomNumberOfNewParameters( |
| 69 |
GetNumberOfParameters(function)); |
| 70 |
|
| 71 |
for (uint32_t i = 0; i < num_new_parameters; ++i) { |
| 72 |
auto current_type_id = |
| 73 |
type_candidates[GetFuzzerContext()->RandomIndex(type_candidates)]; |
| 74 |
auto current_type = |
| 75 |
GetIRContext()->get_type_mgr()->GetType(current_type_id); |
| 76 |
std::map<uint32_t, uint32_t> call_parameter_ids; |
| 77 |
|
| 78 |
// Consider the case when a pointer type was selected. |
| 79 |
if (current_type->kind() == opt::analysis::Type::kPointer) { |
| 80 |
auto storage_class = fuzzerutil::GetStorageClassFromPointerType( |
| 81 |
GetIRContext(), current_type_id); |
| 82 |
switch (storage_class) { |
| 83 |
case SpvStorageClassFunction: { |
| 84 |
// In every caller find or create a local variable that has the |
| 85 |
// selected type. |
| 86 |
for (auto* instr : |
| 87 |
fuzzerutil::GetCallers(GetIRContext(), function.result_id())) { |
| 88 |
auto block = GetIRContext()->get_instr_block(instr); |
| 89 |
auto function_id = block->GetParent()->result_id(); |
| 90 |
uint32_t variable_id = |
| 91 |
FindOrCreateLocalVariable(current_type_id, function_id, true); |
| 92 |
call_parameter_ids[instr->result_id()] = variable_id; |
| 93 |
} |
| 94 |
} break; |
| 95 |
case SpvStorageClassPrivate: |
| 96 |
case SpvStorageClassWorkgroup: { |
| 97 |
// If there exists at least one caller, find or create a global |
| 98 |
// variable that has the selected type. |
| 99 |
std::vector<opt::Instruction*> callers = |
| 100 |
fuzzerutil::GetCallers(GetIRContext(), function.result_id()); |
| 101 |
if (!callers.empty()) { |
| 102 |
uint32_t variable_id = |
| 103 |
FindOrCreateGlobalVariable(current_type_id, true); |
| 104 |
for (auto* instr : callers) { |
| 105 |
call_parameter_ids[instr->result_id()] = variable_id; |
| 106 |
} |
| 107 |
} |
| 108 |
} break; |
| 109 |
default: |
| 110 |
break; |
| 111 |
} |
| 112 |
} else { |
| 113 |
// If there exists at least one caller, find or create a zero constant |
| 114 |
// that has the selected type. |
| 115 |
std::vector<opt::Instruction*> callers = |
| 116 |
fuzzerutil::GetCallers(GetIRContext(), function.result_id()); |
| 117 |
if (!callers.empty()) { |
| 118 |
uint32_t constant_id = |
| 119 |
FindOrCreateZeroConstant(current_type_id, true); |
| 120 |
for (auto* instr : |
| 121 |
fuzzerutil::GetCallers(GetIRContext(), function.result_id())) { |
| 122 |
call_parameter_ids[instr->result_id()] = constant_id; |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
ApplyTransformation(TransformationAddParameter( |
| 128 |
function.result_id(), GetFuzzerContext()->GetFreshId(), |
| 129 |
current_type_id, std::move(call_parameter_ids), |
| 130 |
GetFuzzerContext()->GetFreshId())); |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
uint32_t FuzzerPassAddParameters::GetNumberOfParameters( |
| 136 |
const opt::Function& function) const { |
| 137 |
const auto* type = GetIRContext()->get_type_mgr()->GetType( |
| 138 |
function.DefInst().GetSingleWordInOperand(1)); |
| 139 |
assert(type && type->AsFunction()); |
| 140 |
|
| 141 |
return static_cast<uint32_t>(type->AsFunction()->param_types().size()); |
| 142 |
} |
| 143 |
|
| 144 |
} // namespace fuzz |
| 145 |
} // namespace spvtools |