spvtools_opt_fuzzer_common.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 "test/fuzzers/spvtools_opt_fuzzer_common.h"
  15. #include "source/opt/build_module.h"
  16. #include "test/fuzzers/random_generator.h"
  17. namespace spvtools {
  18. namespace fuzzers {
  19. int OptFuzzerTestOneInput(
  20. const uint8_t* data, size_t size,
  21. const std::function<void(spvtools::Optimizer&)>& register_passes) {
  22. if (size < 1) {
  23. return 0;
  24. }
  25. spvtools::fuzzers::RandomGenerator random_gen(data, size);
  26. auto target_env = random_gen.GetTargetEnv();
  27. spvtools::Optimizer optimizer(target_env);
  28. optimizer.SetMessageConsumer([](spv_message_level_t, const char*,
  29. const spv_position_t&, const char*) {});
  30. std::vector<uint32_t> input;
  31. input.resize(size >> 2);
  32. size_t count = 0;
  33. for (size_t i = 0; (i + 3) < size; i += 4) {
  34. input[count++] = data[i] | (data[i + 1] << 8) | (data[i + 2] << 16) |
  35. (data[i + 3]) << 24;
  36. }
  37. // The largest possible id bound is used when running the optimizer, to avoid
  38. // the problem of id overflows.
  39. const size_t kFinalIdLimit = UINT32_MAX;
  40. // The input is scanned to check that it does not already use an id too close
  41. // to this limit. This still gives the optimizer a large set of ids to
  42. // consume. It is thus very unlikely that id overflow will occur during
  43. // fuzzing. If it does, then the initial id limit should be decreased.
  44. const size_t kInitialIdLimit = kFinalIdLimit - 1000000U;
  45. // Build the module and scan it to check that all used ids are below the
  46. // initial limit.
  47. auto ir_context =
  48. spvtools::BuildModule(target_env, nullptr, input.data(), input.size());
  49. if (ir_context == nullptr) {
  50. // It was not possible to build a valid module; that's OK - skip this input.
  51. return 0;
  52. }
  53. if (ir_context->module()->id_bound() >= kInitialIdLimit) {
  54. // The input already has a very large id bound. The input is thus abandoned,
  55. // to avoid the possibility of ending up hitting the id bound limit.
  56. return 0;
  57. }
  58. // Set the optimizer and its validator up with the largest possible id bound
  59. // limit.
  60. spvtools::ValidatorOptions validator_options;
  61. spvtools::OptimizerOptions optimizer_options;
  62. optimizer_options.set_max_id_bound(kFinalIdLimit);
  63. validator_options.SetUniversalLimit(spv_validator_limit_max_id_bound,
  64. kFinalIdLimit);
  65. optimizer_options.set_validator_options(validator_options);
  66. register_passes(optimizer);
  67. optimizer.Run(input.data(), input.size(), &input, optimizer_options);
  68. return 0;
  69. }
  70. } // namespace fuzzers
  71. } // namespace spvtools