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_FACT_MANAGER_H_ 16 #define SOURCE_FUZZ_FACT_MANAGER_H_ 17 18 #include <memory> 19 #include <set> 20 #include <utility> 21 #include <vector> 22 23 #include "source/fuzz/data_descriptor.h" 24 #include "source/fuzz/protobufs/spirvfuzz_protobufs.h" 25 #include "source/opt/constants.h" 26 27 namespace spvtools { 28 namespace fuzz { 29 30 // Keeps track of facts about the module being transformed on which the fuzzing 31 // process can depend. Some initial facts can be provided, for example about 32 // guarantees on the values of inputs to SPIR-V entry points. Transformations 33 // may then rely on these facts, can add further facts that they establish. 34 // Facts are intended to be simple properties that either cannot be deduced from 35 // the module (such as properties that are guaranteed to hold for entry point 36 // inputs), or that are established by transformations, likely to be useful for 37 // future transformations, and not completely trivial to deduce straight from 38 // the module. 39 class FactManager { 40 public: 41 FactManager(); 42 43 ~FactManager(); 44 45 // Adds all the facts from |facts|, checking them for validity with respect to 46 // |context|. Warnings about invalid facts are communicated via 47 // |message_consumer|; such facts are otherwise ignored. 48 void AddFacts(const MessageConsumer& message_consumer, 49 const protobufs::FactSequence& facts, opt::IRContext* context); 50 51 // Checks the fact for validity with respect to |context|. Returns false, 52 // with no side effects, if the fact is invalid. Otherwise adds |fact| to the 53 // fact manager. 54 bool AddFact(const protobufs::Fact& fact, opt::IRContext* context); 55 56 // Record the fact that |data1| and |data2| are synonymous. 57 void AddFactDataSynonym(const protobufs::DataDescriptor& data1, 58 const protobufs::DataDescriptor& data2, 59 opt::IRContext* context); 60 61 // Records the fact that |block_id| is dead. 62 void AddFactBlockIsDead(uint32_t block_id); 63 64 // Records the fact that |function_id| is livesafe. 65 void AddFactFunctionIsLivesafe(uint32_t function_id); 66 67 // Records the fact that the value of the pointee associated with |pointer_id| 68 // is irrelevant: it does not affect the observable behaviour of the module. 69 void AddFactValueOfPointeeIsIrrelevant(uint32_t pointer_id); 70 71 // Records the fact that |lhs_id| is defined by the equation: 72 // 73 // |lhs_id| = |opcode| |rhs_id[0]| ... |rhs_id[N-1]| 74 // 75 void AddFactIdEquation(uint32_t lhs_id, SpvOp opcode, 76 const std::vector<uint32_t>& rhs_id, 77 opt::IRContext* context); 78 79 // The fact manager is responsible for managing a few distinct categories of 80 // facts. In principle there could be different fact managers for each kind 81 // of fact, but in practice providing one 'go to' place for facts is 82 // convenient. To keep some separation, the public methods of the fact 83 // manager should be grouped according to the kind of fact to which they 84 // relate. 85 86 //============================== 87 // Querying facts about uniform constants 88 89 // Provides the distinct type ids for which at least one "constant == 90 // uniform element" fact is known. 91 std::vector<uint32_t> GetTypesForWhichUniformValuesAreKnown() const; 92 93 // Provides distinct constant ids with type |type_id| for which at least one 94 // "constant == uniform element" fact is known. If multiple identically- 95 // valued constants are relevant, only one will appear in the sequence. 96 std::vector<uint32_t> GetConstantsAvailableFromUniformsForType( 97 opt::IRContext* ir_context, uint32_t type_id) const; 98 99 // Provides details of all uniform elements that are known to be equal to the 100 // constant associated with |constant_id| in |ir_context|. 101 const std::vector<protobufs::UniformBufferElementDescriptor> 102 GetUniformDescriptorsForConstant(opt::IRContext* ir_context, 103 uint32_t constant_id) const; 104 105 // Returns the id of a constant whose value is known to match that of 106 // |uniform_descriptor|, and whose type matches the type of the uniform 107 // element. If multiple such constant is exist, the one that is returned 108 // is arbitrary. Returns 0 if no such constant id exists. 109 uint32_t GetConstantFromUniformDescriptor( 110 opt::IRContext* context, 111 const protobufs::UniformBufferElementDescriptor& uniform_descriptor) 112 const; 113 114 // Returns all "constant == uniform element" facts known to the fact 115 // manager, pairing each fact with id of the type that is associated with 116 // both the constant and the uniform element. 117 const std::vector<std::pair<protobufs::FactConstantUniform, uint32_t>>& 118 GetConstantUniformFactsAndTypes() const; 119 120 // End of uniform constant facts 121 //============================== 122 123 //============================== 124 // Querying facts about id synonyms 125 126 // Returns every id for which a fact of the form "this id is synonymous with 127 // this piece of data" is known. 128 std::vector<uint32_t> GetIdsForWhichSynonymsAreKnown( 129 opt::IRContext* context) const; 130 131 // Returns the equivalence class of all known synonyms of |id|, or an empty 132 // set if no synonyms are known. 133 std::vector<const protobufs::DataDescriptor*> GetSynonymsForId( 134 uint32_t id, opt::IRContext* context) const; 135 136 // Returns the equivalence class of all known synonyms of |data_descriptor|, 137 // or empty if no synonyms are known. 138 std::vector<const protobufs::DataDescriptor*> GetSynonymsForDataDescriptor( 139 const protobufs::DataDescriptor& data_descriptor, 140 opt::IRContext* context) const; 141 142 // Returns true if and ony if |data_descriptor1| and |data_descriptor2| are 143 // known to be synonymous. 144 bool IsSynonymous(const protobufs::DataDescriptor& data_descriptor1, 145 const protobufs::DataDescriptor& data_descriptor2, 146 opt::IRContext* context) const; 147 148 // End of id synonym facts 149 //============================== 150 151 //============================== 152 // Querying facts about dead blocks 153 154 // Returns true if and ony if |block_id| is the id of a block known to be 155 // dynamically unreachable. 156 bool BlockIsDead(uint32_t block_id) const; 157 158 // End of dead block facts 159 //============================== 160 161 //============================== 162 // Querying facts about livesafe function 163 164 // Returns true if and ony if |function_id| is the id of a function known 165 // to be livesafe. 166 bool FunctionIsLivesafe(uint32_t function_id) const; 167 168 // End of dead livesafe function facts 169 //============================== 170 171 //============================== 172 // Querying facts about pointers with irrelevant pointee values 173 174 // Returns true if and ony if the value of the pointee associated with 175 // |pointer_id| is irrelevant. 176 bool PointeeValueIsIrrelevant(uint32_t pointer_id) const; 177 178 // End of irrelevant pointee value facts 179 //============================== 180 181 private: 182 // For each distinct kind of fact to be managed, we use a separate opaque 183 // class type. 184 185 class ConstantUniformFacts; // Opaque class for management of 186 // constant uniform facts. 187 std::unique_ptr<ConstantUniformFacts> 188 uniform_constant_facts_; // Unique pointer to internal data. 189 190 class DataSynonymAndIdEquationFacts; // Opaque class for management of data 191 // synonym and id equation facts. 192 std::unique_ptr<DataSynonymAndIdEquationFacts> 193 data_synonym_and_id_equation_facts_; // Unique pointer to internal data. 194 195 class DeadBlockFacts; // Opaque class for management of dead block facts. 196 std::unique_ptr<DeadBlockFacts> 197 dead_block_facts_; // Unique pointer to internal data. 198 199 class LivesafeFunctionFacts; // Opaque class for management of livesafe 200 // function facts. 201 std::unique_ptr<LivesafeFunctionFacts> 202 livesafe_function_facts_; // Unique pointer to internal data. 203 204 class IrrelevantPointeeValueFacts; // Opaque class for management of 205 // facts about pointers whose pointee values do not matter. 206 std::unique_ptr<IrrelevantPointeeValueFacts> 207 irrelevant_pointee_value_facts_; // Unique pointer to internal data. 208 }; 209 210 } // namespace fuzz 211 } // namespace spvtools 212 213 #endif // SOURCE_FUZZ_FACT_MANAGER_H_ 214