• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2019 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_donate_modules.h"
16 
17 #include <map>
18 #include <queue>
19 #include <set>
20 
21 #include "source/fuzz/call_graph.h"
22 #include "source/fuzz/instruction_message.h"
23 #include "source/fuzz/transformation_add_constant_boolean.h"
24 #include "source/fuzz/transformation_add_constant_composite.h"
25 #include "source/fuzz/transformation_add_constant_null.h"
26 #include "source/fuzz/transformation_add_constant_scalar.h"
27 #include "source/fuzz/transformation_add_function.h"
28 #include "source/fuzz/transformation_add_global_undef.h"
29 #include "source/fuzz/transformation_add_global_variable.h"
30 #include "source/fuzz/transformation_add_spec_constant_op.h"
31 #include "source/fuzz/transformation_add_type_array.h"
32 #include "source/fuzz/transformation_add_type_boolean.h"
33 #include "source/fuzz/transformation_add_type_float.h"
34 #include "source/fuzz/transformation_add_type_function.h"
35 #include "source/fuzz/transformation_add_type_int.h"
36 #include "source/fuzz/transformation_add_type_matrix.h"
37 #include "source/fuzz/transformation_add_type_pointer.h"
38 #include "source/fuzz/transformation_add_type_struct.h"
39 #include "source/fuzz/transformation_add_type_vector.h"
40 
41 namespace spvtools {
42 namespace fuzz {
43 
FuzzerPassDonateModules(opt::IRContext * ir_context,TransformationContext * transformation_context,FuzzerContext * fuzzer_context,protobufs::TransformationSequence * transformations,bool ignore_inapplicable_transformations,std::vector<fuzzerutil::ModuleSupplier> donor_suppliers)44 FuzzerPassDonateModules::FuzzerPassDonateModules(
45     opt::IRContext* ir_context, TransformationContext* transformation_context,
46     FuzzerContext* fuzzer_context,
47     protobufs::TransformationSequence* transformations,
48     bool ignore_inapplicable_transformations,
49     std::vector<fuzzerutil::ModuleSupplier> donor_suppliers)
50     : FuzzerPass(ir_context, transformation_context, fuzzer_context,
51                  transformations, ignore_inapplicable_transformations),
52       donor_suppliers_(std::move(donor_suppliers)) {}
53 
Apply()54 void FuzzerPassDonateModules::Apply() {
55   // If there are no donor suppliers, this fuzzer pass is a no-op.
56   if (donor_suppliers_.empty()) {
57     return;
58   }
59 
60   // Donate at least one module, and probabilistically decide when to stop
61   // donating modules.
62   do {
63     // Choose a donor supplier at random, and get the module that it provides.
64     std::unique_ptr<opt::IRContext> donor_ir_context = donor_suppliers_.at(
65         GetFuzzerContext()->RandomIndex(donor_suppliers_))();
66     assert(donor_ir_context != nullptr && "Supplying of donor failed");
67     assert(
68         fuzzerutil::IsValid(donor_ir_context.get(),
69                             GetTransformationContext()->GetValidatorOptions(),
70                             fuzzerutil::kSilentMessageConsumer) &&
71         "The donor module must be valid");
72     // Donate the supplied module.
73     //
74     // Randomly decide whether to make the module livesafe (see
75     // FactFunctionIsLivesafe); doing so allows it to be used for live code
76     // injection but restricts its behaviour to allow this, and means that its
77     // functions cannot be transformed as if they were arbitrary dead code.
78     bool make_livesafe = GetFuzzerContext()->ChoosePercentage(
79         GetFuzzerContext()->ChanceOfMakingDonorLivesafe());
80     DonateSingleModule(donor_ir_context.get(), make_livesafe);
81   } while (GetFuzzerContext()->ChoosePercentage(
82       GetFuzzerContext()->GetChanceOfDonatingAdditionalModule()));
83 }
84 
DonateSingleModule(opt::IRContext * donor_ir_context,bool make_livesafe)85 void FuzzerPassDonateModules::DonateSingleModule(
86     opt::IRContext* donor_ir_context, bool make_livesafe) {
87   // Check that the donated module has capabilities, supported by the recipient
88   // module.
89   for (const auto& capability_inst : donor_ir_context->capabilities()) {
90     auto capability =
91         static_cast<SpvCapability>(capability_inst.GetSingleWordInOperand(0));
92     if (!GetIRContext()->get_feature_mgr()->HasCapability(capability)) {
93       return;
94     }
95   }
96 
97   // The ids used by the donor module may very well clash with ids defined in
98   // the recipient module.  Furthermore, some instructions defined in the donor
99   // module will be equivalent to instructions defined in the recipient module,
100   // and it is not always legal to re-declare equivalent instructions.  For
101   // example, OpTypeVoid cannot be declared twice.
102   //
103   // To handle this, we maintain a mapping from an id used in the donor module
104   // to the corresponding id that will be used by the donated code when it
105   // appears in the recipient module.
106   //
107   // This mapping is populated in two ways:
108   // (1) by mapping a donor instruction's result id to the id of some equivalent
109   //     existing instruction in the recipient (e.g. this has to be done for
110   //     OpTypeVoid)
111   // (2) by mapping a donor instruction's result id to a freshly chosen id that
112   //     is guaranteed to be different from any id already used by the recipient
113   //     (or from any id already chosen to handle a previous donor id)
114   std::map<uint32_t, uint32_t> original_id_to_donated_id;
115 
116   HandleExternalInstructionImports(donor_ir_context,
117                                    &original_id_to_donated_id);
118   HandleTypesAndValues(donor_ir_context, &original_id_to_donated_id);
119   HandleFunctions(donor_ir_context, &original_id_to_donated_id, make_livesafe);
120 
121   // TODO(https://github.com/KhronosGroup/SPIRV-Tools/issues/3115) Handle some
122   //  kinds of decoration.
123 }
124 
AdaptStorageClass(SpvStorageClass donor_storage_class)125 SpvStorageClass FuzzerPassDonateModules::AdaptStorageClass(
126     SpvStorageClass donor_storage_class) {
127   switch (donor_storage_class) {
128     case SpvStorageClassFunction:
129     case SpvStorageClassPrivate:
130     case SpvStorageClassWorkgroup:
131       // We leave these alone
132       return donor_storage_class;
133     case SpvStorageClassInput:
134     case SpvStorageClassOutput:
135     case SpvStorageClassUniform:
136     case SpvStorageClassUniformConstant:
137     case SpvStorageClassPushConstant:
138     case SpvStorageClassImage:
139     case SpvStorageClassStorageBuffer:
140       // We change these to Private
141       return SpvStorageClassPrivate;
142     default:
143       // Handle other cases on demand.
144       assert(false && "Currently unsupported storage class.");
145       return SpvStorageClassMax;
146   }
147 }
148 
HandleExternalInstructionImports(opt::IRContext * donor_ir_context,std::map<uint32_t,uint32_t> * original_id_to_donated_id)149 void FuzzerPassDonateModules::HandleExternalInstructionImports(
150     opt::IRContext* donor_ir_context,
151     std::map<uint32_t, uint32_t>* original_id_to_donated_id) {
152   // Consider every external instruction set import in the donor module.
153   for (auto& donor_import : donor_ir_context->module()->ext_inst_imports()) {
154     const auto& donor_import_name_words = donor_import.GetInOperand(0).words;
155     // Look for an identical import in the recipient module.
156     for (auto& existing_import : GetIRContext()->module()->ext_inst_imports()) {
157       const auto& existing_import_name_words =
158           existing_import.GetInOperand(0).words;
159       if (donor_import_name_words == existing_import_name_words) {
160         // A matching import has found.  Map the result id for the donor import
161         // to the id of the existing import, so that when donor instructions
162         // rely on the import they will be rewritten to use the existing import.
163         original_id_to_donated_id->insert(
164             {donor_import.result_id(), existing_import.result_id()});
165         break;
166       }
167     }
168     // TODO(https://github.com/KhronosGroup/SPIRV-Tools/issues/3116): At present
169     //  we do not handle donation of instruction imports, i.e. we do not allow
170     //  the donor to import instruction sets that the recipient did not already
171     //  import.  It might be a good idea to allow this, but it requires some
172     //  thought.
173     assert(original_id_to_donated_id->count(donor_import.result_id()) &&
174            "Donation of imports is not yet supported.");
175   }
176 }
177 
HandleTypesAndValues(opt::IRContext * donor_ir_context,std::map<uint32_t,uint32_t> * original_id_to_donated_id)178 void FuzzerPassDonateModules::HandleTypesAndValues(
179     opt::IRContext* donor_ir_context,
180     std::map<uint32_t, uint32_t>* original_id_to_donated_id) {
181   // Consider every type/global/constant/undef in the module.
182   for (auto& type_or_value : donor_ir_context->module()->types_values()) {
183     HandleTypeOrValue(type_or_value, original_id_to_donated_id);
184   }
185 }
186 
HandleTypeOrValue(const opt::Instruction & type_or_value,std::map<uint32_t,uint32_t> * original_id_to_donated_id)187 void FuzzerPassDonateModules::HandleTypeOrValue(
188     const opt::Instruction& type_or_value,
189     std::map<uint32_t, uint32_t>* original_id_to_donated_id) {
190   // The type/value instruction generates a result id, and we need to associate
191   // the donor's result id with a new result id.  That new result id will either
192   // be the id of some existing instruction, or a fresh id.  This variable
193   // captures it.
194   uint32_t new_result_id;
195 
196   // Decide how to handle each kind of instruction on a case-by-case basis.
197   //
198   // Because the donor module is required to be valid, when we encounter a
199   // type comprised of component types (e.g. an aggregate or pointer), we know
200   // that its component types will have been considered previously, and that
201   // |original_id_to_donated_id| will already contain an entry for them.
202   switch (type_or_value.opcode()) {
203     case SpvOpTypeImage:
204     case SpvOpTypeSampledImage:
205     case SpvOpTypeSampler:
206       // We do not donate types and variables that relate to images and
207       // samplers, so we skip these types and subsequently skip anything that
208       // depends on them.
209       return;
210     case SpvOpTypeVoid: {
211       // Void has to exist already in order for us to have an entry point.
212       // Get the existing id of void.
213       opt::analysis::Void void_type;
214       new_result_id = GetIRContext()->get_type_mgr()->GetId(&void_type);
215       assert(new_result_id &&
216              "The module being transformed will always have 'void' type "
217              "declared.");
218     } break;
219     case SpvOpTypeBool: {
220       // Bool cannot be declared multiple times, so use its existing id if
221       // present, or add a declaration of Bool with a fresh id if not.
222       opt::analysis::Bool bool_type;
223       auto bool_type_id = GetIRContext()->get_type_mgr()->GetId(&bool_type);
224       if (bool_type_id) {
225         new_result_id = bool_type_id;
226       } else {
227         new_result_id = GetFuzzerContext()->GetFreshId();
228         ApplyTransformation(TransformationAddTypeBoolean(new_result_id));
229       }
230     } break;
231     case SpvOpTypeInt: {
232       // Int cannot be declared multiple times with the same width and
233       // signedness, so check whether an existing identical Int type is
234       // present and use its id if so.  Otherwise add a declaration of the
235       // Int type used by the donor, with a fresh id.
236       const uint32_t width = type_or_value.GetSingleWordInOperand(0);
237       const bool is_signed =
238           static_cast<bool>(type_or_value.GetSingleWordInOperand(1));
239       opt::analysis::Integer int_type(width, is_signed);
240       auto int_type_id = GetIRContext()->get_type_mgr()->GetId(&int_type);
241       if (int_type_id) {
242         new_result_id = int_type_id;
243       } else {
244         new_result_id = GetFuzzerContext()->GetFreshId();
245         ApplyTransformation(
246             TransformationAddTypeInt(new_result_id, width, is_signed));
247       }
248     } break;
249     case SpvOpTypeFloat: {
250       // Similar to SpvOpTypeInt.
251       const uint32_t width = type_or_value.GetSingleWordInOperand(0);
252       opt::analysis::Float float_type(width);
253       auto float_type_id = GetIRContext()->get_type_mgr()->GetId(&float_type);
254       if (float_type_id) {
255         new_result_id = float_type_id;
256       } else {
257         new_result_id = GetFuzzerContext()->GetFreshId();
258         ApplyTransformation(TransformationAddTypeFloat(new_result_id, width));
259       }
260     } break;
261     case SpvOpTypeVector: {
262       // It is not legal to have two Vector type declarations with identical
263       // element types and element counts, so check whether an existing
264       // identical Vector type is present and use its id if so.  Otherwise add
265       // a declaration of the Vector type used by the donor, with a fresh id.
266 
267       // When considering the vector's component type id, we look up the id
268       // use in the donor to find the id to which this has been remapped.
269       uint32_t component_type_id = original_id_to_donated_id->at(
270           type_or_value.GetSingleWordInOperand(0));
271       auto component_type =
272           GetIRContext()->get_type_mgr()->GetType(component_type_id);
273       assert(component_type && "The base type should be registered.");
274       auto component_count = type_or_value.GetSingleWordInOperand(1);
275       opt::analysis::Vector vector_type(component_type, component_count);
276       auto vector_type_id = GetIRContext()->get_type_mgr()->GetId(&vector_type);
277       if (vector_type_id) {
278         new_result_id = vector_type_id;
279       } else {
280         new_result_id = GetFuzzerContext()->GetFreshId();
281         ApplyTransformation(TransformationAddTypeVector(
282             new_result_id, component_type_id, component_count));
283       }
284     } break;
285     case SpvOpTypeMatrix: {
286       // Similar to SpvOpTypeVector.
287       uint32_t column_type_id = original_id_to_donated_id->at(
288           type_or_value.GetSingleWordInOperand(0));
289       auto column_type =
290           GetIRContext()->get_type_mgr()->GetType(column_type_id);
291       assert(column_type && column_type->AsVector() &&
292              "The column type should be a registered vector type.");
293       auto column_count = type_or_value.GetSingleWordInOperand(1);
294       opt::analysis::Matrix matrix_type(column_type, column_count);
295       auto matrix_type_id = GetIRContext()->get_type_mgr()->GetId(&matrix_type);
296       if (matrix_type_id) {
297         new_result_id = matrix_type_id;
298       } else {
299         new_result_id = GetFuzzerContext()->GetFreshId();
300         ApplyTransformation(TransformationAddTypeMatrix(
301             new_result_id, column_type_id, column_count));
302       }
303 
304     } break;
305     case SpvOpTypeArray: {
306       // It is OK to have multiple structurally identical array types, so
307       // we go ahead and add a remapped version of the type declared by the
308       // donor.
309       uint32_t component_type_id = type_or_value.GetSingleWordInOperand(0);
310       if (!original_id_to_donated_id->count(component_type_id)) {
311         // We did not donate the component type of this array type, so we
312         // cannot donate the array type.
313         return;
314       }
315       new_result_id = GetFuzzerContext()->GetFreshId();
316       ApplyTransformation(TransformationAddTypeArray(
317           new_result_id, original_id_to_donated_id->at(component_type_id),
318           original_id_to_donated_id->at(
319               type_or_value.GetSingleWordInOperand(1))));
320     } break;
321     case SpvOpTypeRuntimeArray: {
322       // A runtime array is allowed as the final member of an SSBO.  During
323       // donation we turn runtime arrays into fixed-size arrays.  For dead
324       // code donations this is OK because the array is never indexed into at
325       // runtime, so it does not matter what its size is.  For live-safe code,
326       // all accesses are made in-bounds, so this is also OK.
327       //
328       // The special OpArrayLength instruction, which works on runtime arrays,
329       // is rewritten to yield the fixed length that is used for the array.
330 
331       uint32_t component_type_id = type_or_value.GetSingleWordInOperand(0);
332       if (!original_id_to_donated_id->count(component_type_id)) {
333         // We did not donate the component type of this runtime array type, so
334         // we cannot donate it as a fixed-size array.
335         return;
336       }
337       new_result_id = GetFuzzerContext()->GetFreshId();
338       ApplyTransformation(TransformationAddTypeArray(
339           new_result_id, original_id_to_donated_id->at(component_type_id),
340           FindOrCreateIntegerConstant(
341               {GetFuzzerContext()->GetRandomSizeForNewArray()}, 32, false,
342               false)));
343     } break;
344     case SpvOpTypeStruct: {
345       // Similar to SpvOpTypeArray.
346       std::vector<uint32_t> member_type_ids;
347       for (uint32_t i = 0; i < type_or_value.NumInOperands(); i++) {
348         auto component_type_id = type_or_value.GetSingleWordInOperand(i);
349         if (!original_id_to_donated_id->count(component_type_id)) {
350           // We did not donate every member type for this struct type, so we
351           // cannot donate the struct type.
352           return;
353         }
354         member_type_ids.push_back(
355             original_id_to_donated_id->at(component_type_id));
356       }
357       new_result_id = GetFuzzerContext()->GetFreshId();
358       ApplyTransformation(
359           TransformationAddTypeStruct(new_result_id, member_type_ids));
360     } break;
361     case SpvOpTypePointer: {
362       // Similar to SpvOpTypeArray.
363       uint32_t pointee_type_id = type_or_value.GetSingleWordInOperand(1);
364       if (!original_id_to_donated_id->count(pointee_type_id)) {
365         // We did not donate the pointee type for this pointer type, so we
366         // cannot donate the pointer type.
367         return;
368       }
369       new_result_id = GetFuzzerContext()->GetFreshId();
370       ApplyTransformation(TransformationAddTypePointer(
371           new_result_id,
372           AdaptStorageClass(static_cast<SpvStorageClass>(
373               type_or_value.GetSingleWordInOperand(0))),
374           original_id_to_donated_id->at(pointee_type_id)));
375     } break;
376     case SpvOpTypeFunction: {
377       // It is not OK to have multiple function types that use identical ids
378       // for their return and parameter types.  We thus go through all
379       // existing function types to look for a match.  We do not use the
380       // type manager here because we want to regard two function types that
381       // are structurally identical but that differ with respect to the
382       // actual ids used for pointer types as different.
383       //
384       // Example:
385       //
386       // %1 = OpTypeVoid
387       // %2 = OpTypeInt 32 0
388       // %3 = OpTypePointer Function %2
389       // %4 = OpTypePointer Function %2
390       // %5 = OpTypeFunction %1 %3
391       // %6 = OpTypeFunction %1 %4
392       //
393       // We regard %5 and %6 as distinct function types here, even though
394       // they both have the form "uint32* -> void"
395 
396       std::vector<uint32_t> return_and_parameter_types;
397       for (uint32_t i = 0; i < type_or_value.NumInOperands(); i++) {
398         uint32_t return_or_parameter_type =
399             type_or_value.GetSingleWordInOperand(i);
400         if (!original_id_to_donated_id->count(return_or_parameter_type)) {
401           // We did not donate every return/parameter type for this function
402           // type, so we cannot donate the function type.
403           return;
404         }
405         return_and_parameter_types.push_back(
406             original_id_to_donated_id->at(return_or_parameter_type));
407       }
408       uint32_t existing_function_id = fuzzerutil::FindFunctionType(
409           GetIRContext(), return_and_parameter_types);
410       if (existing_function_id) {
411         new_result_id = existing_function_id;
412       } else {
413         // No match was found, so add a remapped version of the function type
414         // to the module, with a fresh id.
415         new_result_id = GetFuzzerContext()->GetFreshId();
416         std::vector<uint32_t> argument_type_ids;
417         for (uint32_t i = 1; i < type_or_value.NumInOperands(); i++) {
418           argument_type_ids.push_back(original_id_to_donated_id->at(
419               type_or_value.GetSingleWordInOperand(i)));
420         }
421         ApplyTransformation(TransformationAddTypeFunction(
422             new_result_id,
423             original_id_to_donated_id->at(
424                 type_or_value.GetSingleWordInOperand(0)),
425             argument_type_ids));
426       }
427     } break;
428     case SpvOpSpecConstantOp: {
429       new_result_id = GetFuzzerContext()->GetFreshId();
430       auto type_id = original_id_to_donated_id->at(type_or_value.type_id());
431       auto opcode = static_cast<SpvOp>(type_or_value.GetSingleWordInOperand(0));
432 
433       // Make sure we take into account |original_id_to_donated_id| when
434       // computing operands for OpSpecConstantOp.
435       opt::Instruction::OperandList operands;
436       for (uint32_t i = 1; i < type_or_value.NumInOperands(); ++i) {
437         const auto& operand = type_or_value.GetInOperand(i);
438         auto data =
439             operand.type == SPV_OPERAND_TYPE_ID
440                 ? opt::Operand::OperandData{original_id_to_donated_id->at(
441                       operand.words[0])}
442                 : operand.words;
443 
444         operands.push_back({operand.type, std::move(data)});
445       }
446 
447       ApplyTransformation(TransformationAddSpecConstantOp(
448           new_result_id, type_id, opcode, std::move(operands)));
449     } break;
450     case SpvOpSpecConstantTrue:
451     case SpvOpSpecConstantFalse:
452     case SpvOpConstantTrue:
453     case SpvOpConstantFalse: {
454       // It is OK to have duplicate definitions of True and False, so add
455       // these to the module, using a remapped Bool type.
456       new_result_id = GetFuzzerContext()->GetFreshId();
457       auto value = type_or_value.opcode() == SpvOpConstantTrue ||
458                    type_or_value.opcode() == SpvOpSpecConstantTrue;
459       ApplyTransformation(
460           TransformationAddConstantBoolean(new_result_id, value, false));
461     } break;
462     case SpvOpSpecConstant:
463     case SpvOpConstant: {
464       // It is OK to have duplicate constant definitions, so add this to the
465       // module using a remapped result type.
466       new_result_id = GetFuzzerContext()->GetFreshId();
467       std::vector<uint32_t> data_words;
468       type_or_value.ForEachInOperand([&data_words](const uint32_t* in_operand) {
469         data_words.push_back(*in_operand);
470       });
471       ApplyTransformation(TransformationAddConstantScalar(
472           new_result_id, original_id_to_donated_id->at(type_or_value.type_id()),
473           data_words, false));
474     } break;
475     case SpvOpSpecConstantComposite:
476     case SpvOpConstantComposite: {
477       assert(original_id_to_donated_id->count(type_or_value.type_id()) &&
478              "Composite types for which it is possible to create a constant "
479              "should have been donated.");
480 
481       // It is OK to have duplicate constant composite definitions, so add
482       // this to the module using remapped versions of all constituent ids and
483       // the result type.
484       new_result_id = GetFuzzerContext()->GetFreshId();
485       std::vector<uint32_t> constituent_ids;
486       type_or_value.ForEachInId([&constituent_ids, &original_id_to_donated_id](
487                                     const uint32_t* constituent_id) {
488         assert(original_id_to_donated_id->count(*constituent_id) &&
489                "The constants used to construct this composite should "
490                "have been donated.");
491         constituent_ids.push_back(
492             original_id_to_donated_id->at(*constituent_id));
493       });
494       ApplyTransformation(TransformationAddConstantComposite(
495           new_result_id, original_id_to_donated_id->at(type_or_value.type_id()),
496           constituent_ids, false));
497     } break;
498     case SpvOpConstantNull: {
499       if (!original_id_to_donated_id->count(type_or_value.type_id())) {
500         // We did not donate the type associated with this null constant, so
501         // we cannot donate the null constant.
502         return;
503       }
504 
505       // It is fine to have multiple OpConstantNull instructions of the same
506       // type, so we just add this to the recipient module.
507       new_result_id = GetFuzzerContext()->GetFreshId();
508       ApplyTransformation(TransformationAddConstantNull(
509           new_result_id,
510           original_id_to_donated_id->at(type_or_value.type_id())));
511     } break;
512     case SpvOpVariable: {
513       if (!original_id_to_donated_id->count(type_or_value.type_id())) {
514         // We did not donate the pointer type associated with this variable,
515         // so we cannot donate the variable.
516         return;
517       }
518 
519       // This is a global variable that could have one of various storage
520       // classes.  However, we change all global variable pointer storage
521       // classes (such as Uniform, Input and Output) to private when donating
522       // pointer types, with the exception of the Workgroup storage class.
523       //
524       // Thus this variable's pointer type is guaranteed to have storage class
525       // Private or Workgroup.
526       //
527       // We add a global variable with either Private or Workgroup storage
528       // class, using remapped versions of the result type and initializer ids
529       // for the global variable in the donor.
530       //
531       // We regard the added variable as having an irrelevant value.  This
532       // means that future passes can add stores to the variable in any
533       // way they wish, and pass them as pointer parameters to functions
534       // without worrying about whether their data might get modified.
535       new_result_id = GetFuzzerContext()->GetFreshId();
536       uint32_t remapped_pointer_type =
537           original_id_to_donated_id->at(type_or_value.type_id());
538       uint32_t initializer_id;
539       SpvStorageClass storage_class =
540           static_cast<SpvStorageClass>(type_or_value.GetSingleWordInOperand(
541               0)) == SpvStorageClassWorkgroup
542               ? SpvStorageClassWorkgroup
543               : SpvStorageClassPrivate;
544       if (type_or_value.NumInOperands() == 1) {
545         // The variable did not have an initializer.  Initialize it to zero
546         // if it has Private storage class (to limit problems associated with
547         // uninitialized data), and leave it uninitialized if it has Workgroup
548         // storage class (as Workgroup variables cannot have initializers).
549 
550         // TODO(https://github.com/KhronosGroup/SPIRV-Tools/issues/3275): we
551         //  could initialize Workgroup variables at the start of an entry
552         //  point, and should do so if their uninitialized nature proves
553         //  problematic.
554         initializer_id = storage_class == SpvStorageClassWorkgroup
555                              ? 0
556                              : FindOrCreateZeroConstant(
557                                    fuzzerutil::GetPointeeTypeIdFromPointerType(
558                                        GetIRContext(), remapped_pointer_type),
559                                    false);
560       } else {
561         // The variable already had an initializer; use its remapped id.
562         initializer_id = original_id_to_donated_id->at(
563             type_or_value.GetSingleWordInOperand(1));
564       }
565       ApplyTransformation(
566           TransformationAddGlobalVariable(new_result_id, remapped_pointer_type,
567                                           storage_class, initializer_id, true));
568     } break;
569     case SpvOpUndef: {
570       if (!original_id_to_donated_id->count(type_or_value.type_id())) {
571         // We did not donate the type associated with this undef, so we cannot
572         // donate the undef.
573         return;
574       }
575 
576       // It is fine to have multiple Undef instructions of the same type, so
577       // we just add this to the recipient module.
578       new_result_id = GetFuzzerContext()->GetFreshId();
579       ApplyTransformation(TransformationAddGlobalUndef(
580           new_result_id,
581           original_id_to_donated_id->at(type_or_value.type_id())));
582     } break;
583     default: {
584       assert(0 && "Unknown type/value.");
585       new_result_id = 0;
586     } break;
587   }
588 
589   // Update the id mapping to associate the instruction's result id with its
590   // corresponding id in the recipient.
591   original_id_to_donated_id->insert({type_or_value.result_id(), new_result_id});
592 }
593 
HandleFunctions(opt::IRContext * donor_ir_context,std::map<uint32_t,uint32_t> * original_id_to_donated_id,bool make_livesafe)594 void FuzzerPassDonateModules::HandleFunctions(
595     opt::IRContext* donor_ir_context,
596     std::map<uint32_t, uint32_t>* original_id_to_donated_id,
597     bool make_livesafe) {
598   // Get the ids of functions in the donor module, topologically sorted
599   // according to the donor's call graph.
600   auto topological_order =
601       CallGraph(donor_ir_context).GetFunctionsInTopologicalOrder();
602 
603   // Donate the functions in reverse topological order.  This ensures that a
604   // function gets donated before any function that depends on it.  This allows
605   // donation of the functions to be separated into a number of transformations,
606   // each adding one function, such that every prefix of transformations leaves
607   // the module valid.
608   for (auto function_id = topological_order.rbegin();
609        function_id != topological_order.rend(); ++function_id) {
610     // Find the function to be donated.
611     opt::Function* function_to_donate = nullptr;
612     for (auto& function : *donor_ir_context->module()) {
613       if (function.result_id() == *function_id) {
614         function_to_donate = &function;
615         break;
616       }
617     }
618     assert(function_to_donate && "Function to be donated was not found.");
619 
620     if (!original_id_to_donated_id->count(
621             function_to_donate->DefInst().GetSingleWordInOperand(1))) {
622       // We were not able to donate this function's type, so we cannot donate
623       // the function.
624       continue;
625     }
626 
627     // We will collect up protobuf messages representing the donor function's
628     // instructions here, and use them to create an AddFunction transformation.
629     std::vector<protobufs::Instruction> donated_instructions;
630 
631     // This set tracks the ids of those instructions for which donation was
632     // completely skipped: neither the instruction nor a substitute for it was
633     // donated.
634     std::set<uint32_t> skipped_instructions;
635 
636     // Consider every instruction of the donor function.
637     function_to_donate->ForEachInst(
638         [this, &donated_instructions, donor_ir_context,
639          &original_id_to_donated_id,
640          &skipped_instructions](const opt::Instruction* instruction) {
641           if (instruction->opcode() == SpvOpArrayLength) {
642             // We treat OpArrayLength specially.
643             HandleOpArrayLength(*instruction, original_id_to_donated_id,
644                                 &donated_instructions);
645           } else if (!CanDonateInstruction(donor_ir_context, *instruction,
646                                            *original_id_to_donated_id,
647                                            skipped_instructions)) {
648             // This is an instruction that we cannot directly donate.
649             HandleDifficultInstruction(*instruction, original_id_to_donated_id,
650                                        &donated_instructions,
651                                        &skipped_instructions);
652           } else {
653             PrepareInstructionForDonation(*instruction, donor_ir_context,
654                                           original_id_to_donated_id,
655                                           &donated_instructions);
656           }
657         });
658 
659     // If |make_livesafe| is true, try to add the function in a livesafe manner.
660     // Otherwise (if |make_lifesafe| is false or an attempt to make the function
661     // livesafe has failed), add the function in a non-livesafe manner.
662     if (!make_livesafe ||
663         !MaybeAddLivesafeFunction(*function_to_donate, donor_ir_context,
664                                   *original_id_to_donated_id,
665                                   donated_instructions)) {
666       ApplyTransformation(TransformationAddFunction(donated_instructions));
667     }
668   }
669 }
670 
CanDonateInstruction(opt::IRContext * donor_ir_context,const opt::Instruction & instruction,const std::map<uint32_t,uint32_t> & original_id_to_donated_id,const std::set<uint32_t> & skipped_instructions) const671 bool FuzzerPassDonateModules::CanDonateInstruction(
672     opt::IRContext* donor_ir_context, const opt::Instruction& instruction,
673     const std::map<uint32_t, uint32_t>& original_id_to_donated_id,
674     const std::set<uint32_t>& skipped_instructions) const {
675   if (instruction.type_id() &&
676       !original_id_to_donated_id.count(instruction.type_id())) {
677     // We could not donate the result type of this instruction, so we cannot
678     // donate the instruction.
679     return false;
680   }
681 
682   // Now consider instructions we specifically want to skip because we do not
683   // yet support them.
684   switch (instruction.opcode()) {
685     case SpvOpAtomicLoad:
686     case SpvOpAtomicStore:
687     case SpvOpAtomicExchange:
688     case SpvOpAtomicCompareExchange:
689     case SpvOpAtomicCompareExchangeWeak:
690     case SpvOpAtomicIIncrement:
691     case SpvOpAtomicIDecrement:
692     case SpvOpAtomicIAdd:
693     case SpvOpAtomicISub:
694     case SpvOpAtomicSMin:
695     case SpvOpAtomicUMin:
696     case SpvOpAtomicSMax:
697     case SpvOpAtomicUMax:
698     case SpvOpAtomicAnd:
699     case SpvOpAtomicOr:
700     case SpvOpAtomicXor:
701       // We conservatively ignore all atomic instructions at present.
702       // TODO(https://github.com/KhronosGroup/SPIRV-Tools/issues/3276): Consider
703       //  being less conservative here.
704     case SpvOpImageSampleImplicitLod:
705     case SpvOpImageSampleExplicitLod:
706     case SpvOpImageSampleDrefImplicitLod:
707     case SpvOpImageSampleDrefExplicitLod:
708     case SpvOpImageSampleProjImplicitLod:
709     case SpvOpImageSampleProjExplicitLod:
710     case SpvOpImageSampleProjDrefImplicitLod:
711     case SpvOpImageSampleProjDrefExplicitLod:
712     case SpvOpImageFetch:
713     case SpvOpImageGather:
714     case SpvOpImageDrefGather:
715     case SpvOpImageRead:
716     case SpvOpImageWrite:
717     case SpvOpImageSparseSampleImplicitLod:
718     case SpvOpImageSparseSampleExplicitLod:
719     case SpvOpImageSparseSampleDrefImplicitLod:
720     case SpvOpImageSparseSampleDrefExplicitLod:
721     case SpvOpImageSparseSampleProjImplicitLod:
722     case SpvOpImageSparseSampleProjExplicitLod:
723     case SpvOpImageSparseSampleProjDrefImplicitLod:
724     case SpvOpImageSparseSampleProjDrefExplicitLod:
725     case SpvOpImageSparseFetch:
726     case SpvOpImageSparseGather:
727     case SpvOpImageSparseDrefGather:
728     case SpvOpImageSparseRead:
729     case SpvOpImageSampleFootprintNV:
730     case SpvOpImage:
731     case SpvOpImageQueryFormat:
732     case SpvOpImageQueryLevels:
733     case SpvOpImageQueryLod:
734     case SpvOpImageQueryOrder:
735     case SpvOpImageQuerySamples:
736     case SpvOpImageQuerySize:
737     case SpvOpImageQuerySizeLod:
738     case SpvOpSampledImage:
739       // We ignore all instructions related to accessing images, since we do not
740       // donate images.
741       return false;
742     case SpvOpLoad:
743       switch (donor_ir_context->get_def_use_mgr()
744                   ->GetDef(instruction.type_id())
745                   ->opcode()) {
746         case SpvOpTypeImage:
747         case SpvOpTypeSampledImage:
748         case SpvOpTypeSampler:
749           // Again, we ignore instructions that relate to accessing images.
750           return false;
751         default:
752           break;
753       }
754     default:
755       break;
756   }
757 
758   // Examine each id input operand to the instruction.  If it turns out that we
759   // have skipped any of these operands then we cannot donate the instruction.
760   bool result = true;
761   instruction.WhileEachInId(
762       [donor_ir_context, &original_id_to_donated_id, &result,
763        &skipped_instructions](const uint32_t* in_id) -> bool {
764         if (!original_id_to_donated_id.count(*in_id)) {
765           // We do not have a mapped result id for this id operand.  That either
766           // means that it is a forward reference (which is OK), that we skipped
767           // the instruction that generated it (which is not OK), or that it is
768           // the id of a function or global value that we did not donate (which
769           // is not OK).  We check for the latter two cases.
770           if (skipped_instructions.count(*in_id) ||
771               // A function or global value does not have an associated basic
772               // block.
773               !donor_ir_context->get_instr_block(*in_id)) {
774             result = false;
775             return false;
776           }
777         }
778         return true;
779       });
780   return result;
781 }
782 
IsBasicType(const opt::Instruction & instruction) const783 bool FuzzerPassDonateModules::IsBasicType(
784     const opt::Instruction& instruction) const {
785   switch (instruction.opcode()) {
786     case SpvOpTypeArray:
787     case SpvOpTypeBool:
788     case SpvOpTypeFloat:
789     case SpvOpTypeInt:
790     case SpvOpTypeMatrix:
791     case SpvOpTypeStruct:
792     case SpvOpTypeVector:
793       return true;
794     default:
795       return false;
796   }
797 }
798 
HandleOpArrayLength(const opt::Instruction & instruction,std::map<uint32_t,uint32_t> * original_id_to_donated_id,std::vector<protobufs::Instruction> * donated_instructions) const799 void FuzzerPassDonateModules::HandleOpArrayLength(
800     const opt::Instruction& instruction,
801     std::map<uint32_t, uint32_t>* original_id_to_donated_id,
802     std::vector<protobufs::Instruction>* donated_instructions) const {
803   assert(instruction.opcode() == SpvOpArrayLength &&
804          "Precondition: instruction must be OpArrayLength.");
805   uint32_t donated_variable_id =
806       original_id_to_donated_id->at(instruction.GetSingleWordInOperand(0));
807   auto donated_variable_instruction =
808       GetIRContext()->get_def_use_mgr()->GetDef(donated_variable_id);
809   auto pointer_to_struct_instruction =
810       GetIRContext()->get_def_use_mgr()->GetDef(
811           donated_variable_instruction->type_id());
812   assert(pointer_to_struct_instruction->opcode() == SpvOpTypePointer &&
813          "Type of variable must be pointer.");
814   auto donated_struct_type_instruction =
815       GetIRContext()->get_def_use_mgr()->GetDef(
816           pointer_to_struct_instruction->GetSingleWordInOperand(1));
817   assert(donated_struct_type_instruction->opcode() == SpvOpTypeStruct &&
818          "Pointee type of pointer used by OpArrayLength must be struct.");
819   assert(donated_struct_type_instruction->NumInOperands() ==
820              instruction.GetSingleWordInOperand(1) + 1 &&
821          "OpArrayLength must refer to the final member of the given "
822          "struct.");
823   uint32_t fixed_size_array_type_id =
824       donated_struct_type_instruction->GetSingleWordInOperand(
825           donated_struct_type_instruction->NumInOperands() - 1);
826   auto fixed_size_array_type_instruction =
827       GetIRContext()->get_def_use_mgr()->GetDef(fixed_size_array_type_id);
828   assert(fixed_size_array_type_instruction->opcode() == SpvOpTypeArray &&
829          "The donated array type must be fixed-size.");
830   auto array_size_id =
831       fixed_size_array_type_instruction->GetSingleWordInOperand(1);
832 
833   if (instruction.result_id() &&
834       !original_id_to_donated_id->count(instruction.result_id())) {
835     original_id_to_donated_id->insert(
836         {instruction.result_id(), GetFuzzerContext()->GetFreshId()});
837   }
838 
839   donated_instructions->push_back(MakeInstructionMessage(
840       SpvOpCopyObject, original_id_to_donated_id->at(instruction.type_id()),
841       original_id_to_donated_id->at(instruction.result_id()),
842       opt::Instruction::OperandList({{SPV_OPERAND_TYPE_ID, {array_size_id}}})));
843 }
844 
HandleDifficultInstruction(const opt::Instruction & instruction,std::map<uint32_t,uint32_t> * original_id_to_donated_id,std::vector<protobufs::Instruction> * donated_instructions,std::set<uint32_t> * skipped_instructions)845 void FuzzerPassDonateModules::HandleDifficultInstruction(
846     const opt::Instruction& instruction,
847     std::map<uint32_t, uint32_t>* original_id_to_donated_id,
848     std::vector<protobufs::Instruction>* donated_instructions,
849     std::set<uint32_t>* skipped_instructions) {
850   if (!instruction.result_id()) {
851     // It does not generate a result id, so it can be ignored.
852     return;
853   }
854   if (!original_id_to_donated_id->count(instruction.type_id())) {
855     // We cannot handle this instruction's result type, so we need to skip it
856     // all together.
857     skipped_instructions->insert(instruction.result_id());
858     return;
859   }
860 
861   // We now attempt to replace the instruction with an OpCopyObject.
862   // TODO(https://github.com/KhronosGroup/SPIRV-Tools/issues/3278): We could do
863   //  something more refined here - we could check which operands to the
864   //  instruction could not be donated and replace those operands with
865   //  references to other ids (such as constants), so that we still get an
866   //  instruction with the opcode and easy-to-handle operands of the donor
867   //  instruction.
868   auto remapped_type_id = original_id_to_donated_id->at(instruction.type_id());
869   if (!IsBasicType(
870           *GetIRContext()->get_def_use_mgr()->GetDef(remapped_type_id))) {
871     // The instruction has a non-basic result type, so we cannot replace it with
872     // an object copy of a constant.  We thus skip it completely.
873     // TODO(https://github.com/KhronosGroup/SPIRV-Tools/issues/3279): We could
874     //  instead look for an available id of the right type and generate an
875     //  OpCopyObject of that id.
876     skipped_instructions->insert(instruction.result_id());
877     return;
878   }
879 
880   // We are going to add an OpCopyObject instruction.  Add a mapping for the
881   // result id of the original instruction if does not already exist (it may
882   // exist in the case that it has been forward-referenced).
883   if (!original_id_to_donated_id->count(instruction.result_id())) {
884     original_id_to_donated_id->insert(
885         {instruction.result_id(), GetFuzzerContext()->GetFreshId()});
886   }
887 
888   // We find or add a zero constant to the receiving module for the type in
889   // question, and add an OpCopyObject instruction that copies this zero.
890   //
891   // We mark the constant as irrelevant so that we can replace it with a
892   // more interesting value later.
893   auto zero_constant = FindOrCreateZeroConstant(remapped_type_id, true);
894   donated_instructions->push_back(MakeInstructionMessage(
895       SpvOpCopyObject, remapped_type_id,
896       original_id_to_donated_id->at(instruction.result_id()),
897       opt::Instruction::OperandList({{SPV_OPERAND_TYPE_ID, {zero_constant}}})));
898 }
899 
PrepareInstructionForDonation(const opt::Instruction & instruction,opt::IRContext * donor_ir_context,std::map<uint32_t,uint32_t> * original_id_to_donated_id,std::vector<protobufs::Instruction> * donated_instructions)900 void FuzzerPassDonateModules::PrepareInstructionForDonation(
901     const opt::Instruction& instruction, opt::IRContext* donor_ir_context,
902     std::map<uint32_t, uint32_t>* original_id_to_donated_id,
903     std::vector<protobufs::Instruction>* donated_instructions) {
904   // Get the instruction's input operands into donation-ready form,
905   // remapping any id uses in the process.
906   opt::Instruction::OperandList input_operands;
907 
908   // Consider each input operand in turn.
909   for (uint32_t in_operand_index = 0;
910        in_operand_index < instruction.NumInOperands(); in_operand_index++) {
911     std::vector<uint32_t> operand_data;
912     const opt::Operand& in_operand = instruction.GetInOperand(in_operand_index);
913     // Check whether this operand is an id.
914     if (spvIsIdType(in_operand.type)) {
915       // This is an id operand - it consists of a single word of data,
916       // which needs to be remapped so that it is replaced with the
917       // donated form of the id.
918       auto operand_id = in_operand.words[0];
919       if (!original_id_to_donated_id->count(operand_id)) {
920         // This is a forward reference.  We will choose a corresponding
921         // donor id for the referenced id and update the mapping to
922         // reflect it.
923 
924         // Keep release compilers happy because |donor_ir_context| is only used
925         // in this assertion.
926         (void)(donor_ir_context);
927         assert((donor_ir_context->get_def_use_mgr()
928                         ->GetDef(operand_id)
929                         ->opcode() == SpvOpLabel ||
930                 instruction.opcode() == SpvOpPhi) &&
931                "Unsupported forward reference.");
932         original_id_to_donated_id->insert(
933             {operand_id, GetFuzzerContext()->GetFreshId()});
934       }
935       operand_data.push_back(original_id_to_donated_id->at(operand_id));
936     } else {
937       // For non-id operands, we just add each of the data words.
938       for (auto word : in_operand.words) {
939         operand_data.push_back(word);
940       }
941     }
942     input_operands.push_back({in_operand.type, operand_data});
943   }
944 
945   if (instruction.opcode() == SpvOpVariable &&
946       instruction.NumInOperands() == 1) {
947     // This is an uninitialized local variable.  Initialize it to zero.
948     input_operands.push_back(
949         {SPV_OPERAND_TYPE_ID,
950          {FindOrCreateZeroConstant(
951              fuzzerutil::GetPointeeTypeIdFromPointerType(
952                  GetIRContext(),
953                  original_id_to_donated_id->at(instruction.type_id())),
954              false)}});
955   }
956 
957   if (instruction.result_id() &&
958       !original_id_to_donated_id->count(instruction.result_id())) {
959     original_id_to_donated_id->insert(
960         {instruction.result_id(), GetFuzzerContext()->GetFreshId()});
961   }
962 
963   // Remap the result type and result id (if present) of the
964   // instruction, and turn it into a protobuf message.
965   donated_instructions->push_back(MakeInstructionMessage(
966       instruction.opcode(),
967       instruction.type_id()
968           ? original_id_to_donated_id->at(instruction.type_id())
969           : 0,
970       instruction.result_id()
971           ? original_id_to_donated_id->at(instruction.result_id())
972           : 0,
973       input_operands));
974 }
975 
CreateLoopLimiterInfo(opt::IRContext * donor_ir_context,const opt::BasicBlock & loop_header,const std::map<uint32_t,uint32_t> & original_id_to_donated_id,protobufs::LoopLimiterInfo * out)976 bool FuzzerPassDonateModules::CreateLoopLimiterInfo(
977     opt::IRContext* donor_ir_context, const opt::BasicBlock& loop_header,
978     const std::map<uint32_t, uint32_t>& original_id_to_donated_id,
979     protobufs::LoopLimiterInfo* out) {
980   assert(loop_header.IsLoopHeader() && "|loop_header| is not a loop header");
981 
982   // Grab the loop header's id, mapped to its donated value.
983   out->set_loop_header_id(original_id_to_donated_id.at(loop_header.id()));
984 
985   // Get fresh ids that will be used to load the loop limiter, increment
986   // it, compare it with the loop limit, and an id for a new block that
987   // will contain the loop's original terminator.
988   out->set_load_id(GetFuzzerContext()->GetFreshId());
989   out->set_increment_id(GetFuzzerContext()->GetFreshId());
990   out->set_compare_id(GetFuzzerContext()->GetFreshId());
991   out->set_logical_op_id(GetFuzzerContext()->GetFreshId());
992 
993   // We are creating a branch from the back-edge block to the merge block. Thus,
994   // if merge block has any OpPhi instructions, we might need to adjust
995   // them.
996 
997   // Note that the loop might have an unreachable back-edge block. This means
998   // that the loop can't iterate, so we don't need to adjust anything.
999   const auto back_edge_block_id = TransformationAddFunction::GetBackEdgeBlockId(
1000       donor_ir_context, loop_header.id());
1001   if (!back_edge_block_id) {
1002     return true;
1003   }
1004 
1005   auto* back_edge_block = donor_ir_context->cfg()->block(back_edge_block_id);
1006   assert(back_edge_block && "|back_edge_block_id| is invalid");
1007 
1008   const auto* merge_block =
1009       donor_ir_context->cfg()->block(loop_header.MergeBlockId());
1010   assert(merge_block && "Loop header has invalid merge block id");
1011 
1012   // We don't need to adjust anything if there is already a branch from
1013   // the back-edge block to the merge block.
1014   if (back_edge_block->IsSuccessor(merge_block)) {
1015     return true;
1016   }
1017 
1018   // Adjust OpPhi instructions in the |merge_block|.
1019   for (const auto& inst : *merge_block) {
1020     if (inst.opcode() != SpvOpPhi) {
1021       break;
1022     }
1023 
1024     // There is no simple way to ensure that a chosen operand for the OpPhi
1025     // instruction will never cause any problems (e.g. if we choose an
1026     // integer id, it might have a zero value when we branch from the back
1027     // edge block. This might cause a division by 0 later in the function.).
1028     // Thus, we ignore possible problems and proceed as follows:
1029     // - if any of the existing OpPhi operands dominates the back-edge
1030     //   block - use it
1031     // - if OpPhi has a basic type (see IsBasicType method) - create
1032     //   a zero constant
1033     // - otherwise, we can't add a livesafe function.
1034     uint32_t suitable_operand_id = 0;
1035     for (uint32_t i = 0; i < inst.NumInOperands(); i += 2) {
1036       auto dependency_inst_id = inst.GetSingleWordInOperand(i);
1037 
1038       if (fuzzerutil::IdIsAvailableBeforeInstruction(
1039               donor_ir_context, back_edge_block->terminator(),
1040               dependency_inst_id)) {
1041         suitable_operand_id = original_id_to_donated_id.at(dependency_inst_id);
1042         break;
1043       }
1044     }
1045 
1046     if (suitable_operand_id == 0 &&
1047         IsBasicType(
1048             *donor_ir_context->get_def_use_mgr()->GetDef(inst.type_id()))) {
1049       // We mark this constant as irrelevant so that we can replace it
1050       // with more interesting value later.
1051       suitable_operand_id = FindOrCreateZeroConstant(
1052           original_id_to_donated_id.at(inst.type_id()), true);
1053     }
1054 
1055     if (suitable_operand_id == 0) {
1056       return false;
1057     }
1058 
1059     out->add_phi_id(suitable_operand_id);
1060   }
1061 
1062   return true;
1063 }
1064 
MaybeAddLivesafeFunction(const opt::Function & function_to_donate,opt::IRContext * donor_ir_context,const std::map<uint32_t,uint32_t> & original_id_to_donated_id,const std::vector<protobufs::Instruction> & donated_instructions)1065 bool FuzzerPassDonateModules::MaybeAddLivesafeFunction(
1066     const opt::Function& function_to_donate, opt::IRContext* donor_ir_context,
1067     const std::map<uint32_t, uint32_t>& original_id_to_donated_id,
1068     const std::vector<protobufs::Instruction>& donated_instructions) {
1069   // Various types and constants must be in place for a function to be made
1070   // live-safe.  Add them if not already present.
1071   FindOrCreateBoolType();  // Needed for comparisons
1072   FindOrCreatePointerToIntegerType(
1073       32, false, SpvStorageClassFunction);  // Needed for adding loop limiters
1074   FindOrCreateIntegerConstant({0}, 32, false,
1075                               false);  // Needed for initializing loop limiters
1076   FindOrCreateIntegerConstant({1}, 32, false,
1077                               false);  // Needed for incrementing loop limiters
1078 
1079   // Get a fresh id for the variable that will be used as a loop limiter.
1080   const uint32_t loop_limiter_variable_id = GetFuzzerContext()->GetFreshId();
1081   // Choose a random loop limit, and add the required constant to the
1082   // module if not already there.
1083   const uint32_t loop_limit = FindOrCreateIntegerConstant(
1084       {GetFuzzerContext()->GetRandomLoopLimit()}, 32, false, false);
1085 
1086   // Consider every loop header in the function to donate, and create a
1087   // structure capturing the ids to be used for manipulating the loop
1088   // limiter each time the loop is iterated.
1089   std::vector<protobufs::LoopLimiterInfo> loop_limiters;
1090   for (auto& block : function_to_donate) {
1091     if (block.IsLoopHeader()) {
1092       protobufs::LoopLimiterInfo loop_limiter;
1093 
1094       if (!CreateLoopLimiterInfo(donor_ir_context, block,
1095                                  original_id_to_donated_id, &loop_limiter)) {
1096         return false;
1097       }
1098 
1099       loop_limiters.emplace_back(std::move(loop_limiter));
1100     }
1101   }
1102 
1103   // Consider every access chain in the function to donate, and create a
1104   // structure containing the ids necessary to clamp the access chain
1105   // indices to be in-bounds.
1106   std::vector<protobufs::AccessChainClampingInfo> access_chain_clamping_info;
1107   for (auto& block : function_to_donate) {
1108     for (auto& inst : block) {
1109       switch (inst.opcode()) {
1110         case SpvOpAccessChain:
1111         case SpvOpInBoundsAccessChain: {
1112           protobufs::AccessChainClampingInfo clamping_info;
1113           clamping_info.set_access_chain_id(
1114               original_id_to_donated_id.at(inst.result_id()));
1115 
1116           auto base_object = donor_ir_context->get_def_use_mgr()->GetDef(
1117               inst.GetSingleWordInOperand(0));
1118           assert(base_object && "The base object must exist.");
1119           auto pointer_type = donor_ir_context->get_def_use_mgr()->GetDef(
1120               base_object->type_id());
1121           assert(pointer_type && pointer_type->opcode() == SpvOpTypePointer &&
1122                  "The base object must have pointer type.");
1123 
1124           auto should_be_composite_type =
1125               donor_ir_context->get_def_use_mgr()->GetDef(
1126                   pointer_type->GetSingleWordInOperand(1));
1127 
1128           // Walk the access chain, creating fresh ids to facilitate
1129           // clamping each index.  For simplicity we do this for every
1130           // index, even though constant indices will not end up being
1131           // clamped.
1132           for (uint32_t index = 1; index < inst.NumInOperands(); index++) {
1133             auto compare_and_select_ids =
1134                 clamping_info.add_compare_and_select_ids();
1135             compare_and_select_ids->set_first(GetFuzzerContext()->GetFreshId());
1136             compare_and_select_ids->set_second(
1137                 GetFuzzerContext()->GetFreshId());
1138 
1139             // Get the bound for the component being indexed into.
1140             uint32_t bound;
1141             if (should_be_composite_type->opcode() == SpvOpTypeRuntimeArray) {
1142               // The donor is indexing into a runtime array.  We do not
1143               // donate runtime arrays.  Instead, we donate a corresponding
1144               // fixed-size array for every runtime array.  We should thus
1145               // find that donor composite type's result id maps to a fixed-
1146               // size array.
1147               auto fixed_size_array_type =
1148                   GetIRContext()->get_def_use_mgr()->GetDef(
1149                       original_id_to_donated_id.at(
1150                           should_be_composite_type->result_id()));
1151               assert(fixed_size_array_type->opcode() == SpvOpTypeArray &&
1152                      "A runtime array type in the donor should have been "
1153                      "replaced by a fixed-sized array in the recipient.");
1154               // The size of this fixed-size array is a suitable bound.
1155               bound = fuzzerutil::GetBoundForCompositeIndex(
1156                   *fixed_size_array_type, GetIRContext());
1157             } else {
1158               bound = fuzzerutil::GetBoundForCompositeIndex(
1159                   *should_be_composite_type, donor_ir_context);
1160             }
1161             const uint32_t index_id = inst.GetSingleWordInOperand(index);
1162             auto index_inst =
1163                 donor_ir_context->get_def_use_mgr()->GetDef(index_id);
1164             auto index_type_inst = donor_ir_context->get_def_use_mgr()->GetDef(
1165                 index_inst->type_id());
1166             assert(index_type_inst->opcode() == SpvOpTypeInt);
1167             opt::analysis::Integer* index_int_type =
1168                 donor_ir_context->get_type_mgr()
1169                     ->GetType(index_type_inst->result_id())
1170                     ->AsInteger();
1171             if (index_inst->opcode() != SpvOpConstant) {
1172               // We will have to clamp this index, so we need a constant
1173               // whose value is one less than the bound, to compare
1174               // against and to use as the clamped value.
1175               FindOrCreateIntegerConstant({bound - 1}, 32,
1176                                           index_int_type->IsSigned(), false);
1177             }
1178             should_be_composite_type =
1179                 TransformationAddFunction::FollowCompositeIndex(
1180                     donor_ir_context, *should_be_composite_type, index_id);
1181           }
1182           access_chain_clamping_info.push_back(clamping_info);
1183           break;
1184         }
1185         default:
1186           break;
1187       }
1188     }
1189   }
1190 
1191   // If |function_to_donate| has non-void return type and contains an
1192   // OpKill/OpUnreachable instruction, then a value is needed in order to turn
1193   // these into instructions of the form OpReturnValue %value_id.
1194   uint32_t kill_unreachable_return_value_id = 0;
1195   auto function_return_type_inst =
1196       donor_ir_context->get_def_use_mgr()->GetDef(function_to_donate.type_id());
1197   if (function_return_type_inst->opcode() != SpvOpTypeVoid &&
1198       fuzzerutil::FunctionContainsOpKillOrUnreachable(function_to_donate)) {
1199     kill_unreachable_return_value_id = FindOrCreateZeroConstant(
1200         original_id_to_donated_id.at(function_return_type_inst->result_id()),
1201         false);
1202   }
1203 
1204   // Try to add the function in a livesafe manner. This may fail due to edge
1205   // cases, e.g. where adding loop limiters changes dominance such that the
1206   // module becomes invalid. It would be ideal to handle all such edge cases,
1207   // but as they are rare it is more pragmatic to bail out of making the
1208   // function livesafe if the transformation's precondition fails to hold.
1209   return MaybeApplyTransformation(TransformationAddFunction(
1210       donated_instructions, loop_limiter_variable_id, loop_limit, loop_limiters,
1211       kill_unreachable_return_value_id, access_chain_clamping_info));
1212 }
1213 
1214 }  // namespace fuzz
1215 }  // namespace spvtools
1216