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/transformation_add_synonym.h"
16
17 #include <utility>
18
19 #include "source/fuzz/fuzzer_util.h"
20 #include "source/fuzz/instruction_descriptor.h"
21
22 namespace spvtools {
23 namespace fuzz {
24
TransformationAddSynonym(protobufs::TransformationAddSynonym message)25 TransformationAddSynonym::TransformationAddSynonym(
26 protobufs::TransformationAddSynonym message)
27 : message_(std::move(message)) {}
28
TransformationAddSynonym(uint32_t result_id,protobufs::TransformationAddSynonym::SynonymType synonym_type,uint32_t synonym_fresh_id,const protobufs::InstructionDescriptor & insert_before)29 TransformationAddSynonym::TransformationAddSynonym(
30 uint32_t result_id,
31 protobufs::TransformationAddSynonym::SynonymType synonym_type,
32 uint32_t synonym_fresh_id,
33 const protobufs::InstructionDescriptor& insert_before) {
34 message_.set_result_id(result_id);
35 message_.set_synonym_type(synonym_type);
36 message_.set_synonym_fresh_id(synonym_fresh_id);
37 *message_.mutable_insert_before() = insert_before;
38 }
39
IsApplicable(opt::IRContext * ir_context,const TransformationContext & transformation_context) const40 bool TransformationAddSynonym::IsApplicable(
41 opt::IRContext* ir_context,
42 const TransformationContext& transformation_context) const {
43 assert(protobufs::TransformationAddSynonym::SynonymType_IsValid(
44 message_.synonym_type()) &&
45 "Synonym type is invalid");
46
47 // |synonym_fresh_id| must be fresh.
48 if (!fuzzerutil::IsFreshId(ir_context, message_.synonym_fresh_id())) {
49 return false;
50 }
51
52 // Check that |message_.result_id| is valid.
53 auto* synonym = ir_context->get_def_use_mgr()->GetDef(message_.result_id());
54 if (!synonym) {
55 return false;
56 }
57
58 // Check that we can apply |synonym_type| to |result_id|.
59 if (!IsInstructionValid(ir_context, transformation_context, synonym,
60 message_.synonym_type())) {
61 return false;
62 }
63
64 // Check that |insert_before| is valid.
65 auto* insert_before_inst =
66 FindInstruction(message_.insert_before(), ir_context);
67 if (!insert_before_inst) {
68 return false;
69 }
70
71 const auto* insert_before_inst_block =
72 ir_context->get_instr_block(insert_before_inst);
73 assert(insert_before_inst_block &&
74 "|insert_before_inst| must be in some block");
75
76 if (transformation_context.GetFactManager()->BlockIsDead(
77 insert_before_inst_block->id())) {
78 // We don't create synonyms in dead blocks.
79 return false;
80 }
81
82 // Check that we can insert |message._synonymous_instruction| before
83 // |message_.insert_before| instruction. We use OpIAdd to represent some
84 // instruction that can produce a synonym.
85 if (!fuzzerutil::CanInsertOpcodeBeforeInstruction(SpvOpIAdd,
86 insert_before_inst)) {
87 return false;
88 }
89
90 // A constant instruction must be present in the module if required.
91 if (IsAdditionalConstantRequired(message_.synonym_type()) &&
92 MaybeGetConstantId(ir_context, transformation_context) == 0) {
93 return false;
94 }
95
96 // Domination rules must be satisfied.
97 return fuzzerutil::IdIsAvailableBeforeInstruction(
98 ir_context, insert_before_inst, message_.result_id());
99 }
100
Apply(opt::IRContext * ir_context,TransformationContext * transformation_context) const101 void TransformationAddSynonym::Apply(
102 opt::IRContext* ir_context,
103 TransformationContext* transformation_context) const {
104 // Add a synonymous instruction.
105 auto new_instruction =
106 MakeSynonymousInstruction(ir_context, *transformation_context);
107 auto new_instruction_ptr = new_instruction.get();
108 auto insert_before = FindInstruction(message_.insert_before(), ir_context);
109 insert_before->InsertBefore(std::move(new_instruction));
110
111 fuzzerutil::UpdateModuleIdBound(ir_context, message_.synonym_fresh_id());
112
113 // Inform the def-use manager about the new instruction and record its basic
114 // block.
115 ir_context->get_def_use_mgr()->AnalyzeInstDefUse(new_instruction_ptr);
116 ir_context->set_instr_block(new_instruction_ptr,
117 ir_context->get_instr_block(insert_before));
118
119 // Propagate PointeeValueIsIrrelevant fact.
120 const auto* new_synonym_type = ir_context->get_type_mgr()->GetType(
121 fuzzerutil::GetTypeId(ir_context, message_.synonym_fresh_id()));
122 assert(new_synonym_type && "New synonym should have a valid type");
123
124 if (transformation_context->GetFactManager()->PointeeValueIsIrrelevant(
125 message_.result_id()) &&
126 new_synonym_type->AsPointer()) {
127 transformation_context->GetFactManager()->AddFactValueOfPointeeIsIrrelevant(
128 message_.synonym_fresh_id());
129 }
130
131 // Mark two ids as synonymous.
132 transformation_context->GetFactManager()->AddFactDataSynonym(
133 MakeDataDescriptor(message_.result_id(), {}),
134 MakeDataDescriptor(message_.synonym_fresh_id(), {}));
135 }
136
ToMessage() const137 protobufs::Transformation TransformationAddSynonym::ToMessage() const {
138 protobufs::Transformation result;
139 *result.mutable_add_synonym() = message_;
140 return result;
141 }
142
IsInstructionValid(opt::IRContext * ir_context,const TransformationContext & transformation_context,opt::Instruction * inst,protobufs::TransformationAddSynonym::SynonymType synonym_type)143 bool TransformationAddSynonym::IsInstructionValid(
144 opt::IRContext* ir_context,
145 const TransformationContext& transformation_context, opt::Instruction* inst,
146 protobufs::TransformationAddSynonym::SynonymType synonym_type) {
147 // Instruction must have a result id, type id. We skip OpUndef and
148 // OpConstantNull.
149 if (!inst || !inst->result_id() || !inst->type_id() ||
150 inst->opcode() == SpvOpUndef || inst->opcode() == SpvOpConstantNull) {
151 return false;
152 }
153
154 if (!fuzzerutil::CanMakeSynonymOf(ir_context, transformation_context, inst)) {
155 return false;
156 }
157
158 switch (synonym_type) {
159 case protobufs::TransformationAddSynonym::ADD_ZERO:
160 case protobufs::TransformationAddSynonym::SUB_ZERO:
161 case protobufs::TransformationAddSynonym::MUL_ONE: {
162 // The instruction must be either scalar or vector of integers or floats.
163 const auto* type = ir_context->get_type_mgr()->GetType(inst->type_id());
164 assert(type && "Instruction's result id is invalid");
165
166 if (const auto* vector = type->AsVector()) {
167 return vector->element_type()->AsInteger() ||
168 vector->element_type()->AsFloat();
169 }
170
171 return type->AsInteger() || type->AsFloat();
172 }
173 case protobufs::TransformationAddSynonym::COPY_OBJECT:
174 // All checks for OpCopyObject are handled by
175 // fuzzerutil::CanMakeSynonymOf.
176 return true;
177 case protobufs::TransformationAddSynonym::LOGICAL_AND:
178 case protobufs::TransformationAddSynonym::LOGICAL_OR: {
179 // The instruction must be either a scalar or a vector of booleans.
180 const auto* type = ir_context->get_type_mgr()->GetType(inst->type_id());
181 assert(type && "Instruction's result id is invalid");
182 return (type->AsVector() && type->AsVector()->element_type()->AsBool()) ||
183 type->AsBool();
184 }
185 default:
186 assert(false && "Synonym type is not supported");
187 return false;
188 }
189 }
190
191 std::unique_ptr<opt::Instruction>
MakeSynonymousInstruction(opt::IRContext * ir_context,const TransformationContext & transformation_context) const192 TransformationAddSynonym::MakeSynonymousInstruction(
193 opt::IRContext* ir_context,
194 const TransformationContext& transformation_context) const {
195 auto synonym_type_id =
196 fuzzerutil::GetTypeId(ir_context, message_.result_id());
197 assert(synonym_type_id && "Synonym has invalid type id");
198
199 switch (message_.synonym_type()) {
200 case protobufs::TransformationAddSynonym::SUB_ZERO:
201 case protobufs::TransformationAddSynonym::MUL_ONE:
202 case protobufs::TransformationAddSynonym::ADD_ZERO: {
203 const auto* synonym_type =
204 ir_context->get_type_mgr()->GetType(synonym_type_id);
205 assert(synonym_type && "Synonym has invalid type");
206
207 // Compute instruction's opcode based on the type of the operand.
208 // We have already checked that the operand is either a scalar or a vector
209 // of either integers or floats.
210 auto is_integral =
211 (synonym_type->AsVector() &&
212 synonym_type->AsVector()->element_type()->AsInteger()) ||
213 synonym_type->AsInteger();
214 auto opcode = SpvOpNop;
215 switch (message_.synonym_type()) {
216 case protobufs::TransformationAddSynonym::SUB_ZERO:
217 opcode = is_integral ? SpvOpISub : SpvOpFSub;
218 break;
219 case protobufs::TransformationAddSynonym::MUL_ONE:
220 opcode = is_integral ? SpvOpIMul : SpvOpFMul;
221 break;
222 case protobufs::TransformationAddSynonym::ADD_ZERO:
223 opcode = is_integral ? SpvOpIAdd : SpvOpFAdd;
224 break;
225 default:
226 assert(false && "Unreachable");
227 break;
228 }
229
230 return MakeUnique<opt::Instruction>(
231 ir_context, opcode, synonym_type_id, message_.synonym_fresh_id(),
232 opt::Instruction::OperandList{
233 {SPV_OPERAND_TYPE_ID, {message_.result_id()}},
234 {SPV_OPERAND_TYPE_ID,
235 {MaybeGetConstantId(ir_context, transformation_context)}}});
236 }
237 case protobufs::TransformationAddSynonym::COPY_OBJECT:
238 return MakeUnique<opt::Instruction>(
239 ir_context, SpvOpCopyObject, synonym_type_id,
240 message_.synonym_fresh_id(),
241 opt::Instruction::OperandList{
242 {SPV_OPERAND_TYPE_ID, {message_.result_id()}}});
243 case protobufs::TransformationAddSynonym::LOGICAL_OR:
244 case protobufs::TransformationAddSynonym::LOGICAL_AND: {
245 auto opcode = message_.synonym_type() ==
246 protobufs::TransformationAddSynonym::LOGICAL_OR
247 ? SpvOpLogicalOr
248 : SpvOpLogicalAnd;
249 return MakeUnique<opt::Instruction>(
250 ir_context, opcode, synonym_type_id, message_.synonym_fresh_id(),
251 opt::Instruction::OperandList{
252 {SPV_OPERAND_TYPE_ID, {message_.result_id()}},
253 {SPV_OPERAND_TYPE_ID,
254 {MaybeGetConstantId(ir_context, transformation_context)}}});
255 }
256 default:
257 assert(false && "Unhandled synonym type");
258 return nullptr;
259 }
260 }
261
MaybeGetConstantId(opt::IRContext * ir_context,const TransformationContext & transformation_context) const262 uint32_t TransformationAddSynonym::MaybeGetConstantId(
263 opt::IRContext* ir_context,
264 const TransformationContext& transformation_context) const {
265 assert(IsAdditionalConstantRequired(message_.synonym_type()) &&
266 "Synonym type doesn't require an additional constant");
267
268 auto synonym_type_id =
269 fuzzerutil::GetTypeId(ir_context, message_.result_id());
270 assert(synonym_type_id && "Synonym has invalid type id");
271
272 switch (message_.synonym_type()) {
273 case protobufs::TransformationAddSynonym::ADD_ZERO:
274 case protobufs::TransformationAddSynonym::SUB_ZERO:
275 case protobufs::TransformationAddSynonym::LOGICAL_OR:
276 return fuzzerutil::MaybeGetZeroConstant(
277 ir_context, transformation_context, synonym_type_id, false);
278 case protobufs::TransformationAddSynonym::MUL_ONE:
279 case protobufs::TransformationAddSynonym::LOGICAL_AND: {
280 auto synonym_type = ir_context->get_type_mgr()->GetType(synonym_type_id);
281 assert(synonym_type && "Synonym has invalid type");
282
283 if (const auto* vector = synonym_type->AsVector()) {
284 auto element_type_id =
285 ir_context->get_type_mgr()->GetId(vector->element_type());
286 assert(element_type_id && "Vector's element type is invalid");
287
288 auto one_word =
289 vector->element_type()->AsFloat() ? fuzzerutil::FloatToWord(1) : 1u;
290 if (auto scalar_one_id = fuzzerutil::MaybeGetScalarConstant(
291 ir_context, transformation_context, {one_word}, element_type_id,
292 false)) {
293 return fuzzerutil::MaybeGetCompositeConstant(
294 ir_context, transformation_context,
295 std::vector<uint32_t>(vector->element_count(), scalar_one_id),
296 synonym_type_id, false);
297 }
298
299 return 0;
300 } else {
301 return fuzzerutil::MaybeGetScalarConstant(
302 ir_context, transformation_context,
303 {synonym_type->AsFloat() ? fuzzerutil::FloatToWord(1) : 1u},
304 synonym_type_id, false);
305 }
306 }
307 default:
308 // The assertion at the beginning of the function will fail in the debug
309 // mode.
310 return 0;
311 }
312 }
313
IsAdditionalConstantRequired(protobufs::TransformationAddSynonym::SynonymType synonym_type)314 bool TransformationAddSynonym::IsAdditionalConstantRequired(
315 protobufs::TransformationAddSynonym::SynonymType synonym_type) {
316 switch (synonym_type) {
317 case protobufs::TransformationAddSynonym::ADD_ZERO:
318 case protobufs::TransformationAddSynonym::SUB_ZERO:
319 case protobufs::TransformationAddSynonym::LOGICAL_OR:
320 case protobufs::TransformationAddSynonym::MUL_ONE:
321 case protobufs::TransformationAddSynonym::LOGICAL_AND:
322 return true;
323 default:
324 return false;
325 }
326 }
327
GetFreshIds() const328 std::unordered_set<uint32_t> TransformationAddSynonym::GetFreshIds() const {
329 return {message_.synonym_fresh_id()};
330 }
331
332 } // namespace fuzz
333 } // namespace spvtools
334