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,
155 *inst)) {
156 return false;
157 }
158
159 switch (synonym_type) {
160 case protobufs::TransformationAddSynonym::ADD_ZERO:
161 case protobufs::TransformationAddSynonym::SUB_ZERO:
162 case protobufs::TransformationAddSynonym::MUL_ONE: {
163 // The instruction must be either scalar or vector of integers or floats.
164 const auto* type = ir_context->get_type_mgr()->GetType(inst->type_id());
165 assert(type && "Instruction's result id is invalid");
166
167 if (const auto* vector = type->AsVector()) {
168 return vector->element_type()->AsInteger() ||
169 vector->element_type()->AsFloat();
170 }
171
172 return type->AsInteger() || type->AsFloat();
173 }
174 case protobufs::TransformationAddSynonym::BITWISE_OR:
175 case protobufs::TransformationAddSynonym::BITWISE_XOR: {
176 // The instruction must be either an integer or a vector of integers.
177 const auto* type = ir_context->get_type_mgr()->GetType(inst->type_id());
178 assert(type && "Instruction's result id is invalid");
179
180 if (const auto* vector = type->AsVector()) {
181 return vector->element_type()->AsInteger();
182 }
183
184 return type->AsInteger();
185 }
186 case protobufs::TransformationAddSynonym::COPY_OBJECT:
187 // All checks for OpCopyObject are handled by
188 // fuzzerutil::CanMakeSynonymOf.
189 return true;
190 case protobufs::TransformationAddSynonym::LOGICAL_AND:
191 case protobufs::TransformationAddSynonym::LOGICAL_OR: {
192 // The instruction must be either a scalar or a vector of booleans.
193 const auto* type = ir_context->get_type_mgr()->GetType(inst->type_id());
194 assert(type && "Instruction's result id is invalid");
195 return (type->AsVector() && type->AsVector()->element_type()->AsBool()) ||
196 type->AsBool();
197 }
198 default:
199 assert(false && "Synonym type is not supported");
200 return false;
201 }
202 }
203
204 std::unique_ptr<opt::Instruction>
MakeSynonymousInstruction(opt::IRContext * ir_context,const TransformationContext & transformation_context) const205 TransformationAddSynonym::MakeSynonymousInstruction(
206 opt::IRContext* ir_context,
207 const TransformationContext& transformation_context) const {
208 auto synonym_type_id =
209 fuzzerutil::GetTypeId(ir_context, message_.result_id());
210 assert(synonym_type_id && "Synonym has invalid type id");
211 auto opcode = SpvOpNop;
212 const auto* synonym_type =
213 ir_context->get_type_mgr()->GetType(synonym_type_id);
214 assert(synonym_type && "Synonym has invalid type");
215
216 auto is_integral = (synonym_type->AsVector() &&
217 synonym_type->AsVector()->element_type()->AsInteger()) ||
218 synonym_type->AsInteger();
219
220 switch (message_.synonym_type()) {
221 case protobufs::TransformationAddSynonym::SUB_ZERO:
222 opcode = is_integral ? SpvOpISub : SpvOpFSub;
223 break;
224 case protobufs::TransformationAddSynonym::MUL_ONE:
225 opcode = is_integral ? SpvOpIMul : SpvOpFMul;
226 break;
227 case protobufs::TransformationAddSynonym::ADD_ZERO:
228 opcode = is_integral ? SpvOpIAdd : SpvOpFAdd;
229 break;
230 case protobufs::TransformationAddSynonym::LOGICAL_OR:
231 opcode = SpvOpLogicalOr;
232 break;
233 case protobufs::TransformationAddSynonym::LOGICAL_AND:
234 opcode = SpvOpLogicalAnd;
235 break;
236 case protobufs::TransformationAddSynonym::BITWISE_OR:
237 opcode = SpvOpBitwiseOr;
238 break;
239 case protobufs::TransformationAddSynonym::BITWISE_XOR:
240 opcode = SpvOpBitwiseXor;
241 break;
242
243 case protobufs::TransformationAddSynonym::COPY_OBJECT:
244 return MakeUnique<opt::Instruction>(
245 ir_context, SpvOpCopyObject, synonym_type_id,
246 message_.synonym_fresh_id(),
247 opt::Instruction::OperandList{
248 {SPV_OPERAND_TYPE_ID, {message_.result_id()}}});
249
250 default:
251 assert(false && "Unhandled synonym type");
252 return nullptr;
253 }
254
255 return MakeUnique<opt::Instruction>(
256 ir_context, opcode, synonym_type_id, message_.synonym_fresh_id(),
257 opt::Instruction::OperandList{
258 {SPV_OPERAND_TYPE_ID, {message_.result_id()}},
259 {SPV_OPERAND_TYPE_ID,
260 {MaybeGetConstantId(ir_context, transformation_context)}}});
261 }
262
MaybeGetConstantId(opt::IRContext * ir_context,const TransformationContext & transformation_context) const263 uint32_t TransformationAddSynonym::MaybeGetConstantId(
264 opt::IRContext* ir_context,
265 const TransformationContext& transformation_context) const {
266 assert(IsAdditionalConstantRequired(message_.synonym_type()) &&
267 "Synonym type doesn't require an additional constant");
268
269 auto synonym_type_id =
270 fuzzerutil::GetTypeId(ir_context, message_.result_id());
271 assert(synonym_type_id && "Synonym has invalid type id");
272
273 switch (message_.synonym_type()) {
274 case protobufs::TransformationAddSynonym::ADD_ZERO:
275 case protobufs::TransformationAddSynonym::SUB_ZERO:
276 case protobufs::TransformationAddSynonym::LOGICAL_OR:
277 case protobufs::TransformationAddSynonym::BITWISE_OR:
278 case protobufs::TransformationAddSynonym::BITWISE_XOR:
279 return fuzzerutil::MaybeGetZeroConstant(
280 ir_context, transformation_context, synonym_type_id, false);
281 case protobufs::TransformationAddSynonym::MUL_ONE:
282 case protobufs::TransformationAddSynonym::LOGICAL_AND: {
283 auto synonym_type = ir_context->get_type_mgr()->GetType(synonym_type_id);
284 assert(synonym_type && "Synonym has invalid type");
285
286 if (const auto* vector = synonym_type->AsVector()) {
287 auto element_type_id =
288 ir_context->get_type_mgr()->GetId(vector->element_type());
289 assert(element_type_id && "Vector's element type is invalid");
290
291 auto one_word =
292 vector->element_type()->AsFloat() ? fuzzerutil::FloatToWord(1) : 1u;
293 if (auto scalar_one_id = fuzzerutil::MaybeGetScalarConstant(
294 ir_context, transformation_context, {one_word}, element_type_id,
295 false)) {
296 return fuzzerutil::MaybeGetCompositeConstant(
297 ir_context, transformation_context,
298 std::vector<uint32_t>(vector->element_count(), scalar_one_id),
299 synonym_type_id, false);
300 }
301
302 return 0;
303 } else {
304 return fuzzerutil::MaybeGetScalarConstant(
305 ir_context, transformation_context,
306 {synonym_type->AsFloat() ? fuzzerutil::FloatToWord(1) : 1u},
307 synonym_type_id, false);
308 }
309 }
310 default:
311 // The assertion at the beginning of the function will fail in the debug
312 // mode.
313 return 0;
314 }
315 }
316
IsAdditionalConstantRequired(protobufs::TransformationAddSynonym::SynonymType synonym_type)317 bool TransformationAddSynonym::IsAdditionalConstantRequired(
318 protobufs::TransformationAddSynonym::SynonymType synonym_type) {
319 switch (synonym_type) {
320 case protobufs::TransformationAddSynonym::ADD_ZERO:
321 case protobufs::TransformationAddSynonym::SUB_ZERO:
322 case protobufs::TransformationAddSynonym::LOGICAL_OR:
323 case protobufs::TransformationAddSynonym::MUL_ONE:
324 case protobufs::TransformationAddSynonym::LOGICAL_AND:
325 case protobufs::TransformationAddSynonym::BITWISE_OR:
326 case protobufs::TransformationAddSynonym::BITWISE_XOR:
327 return true;
328 default:
329 return false;
330 }
331 }
332
GetFreshIds() const333 std::unordered_set<uint32_t> TransformationAddSynonym::GetFreshIds() const {
334 return {message_.synonym_fresh_id()};
335 }
336
337 } // namespace fuzz
338 } // namespace spvtools
339