1 // Copyright (c) 2019 Google LLC
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_outline_functions.h"
16
17 #include <vector>
18
19 #include "source/fuzz/fuzzer_util.h"
20 #include "source/fuzz/transformation_outline_function.h"
21
22 namespace spvtools {
23 namespace fuzz {
24
FuzzerPassOutlineFunctions(opt::IRContext * ir_context,FactManager * fact_manager,FuzzerContext * fuzzer_context,protobufs::TransformationSequence * transformations)25 FuzzerPassOutlineFunctions::FuzzerPassOutlineFunctions(
26 opt::IRContext* ir_context, FactManager* fact_manager,
27 FuzzerContext* fuzzer_context,
28 protobufs::TransformationSequence* transformations)
29 : FuzzerPass(ir_context, fact_manager, fuzzer_context, transformations) {}
30
31 FuzzerPassOutlineFunctions::~FuzzerPassOutlineFunctions() = default;
32
Apply()33 void FuzzerPassOutlineFunctions::Apply() {
34 std::vector<opt::Function*> original_functions;
35 for (auto& function : *GetIRContext()->module()) {
36 original_functions.push_back(&function);
37 }
38 for (auto& function : original_functions) {
39 if (!GetFuzzerContext()->ChoosePercentage(
40 GetFuzzerContext()->GetChanceOfOutliningFunction())) {
41 continue;
42 }
43 std::vector<opt::BasicBlock*> blocks;
44 for (auto& block : *function) {
45 blocks.push_back(&block);
46 }
47 auto entry_block = blocks[GetFuzzerContext()->RandomIndex(blocks)];
48 auto dominator_analysis = GetIRContext()->GetDominatorAnalysis(function);
49 auto postdominator_analysis =
50 GetIRContext()->GetPostDominatorAnalysis(function);
51 std::vector<opt::BasicBlock*> candidate_exit_blocks;
52 for (auto postdominates_entry_block = entry_block;
53 postdominates_entry_block != nullptr;
54 postdominates_entry_block = postdominator_analysis->ImmediateDominator(
55 postdominates_entry_block)) {
56 if (dominator_analysis->Dominates(entry_block,
57 postdominates_entry_block)) {
58 candidate_exit_blocks.push_back(postdominates_entry_block);
59 }
60 }
61 if (candidate_exit_blocks.empty()) {
62 continue;
63 }
64 auto exit_block = candidate_exit_blocks[GetFuzzerContext()->RandomIndex(
65 candidate_exit_blocks)];
66
67 auto region_blocks = TransformationOutlineFunction::GetRegionBlocks(
68 GetIRContext(), entry_block, exit_block);
69 std::map<uint32_t, uint32_t> input_id_to_fresh_id;
70 for (auto id : TransformationOutlineFunction::GetRegionInputIds(
71 GetIRContext(), region_blocks, exit_block)) {
72 input_id_to_fresh_id[id] = GetFuzzerContext()->GetFreshId();
73 }
74 std::map<uint32_t, uint32_t> output_id_to_fresh_id;
75 for (auto id : TransformationOutlineFunction::GetRegionOutputIds(
76 GetIRContext(), region_blocks, exit_block)) {
77 output_id_to_fresh_id[id] = GetFuzzerContext()->GetFreshId();
78 }
79 TransformationOutlineFunction transformation(
80 entry_block->id(), exit_block->id(),
81 /*new_function_struct_return_type_id*/
82 GetFuzzerContext()->GetFreshId(),
83 /*new_function_type_id*/ GetFuzzerContext()->GetFreshId(),
84 /*new_function_id*/ GetFuzzerContext()->GetFreshId(),
85 /*new_function_region_entry_block*/
86 GetFuzzerContext()->GetFreshId(),
87 /*new_caller_result_id*/ GetFuzzerContext()->GetFreshId(),
88 /*new_callee_result_id*/ GetFuzzerContext()->GetFreshId(),
89 /*input_id_to_fresh_id*/ std::move(input_id_to_fresh_id),
90 /*output_id_to_fresh_id*/ std::move(output_id_to_fresh_id));
91 if (transformation.IsApplicable(GetIRContext(), *GetFactManager())) {
92 transformation.Apply(GetIRContext(), GetFactManager());
93 *GetTransformations()->add_transformation() = transformation.ToMessage();
94 }
95 }
96 }
97
98 } // namespace fuzz
99 } // namespace spvtools
100