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