Debian Codegraph @

third_party/spirv-tools/source/fuzz/fuzzer_pass_push_ids_through_variables.cpp

raw
1 // Copyright (c) 2020 André Perez Maselco
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_push_ids_through_variables.h"
16
17 #include "source/fuzz/fuzzer_util.h"
18 #include "source/fuzz/instruction_descriptor.h"
19 #include "source/fuzz/transformation_push_id_through_variable.h"
20
21 namespace spvtools {
22 namespace fuzz {
23
24 FuzzerPassPushIdsThroughVariables::FuzzerPassPushIdsThroughVariables(
25 opt::IRContext* ir_context, TransformationContext* transformation_context,
26 FuzzerContext* fuzzer_context,
27 protobufs::TransformationSequence* transformations)
28 : FuzzerPass(ir_context, transformation_context, fuzzer_context,
29 transformations) {}
30
31 FuzzerPassPushIdsThroughVariables::~FuzzerPassPushIdsThroughVariables() =
32 default;
33
34 void FuzzerPassPushIdsThroughVariables::Apply() {
35 ForEachInstructionWithInstructionDescriptor(
36 [this](opt::Function* function, opt::BasicBlock* block,
37 opt::BasicBlock::iterator instruction_iterator,
38 const protobufs::InstructionDescriptor& instruction_descriptor)
39 -> void {
40 assert(instruction_iterator->opcode() ==
41 instruction_descriptor.target_instruction_opcode() &&
42 "The opcode of the instruction we might insert before must be "
43 "the same as the opcode in the descriptor for the instruction");
44
45 // Randomly decide whether to try pushing an id through a variable.
46 if (!GetFuzzerContext()->ChoosePercentage(
47 GetFuzzerContext()->GetChanceOfPushingIdThroughVariable())) {
48 return;
49 }
50
51 // The block containing the instruction we are going to insert before
52 // must be reachable.
53 if (!fuzzerutil::BlockIsReachableInItsFunction(GetIRContext(), block)) {
54 return;
55 }
56
57 // It must be valid to insert OpStore and OpLoad instructions
58 // before the instruction to insert before.
59 if (!fuzzerutil::CanInsertOpcodeBeforeInstruction(
60 SpvOpStore, instruction_iterator) ||
61 !fuzzerutil::CanInsertOpcodeBeforeInstruction(
62 SpvOpLoad, instruction_iterator)) {
63 return;
64 }
65
66 // Randomly decides whether a global or local variable will be added.
67 auto variable_storage_class = GetFuzzerContext()->ChooseEven()
68 ? SpvStorageClassPrivate
69 : SpvStorageClassFunction;
70
71 // Gets the available basic and pointer types.
72 auto basic_type_ids_and_pointers =
73 GetAvailableBasicTypesAndPointers(variable_storage_class);
74 auto& basic_types = basic_type_ids_and_pointers.first;
75 uint32_t basic_type_id =
76 basic_types[GetFuzzerContext()->RandomIndex(basic_types)];
77
78 // Looks for ids that we might wish to consider pushing through a
79 // variable.
80 std::vector<opt::Instruction*> value_instructions =
81 FindAvailableInstructions(
82 function, block, instruction_iterator,
83 [this, basic_type_id, instruction_descriptor](
84 opt::IRContext* ir_context,
85 opt::Instruction* instruction) -> bool {
86 if (!instruction->result_id() || !instruction->type_id()) {
87 return false;
88 }
89
90 if (instruction->type_id() != basic_type_id) {
91 return false;
92 }
93
94 // If the id is irrelevant, we can use it since it will not
95 // participate in DataSynonym fact. Otherwise, we should be
96 // able to produce a synonym out of the id.
97 if (!GetTransformationContext()
98 ->GetFactManager()
99 ->IdIsIrrelevant(instruction->result_id()) &&
100 !fuzzerutil::CanMakeSynonymOf(ir_context,
101 *GetTransformationContext(),
102 instruction)) {
103 return false;
104 }
105
106 return fuzzerutil::IdIsAvailableBeforeInstruction(
107 ir_context,
108 FindInstruction(instruction_descriptor, ir_context),
109 instruction->result_id());
110 });
111
112 if (value_instructions.empty()) {
113 return;
114 }
115
116 // If the pointer type does not exist, then create it.
117 FindOrCreatePointerType(basic_type_id, variable_storage_class);
118
119 // TODO(https://github.com/KhronosGroup/SPIRV-Tools/issues/3403):
120 // type support here is limited by the FindOrCreateZeroConstant
121 // function.
122 const auto* type_inst =
123 GetIRContext()->get_def_use_mgr()->GetDef(basic_type_id);
124 assert(type_inst);
125 switch (type_inst->opcode()) {
126 case SpvOpTypeBool:
127 case SpvOpTypeFloat:
128 case SpvOpTypeInt:
129 case SpvOpTypeArray:
130 case SpvOpTypeMatrix:
131 case SpvOpTypeVector:
132 case SpvOpTypeStruct:
133 break;
134 default:
135 return;
136 }
137
138 // Create a constant to initialize the variable from. This might update
139 // module's id bound so it must be done before any fresh ids are
140 // computed.
141 auto initializer_id = FindOrCreateZeroConstant(basic_type_id, false);
142
143 // Applies the push id through variable transformation.
144 ApplyTransformation(TransformationPushIdThroughVariable(
145 value_instructions[GetFuzzerContext()->RandomIndex(
146 value_instructions)]
147 ->result_id(),
148 GetFuzzerContext()->GetFreshId(), GetFuzzerContext()->GetFreshId(),
149 variable_storage_class, initializer_id, instruction_descriptor));
150 });
151 }
152
153 } // namespace fuzz
154 } // namespace spvtools