replayer.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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/replayer.h"
  15. #include <utility>
  16. #include "source/fuzz/fact_manager.h"
  17. #include "source/fuzz/protobufs/spirvfuzz_protobufs.h"
  18. #include "source/fuzz/transformation.h"
  19. #include "source/fuzz/transformation_add_constant_boolean.h"
  20. #include "source/fuzz/transformation_add_constant_scalar.h"
  21. #include "source/fuzz/transformation_add_dead_break.h"
  22. #include "source/fuzz/transformation_add_type_boolean.h"
  23. #include "source/fuzz/transformation_add_type_float.h"
  24. #include "source/fuzz/transformation_add_type_int.h"
  25. #include "source/fuzz/transformation_add_type_pointer.h"
  26. #include "source/fuzz/transformation_move_block_down.h"
  27. #include "source/fuzz/transformation_replace_boolean_constant_with_constant_binary.h"
  28. #include "source/fuzz/transformation_replace_constant_with_uniform.h"
  29. #include "source/fuzz/transformation_split_block.h"
  30. #include "source/opt/build_module.h"
  31. #include "source/util/make_unique.h"
  32. namespace spvtools {
  33. namespace fuzz {
  34. struct Replayer::Impl {
  35. explicit Impl(spv_target_env env) : target_env(env) {}
  36. const spv_target_env target_env; // Target environment.
  37. MessageConsumer consumer; // Message consumer.
  38. };
  39. Replayer::Replayer(spv_target_env env) : impl_(MakeUnique<Impl>(env)) {}
  40. Replayer::~Replayer() = default;
  41. void Replayer::SetMessageConsumer(MessageConsumer c) {
  42. impl_->consumer = std::move(c);
  43. }
  44. Replayer::ReplayerResultStatus Replayer::Run(
  45. const std::vector<uint32_t>& binary_in,
  46. const protobufs::FactSequence& initial_facts,
  47. const protobufs::TransformationSequence& transformation_sequence_in,
  48. 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. if (!tools.IsValid()) {
  55. impl_->consumer(SPV_MSG_ERROR, nullptr, {},
  56. "Failed to create SPIRV-Tools interface; stopping.");
  57. return Replayer::ReplayerResultStatus::kFailedToCreateSpirvToolsInterface;
  58. }
  59. // Initial binary should be valid.
  60. if (!tools.Validate(&binary_in[0], binary_in.size())) {
  61. impl_->consumer(SPV_MSG_INFO, nullptr, {},
  62. "Initial binary is invalid; stopping.");
  63. return Replayer::ReplayerResultStatus::kInitialBinaryInvalid;
  64. }
  65. // Build the module from the input binary.
  66. std::unique_ptr<opt::IRContext> ir_context = BuildModule(
  67. impl_->target_env, impl_->consumer, binary_in.data(), binary_in.size());
  68. assert(ir_context);
  69. FactManager fact_manager;
  70. fact_manager.AddFacts(impl_->consumer, initial_facts, ir_context.get());
  71. // Consider the transformation proto messages in turn.
  72. for (auto& message : transformation_sequence_in.transformation()) {
  73. auto transformation = Transformation::FromMessage(message);
  74. // Check whether the transformation can be applied.
  75. if (transformation->IsApplicable(ir_context.get(), fact_manager)) {
  76. // The transformation is applicable, so apply it, and copy it to the
  77. // sequence of transformations that were applied.
  78. transformation->Apply(ir_context.get(), &fact_manager);
  79. *transformation_sequence_out->add_transformation() = message;
  80. }
  81. }
  82. // Write out the module as a binary.
  83. ir_context->module()->ToBinary(binary_out, false);
  84. return Replayer::ReplayerResultStatus::kComplete;
  85. }
  86. } // namespace fuzz
  87. } // namespace spvtools