2
0

spvtools_fuzz_fuzzer.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright (c) 2021 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. #include <cstdint>
  15. #include <vector>
  16. #include "source/fuzz/fuzzer.h"
  17. #include "source/fuzz/pseudo_random_generator.h"
  18. #include "spirv-tools/libspirv.hpp"
  19. #include "test/fuzzers/random_generator.h"
  20. extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
  21. if (size == 0 || (size % sizeof(uint32_t)) != 0) {
  22. // An empty binary, or a binary whose size is not a multiple of word-size,
  23. // cannot be valid, so can be rejected immediately.
  24. return 0;
  25. }
  26. std::vector<uint32_t> initial_binary(size / sizeof(uint32_t));
  27. memcpy(initial_binary.data(), data, size);
  28. spvtools::ValidatorOptions validator_options;
  29. spvtools::MessageConsumer message_consumer =
  30. [](spv_message_level_t, const char*, const spv_position_t&, const char*) {
  31. };
  32. spvtools::fuzzers::RandomGenerator random_gen(data, size);
  33. auto target_env = random_gen.GetTargetEnv();
  34. std::unique_ptr<spvtools::opt::IRContext> ir_context;
  35. if (!spvtools::fuzz::fuzzerutil::BuildIRContext(
  36. target_env, message_consumer, initial_binary, validator_options,
  37. &ir_context)) {
  38. // The input is invalid - give up.
  39. return 0;
  40. }
  41. std::vector<spvtools::fuzz::fuzzerutil::ModuleSupplier> donor_suppliers = {
  42. [&initial_binary, message_consumer, target_env,
  43. &validator_options]() -> std::unique_ptr<spvtools::opt::IRContext> {
  44. std::unique_ptr<spvtools::opt::IRContext> result;
  45. if (!spvtools::fuzz::fuzzerutil::BuildIRContext(
  46. target_env, message_consumer, initial_binary, validator_options,
  47. &result)) {
  48. // The input was successfully parsed and validated first time around,
  49. // so something is wrong if it is now invalid.
  50. abort();
  51. }
  52. return result;
  53. }};
  54. uint32_t seed = random_gen.GetUInt32(std::numeric_limits<uint32_t>::max());
  55. auto fuzzer_context = spvtools::MakeUnique<spvtools::fuzz::FuzzerContext>(
  56. spvtools::MakeUnique<spvtools::fuzz::PseudoRandomGenerator>(seed),
  57. spvtools::fuzz::FuzzerContext::GetMinFreshId(ir_context.get()), false);
  58. auto transformation_context =
  59. spvtools::MakeUnique<spvtools::fuzz::TransformationContext>(
  60. spvtools::MakeUnique<spvtools::fuzz::FactManager>(ir_context.get()),
  61. validator_options);
  62. spvtools::fuzz::Fuzzer fuzzer(
  63. std::move(ir_context), std::move(transformation_context),
  64. std::move(fuzzer_context), message_consumer, donor_suppliers, false,
  65. spvtools::fuzz::RepeatedPassStrategy::kLoopedWithRecommendations, true,
  66. validator_options);
  67. fuzzer.Run(0);
  68. return 0;
  69. }