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_wrap_regions_in_selections.h"
16 
17 #include "source/fuzz/fuzzer_context.h"
18 #include "source/fuzz/instruction_descriptor.h"
19 #include "source/fuzz/transformation_split_block.h"
20 #include "source/fuzz/transformation_wrap_region_in_selection.h"
21 
22 namespace spvtools {
23 namespace fuzz {
24 
FuzzerPassWrapRegionsInSelections(opt::IRContext * ir_context,TransformationContext * transformation_context,FuzzerContext * fuzzer_context,protobufs::TransformationSequence * transformations,bool ignore_inapplicable_transformations)25 FuzzerPassWrapRegionsInSelections::FuzzerPassWrapRegionsInSelections(
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 FuzzerPassWrapRegionsInSelections::Apply() {
34   for (auto& function : *GetIRContext()->module()) {
35     if (!GetFuzzerContext()->ChoosePercentage(
36             GetFuzzerContext()->GetChanceOfWrappingRegionInSelection())) {
37       continue;
38     }
39 
40     // It is easier to select an element at random from a vector than from an
41     // instruction list.
42     std::vector<opt::BasicBlock*> header_block_candidates;
43     for (auto& block : function) {
44       header_block_candidates.push_back(&block);
45     }
46 
47     if (header_block_candidates.empty()) {
48       continue;
49     }
50 
51     // Try to get a header block candidate that will increase the chances of the
52     // transformation being applicable.
53     auto* header_block_candidate = MaybeGetHeaderBlockCandidate(
54         header_block_candidates[GetFuzzerContext()->RandomIndex(
55             header_block_candidates)]);
56     if (!header_block_candidate) {
57       continue;
58     }
59 
60     std::vector<opt::BasicBlock*> merge_block_candidates;
61     for (auto& block : function) {
62       if (GetIRContext()->GetDominatorAnalysis(&function)->StrictlyDominates(
63               header_block_candidate, &block) &&
64           GetIRContext()
65               ->GetPostDominatorAnalysis(&function)
66               ->StrictlyDominates(&block, header_block_candidate)) {
67         merge_block_candidates.push_back(&block);
68       }
69     }
70 
71     if (merge_block_candidates.empty()) {
72       continue;
73     }
74 
75     // Try to get a merge block candidate that will increase the chances of the
76     // transformation being applicable.
77     auto* merge_block_candidate = MaybeGetMergeBlockCandidate(
78         merge_block_candidates[GetFuzzerContext()->RandomIndex(
79             merge_block_candidates)]);
80     if (!merge_block_candidate) {
81       continue;
82     }
83 
84     if (!TransformationWrapRegionInSelection::IsApplicableToBlockRange(
85             GetIRContext(), header_block_candidate->id(),
86             merge_block_candidate->id())) {
87       continue;
88     }
89 
90     // This boolean constant will be used as a condition for the
91     // OpBranchConditional instruction. We mark it as irrelevant to be able to
92     // replace it with a more interesting value later.
93     auto branch_condition = GetFuzzerContext()->ChooseEven();
94     FindOrCreateBoolConstant(branch_condition, true);
95 
96     ApplyTransformation(TransformationWrapRegionInSelection(
97         header_block_candidate->id(), merge_block_candidate->id(),
98         branch_condition));
99   }
100 }
101 
102 opt::BasicBlock*
MaybeGetHeaderBlockCandidate(opt::BasicBlock * header_block_candidate)103 FuzzerPassWrapRegionsInSelections::MaybeGetHeaderBlockCandidate(
104     opt::BasicBlock* header_block_candidate) {
105   // Try to create a preheader if |header_block_candidate| is a loop header.
106   if (header_block_candidate->IsLoopHeader()) {
107     // GetOrCreateSimpleLoopPreheader only supports reachable blocks.
108     return GetIRContext()->cfg()->preds(header_block_candidate->id()).size() ==
109                    1
110                ? nullptr
111                : GetOrCreateSimpleLoopPreheader(header_block_candidate->id());
112   }
113 
114   // Try to split |header_block_candidate| if it's already a header block.
115   if (header_block_candidate->GetMergeInst()) {
116     SplitBlockAfterOpPhiOrOpVariable(header_block_candidate->id());
117   }
118 
119   return header_block_candidate;
120 }
121 
MaybeGetMergeBlockCandidate(opt::BasicBlock * merge_block_candidate)122 opt::BasicBlock* FuzzerPassWrapRegionsInSelections::MaybeGetMergeBlockCandidate(
123     opt::BasicBlock* merge_block_candidate) {
124   // If |merge_block_candidate| is a merge block of some construct, try to split
125   // it and return a newly created block.
126   if (GetIRContext()->GetStructuredCFGAnalysis()->IsMergeBlock(
127           merge_block_candidate->id())) {
128     // We can't split a merge block if it's also a loop header.
129     return merge_block_candidate->IsLoopHeader()
130                ? nullptr
131                : SplitBlockAfterOpPhiOrOpVariable(merge_block_candidate->id());
132   }
133 
134   return merge_block_candidate;
135 }
136 
137 }  // namespace fuzz
138 }  // namespace spvtools
139