• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2020 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_loads.h"
16 
17 #include "source/fuzz/fuzzer_util.h"
18 #include "source/fuzz/transformation_load.h"
19 
20 namespace spvtools {
21 namespace fuzz {
22 
FuzzerPassAddLoads(opt::IRContext * ir_context,TransformationContext * transformation_context,FuzzerContext * fuzzer_context,protobufs::TransformationSequence * transformations)23 FuzzerPassAddLoads::FuzzerPassAddLoads(
24     opt::IRContext* ir_context, TransformationContext* transformation_context,
25     FuzzerContext* fuzzer_context,
26     protobufs::TransformationSequence* transformations)
27     : FuzzerPass(ir_context, transformation_context, fuzzer_context,
28                  transformations) {}
29 
Apply()30 void FuzzerPassAddLoads::Apply() {
31   ForEachInstructionWithInstructionDescriptor(
32       [this](opt::Function* function, opt::BasicBlock* block,
33              opt::BasicBlock::iterator inst_it,
34              const protobufs::InstructionDescriptor& instruction_descriptor)
35           -> void {
36         assert(inst_it->opcode() ==
37                    instruction_descriptor.target_instruction_opcode() &&
38                "The opcode of the instruction we might insert before must be "
39                "the same as the opcode in the descriptor for the instruction");
40 
41         // Check whether it is legitimate to insert a load before this
42         // instruction.
43         if (!fuzzerutil::CanInsertOpcodeBeforeInstruction(SpvOpLoad, inst_it)) {
44           return;
45         }
46 
47         // Randomly decide whether to try inserting a load here.
48         if (!GetFuzzerContext()->ChoosePercentage(
49                 GetFuzzerContext()->GetChanceOfAddingLoad())) {
50           return;
51         }
52 
53         std::vector<opt::Instruction*> relevant_instructions =
54             FindAvailableInstructions(
55                 function, block, inst_it,
56                 [](opt::IRContext* context,
57                    opt::Instruction* instruction) -> bool {
58                   if (!instruction->result_id() || !instruction->type_id()) {
59                     return false;
60                   }
61                   switch (instruction->opcode()) {
62                     case SpvOpConstantNull:
63                     case SpvOpUndef:
64                       // Do not allow loading from a null or undefined pointer;
65                       // this might be OK if the block is dead, but for now we
66                       // conservatively avoid it.
67                       return false;
68                     default:
69                       break;
70                   }
71                   return context->get_def_use_mgr()
72                              ->GetDef(instruction->type_id())
73                              ->opcode() == SpvOpTypePointer;
74                 });
75 
76         // At this point, |relevant_instructions| contains all the pointers
77         // we might think of loading from.
78         if (relevant_instructions.empty()) {
79           return;
80         }
81 
82         // Choose a pointer at random, and create and apply a loading
83         // transformation based on it.
84         ApplyTransformation(TransformationLoad(
85             GetFuzzerContext()->GetFreshId(),
86             relevant_instructions[GetFuzzerContext()->RandomIndex(
87                                       relevant_instructions)]
88                 ->result_id(),
89             instruction_descriptor));
90       });
91 }
92 
93 }  // namespace fuzz
94 }  // namespace spvtools
95