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