replayer.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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, bool validate)
  36. : target_env(env), validate_during_replay(validate) {}
  37. const spv_target_env target_env; // Target environment.
  38. MessageConsumer consumer; // Message consumer.
  39. const bool validate_during_replay; // Controls whether the validator should
  40. // be run after every replay step.
  41. };
  42. Replayer::Replayer(spv_target_env env, bool validate_during_replay)
  43. : impl_(MakeUnique<Impl>(env, validate_during_replay)) {}
  44. Replayer::~Replayer() = default;
  45. void Replayer::SetMessageConsumer(MessageConsumer c) {
  46. impl_->consumer = std::move(c);
  47. }
  48. Replayer::ReplayerResultStatus Replayer::Run(
  49. const std::vector<uint32_t>& binary_in,
  50. const protobufs::FactSequence& initial_facts,
  51. const protobufs::TransformationSequence& transformation_sequence_in,
  52. std::vector<uint32_t>* binary_out,
  53. protobufs::TransformationSequence* transformation_sequence_out) const {
  54. // Check compatibility between the library version being linked with and the
  55. // header files being used.
  56. GOOGLE_PROTOBUF_VERIFY_VERSION;
  57. spvtools::SpirvTools tools(impl_->target_env);
  58. if (!tools.IsValid()) {
  59. impl_->consumer(SPV_MSG_ERROR, nullptr, {},
  60. "Failed to create SPIRV-Tools interface; stopping.");
  61. return Replayer::ReplayerResultStatus::kFailedToCreateSpirvToolsInterface;
  62. }
  63. // Initial binary should be valid.
  64. if (!tools.Validate(&binary_in[0], binary_in.size())) {
  65. impl_->consumer(SPV_MSG_INFO, nullptr, {},
  66. "Initial binary is invalid; stopping.");
  67. return Replayer::ReplayerResultStatus::kInitialBinaryInvalid;
  68. }
  69. // Build the module from the input binary.
  70. std::unique_ptr<opt::IRContext> ir_context = BuildModule(
  71. impl_->target_env, impl_->consumer, binary_in.data(), binary_in.size());
  72. assert(ir_context);
  73. // For replay validation, we track the last valid SPIR-V binary that was
  74. // observed. Initially this is the input binary.
  75. std::vector<uint32_t> last_valid_binary;
  76. if (impl_->validate_during_replay) {
  77. last_valid_binary = binary_in;
  78. }
  79. FactManager fact_manager;
  80. fact_manager.AddFacts(impl_->consumer, initial_facts, ir_context.get());
  81. // Consider the transformation proto messages in turn.
  82. for (auto& message : transformation_sequence_in.transformation()) {
  83. auto transformation = Transformation::FromMessage(message);
  84. // Check whether the transformation can be applied.
  85. if (transformation->IsApplicable(ir_context.get(), fact_manager)) {
  86. // The transformation is applicable, so apply it, and copy it to the
  87. // sequence of transformations that were applied.
  88. transformation->Apply(ir_context.get(), &fact_manager);
  89. *transformation_sequence_out->add_transformation() = message;
  90. if (impl_->validate_during_replay) {
  91. std::vector<uint32_t> binary_to_validate;
  92. ir_context->module()->ToBinary(&binary_to_validate, false);
  93. // Check whether the latest transformation led to a valid binary.
  94. if (!tools.Validate(&binary_to_validate[0],
  95. binary_to_validate.size())) {
  96. impl_->consumer(SPV_MSG_INFO, nullptr, {},
  97. "Binary became invalid during replay (set a "
  98. "breakpoint to inspect); stopping.");
  99. return Replayer::ReplayerResultStatus::kReplayValidationFailure;
  100. }
  101. // The binary was valid, so it becomes the latest valid binary.
  102. last_valid_binary = std::move(binary_to_validate);
  103. }
  104. }
  105. }
  106. // Write out the module as a binary.
  107. ir_context->module()->ToBinary(binary_out, false);
  108. return Replayer::ReplayerResultStatus::kComplete;
  109. }
  110. } // namespace fuzz
  111. } // namespace spvtools