1
// Copyright (c) 2020 André Perez Maselco
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
7
// http://www.apache.org/licenses/LICENSE-2.0
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.
15
#include "source/fuzz/fuzzer_pass_inline_functions.h"
17
#include "source/fuzz/fuzzer_util.h"
18
#include "source/fuzz/instruction_descriptor.h"
19
#include "source/fuzz/transformation_inline_function.h"
20
#include "source/fuzz/transformation_split_block.h"
25
FuzzerPassInlineFunctions::FuzzerPassInlineFunctions(
26
opt::IRContext* ir_context, TransformationContext* transformation_context,
27
FuzzerContext* fuzzer_context,
28
protobufs::TransformationSequence* transformations)
29
: FuzzerPass(ir_context, transformation_context, fuzzer_context,
32
FuzzerPassInlineFunctions::~FuzzerPassInlineFunctions() = default;
34
void FuzzerPassInlineFunctions::Apply() {
35
// |function_call_instructions| are the instructions that will be inlined.
36
// First, they will be collected and then do the inlining in another loop.
37
// This avoids changing the module while it is being inspected.
38
std::vector<opt::Instruction*> function_call_instructions;
40
for (auto& function : *GetIRContext()->module()) {
41
for (auto& block : function) {
42
for (auto& instruction : block) {
43
if (!GetFuzzerContext()->ChoosePercentage(
44
GetFuzzerContext()->GetChanceOfInliningFunction())) {
48
// |instruction| must be suitable for inlining.
49
if (!TransformationInlineFunction::IsSuitableForInlining(
50
GetIRContext(), &instruction)) {
54
function_call_instructions.push_back(&instruction);
59
// Once the function calls have been collected, it's time to actually create
60
// and apply the inlining transformations.
61
for (auto& function_call_instruction : function_call_instructions) {
62
// If |function_call_instruction| is not the penultimate instruction in its
63
// block or its block termination instruction is not OpBranch, then try to
64
// split |function_call_block| such that the conditions are met.
65
auto* function_call_block =
66
GetIRContext()->get_instr_block(function_call_instruction);
67
if ((function_call_instruction != &*--function_call_block->tail() ||
68
function_call_block->terminator()->opcode() != SpvOpBranch) &&
69
!MaybeApplyTransformation(TransformationSplitBlock(
70
MakeInstructionDescriptor(GetIRContext(),
71
function_call_instruction->NextNode()),
72
GetFuzzerContext()->GetFreshId()))) {
76
auto* called_function = fuzzerutil::FindFunction(
77
GetIRContext(), function_call_instruction->GetSingleWordInOperand(0));
79
// Mapping the called function instructions.
80
std::map<uint32_t, uint32_t> result_id_map;
81
for (auto& called_function_block : *called_function) {
82
// The called function entry block label will not be inlined.
83
if (&called_function_block != &*called_function->entry()) {
84
result_id_map[called_function_block.GetLabelInst()->result_id()] =
85
GetFuzzerContext()->GetFreshId();
88
for (auto& instruction_to_inline : called_function_block) {
89
// The instructions are mapped to fresh ids.
90
if (instruction_to_inline.HasResultId()) {
91
result_id_map[instruction_to_inline.result_id()] =
92
GetFuzzerContext()->GetFreshId();
97
// Applies the inline function transformation.
98
ApplyTransformation(TransformationInlineFunction(
99
function_call_instruction->result_id(), result_id_map));
104
} // namespace spvtools