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