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,bool ignore_inapplicable_transformations)23 FuzzerPassAddLoads::FuzzerPassAddLoads(
24 opt::IRContext* ir_context, TransformationContext* transformation_context,
25 FuzzerContext* fuzzer_context,
26 protobufs::TransformationSequence* transformations,
27 bool ignore_inapplicable_transformations)
28 : FuzzerPass(ir_context, transformation_context, fuzzer_context,
29 transformations, ignore_inapplicable_transformations) {}
30
Apply()31 void FuzzerPassAddLoads::Apply() {
32 ForEachInstructionWithInstructionDescriptor(
33 [this](opt::Function* function, opt::BasicBlock* block,
34 opt::BasicBlock::iterator inst_it,
35 const protobufs::InstructionDescriptor& instruction_descriptor)
36 -> void {
37 assert(
38 inst_it->opcode() ==
39 spv::Op(instruction_descriptor.target_instruction_opcode()) &&
40 "The opcode of the instruction we might insert before must be "
41 "the same as the opcode in the descriptor for the instruction");
42
43 // Randomly decide whether to try inserting a load here.
44 if (!GetFuzzerContext()->ChoosePercentage(
45 GetFuzzerContext()->GetChanceOfAddingLoad())) {
46 return;
47 }
48
49 // Check whether it is legitimate to insert a load or atomic load before
50 // this instruction.
51 if (!fuzzerutil::CanInsertOpcodeBeforeInstruction(spv::Op::OpLoad,
52 inst_it)) {
53 return;
54 }
55 if (!fuzzerutil::CanInsertOpcodeBeforeInstruction(spv::Op::OpAtomicLoad,
56 inst_it)) {
57 return;
58 }
59
60 std::vector<opt::Instruction*> relevant_instructions =
61 FindAvailableInstructions(
62 function, block, inst_it,
63 [](opt::IRContext* context,
64 opt::Instruction* instruction) -> bool {
65 if (!instruction->result_id() || !instruction->type_id()) {
66 return false;
67 }
68 switch (instruction->opcode()) {
69 case spv::Op::OpConstantNull:
70 case spv::Op::OpUndef:
71 // Do not allow loading from a null or undefined pointer;
72 // this might be OK if the block is dead, but for now we
73 // conservatively avoid it.
74 return false;
75 default:
76 break;
77 }
78 return context->get_def_use_mgr()
79 ->GetDef(instruction->type_id())
80 ->opcode() == spv::Op::OpTypePointer;
81 });
82
83 // At this point, |relevant_instructions| contains all the pointers
84 // we might think of loading from.
85 if (relevant_instructions.empty()) {
86 return;
87 }
88
89 auto chosen_instruction =
90 relevant_instructions[GetFuzzerContext()->RandomIndex(
91 relevant_instructions)];
92
93 bool is_atomic_load = false;
94 uint32_t memory_scope_id = 0;
95 uint32_t memory_semantics_id = 0;
96
97 auto storage_class = static_cast<spv::StorageClass>(
98 GetIRContext()
99 ->get_def_use_mgr()
100 ->GetDef(chosen_instruction->type_id())
101 ->GetSingleWordInOperand(0));
102
103 switch (storage_class) {
104 case spv::StorageClass::StorageBuffer:
105 case spv::StorageClass::PhysicalStorageBuffer:
106 case spv::StorageClass::Workgroup:
107 case spv::StorageClass::CrossWorkgroup:
108 case spv::StorageClass::AtomicCounter:
109 case spv::StorageClass::Image:
110 if (GetFuzzerContext()->ChoosePercentage(
111 GetFuzzerContext()->GetChanceOfAddingAtomicLoad())) {
112 is_atomic_load = true;
113
114 memory_scope_id = FindOrCreateConstant(
115 {uint32_t(spv::Scope::Invocation)},
116 FindOrCreateIntegerType(32, GetFuzzerContext()->ChooseEven()),
117 false);
118
119 memory_semantics_id = FindOrCreateConstant(
120 {static_cast<uint32_t>(
121 fuzzerutil::GetMemorySemanticsForStorageClass(
122 storage_class))},
123 FindOrCreateIntegerType(32, GetFuzzerContext()->ChooseEven()),
124 false);
125 }
126 break;
127
128 default:
129 break;
130 }
131
132 // Create and apply the transformation.
133 ApplyTransformation(TransformationLoad(
134 GetFuzzerContext()->GetFreshId(), chosen_instruction->result_id(),
135 is_atomic_load, memory_scope_id, memory_semantics_id,
136 instruction_descriptor));
137 });
138 }
139
140 } // namespace fuzz
141 } // namespace spvtools
142