fuzzer.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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/fuzzer_util.h"
  19. #include "source/fuzz/protobufs/spirvfuzz_protobufs.h"
  20. #include "spirv-tools/libspirv.hpp"
  21. namespace spvtools {
  22. namespace fuzz {
  23. // Transforms a SPIR-V module into a semantically equivalent SPIR-V module by
  24. // running a number of randomized fuzzer passes.
  25. class Fuzzer {
  26. public:
  27. // Possible statuses that can result from running the fuzzer.
  28. enum class FuzzerResultStatus {
  29. kComplete,
  30. kFailedToCreateSpirvToolsInterface,
  31. kFuzzerPassLedToInvalidModule,
  32. kInitialBinaryInvalid,
  33. };
  34. // Constructs a fuzzer from the given target environment |env|. |seed| is a
  35. // seed for pseudo-random number generation.
  36. // |validate_after_each_fuzzer_pass| controls whether the validator will be
  37. // invoked after every fuzzer pass is applied.
  38. Fuzzer(spv_target_env env, uint32_t seed,
  39. bool validate_after_each_fuzzer_pass,
  40. spv_validator_options validator_options);
  41. // Disables copy/move constructor/assignment operations.
  42. Fuzzer(const Fuzzer&) = delete;
  43. Fuzzer(Fuzzer&&) = delete;
  44. Fuzzer& operator=(const Fuzzer&) = delete;
  45. Fuzzer& operator=(Fuzzer&&) = delete;
  46. ~Fuzzer();
  47. // Sets the message consumer to the given |consumer|. The |consumer| will be
  48. // invoked once for each message communicated from the library.
  49. void SetMessageConsumer(MessageConsumer consumer);
  50. // Transforms |binary_in| to |binary_out| by running a number of randomized
  51. // fuzzer passes. Initial facts about the input binary and the context in
  52. // which it will execute are provided via |initial_facts|. A source of donor
  53. // modules to be used by transformations is provided via |donor_suppliers|.
  54. // The transformation sequence that was applied is returned via
  55. // |transformation_sequence_out|.
  56. FuzzerResultStatus Run(
  57. const std::vector<uint32_t>& binary_in,
  58. const protobufs::FactSequence& initial_facts,
  59. const std::vector<fuzzerutil::ModuleSupplier>& donor_suppliers,
  60. std::vector<uint32_t>* binary_out,
  61. protobufs::TransformationSequence* transformation_sequence_out) const;
  62. private:
  63. struct Impl; // Opaque struct for holding internal data.
  64. std::unique_ptr<Impl> impl_; // Unique pointer to internal data.
  65. };
  66. } // namespace fuzz
  67. } // namespace spvtools
  68. #endif // SOURCE_FUZZ_FUZZER_H_