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_CONSTRUCT_COMPOSITES_H_ 16 #define SOURCE_FUZZ_FUZZER_PASS_CONSTRUCT_COMPOSITES_H_ 17 18 #include <unordered_map> 19 #include <vector> 20 21 #include "source/fuzz/fuzzer_pass.h" 22 23 namespace spvtools { 24 namespace fuzz { 25 26 // A fuzzer pass for constructing composite objects from smaller objects. 27 class FuzzerPassConstructComposites : public FuzzerPass { 28 public: 29 FuzzerPassConstructComposites( 30 opt::IRContext* ir_context, TransformationContext* transformation_context, 31 FuzzerContext* fuzzer_context, 32 protobufs::TransformationSequence* transformations); 33 34 void Apply() override; 35 36 private: 37 // Used to map a type id to the ids of relevant instructions of the type. 38 using TypeIdToInstructions = 39 std::unordered_map<uint32_t, std::vector<uint32_t>>; 40 41 // Requires that |array_type_instruction| has opcode OpTypeArray. 42 // Attempts to find suitable instruction result ids from the values of 43 // |type_id_to_available_instructions| that would allow a composite of type 44 // |array_type_instruction| to be constructed. Returns said ids if they can 45 // be found and an empty vector otherwise. 46 std::vector<uint32_t> FindComponentsToConstructArray( 47 const opt::Instruction& array_type_instruction, 48 const TypeIdToInstructions& type_id_to_available_instructions); 49 50 // Similar to FindComponentsToConstructArray, but for matrices. 51 std::vector<uint32_t> FindComponentsToConstructMatrix( 52 const opt::Instruction& matrix_type_instruction, 53 const TypeIdToInstructions& type_id_to_available_instructions); 54 55 // Similar to FindComponentsToConstructArray, but for structs. 56 std::vector<uint32_t> FindComponentsToConstructStruct( 57 const opt::Instruction& struct_type_instruction, 58 const TypeIdToInstructions& type_id_to_available_instructions); 59 60 // Similar to FindComponentsToConstructArray, but for vectors. 61 std::vector<uint32_t> FindComponentsToConstructVector( 62 const opt::Instruction& vector_type_instruction, 63 const TypeIdToInstructions& type_id_to_available_instructions); 64 }; 65 66 } // namespace fuzz 67 } // namespace spvtools 68 69 #endif // SOURCE_FUZZ_FUZZER_PASS_CONSTRUCT_COMPOSITES_H_ 70