replayer.cpp 5.8 KB

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