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_split_blocks.h"
16 
17 #include <vector>
18 
19 #include "source/fuzz/instruction_descriptor.h"
20 #include "source/fuzz/transformation_split_block.h"
21 
22 namespace spvtools {
23 namespace fuzz {
24 
FuzzerPassSplitBlocks(opt::IRContext * ir_context,TransformationContext * transformation_context,FuzzerContext * fuzzer_context,protobufs::TransformationSequence * transformations)25 FuzzerPassSplitBlocks::FuzzerPassSplitBlocks(
26     opt::IRContext* ir_context, TransformationContext* transformation_context,
27     FuzzerContext* fuzzer_context,
28     protobufs::TransformationSequence* transformations)
29     : FuzzerPass(ir_context, transformation_context, fuzzer_context,
30                  transformations) {}
31 
32 FuzzerPassSplitBlocks::~FuzzerPassSplitBlocks() = default;
33 
Apply()34 void FuzzerPassSplitBlocks::Apply() {
35   // Gather up pointers to all the blocks in the module.  We are then able to
36   // iterate over these pointers and split the blocks to which they point;
37   // we cannot safely split blocks while we iterate through the module.
38   std::vector<opt::BasicBlock*> blocks;
39   for (auto& function : *GetIRContext()->module()) {
40     for (auto& block : function) {
41       blocks.push_back(&block);
42     }
43   }
44 
45   // Now go through all the block pointers that were gathered.
46   for (auto& block : blocks) {
47     // Probabilistically decide whether to try to split this block.
48     if (!GetFuzzerContext()->ChoosePercentage(
49             GetFuzzerContext()->GetChanceOfSplittingBlock())) {
50       // We are not going to try to split this block.
51       continue;
52     }
53 
54     // TODO(https://github.com/KhronosGroup/SPIRV-Tools/issues/2964): consider
55     //  taking a simpler approach to identifying the instruction before which
56     //  to split a block.
57 
58     // We are going to try to split this block.  We now need to choose where
59     // to split it.  We describe the instruction before which we would like to
60     // split a block via an InstructionDescriptor, details of which are
61     // commented in the protobufs definition file.
62     std::vector<protobufs::InstructionDescriptor> instruction_descriptors;
63 
64     // The initial base instruction is the block label.
65     uint32_t base = block->id();
66 
67     // Counts the number of times we have seen each opcode since we reset the
68     // base instruction.
69     std::map<SpvOp, uint32_t> skip_count;
70 
71     // Consider every instruction in the block.  The label is excluded: it is
72     // only necessary to consider it as a base in case the first instruction
73     // in the block does not have a result id.
74     for (auto& inst : *block) {
75       if (inst.HasResultId()) {
76         // In the case that the instruction has a result id, we use the
77         // instruction as its own base, and clear the skip counts we have
78         // collected.
79         base = inst.result_id();
80         skip_count.clear();
81       }
82       const SpvOp opcode = inst.opcode();
83       instruction_descriptors.emplace_back(MakeInstructionDescriptor(
84           base, opcode, skip_count.count(opcode) ? skip_count.at(opcode) : 0));
85       if (!inst.HasResultId()) {
86         skip_count[opcode] =
87             skip_count.count(opcode) ? skip_count.at(opcode) + 1 : 1;
88       }
89     }
90     // Having identified all the places we might be able to split the block,
91     // we choose one of them.
92     auto transformation = TransformationSplitBlock(
93         instruction_descriptors[GetFuzzerContext()->RandomIndex(
94             instruction_descriptors)],
95         GetFuzzerContext()->GetFreshId());
96     // If the position we have chosen turns out to be a valid place to split
97     // the block, we apply the split. Otherwise the block just doesn't get
98     // split.
99     MaybeApplyTransformation(transformation);
100   }
101 }
102 
103 }  // namespace fuzz
104 }  // namespace spvtools
105