• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_add_dead_breaks.h"
16 
17 #include "source/fuzz/fuzzer_util.h"
18 #include "source/fuzz/transformation_add_dead_break.h"
19 #include "source/opt/ir_context.h"
20 
21 namespace spvtools {
22 namespace fuzz {
23 
FuzzerPassAddDeadBreaks(opt::IRContext * ir_context,TransformationContext * transformation_context,FuzzerContext * fuzzer_context,protobufs::TransformationSequence * transformations)24 FuzzerPassAddDeadBreaks::FuzzerPassAddDeadBreaks(
25     opt::IRContext* ir_context, TransformationContext* transformation_context,
26     FuzzerContext* fuzzer_context,
27     protobufs::TransformationSequence* transformations)
28     : FuzzerPass(ir_context, transformation_context, fuzzer_context,
29                  transformations) {}
30 
Apply()31 void FuzzerPassAddDeadBreaks::Apply() {
32   // We first collect up lots of possibly-applicable transformations.
33   std::vector<TransformationAddDeadBreak> candidate_transformations;
34   // We consider each function separately.
35   for (auto& function : *GetIRContext()->module()) {
36     // For a given function, we find all the merge blocks in that function.
37     std::vector<opt::BasicBlock*> merge_blocks;
38     for (auto& block : function) {
39       auto maybe_merge_id = block.MergeBlockIdIfAny();
40       if (maybe_merge_id) {
41         auto merge_block =
42             fuzzerutil::MaybeFindBlock(GetIRContext(), maybe_merge_id);
43 
44         assert(merge_block && "Merge block can't be null");
45 
46         merge_blocks.push_back(merge_block);
47       }
48     }
49     // We rather aggressively consider the possibility of adding a break from
50     // every block in the function to every merge block.  Many of these will be
51     // inapplicable as they would be illegal.  That's OK - we later discard the
52     // ones that turn out to be no good.
53     for (auto& block : function) {
54       for (auto* merge_block : merge_blocks) {
55         // Populate this vector with ids that are available at the branch point
56         // of this basic block. We will use these ids to update OpPhi
57         // instructions later.
58         std::vector<uint32_t> phi_ids;
59 
60         // Determine how we need to adjust OpPhi instructions' operands
61         // for this transformation to be valid.
62         //
63         // If |block| has a branch to |merge_block|, the latter must have all of
64         // its OpPhi instructions set up correctly - we don't need to adjust
65         // anything.
66         if (!block.IsSuccessor(merge_block)) {
67           merge_block->ForEachPhiInst([this, &phi_ids](opt::Instruction* phi) {
68             // Add an additional operand for OpPhi instruction.  Use a constant
69             // if possible, and an undef otherwise.
70             if (fuzzerutil::CanCreateConstant(GetIRContext(), phi->type_id())) {
71               // We mark the constant as irrelevant so that we can replace it
72               // with a more interesting value later.
73               phi_ids.push_back(FindOrCreateZeroConstant(phi->type_id(), true));
74             } else {
75               phi_ids.push_back(FindOrCreateGlobalUndef(phi->type_id()));
76             }
77           });
78         }
79 
80         // Make sure the module has a required boolean constant to be used in
81         // OpBranchConditional instruction.
82         auto break_condition = GetFuzzerContext()->ChooseEven();
83         FindOrCreateBoolConstant(break_condition, false);
84 
85         auto candidate_transformation = TransformationAddDeadBreak(
86             block.id(), merge_block->id(), break_condition, std::move(phi_ids));
87         if (candidate_transformation.IsApplicable(
88                 GetIRContext(), *GetTransformationContext())) {
89           // Only consider a transformation as a candidate if it is applicable.
90           candidate_transformations.push_back(
91               std::move(candidate_transformation));
92         }
93       }
94     }
95   }
96 
97   // Go through the candidate transformations that were accumulated,
98   // probabilistically deciding whether to consider each one further and
99   // applying the still-applicable ones that are considered further.
100   //
101   // We iterate through the candidate transformations in a random order by
102   // repeatedly removing a random candidate transformation from the sequence
103   // until no candidate transformations remain.  This is done because
104   // transformations can potentially disable one another, so that iterating
105   // through them in order would lead to a higher probability of
106   // transformations appearing early in the sequence being applied compared
107   // with later transformations.
108   while (!candidate_transformations.empty()) {
109     // Choose a random index into the sequence of remaining candidate
110     // transformations.
111     auto index = GetFuzzerContext()->RandomIndex(candidate_transformations);
112     // Remove the transformation at the chosen index from the sequence.
113     auto transformation = std::move(candidate_transformations[index]);
114     candidate_transformations.erase(candidate_transformations.begin() + index);
115     // Probabilistically decide whether to try to apply it vs. ignore it, in the
116     // case that it is applicable.
117     if (GetFuzzerContext()->ChoosePercentage(
118             GetFuzzerContext()->GetChanceOfAddingDeadBreak())) {
119       MaybeApplyTransformation(transformation);
120     }
121   }
122 }
123 
124 }  // namespace fuzz
125 }  // namespace spvtools
126