• 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 "spirv-tools/libspirv.hpp"
23 
24 namespace spvtools {
25 namespace fuzz {
26 
27 // Transforms a SPIR-V module into a semantically equivalent SPIR-V module by
28 // applying a series of pre-defined transformations.
29 class Replayer {
30  public:
31   // Possible statuses that can result from running the replayer.
32   enum ReplayerResultStatus {
33     kComplete,
34     kFailedToCreateSpirvToolsInterface,
35     kInitialBinaryInvalid,
36     kReplayValidationFailure,
37   };
38 
39   // Constructs a replayer from the given target environment.
40   Replayer(spv_target_env env, bool validate_during_replay,
41            spv_validator_options validator_options);
42 
43   // Disables copy/move constructor/assignment operations.
44   Replayer(const Replayer&) = delete;
45   Replayer(Replayer&&) = delete;
46   Replayer& operator=(const Replayer&) = delete;
47   Replayer& operator=(Replayer&&) = delete;
48 
49   ~Replayer();
50 
51   // Sets the message consumer to the given |consumer|. The |consumer| will be
52   // invoked once for each message communicated from the library.
53   void SetMessageConsumer(MessageConsumer consumer);
54 
55   // Transforms |binary_in| to |binary_out| by attempting to apply the
56   // transformations from |transformation_sequence_in|.  Initial facts about the
57   // input binary and the context in which it will execute are provided via
58   // |initial_facts|.  The transformations that were successfully applied are
59   // returned via |transformation_sequence_out|.
60   ReplayerResultStatus Run(
61       const std::vector<uint32_t>& binary_in,
62       const protobufs::FactSequence& initial_facts,
63       const protobufs::TransformationSequence& transformation_sequence_in,
64       std::vector<uint32_t>* binary_out,
65       protobufs::TransformationSequence* transformation_sequence_out) const;
66 
67  private:
68   struct Impl;                  // Opaque struct for holding internal data.
69   std::unique_ptr<Impl> impl_;  // Unique pointer to internal data.
70 };
71 
72 }  // namespace fuzz
73 }  // namespace spvtools
74 
75 #endif  // SOURCE_FUZZ_REPLAYER_H_
76