1 // Copyright (c) 2020 André Perez Maselco
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_inline_functions.h"
16
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"
21
22 namespace spvtools {
23 namespace fuzz {
24
FuzzerPassInlineFunctions(opt::IRContext * ir_context,TransformationContext * transformation_context,FuzzerContext * fuzzer_context,protobufs::TransformationSequence * transformations,bool ignore_inapplicable_transformations)25 FuzzerPassInlineFunctions::FuzzerPassInlineFunctions(
26 opt::IRContext* ir_context, TransformationContext* transformation_context,
27 FuzzerContext* fuzzer_context,
28 protobufs::TransformationSequence* transformations,
29 bool ignore_inapplicable_transformations)
30 : FuzzerPass(ir_context, transformation_context, fuzzer_context,
31 transformations, ignore_inapplicable_transformations) {}
32
Apply()33 void FuzzerPassInlineFunctions::Apply() {
34 // |function_call_instructions| are the instructions that will be inlined.
35 // First, they will be collected and then do the inlining in another loop.
36 // This avoids changing the module while it is being inspected.
37 std::vector<opt::Instruction*> function_call_instructions;
38
39 for (auto& function : *GetIRContext()->module()) {
40 for (auto& block : function) {
41 for (auto& instruction : block) {
42 if (!GetFuzzerContext()->ChoosePercentage(
43 GetFuzzerContext()->GetChanceOfInliningFunction())) {
44 continue;
45 }
46
47 // |instruction| must be suitable for inlining.
48 if (!TransformationInlineFunction::IsSuitableForInlining(
49 GetIRContext(), &instruction)) {
50 continue;
51 }
52
53 function_call_instructions.push_back(&instruction);
54 }
55 }
56 }
57
58 // Once the function calls have been collected, it's time to actually create
59 // and apply the inlining transformations.
60 for (auto& function_call_instruction : function_call_instructions) {
61 // If |function_call_instruction| is not the penultimate instruction in its
62 // block or its block termination instruction is not OpBranch, then try to
63 // split |function_call_block| such that the conditions are met.
64 auto* function_call_block =
65 GetIRContext()->get_instr_block(function_call_instruction);
66 if ((function_call_instruction != &*--function_call_block->tail() ||
67 function_call_block->terminator()->opcode() != SpvOpBranch) &&
68 !MaybeApplyTransformation(TransformationSplitBlock(
69 MakeInstructionDescriptor(GetIRContext(),
70 function_call_instruction->NextNode()),
71 GetFuzzerContext()->GetFreshId()))) {
72 continue;
73 }
74
75 auto* called_function = fuzzerutil::FindFunction(
76 GetIRContext(), function_call_instruction->GetSingleWordInOperand(0));
77
78 // Mapping the called function instructions.
79 std::map<uint32_t, uint32_t> result_id_map;
80 for (auto& called_function_block : *called_function) {
81 // The called function entry block label will not be inlined.
82 if (&called_function_block != &*called_function->entry()) {
83 result_id_map[called_function_block.GetLabelInst()->result_id()] =
84 GetFuzzerContext()->GetFreshId();
85 }
86
87 for (auto& instruction_to_inline : called_function_block) {
88 // The instructions are mapped to fresh ids.
89 if (instruction_to_inline.HasResultId()) {
90 result_id_map[instruction_to_inline.result_id()] =
91 GetFuzzerContext()->GetFreshId();
92 }
93 }
94 }
95
96 // Applies the inline function transformation.
97 ApplyTransformation(TransformationInlineFunction(
98 function_call_instruction->result_id(), result_id_map));
99 }
100 }
101
102 } // namespace fuzz
103 } // namespace spvtools
104