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 #ifndef SOURCE_FUZZ_FUZZER_PASS_DONATE_MODULES_H_ 16 #define SOURCE_FUZZ_FUZZER_PASS_DONATE_MODULES_H_ 17 18 #include <vector> 19 20 #include "source/fuzz/fuzzer_pass.h" 21 #include "source/fuzz/fuzzer_util.h" 22 23 namespace spvtools { 24 namespace fuzz { 25 26 // A fuzzer pass that randomly adds code from other SPIR-V modules to the module 27 // being transformed. 28 class FuzzerPassDonateModules : public FuzzerPass { 29 public: 30 FuzzerPassDonateModules( 31 opt::IRContext* ir_context, TransformationContext* transformation_context, 32 FuzzerContext* fuzzer_context, 33 protobufs::TransformationSequence* transformations, 34 const std::vector<fuzzerutil::ModuleSupplier>& donor_suppliers); 35 36 void Apply() override; 37 38 // Donates the global declarations and functions of |donor_ir_context| into 39 // the fuzzer pass's IR context. |make_livesafe| dictates whether the 40 // functions of the donated module will be made livesafe (see 41 // FactFunctionIsLivesafe). 42 void DonateSingleModule(opt::IRContext* donor_ir_context, bool make_livesafe); 43 44 private: 45 // Adapts a storage class coming from a donor module so that it will work 46 // in a recipient module, e.g. by changing Uniform to Private. 47 static SpvStorageClass AdaptStorageClass(SpvStorageClass donor_storage_class); 48 49 // Identifies all external instruction set imports in |donor_ir_context| and 50 // populates |original_id_to_donated_id| with a mapping from the donor's id 51 // for such an import to a corresponding import in the recipient. Aborts if 52 // no such corresponding import is available. 53 void HandleExternalInstructionImports( 54 opt::IRContext* donor_ir_context, 55 std::map<uint32_t, uint32_t>* original_id_to_donated_id); 56 57 // Considers all types, globals, constants and undefs in |donor_ir_context|. 58 // For each instruction, uses |original_to_donated_id| to map its result id to 59 // either (1) the id of an existing identical instruction in the recipient, or 60 // (2) to a fresh id, in which case the instruction is also added to the 61 // recipient (with any operand ids that it uses being remapped via 62 // |original_id_to_donated_id|). 63 void HandleTypesAndValues( 64 opt::IRContext* donor_ir_context, 65 std::map<uint32_t, uint32_t>* original_id_to_donated_id); 66 67 // Helper method for HandleTypesAndValues, to handle a single type/value. 68 void HandleTypeOrValue( 69 const opt::Instruction& type_or_value, 70 std::map<uint32_t, uint32_t>* original_id_to_donated_id); 71 72 // Assumes that |donor_ir_context| does not exhibit recursion. Considers the 73 // functions in |donor_ir_context|'s call graph in a reverse-topologically- 74 // sorted order (leaves-to-root), adding each function to the recipient 75 // module, rewritten to use fresh ids and using |original_id_to_donated_id| to 76 // remap ids. The |make_livesafe| argument captures whether the functions in 77 // the module are required to be made livesafe before being added to the 78 // recipient. 79 void HandleFunctions(opt::IRContext* donor_ir_context, 80 std::map<uint32_t, uint32_t>* original_id_to_donated_id, 81 bool make_livesafe); 82 83 // During donation we will have to ignore some instructions, e.g. because they 84 // use opcodes that we cannot support or because they reference the ids of 85 // instructions that have not been donated. This function encapsulates the 86 // logic for deciding which whether instruction |instruction| from 87 // |donor_ir_context| can be donated. 88 bool CanDonateInstruction( 89 opt::IRContext* donor_ir_context, const opt::Instruction& instruction, 90 const std::map<uint32_t, uint32_t>& original_id_to_donated_id, 91 const std::set<uint32_t>& skipped_instructions) const; 92 93 // We treat the OpArrayLength instruction specially. In the donor shader this 94 // instruction yields the length of a runtime array that is the final member 95 // of a struct. During donation, we will have converted the runtime array 96 // type, and the associated struct field, into a fixed-size array. 97 // 98 // Instead of donating this instruction, we turn it into an OpCopyObject 99 // instruction that copies the size of the fixed-size array. 100 void HandleOpArrayLength( 101 const opt::Instruction& instruction, 102 std::map<uint32_t, uint32_t>* original_id_to_donated_id, 103 std::vector<protobufs::Instruction>* donated_instructions) const; 104 105 // The instruction |instruction| is required to be an instruction that cannot 106 // be easily donated, either because it uses an unsupported opcode, has an 107 // unsupported result type, or uses id operands that could not be donated. 108 // 109 // If |instruction| generates a result id, the function attempts to add a 110 // substitute for |instruction| to |donated_instructions| that has the correct 111 // result type. If this cannot be done, the instruction's result id is added 112 // to |skipped_instructions|. The mapping from donor ids to recipient ids is 113 // managed by |original_id_to_donated_id|. 114 void HandleDifficultInstruction( 115 const opt::Instruction& instruction, 116 std::map<uint32_t, uint32_t>* original_id_to_donated_id, 117 std::vector<protobufs::Instruction>* donated_instructions, 118 std::set<uint32_t>* skipped_instructions); 119 120 // Adds an instruction based in |instruction| to |donated_instructions| in a 121 // form ready for donation. The original instruction comes from 122 // |donor_ir_context|, and |original_id_to_donated_id| maps ids from 123 // |donor_ir_context| to corresponding ids in the recipient module. 124 void PrepareInstructionForDonation( 125 const opt::Instruction& instruction, opt::IRContext* donor_ir_context, 126 std::map<uint32_t, uint32_t>* original_id_to_donated_id, 127 std::vector<protobufs::Instruction>* donated_instructions); 128 129 // Tries to create a protobufs::LoopLimiterInfo given a loop header basic 130 // block. Returns true if successful and outputs loop limiter into the |out| 131 // variable. Otherwise, returns false. |out| contains an undefined value when 132 // this function returns false. 133 bool CreateLoopLimiterInfo( 134 opt::IRContext* donor_ir_context, const opt::BasicBlock& loop_header, 135 const std::map<uint32_t, uint32_t>& original_id_to_donated_id, 136 protobufs::LoopLimiterInfo* out); 137 138 // Requires that |donated_instructions| represents a prepared version of the 139 // instructions of |function_to_donate| (which comes from |donor_ir_context|) 140 // ready for donation, and |original_id_to_donated_id| maps ids from 141 // |donor_ir_context| to their corresponding ids in the recipient module. 142 // 143 // Attempts to add a livesafe version of the function, based on 144 // |donated_instructions|, to the recipient module. Returns true if the 145 // donation was successful, false otherwise. 146 bool MaybeAddLivesafeFunction( 147 const opt::Function& function_to_donate, opt::IRContext* donor_ir_context, 148 const std::map<uint32_t, uint32_t>& original_id_to_donated_id, 149 const std::vector<protobufs::Instruction>& donated_instructions); 150 151 // Returns true if and only if |instruction| is a scalar, vector, matrix, 152 // array or struct; i.e. it is not an opaque type. 153 bool IsBasicType(const opt::Instruction& instruction) const; 154 155 // Functions that supply SPIR-V modules 156 std::vector<fuzzerutil::ModuleSupplier> donor_suppliers_; 157 }; 158 159 } // namespace fuzz 160 } // namespace spvtools 161 162 #endif // SOURCE_FUZZ_FUZZER_PASS_DONATE_MODULES_H_ 163