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