fact_manager.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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_FACT_MANAGER_FACT_MANAGER_H_
  15. #define SOURCE_FUZZ_FACT_MANAGER_FACT_MANAGER_H_
  16. #include <set>
  17. #include <utility>
  18. #include <vector>
  19. #include "source/fuzz/data_descriptor.h"
  20. #include "source/fuzz/fact_manager/constant_uniform_facts.h"
  21. #include "source/fuzz/fact_manager/data_synonym_and_id_equation_facts.h"
  22. #include "source/fuzz/fact_manager/dead_block_facts.h"
  23. #include "source/fuzz/fact_manager/irrelevant_value_facts.h"
  24. #include "source/fuzz/fact_manager/livesafe_function_facts.h"
  25. #include "source/fuzz/protobufs/spirvfuzz_protobufs.h"
  26. #include "source/opt/constants.h"
  27. namespace spvtools {
  28. namespace fuzz {
  29. // Keeps track of facts about the module being transformed on which the fuzzing
  30. // process can depend. Some initial facts can be provided, for example about
  31. // guarantees on the values of inputs to SPIR-V entry points. Transformations
  32. // may then rely on these facts, can add further facts that they establish.
  33. // Facts are intended to be simple properties that either cannot be deduced from
  34. // the module (such as properties that are guaranteed to hold for entry point
  35. // inputs), or that are established by transformations, likely to be useful for
  36. // future transformations, and not completely trivial to deduce straight from
  37. // the module.
  38. class FactManager {
  39. public:
  40. explicit FactManager(opt::IRContext* ir_context);
  41. // Adds all the facts from |facts|, checking them for validity with respect to
  42. // |ir_context_|. Warnings about invalid facts are communicated via
  43. // |message_consumer|; such facts are otherwise ignored.
  44. void AddInitialFacts(const MessageConsumer& message_consumer,
  45. const protobufs::FactSequence& facts);
  46. // Checks the fact for validity with respect to |ir_context_|. Returns false,
  47. // with no side effects, if the fact is invalid. Otherwise adds |fact| to the
  48. // fact manager.
  49. bool MaybeAddFact(const protobufs::Fact& fact);
  50. // Record the fact that |data1| and |data2| are synonymous. Neither |data1|
  51. // nor |data2| may contain an irrelevant id.
  52. void AddFactDataSynonym(const protobufs::DataDescriptor& data1,
  53. const protobufs::DataDescriptor& data2);
  54. // Records the fact that |block_id| is dead. |block_id| must be a result id
  55. // of some OpLabel instruction in the |ir_context_|.
  56. void AddFactBlockIsDead(uint32_t block_id);
  57. // Records the fact that |function_id| is livesafe. |function_id| must be a
  58. // result id of some non-entry-point function in the module.
  59. void AddFactFunctionIsLivesafe(uint32_t function_id);
  60. // Records the fact that the value of the pointee associated with |pointer_id|
  61. // is irrelevant: it does not affect the observable behaviour of the module.
  62. // |pointer_id| must exist in the module and actually be a pointer.
  63. void AddFactValueOfPointeeIsIrrelevant(uint32_t pointer_id);
  64. // Records a fact that the |result_id| is irrelevant (i.e. it doesn't affect
  65. // the semantics of the module).
  66. // |result_id| must exist in the module and it may not be a pointer.
  67. void AddFactIdIsIrrelevant(uint32_t result_id);
  68. // Records the fact that |lhs_id| is defined by the equation:
  69. //
  70. // |lhs_id| = |opcode| |rhs_id[0]| ... |rhs_id[N-1]|
  71. //
  72. // Neither |lhs_id| nor any of |rhs_id| may be irrelevant.
  73. void AddFactIdEquation(uint32_t lhs_id, spv::Op opcode,
  74. const std::vector<uint32_t>& rhs_id);
  75. // Inspects all known facts and adds corollary facts; e.g. if we know that
  76. // a.x == b.x and a.y == b.y, where a and b have vec2 type, we can record
  77. // that a == b holds.
  78. //
  79. // This method is expensive, and should only be called (by applying a
  80. // transformation) at the start of a fuzzer pass that depends on data
  81. // synonym facts, rather than calling it every time a new data synonym fact
  82. // is added.
  83. //
  84. // The parameter |maximum_equivalence_class_size| specifies the size beyond
  85. // which equivalence classes should not be mined for new facts, to avoid
  86. // excessively-long closure computations.
  87. void ComputeClosureOfFacts(uint32_t maximum_equivalence_class_size);
  88. // The fact manager is responsible for managing a few distinct categories of
  89. // facts. In principle there could be different fact managers for each kind
  90. // of fact, but in practice providing one 'go to' place for facts is
  91. // convenient. To keep some separation, the public methods of the fact
  92. // manager should be grouped according to the kind of fact to which they
  93. // relate.
  94. //==============================
  95. // Querying facts about uniform constants
  96. // Provides the distinct type ids for which at least one "constant ==
  97. // uniform element" fact is known.
  98. std::vector<uint32_t> GetTypesForWhichUniformValuesAreKnown() const;
  99. // Provides distinct constant ids with type |type_id| for which at least one
  100. // "constant == uniform element" fact is known. If multiple identically-
  101. // valued constants are relevant, only one will appear in the sequence.
  102. std::vector<uint32_t> GetConstantsAvailableFromUniformsForType(
  103. uint32_t type_id) const;
  104. // Provides details of all uniform elements that are known to be equal to the
  105. // constant associated with |constant_id| in |ir_context_|.
  106. std::vector<protobufs::UniformBufferElementDescriptor>
  107. GetUniformDescriptorsForConstant(uint32_t constant_id) const;
  108. // Returns the id of a constant whose value is known to match that of
  109. // |uniform_descriptor|, and whose type matches the type of the uniform
  110. // element. If multiple such constant is exist, the one that is returned
  111. // is arbitrary. Returns 0 if no such constant id exists.
  112. uint32_t GetConstantFromUniformDescriptor(
  113. const protobufs::UniformBufferElementDescriptor& uniform_descriptor)
  114. const;
  115. // Returns all "constant == uniform element" facts known to the fact
  116. // manager, pairing each fact with id of the type that is associated with
  117. // both the constant and the uniform element.
  118. const std::vector<std::pair<protobufs::FactConstantUniform, uint32_t>>&
  119. GetConstantUniformFactsAndTypes() const;
  120. // End of uniform constant facts
  121. //==============================
  122. //==============================
  123. // Querying facts about id synonyms
  124. // Returns every id for which a fact of the form "this id is synonymous with
  125. // this piece of data" is known.
  126. std::vector<uint32_t> GetIdsForWhichSynonymsAreKnown() const;
  127. // Returns a vector of all data descriptors that participate in DataSynonym
  128. // facts. All descriptors are guaranteed to exist in the |ir_context_|.
  129. std::vector<const protobufs::DataDescriptor*> GetAllSynonyms() const;
  130. // Returns the equivalence class of all known synonyms of |id|, or an empty
  131. // set if no synonyms are known.
  132. std::vector<const protobufs::DataDescriptor*> GetSynonymsForId(
  133. uint32_t id) const;
  134. // Returns the equivalence class of all known synonyms of |data_descriptor|,
  135. // or empty if no synonyms are known.
  136. std::vector<const protobufs::DataDescriptor*> GetSynonymsForDataDescriptor(
  137. const protobufs::DataDescriptor& data_descriptor) const;
  138. // Returns true if and only if |data_descriptor1| and |data_descriptor2| are
  139. // known to be synonymous.
  140. bool IsSynonymous(const protobufs::DataDescriptor& data_descriptor1,
  141. const protobufs::DataDescriptor& data_descriptor2) const;
  142. // End of id synonym facts
  143. //==============================
  144. //==============================
  145. // Querying facts about dead blocks
  146. // Returns true if and only if |block_id| is the id of a block known to be
  147. // dynamically unreachable.
  148. bool BlockIsDead(uint32_t block_id) const;
  149. // End of dead block facts
  150. //==============================
  151. //==============================
  152. // Querying facts about livesafe function
  153. // Returns true if and only if |function_id| is the id of a function known
  154. // to be livesafe.
  155. bool FunctionIsLivesafe(uint32_t function_id) const;
  156. // End of dead livesafe function facts
  157. //==============================
  158. //==============================
  159. // Querying facts about irrelevant values
  160. // Returns true if and only if the value of the pointee associated with
  161. // |pointer_id| is irrelevant.
  162. bool PointeeValueIsIrrelevant(uint32_t pointer_id) const;
  163. // Returns true if there exists a fact that the |result_id| is irrelevant or
  164. // if |result_id| is declared in a block that has been declared dead.
  165. bool IdIsIrrelevant(uint32_t result_id) const;
  166. // Returns a set of all the ids which have been declared irrelevant, or which
  167. // have been declared inside a dead block.
  168. std::unordered_set<uint32_t> GetIrrelevantIds() const;
  169. // End of irrelevant value facts
  170. //==============================
  171. private:
  172. // Keep these in alphabetical order.
  173. fact_manager::ConstantUniformFacts constant_uniform_facts_;
  174. fact_manager::DataSynonymAndIdEquationFacts
  175. data_synonym_and_id_equation_facts_;
  176. fact_manager::DeadBlockFacts dead_block_facts_;
  177. fact_manager::LivesafeFunctionFacts livesafe_function_facts_;
  178. fact_manager::IrrelevantValueFacts irrelevant_value_facts_;
  179. };
  180. } // namespace fuzz
  181. } // namespace spvtools
  182. #endif // SOURCE_FUZZ_FACT_MANAGER_FACT_MANAGER_H_