fuzzer.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. #ifndef SOURCE_FUZZ_FUZZER_H_
  15. #define SOURCE_FUZZ_FUZZER_H_
  16. #include <memory>
  17. #include <vector>
  18. #include "source/fuzz/protobufs/spirvfuzz_protobufs.h"
  19. #include "spirv-tools/libspirv.hpp"
  20. namespace spvtools {
  21. namespace fuzz {
  22. // Transforms a SPIR-V module into a semantically equivalent SPIR-V module by
  23. // running a number of randomized fuzzer passes.
  24. class Fuzzer {
  25. public:
  26. // Possible statuses that can result from running the fuzzer.
  27. enum class FuzzerResultStatus {
  28. kComplete,
  29. kFailedToCreateSpirvToolsInterface,
  30. kInitialBinaryInvalid,
  31. };
  32. // Constructs a fuzzer from the given target environment.
  33. explicit Fuzzer(spv_target_env env);
  34. // Disables copy/move constructor/assignment operations.
  35. Fuzzer(const Fuzzer&) = delete;
  36. Fuzzer(Fuzzer&&) = delete;
  37. Fuzzer& operator=(const Fuzzer&) = delete;
  38. Fuzzer& operator=(Fuzzer&&) = delete;
  39. ~Fuzzer();
  40. // Sets the message consumer to the given |consumer|. The |consumer| will be
  41. // invoked once for each message communicated from the library.
  42. void SetMessageConsumer(MessageConsumer consumer);
  43. // Transforms |binary_in| to |binary_out| by running a number of randomized
  44. // fuzzer passes, controlled via |options|. Initial facts about the input
  45. // binary and the context in which it will execute are provided via
  46. // |initial_facts|. The transformation sequence that was applied is returned
  47. // via |transformation_sequence_out|.
  48. FuzzerResultStatus Run(
  49. const std::vector<uint32_t>& binary_in,
  50. const protobufs::FactSequence& initial_facts,
  51. spv_const_fuzzer_options options, std::vector<uint32_t>* binary_out,
  52. protobufs::TransformationSequence* transformation_sequence_out) const;
  53. private:
  54. struct Impl; // Opaque struct for holding internal data.
  55. std::unique_ptr<Impl> impl_; // Unique pointer to internal data.
  56. };
  57. } // namespace fuzz
  58. } // namespace spvtools
  59. #endif // SOURCE_FUZZ_FUZZER_H_