fuzzer.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #include "source/fuzz/fuzzer.h"
  15. #include <cassert>
  16. #include <sstream>
  17. #include "source/fuzz/fact_manager.h"
  18. #include "source/fuzz/fuzzer_context.h"
  19. #include "source/fuzz/fuzzer_pass_add_dead_breaks.h"
  20. #include "source/fuzz/fuzzer_pass_add_dead_continues.h"
  21. #include "source/fuzz/fuzzer_pass_add_useful_constructs.h"
  22. #include "source/fuzz/fuzzer_pass_obfuscate_constants.h"
  23. #include "source/fuzz/fuzzer_pass_permute_blocks.h"
  24. #include "source/fuzz/fuzzer_pass_split_blocks.h"
  25. #include "source/fuzz/protobufs/spirvfuzz_protobufs.h"
  26. #include "source/fuzz/pseudo_random_generator.h"
  27. #include "source/opt/build_module.h"
  28. #include "source/spirv_fuzzer_options.h"
  29. #include "source/util/make_unique.h"
  30. namespace spvtools {
  31. namespace fuzz {
  32. namespace {
  33. const uint32_t kIdBoundGap = 100;
  34. }
  35. struct Fuzzer::Impl {
  36. explicit Impl(spv_target_env env) : target_env(env) {}
  37. const spv_target_env target_env; // Target environment.
  38. MessageConsumer consumer; // Message consumer.
  39. };
  40. Fuzzer::Fuzzer(spv_target_env env) : impl_(MakeUnique<Impl>(env)) {}
  41. Fuzzer::~Fuzzer() = default;
  42. void Fuzzer::SetMessageConsumer(MessageConsumer c) {
  43. impl_->consumer = std::move(c);
  44. }
  45. Fuzzer::FuzzerResultStatus Fuzzer::Run(
  46. const std::vector<uint32_t>& binary_in,
  47. const protobufs::FactSequence& initial_facts,
  48. spv_const_fuzzer_options options, std::vector<uint32_t>* binary_out,
  49. protobufs::TransformationSequence* transformation_sequence_out) const {
  50. // Check compatibility between the library version being linked with and the
  51. // header files being used.
  52. GOOGLE_PROTOBUF_VERIFY_VERSION;
  53. spvtools::SpirvTools tools(impl_->target_env);
  54. tools.SetMessageConsumer(impl_->consumer);
  55. if (!tools.IsValid()) {
  56. impl_->consumer(SPV_MSG_ERROR, nullptr, {},
  57. "Failed to create SPIRV-Tools interface; stopping.");
  58. return Fuzzer::FuzzerResultStatus::kFailedToCreateSpirvToolsInterface;
  59. }
  60. // Initial binary should be valid.
  61. if (!tools.Validate(&binary_in[0], binary_in.size())) {
  62. impl_->consumer(SPV_MSG_ERROR, nullptr, {},
  63. "Initial binary is invalid; stopping.");
  64. return Fuzzer::FuzzerResultStatus::kInitialBinaryInvalid;
  65. }
  66. // Build the module from the input binary.
  67. std::unique_ptr<opt::IRContext> ir_context = BuildModule(
  68. impl_->target_env, impl_->consumer, binary_in.data(), binary_in.size());
  69. assert(ir_context);
  70. // Make a PRNG, either from a given seed or from a random device.
  71. PseudoRandomGenerator random_generator(
  72. options->has_random_seed ? options->random_seed
  73. : static_cast<uint32_t>(std::random_device()()));
  74. // The fuzzer will introduce new ids into the module. The module's id bound
  75. // gives the smallest id that can be used for this purpose. We add an offset
  76. // to this so that there is a sizeable gap between the ids used in the
  77. // original module and the ids used for fuzzing, as a readability aid.
  78. //
  79. // TODO(https://github.com/KhronosGroup/SPIRV-Tools/issues/2541) consider the
  80. // case where the maximum id bound is reached.
  81. auto minimum_fresh_id = ir_context->module()->id_bound() + kIdBoundGap;
  82. FuzzerContext fuzzer_context(&random_generator, minimum_fresh_id);
  83. FactManager fact_manager;
  84. fact_manager.AddFacts(impl_->consumer, initial_facts, ir_context.get());
  85. // Add some essential ingredients to the module if they are not already
  86. // present, such as boolean constants.
  87. FuzzerPassAddUsefulConstructs(ir_context.get(), &fact_manager,
  88. &fuzzer_context, transformation_sequence_out)
  89. .Apply();
  90. // Apply some semantics-preserving passes.
  91. FuzzerPassSplitBlocks(ir_context.get(), &fact_manager, &fuzzer_context,
  92. transformation_sequence_out)
  93. .Apply();
  94. FuzzerPassAddDeadBreaks(ir_context.get(), &fact_manager, &fuzzer_context,
  95. transformation_sequence_out)
  96. .Apply();
  97. FuzzerPassAddDeadContinues(ir_context.get(), &fact_manager, &fuzzer_context,
  98. transformation_sequence_out)
  99. .Apply();
  100. FuzzerPassObfuscateConstants(ir_context.get(), &fact_manager, &fuzzer_context,
  101. transformation_sequence_out)
  102. .Apply();
  103. // Finally, give the blocks in the module a good shake-up.
  104. FuzzerPassPermuteBlocks(ir_context.get(), &fact_manager, &fuzzer_context,
  105. transformation_sequence_out)
  106. .Apply();
  107. // Encode the module as a binary.
  108. ir_context->module()->ToBinary(binary_out, false);
  109. return Fuzzer::FuzzerResultStatus::kComplete;
  110. }
  111. } // namespace fuzz
  112. } // namespace spvtools