• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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       bool ignore_inapplicable_transformations);
34 
35   void Apply() override;
36 
37  private:
38   // Used to map a type id to the ids of relevant instructions of the type.
39   using TypeIdToInstructions =
40       std::unordered_map<uint32_t, std::vector<uint32_t>>;
41 
42   // Requires that |array_type_instruction| has opcode OpTypeArray.
43   // Attempts to find suitable instruction result ids from the values of
44   // |type_id_to_available_instructions| that would allow a composite of type
45   // |array_type_instruction| to be constructed.  Returns said ids if they can
46   // be found and an empty vector otherwise.
47   std::vector<uint32_t> FindComponentsToConstructArray(
48       const opt::Instruction& array_type_instruction,
49       const TypeIdToInstructions& type_id_to_available_instructions);
50 
51   // Similar to FindComponentsToConstructArray, but for matrices.
52   std::vector<uint32_t> FindComponentsToConstructMatrix(
53       const opt::Instruction& matrix_type_instruction,
54       const TypeIdToInstructions& type_id_to_available_instructions);
55 
56   // Similar to FindComponentsToConstructArray, but for structs.
57   std::vector<uint32_t> FindComponentsToConstructStruct(
58       const opt::Instruction& struct_type_instruction,
59       const TypeIdToInstructions& type_id_to_available_instructions);
60 
61   // Similar to FindComponentsToConstructArray, but for vectors.
62   std::vector<uint32_t> FindComponentsToConstructVector(
63       const opt::Instruction& vector_type_instruction,
64       const TypeIdToInstructions& type_id_to_available_instructions);
65 };
66 
67 }  // namespace fuzz
68 }  // namespace spvtools
69 
70 #endif  // SOURCE_FUZZ_FUZZER_PASS_CONSTRUCT_COMPOSITES_H_
71