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 <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   ~FuzzerPassConstructComposites();
35 
36   void Apply() override;
37 
38  private:
39   // Used to map a type id to relevant instructions whose result type matches
40   // the type id.
41   typedef std::map<uint32_t, std::vector<opt::Instruction*>>
42       TypeIdToInstructions;
43 
44   // Considers all instructions that are available at |inst| - instructions
45   // whose results could be packed into a composite - and updates
46   // |type_id_to_available_instructions| so that each such instruction is
47   // associated with its the id of its result type.
48   void RecordAvailableInstruction(
49       opt::Instruction* inst,
50       TypeIdToInstructions* type_id_to_available_instructions);
51 
52   // Requires that |array_type_instruction| has opcode OpTypeArray.
53   // Attempts to find suitable instruction result ids from the values of
54   // |type_id_to_available_instructions| that would allow a composite of type
55   // |array_type_instruction| to be constructed.  Returns said ids if they can
56   // be found and an empty vector otherwise.
57   std::vector<uint32_t> FindComponentsToConstructArray(
58       const opt::Instruction& array_type_instruction,
59       const TypeIdToInstructions& type_id_to_available_instructions);
60 
61   // Similar to FindComponentsToConstructArray, but for matrices.
62   std::vector<uint32_t> FindComponentsToConstructMatrix(
63       const opt::Instruction& matrix_type_instruction,
64       const TypeIdToInstructions& type_id_to_available_instructions);
65 
66   // Similar to FindComponentsToConstructArray, but for structs.
67   std::vector<uint32_t> FindComponentsToConstructStruct(
68       const opt::Instruction& struct_type_instruction,
69       const TypeIdToInstructions& type_id_to_available_instructions);
70 
71   // Similar to FindComponentsToConstructArray, but for vectors.
72   std::vector<uint32_t> FindComponentsToConstructVector(
73       const opt::Instruction& vector_type_instruction,
74       const TypeIdToInstructions& type_id_to_available_instructions);
75 };
76 
77 }  // namespace fuzz
78 }  // namespace spvtools
79 
80 #endif  // SOURCE_FUZZ_FUZZER_PASS_CONSTRUCT_COMPOSITES_H_
81