• 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_blocks.h"
16 
17 #include <algorithm>
18 
19 #include "source/fuzz/fuzzer_util.h"
20 #include "source/fuzz/transformation_add_dead_block.h"
21 
22 namespace spvtools {
23 namespace fuzz {
24 
25 namespace {
26 
27 const size_t kMaxTransformationsInOnePass = 100U;
28 
29 }  // namespace
30 
FuzzerPassAddDeadBlocks(opt::IRContext * ir_context,TransformationContext * transformation_context,FuzzerContext * fuzzer_context,protobufs::TransformationSequence * transformations,bool ignore_inapplicable_transformations)31 FuzzerPassAddDeadBlocks::FuzzerPassAddDeadBlocks(
32     opt::IRContext* ir_context, TransformationContext* transformation_context,
33     FuzzerContext* fuzzer_context,
34     protobufs::TransformationSequence* transformations,
35     bool ignore_inapplicable_transformations)
36     : FuzzerPass(ir_context, transformation_context, fuzzer_context,
37                  transformations, ignore_inapplicable_transformations) {}
38 
Apply()39 void FuzzerPassAddDeadBlocks::Apply() {
40   // We iterate over all blocks in the module collecting up those at which we
41   // might add a branch to a new dead block.  We then loop over all such
42   // candidates and actually apply transformations.  This separation is to
43   // avoid modifying the module as we traverse it.
44   std::vector<TransformationAddDeadBlock> candidate_transformations;
45   for (auto& function : *GetIRContext()->module()) {
46     for (auto& block : function) {
47       if (!GetFuzzerContext()->ChoosePercentage(
48               GetFuzzerContext()->GetChanceOfAddingDeadBlock())) {
49         continue;
50       }
51 
52       // Make sure the module contains a boolean constant equal to
53       // |condition_value|.
54       bool condition_value = GetFuzzerContext()->ChooseEven();
55       FindOrCreateBoolConstant(condition_value, false);
56 
57       // We speculatively create a transformation, and then apply it (below) if
58       // it turns out to be applicable.  This avoids duplicating the logic for
59       // applicability checking.
60       //
61       // It means that fresh ids for transformations that turn out not to be
62       // applicable end up being unused.
63       candidate_transformations.emplace_back(TransformationAddDeadBlock(
64           GetFuzzerContext()->GetFreshId(), block.id(), condition_value));
65     }
66   }
67   // Applying transformations can be expensive as each transformation requires
68   // dominator information and also invalidates dominator information. We thus
69   // limit the number of transformations that one application of this fuzzer
70   // pass can apply. We choose to do this after identifying all the
71   // transformations that we *might* want to apply, rather than breaking the
72   // above loops once the limit is reached, to avoid biasing towards
73   // transformations that target early parts of the module.
74   GetFuzzerContext()->Shuffle(&candidate_transformations);
75   for (size_t i = 0; i < std::min(kMaxTransformationsInOnePass,
76                                   candidate_transformations.size());
77        i++) {
78     MaybeApplyTransformation(candidate_transformations[i]);
79   }
80 }
81 
82 }  // namespace fuzz
83 }  // namespace spvtools
84