• 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_REPLAYER_H_
16 #define SOURCE_FUZZ_REPLAYER_H_
17 
18 #include <memory>
19 #include <vector>
20 
21 #include "source/fuzz/protobufs/spirvfuzz_protobufs.h"
22 #include "source/fuzz/transformation_context.h"
23 #include "source/opt/ir_context.h"
24 #include "spirv-tools/libspirv.hpp"
25 
26 namespace spvtools {
27 namespace fuzz {
28 
29 // Transforms a SPIR-V module into a semantically equivalent SPIR-V module by
30 // applying a series of pre-defined transformations.
31 class Replayer {
32  public:
33   // Possible statuses that can result from running the replayer.
34   enum class ReplayerResultStatus {
35     kComplete,
36     kFailedToCreateSpirvToolsInterface,
37     kInitialBinaryInvalid,
38     kReplayValidationFailure,
39     kTooManyTransformationsRequested,
40   };
41 
42   struct ReplayerResult {
43     ReplayerResultStatus status;
44     std::unique_ptr<opt::IRContext> transformed_module;
45     std::unique_ptr<TransformationContext> transformation_context;
46     protobufs::TransformationSequence applied_transformations;
47   };
48 
49   Replayer(spv_target_env target_env, MessageConsumer consumer,
50            const std::vector<uint32_t>& binary_in,
51            const protobufs::FactSequence& initial_facts,
52            const protobufs::TransformationSequence& transformation_sequence_in,
53            uint32_t num_transformations_to_apply, bool validate_during_replay,
54            spv_validator_options validator_options);
55 
56   // Disables copy/move constructor/assignment operations.
57   Replayer(const Replayer&) = delete;
58   Replayer(Replayer&&) = delete;
59   Replayer& operator=(const Replayer&) = delete;
60   Replayer& operator=(Replayer&&) = delete;
61 
62   ~Replayer();
63 
64   // Attempts to apply the first |num_transformations_to_apply_| transformations
65   // from |transformation_sequence_in_| to |binary_in_|.  Initial facts about
66   // the input binary and the context in which it will execute are provided via
67   // |initial_facts_|.
68   //
69   // On success, returns a successful result status together with the
70   // transformations that were applied, the IR for the transformed module, and
71   // the transformation context that arises from applying these transformations.
72   // Otherwise, returns an appropriate result status, an empty transformation
73   // sequence, and null pointers for the IR context and transformation context.
74   ReplayerResult Run();
75 
76  private:
77   // Target environment.
78   const spv_target_env target_env_;
79 
80   // Message consumer.
81   MessageConsumer consumer_;
82 
83   // The binary to which transformations are to be applied.
84   const std::vector<uint32_t>& binary_in_;
85 
86   // Initial facts known to hold in advance of applying any transformations.
87   const protobufs::FactSequence& initial_facts_;
88 
89   // The transformations to be replayed.
90   const protobufs::TransformationSequence& transformation_sequence_in_;
91 
92   // The number of transformations that should be replayed.
93   const uint32_t num_transformations_to_apply_;
94 
95   // Controls whether the validator should be run after every replay step.
96   const bool validate_during_replay_;
97 
98   // Options to control validation
99   spv_validator_options validator_options_;
100 };
101 
102 }  // namespace fuzz
103 }  // namespace spvtools
104 
105 #endif  // SOURCE_FUZZ_REPLAYER_H_
106