Debian Codegraph @

third_party/spirv-tools/source/fuzz/fuzzer_pass_donate_modules.h

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