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(inst_it->opcode() ==
38 instruction_descriptor.target_instruction_opcode() &&
39 "The opcode of the instruction we might insert before must be "
40 "the same as the opcode in the descriptor for the instruction");
41
42 // Randomly decide whether to try inserting a load here.
43 if (!GetFuzzerContext()->ChoosePercentage(
44 GetFuzzerContext()->GetChanceOfAddingLoad())) {
45 return;
46 }
47
48 // Check whether it is legitimate to insert a load or atomic load before
49 // this instruction.
50 if (!fuzzerutil::CanInsertOpcodeBeforeInstruction(SpvOpLoad, inst_it)) {
51 return;
52 }
53 if (!fuzzerutil::CanInsertOpcodeBeforeInstruction(SpvOpAtomicLoad,
54 inst_it)) {
55 return;
56 }
57
58 std::vector<opt::Instruction*> relevant_instructions =
59 FindAvailableInstructions(
60 function, block, inst_it,
61 [](opt::IRContext* context,
62 opt::Instruction* instruction) -> bool {
63 if (!instruction->result_id() || !instruction->type_id()) {
64 return false;
65 }
66 switch (instruction->opcode()) {
67 case SpvOpConstantNull:
68 case SpvOpUndef:
69 // Do not allow loading from a null or undefined pointer;
70 // this might be OK if the block is dead, but for now we
71 // conservatively avoid it.
72 return false;
73 default:
74 break;
75 }
76 return context->get_def_use_mgr()
77 ->GetDef(instruction->type_id())
78 ->opcode() == SpvOpTypePointer;
79 });
80
81 // At this point, |relevant_instructions| contains all the pointers
82 // we might think of loading from.
83 if (relevant_instructions.empty()) {
84 return;
85 }
86
87 auto chosen_instruction =
88 relevant_instructions[GetFuzzerContext()->RandomIndex(
89 relevant_instructions)];
90
91 bool is_atomic_load = false;
92 uint32_t memory_scope_id = 0;
93 uint32_t memory_semantics_id = 0;
94
95 auto storage_class = static_cast<SpvStorageClass>(
96 GetIRContext()
97 ->get_def_use_mgr()
98 ->GetDef(chosen_instruction->type_id())
99 ->GetSingleWordInOperand(0));
100
101 switch (storage_class) {
102 case SpvStorageClassStorageBuffer:
103 case SpvStorageClassPhysicalStorageBuffer:
104 case SpvStorageClassWorkgroup:
105 case SpvStorageClassCrossWorkgroup:
106 case SpvStorageClassAtomicCounter:
107 case SpvStorageClassImage:
108 if (GetFuzzerContext()->ChoosePercentage(
109 GetFuzzerContext()->GetChanceOfAddingAtomicLoad())) {
110 is_atomic_load = true;
111
112 memory_scope_id = FindOrCreateConstant(
113 {SpvScopeInvocation},
114 FindOrCreateIntegerType(32, GetFuzzerContext()->ChooseEven()),
115 false);
116
117 memory_semantics_id = FindOrCreateConstant(
118 {static_cast<uint32_t>(
119 fuzzerutil::GetMemorySemanticsForStorageClass(
120 storage_class))},
121 FindOrCreateIntegerType(32, GetFuzzerContext()->ChooseEven()),
122 false);
123 }
124 break;
125
126 default:
127 break;
128 }
129
130 // Create and apply the transformation.
131 ApplyTransformation(TransformationLoad(
132 GetFuzzerContext()->GetFreshId(), chosen_instruction->result_id(),
133 is_atomic_load, memory_scope_id, memory_semantics_id,
134 instruction_descriptor));
135 });
136 }
137
138 } // namespace fuzz
139 } // namespace spvtools
140