fuzzer_pass.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. #ifndef SOURCE_FUZZ_FUZZER_PASS_H_
  15. #define SOURCE_FUZZ_FUZZER_PASS_H_
  16. #include <functional>
  17. #include <vector>
  18. #include "source/fuzz/fuzzer_context.h"
  19. #include "source/fuzz/protobufs/spirvfuzz_protobufs.h"
  20. #include "source/fuzz/transformation.h"
  21. #include "source/fuzz/transformation_context.h"
  22. #include "source/opt/ir_context.h"
  23. namespace spvtools {
  24. namespace fuzz {
  25. // Interface for applying a pass of transformations to a module.
  26. class FuzzerPass {
  27. public:
  28. FuzzerPass(opt::IRContext* ir_context,
  29. TransformationContext* transformation_context,
  30. FuzzerContext* fuzzer_context,
  31. protobufs::TransformationSequence* transformations,
  32. bool ignore_inapplicable_transformations);
  33. virtual ~FuzzerPass();
  34. // Applies the pass to the module |ir_context_|, assuming and updating
  35. // information from |transformation_context_|, and using |fuzzer_context_| to
  36. // guide the process. Appends to |transformations_| all transformations that
  37. // were applied during the pass.
  38. virtual void Apply() = 0;
  39. protected:
  40. opt::IRContext* GetIRContext() const { return ir_context_; }
  41. TransformationContext* GetTransformationContext() const {
  42. return transformation_context_;
  43. }
  44. FuzzerContext* GetFuzzerContext() const { return fuzzer_context_; }
  45. protobufs::TransformationSequence* GetTransformations() const {
  46. return transformations_;
  47. }
  48. // Returns all instructions that are *available* at |inst_it|, which is
  49. // required to be inside block |block| of function |function| - that is, all
  50. // instructions at global scope and all instructions that strictly dominate
  51. // |inst_it|.
  52. //
  53. // Filters said instructions to return only those that satisfy the
  54. // |instruction_is_relevant| predicate. This, for instance, could ignore all
  55. // instructions that have a particular decoration.
  56. std::vector<opt::Instruction*> FindAvailableInstructions(
  57. opt::Function* function, opt::BasicBlock* block,
  58. const opt::BasicBlock::iterator& inst_it,
  59. std::function<bool(opt::IRContext*, opt::Instruction*)>
  60. instruction_is_relevant) const;
  61. // A helper method that iterates through each instruction in each reachable
  62. // block of |function|, at all times tracking an instruction descriptor that
  63. // allows the latest instruction to be located even if it has no result id.
  64. //
  65. // The code to manipulate the instruction descriptor is a bit fiddly. The
  66. // point of this method is to avoiding having to duplicate it in multiple
  67. // transformation passes.
  68. //
  69. // The function |action| is invoked for each instruction |inst_it| in block
  70. // |block| of function |function| that is encountered. The
  71. // |instruction_descriptor| parameter to the function object allows |inst_it|
  72. // to be identified.
  73. //
  74. // In most intended use cases, the job of |action| is to randomly decide
  75. // whether to try to apply some transformation, and then - if selected - to
  76. // attempt to apply it.
  77. void ForEachInstructionWithInstructionDescriptor(
  78. opt::Function* function,
  79. std::function<
  80. void(opt::BasicBlock* block, opt::BasicBlock::iterator inst_it,
  81. const protobufs::InstructionDescriptor& instruction_descriptor)>
  82. action);
  83. // Applies the above overload of ForEachInstructionWithInstructionDescriptor
  84. // to every function in the module, so that |action| is applied to an
  85. // |instruction_descriptor| for every instruction, |inst_it|, of every |block|
  86. // in every |function|.
  87. void ForEachInstructionWithInstructionDescriptor(
  88. std::function<
  89. void(opt::Function* function, opt::BasicBlock* block,
  90. opt::BasicBlock::iterator inst_it,
  91. const protobufs::InstructionDescriptor& instruction_descriptor)>
  92. action);
  93. // A generic helper for applying a transformation that should be applicable
  94. // by construction, and adding it to the sequence of applied transformations.
  95. void ApplyTransformation(const Transformation& transformation);
  96. // A generic helper for applying a transformation only if it is applicable.
  97. // If it is applicable, the transformation is applied and then added to the
  98. // sequence of applied transformations and the function returns true.
  99. // Otherwise, the function returns false.
  100. bool MaybeApplyTransformation(const Transformation& transformation);
  101. // Returns the id of an OpTypeBool instruction. If such an instruction does
  102. // not exist, a transformation is applied to add it.
  103. uint32_t FindOrCreateBoolType();
  104. // Returns the id of an OpTypeInt instruction, with width and signedness
  105. // specified by |width| and |is_signed|, respectively. If such an instruction
  106. // does not exist, a transformation is applied to add it.
  107. uint32_t FindOrCreateIntegerType(uint32_t width, bool is_signed);
  108. // Returns the id of an OpTypeFloat instruction, with width specified by
  109. // |width|. If such an instruction does not exist, a transformation is
  110. // applied to add it.
  111. uint32_t FindOrCreateFloatType(uint32_t width);
  112. // Returns the id of an OpTypeFunction %<return_type_id> %<...argument_id>
  113. // instruction. If such an instruction doesn't exist, a transformation
  114. // is applied to create a new one.
  115. uint32_t FindOrCreateFunctionType(uint32_t return_type_id,
  116. const std::vector<uint32_t>& argument_id);
  117. // Returns the id of an OpTypeVector instruction, with |component_type_id|
  118. // (which must already exist) as its base type, and |component_count|
  119. // elements (which must be in the range [2, 4]). If such an instruction does
  120. // not exist, a transformation is applied to add it.
  121. uint32_t FindOrCreateVectorType(uint32_t component_type_id,
  122. uint32_t component_count);
  123. // Returns the id of an OpTypeMatrix instruction, with |column_count| columns
  124. // and |row_count| rows (each of which must be in the range [2, 4]). If the
  125. // float and vector types required to build this matrix type or the matrix
  126. // type itself do not exist, transformations are applied to add them.
  127. uint32_t FindOrCreateMatrixType(uint32_t column_count, uint32_t row_count);
  128. // Returns the id of an OpTypeStruct instruction with |component_type_ids| as
  129. // type ids for struct's components. If no such a struct type exists,
  130. // transformations are applied to add it. |component_type_ids| may not contain
  131. // a result id of an OpTypeFunction.
  132. uint32_t FindOrCreateStructType(
  133. const std::vector<uint32_t>& component_type_ids);
  134. // Returns the id of a pointer type with base type |base_type_id| (which must
  135. // already exist) and storage class |storage_class|. A transformation is
  136. // applied to add the pointer if it does not already exist.
  137. uint32_t FindOrCreatePointerType(uint32_t base_type_id,
  138. spv::StorageClass storage_class);
  139. // Returns the id of an OpTypePointer instruction, with a integer base
  140. // type of width and signedness specified by |width| and |is_signed|,
  141. // respectively. If the pointer type or required integer base type do not
  142. // exist, transformations are applied to add them.
  143. uint32_t FindOrCreatePointerToIntegerType(uint32_t width, bool is_signed,
  144. spv::StorageClass storage_class);
  145. // Returns the id of an OpConstant instruction, with a integer type of
  146. // width and signedness specified by |width| and |is_signed|, respectively,
  147. // with |words| as its value. If either the required integer type or the
  148. // constant do not exist, transformations are applied to add them.
  149. // The returned id either participates in IdIsIrrelevant fact or not,
  150. // depending on the |is_irrelevant| parameter.
  151. uint32_t FindOrCreateIntegerConstant(const std::vector<uint32_t>& words,
  152. uint32_t width, bool is_signed,
  153. bool is_irrelevant);
  154. // Returns the id of an OpConstant instruction, with a floating-point
  155. // type of width specified by |width|, with |words| as its value. If either
  156. // the required floating-point type or the constant do not exist,
  157. // transformations are applied to add them. The returned id either
  158. // participates in IdIsIrrelevant fact or not, depending on the
  159. // |is_irrelevant| parameter.
  160. uint32_t FindOrCreateFloatConstant(const std::vector<uint32_t>& words,
  161. uint32_t width, bool is_irrelevant);
  162. // Returns the id of an OpConstantTrue or OpConstantFalse instruction,
  163. // according to |value|. If either the required instruction or the bool
  164. // type do not exist, transformations are applied to add them.
  165. // The returned id either participates in IdIsIrrelevant fact or not,
  166. // depending on the |is_irrelevant| parameter.
  167. uint32_t FindOrCreateBoolConstant(bool value, bool is_irrelevant);
  168. // Returns the id of an OpConstant instruction of type with |type_id|
  169. // that consists of |words|. If that instruction doesn't exist,
  170. // transformations are applied to add it. |type_id| must be a valid
  171. // result id of either scalar or boolean OpType* instruction that exists
  172. // in the module. The returned id either participates in IdIsIrrelevant fact
  173. // or not, depending on the |is_irrelevant| parameter.
  174. uint32_t FindOrCreateConstant(const std::vector<uint32_t>& words,
  175. uint32_t type_id, bool is_irrelevant);
  176. // Returns the id of an OpConstantComposite instruction of type with |type_id|
  177. // that consists of |component_ids|. If that instruction doesn't exist,
  178. // transformations are applied to add it. |type_id| must be a valid
  179. // result id of an OpType* instruction that represents a composite type
  180. // (i.e. a vector, matrix, struct or array).
  181. // The returned id either participates in IdIsIrrelevant fact or not,
  182. // depending on the |is_irrelevant| parameter.
  183. uint32_t FindOrCreateCompositeConstant(
  184. const std::vector<uint32_t>& component_ids, uint32_t type_id,
  185. bool is_irrelevant);
  186. // Returns the result id of an instruction of the form:
  187. // %id = OpUndef %|type_id|
  188. // If no such instruction exists, a transformation is applied to add it.
  189. uint32_t FindOrCreateGlobalUndef(uint32_t type_id);
  190. // Returns the id of an OpNullConstant instruction of type |type_id|. If
  191. // that instruction doesn't exist, it is added through a transformation.
  192. // |type_id| must be a valid result id of an OpType* instruction that exists
  193. // in the module.
  194. uint32_t FindOrCreateNullConstant(uint32_t type_id);
  195. // Define a *basic type* to be an integer, boolean or floating-point type,
  196. // or a matrix, vector, struct or fixed-size array built from basic types. In
  197. // particular, a basic type cannot contain an opaque type (such as an image),
  198. // or a runtime-sized array.
  199. //
  200. // Yields a pair, (basic_type_ids, basic_type_ids_to_pointers), such that:
  201. // - basic_type_ids captures every basic type declared in the module.
  202. // - basic_type_ids_to_pointers maps every such basic type to the sequence
  203. // of all pointer types that have storage class |storage_class| and the
  204. // given basic type as their pointee type. The sequence may be empty for
  205. // some basic types if no pointers to those types are defined for the given
  206. // storage class, and the sequence will have multiple elements if there are
  207. // repeated pointer declarations for the same basic type and storage class.
  208. std::pair<std::vector<uint32_t>, std::map<uint32_t, std::vector<uint32_t>>>
  209. GetAvailableBasicTypesAndPointers(spv::StorageClass storage_class) const;
  210. // Given a type id, |scalar_or_composite_type_id|, which must correspond to
  211. // some scalar or composite type, returns the result id of an instruction
  212. // defining a constant of the given type that is zero or false at everywhere.
  213. // If such an instruction does not yet exist, transformations are applied to
  214. // add it. The returned id either participates in IdIsIrrelevant fact or not,
  215. // depending on the |is_irrelevant| parameter.
  216. //
  217. // Examples:
  218. // --------------+-------------------------------
  219. // TYPE | RESULT is id corresponding to
  220. // --------------+-------------------------------
  221. // bool | false
  222. // --------------+-------------------------------
  223. // bvec4 | (false, false, false, false)
  224. // --------------+-------------------------------
  225. // float | 0.0
  226. // --------------+-------------------------------
  227. // vec2 | (0.0, 0.0)
  228. // --------------+-------------------------------
  229. // int[3] | [0, 0, 0]
  230. // --------------+-------------------------------
  231. // struct S { |
  232. // int i; | S(0, false, (0u, 0u))
  233. // bool b; |
  234. // uint2 u; |
  235. // } |
  236. // --------------+-------------------------------
  237. uint32_t FindOrCreateZeroConstant(uint32_t scalar_or_composite_type_id,
  238. bool is_irrelevant);
  239. // Adds a pair (id_use_descriptor, |replacement_id|) to the vector
  240. // |uses_to_replace|, where id_use_descriptor is the id use descriptor
  241. // representing the usage of an id in the |use_inst| instruction, at operand
  242. // index |use_index|, only if the instruction is in a basic block.
  243. // If the instruction is not in a basic block, it does nothing.
  244. void MaybeAddUseToReplace(
  245. opt::Instruction* use_inst, uint32_t use_index, uint32_t replacement_id,
  246. std::vector<std::pair<protobufs::IdUseDescriptor, uint32_t>>*
  247. uses_to_replace);
  248. // Returns the preheader of the loop with header |header_id|, which satisfies
  249. // all of the following conditions:
  250. // - It is the only out-of-loop predecessor of the header
  251. // - It unconditionally branches to the header
  252. // - It is not a loop header itself
  253. // If such preheader does not exist, a new one is added and returned.
  254. // Requires |header_id| to be the label id of a loop header block that is
  255. // reachable in the CFG (and thus has at least 2 predecessors).
  256. opt::BasicBlock* GetOrCreateSimpleLoopPreheader(uint32_t header_id);
  257. // Returns the second block in the pair obtained by splitting |block_id| just
  258. // after the last OpPhi or OpVariable instruction in it. Assumes that the
  259. // block is not a loop header.
  260. opt::BasicBlock* SplitBlockAfterOpPhiOrOpVariable(uint32_t block_id);
  261. // Returns the id of an available local variable (storage class Function) with
  262. // the fact PointeeValueIsIrrelevant set according to
  263. // |pointee_value_is_irrelevant|. If there is no such variable, it creates one
  264. // in the |function| adding a zero initializer constant that is irrelevant.
  265. // The new variable has the fact PointeeValueIsIrrelevant set according to
  266. // |pointee_value_is_irrelevant|. The function returns the id of the created
  267. // variable.
  268. uint32_t FindOrCreateLocalVariable(uint32_t pointer_type_id,
  269. uint32_t function_id,
  270. bool pointee_value_is_irrelevant);
  271. // Returns the id of an available global variable (storage class Private or
  272. // Workgroup) with the fact PointeeValueIsIrrelevant set according to
  273. // |pointee_value_is_irrelevant|. If there is no such variable, it creates
  274. // one, adding a zero initializer constant that is irrelevant. The new
  275. // variable has the fact PointeeValueIsIrrelevant set according to
  276. // |pointee_value_is_irrelevant|. The function returns the id of the created
  277. // variable.
  278. uint32_t FindOrCreateGlobalVariable(uint32_t pointer_type_id,
  279. bool pointee_value_is_irrelevant);
  280. private:
  281. opt::IRContext* ir_context_;
  282. TransformationContext* transformation_context_;
  283. FuzzerContext* fuzzer_context_;
  284. protobufs::TransformationSequence* transformations_;
  285. // If set, then transformations that should be applicable by construction are
  286. // still tested for applicability, and ignored if they turn out to be
  287. // inapplicable. Otherwise, applicability by construction is asserted.
  288. const bool ignore_inapplicable_transformations_;
  289. };
  290. } // namespace fuzz
  291. } // namespace spvtools
  292. #endif // SOURCE_FUZZ_FUZZER_PASS_H_