fuzzer.cpp 5.3 KB

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