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 explicit Replayer(spv_target_env env, bool validate_during_replay); 41 42 // Disables copy/move constructor/assignment operations. 43 Replayer(const Replayer&) = delete; 44 Replayer(Replayer&&) = delete; 45 Replayer& operator=(const Replayer&) = delete; 46 Replayer& operator=(Replayer&&) = delete; 47 48 ~Replayer(); 49 50 // Sets the message consumer to the given |consumer|. The |consumer| will be 51 // invoked once for each message communicated from the library. 52 void SetMessageConsumer(MessageConsumer consumer); 53 54 // Transforms |binary_in| to |binary_out| by attempting to apply the 55 // transformations from |transformation_sequence_in|. Initial facts about the 56 // input binary and the context in which it will execute are provided via 57 // |initial_facts|. The transformations that were successfully applied are 58 // returned via |transformation_sequence_out|. 59 ReplayerResultStatus Run( 60 const std::vector<uint32_t>& binary_in, 61 const protobufs::FactSequence& initial_facts, 62 const protobufs::TransformationSequence& transformation_sequence_in, 63 std::vector<uint32_t>* binary_out, 64 protobufs::TransformationSequence* transformation_sequence_out) const; 65 66 private: 67 struct Impl; // Opaque struct for holding internal data. 68 std::unique_ptr<Impl> impl_; // Unique pointer to internal data. 69 }; 70 71 } // namespace fuzz 72 } // namespace spvtools 73 74 #endif // SOURCE_FUZZ_REPLAYER_H_ 75