Debian Codegraph @

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

raw
1 // Copyright (c) 2020 Vasyl Teliman
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_composite_extract.h"
16
17 #include "source/fuzz/fuzzer_context.h"
18 #include "source/fuzz/fuzzer_util.h"
19 #include "source/fuzz/instruction_descriptor.h"
20 #include "source/fuzz/transformation_composite_extract.h"
21
22 namespace spvtools {
23 namespace fuzz {
24
25 FuzzerPassAddCompositeExtract::FuzzerPassAddCompositeExtract(
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
32 FuzzerPassAddCompositeExtract::~FuzzerPassAddCompositeExtract() = default;
33
34 void FuzzerPassAddCompositeExtract::Apply() {
35 std::vector<const protobufs::DataDescriptor*> composite_synonyms;
36 for (const auto* dd :
37 GetTransformationContext()->GetFactManager()->GetAllSynonyms()) {
38 // |dd| must describe a component of a composite.
39 if (!dd->index().empty()) {
40 composite_synonyms.push_back(dd);
41 }
42 }
43
44 // We don't want to invalidate the module every time we apply this
45 // transformation since rebuilding DominatorAnalysis can be expensive, so we
46 // collect up the transformations we wish to apply and apply them all later.
47 std::vector<TransformationCompositeExtract> transformations;
48
49 ForEachInstructionWithInstructionDescriptor(
50 [this, &composite_synonyms, &transformations](
51 opt::Function* function, opt::BasicBlock* block,
52 opt::BasicBlock::iterator inst_it,
53 const protobufs::InstructionDescriptor& instruction_descriptor) {
54 if (!fuzzerutil::CanInsertOpcodeBeforeInstruction(SpvOpCompositeExtract,
55 inst_it)) {
56 return;
57 }
58
59 if (!GetFuzzerContext()->ChoosePercentage(
60 GetFuzzerContext()->GetChanceOfAddingCompositeExtract())) {
61 return;
62 }
63
64 auto available_composites = FindAvailableInstructions(
65 function, block, inst_it,
66 [](opt::IRContext* ir_context, opt::Instruction* inst) {
67 return inst->type_id() && inst->result_id() &&
68 fuzzerutil::IsCompositeType(
69 ir_context->get_type_mgr()->GetType(inst->type_id()));
70 });
71
72 std::vector<const protobufs::DataDescriptor*> available_synonyms;
73 for (const auto* dd : composite_synonyms) {
74 if (fuzzerutil::IdIsAvailableBeforeInstruction(
75 GetIRContext(), &*inst_it, dd->object())) {
76 available_synonyms.push_back(dd);
77 }
78 }
79
80 if (available_synonyms.empty() && available_composites.empty()) {
81 return;
82 }
83
84 uint32_t composite_id = 0;
85 std::vector<uint32_t> indices;
86
87 if (available_synonyms.empty() || (!available_composites.empty() &&
88 GetFuzzerContext()->ChooseEven())) {
89 const auto* inst =
90 available_composites[GetFuzzerContext()->RandomIndex(
91 available_composites)];
92 composite_id = inst->result_id();
93
94 auto type_id = inst->type_id();
95 do {
96 uint32_t number_of_members = 0;
97
98 const auto* type_inst =
99 GetIRContext()->get_def_use_mgr()->GetDef(type_id);
100 assert(type_inst && "Composite instruction has invalid type id");
101
102 switch (type_inst->opcode()) {
103 case SpvOpTypeArray:
104 number_of_members =
105 fuzzerutil::GetArraySize(*type_inst, GetIRContext());
106 break;
107 case SpvOpTypeVector:
108 case SpvOpTypeMatrix:
109 number_of_members = type_inst->GetSingleWordInOperand(1);
110 break;
111 case SpvOpTypeStruct:
112 number_of_members = type_inst->NumInOperands();
113 break;
114 default:
115 assert(false && "|type_inst| is not a composite");
116 return;
117 }
118
119 if (number_of_members == 0) {
120 return;
121 }
122
123 indices.push_back(
124 GetFuzzerContext()->GetRandomCompositeExtractIndex(
125 number_of_members));
126
127 switch (type_inst->opcode()) {
128 case SpvOpTypeArray:
129 case SpvOpTypeVector:
130 case SpvOpTypeMatrix:
131 type_id = type_inst->GetSingleWordInOperand(0);
132 break;
133 case SpvOpTypeStruct:
134 type_id = type_inst->GetSingleWordInOperand(indices.back());
135 break;
136 default:
137 assert(false && "|type_inst| is not a composite");
138 return;
139 }
140 } while (fuzzerutil::IsCompositeType(
141 GetIRContext()->get_type_mgr()->GetType(type_id)) &&
142 GetFuzzerContext()->ChoosePercentage(
143 GetFuzzerContext()
144 ->GetChanceOfGoingDeeperToExtractComposite()));
145 } else {
146 const auto* dd = available_synonyms[GetFuzzerContext()->RandomIndex(
147 available_synonyms)];
148
149 composite_id = dd->object();
150 indices.assign(dd->index().begin(), dd->index().end());
151 }
152
153 assert(composite_id != 0 && !indices.empty() &&
154 "Composite object should have been chosen correctly");
155
156 transformations.emplace_back(instruction_descriptor,
157 GetFuzzerContext()->GetFreshId(),
158 composite_id, indices);
159 });
160
161 for (const auto& transformation : transformations) {
162 ApplyTransformation(transformation);
163 }
164 }
165
166 } // namespace fuzz
167 } // namespace spvtools