replayer.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 <algorithm>
  16. #include <memory>
  17. #include <utility>
  18. #include "source/fuzz/counter_overflow_id_source.h"
  19. #include "source/fuzz/fact_manager/fact_manager.h"
  20. #include "source/fuzz/protobufs/spirvfuzz_protobufs.h"
  21. #include "source/fuzz/transformation.h"
  22. #include "source/fuzz/transformation_context.h"
  23. #include "source/opt/build_module.h"
  24. #include "source/util/make_unique.h"
  25. namespace spvtools {
  26. namespace fuzz {
  27. Replayer::Replayer(
  28. spv_target_env target_env, MessageConsumer consumer,
  29. const std::vector<uint32_t>& binary_in,
  30. const protobufs::FactSequence& initial_facts,
  31. const protobufs::TransformationSequence& transformation_sequence_in,
  32. uint32_t num_transformations_to_apply, bool validate_during_replay,
  33. spv_validator_options validator_options)
  34. : target_env_(target_env),
  35. consumer_(std::move(consumer)),
  36. binary_in_(binary_in),
  37. initial_facts_(initial_facts),
  38. transformation_sequence_in_(transformation_sequence_in),
  39. num_transformations_to_apply_(num_transformations_to_apply),
  40. validate_during_replay_(validate_during_replay),
  41. validator_options_(validator_options) {}
  42. Replayer::~Replayer() = default;
  43. Replayer::ReplayerResult Replayer::Run() {
  44. // Check compatibility between the library version being linked with and the
  45. // header files being used.
  46. GOOGLE_PROTOBUF_VERIFY_VERSION;
  47. if (num_transformations_to_apply_ >
  48. static_cast<uint32_t>(
  49. transformation_sequence_in_.transformation_size())) {
  50. consumer_(SPV_MSG_ERROR, nullptr, {},
  51. "The number of transformations to be replayed must not "
  52. "exceed the size of the transformation sequence.");
  53. return {Replayer::ReplayerResultStatus::kTooManyTransformationsRequested,
  54. nullptr, nullptr, protobufs::TransformationSequence()};
  55. }
  56. spvtools::SpirvTools tools(target_env_);
  57. if (!tools.IsValid()) {
  58. consumer_(SPV_MSG_ERROR, nullptr, {},
  59. "Failed to create SPIRV-Tools interface; stopping.");
  60. return {Replayer::ReplayerResultStatus::kFailedToCreateSpirvToolsInterface,
  61. nullptr, nullptr, protobufs::TransformationSequence()};
  62. }
  63. // Initial binary should be valid.
  64. if (!tools.Validate(&binary_in_[0], binary_in_.size(), validator_options_)) {
  65. consumer_(SPV_MSG_INFO, nullptr, {},
  66. "Initial binary is invalid; stopping.");
  67. return {Replayer::ReplayerResultStatus::kInitialBinaryInvalid, nullptr,
  68. nullptr, protobufs::TransformationSequence()};
  69. }
  70. // Build the module from the input binary.
  71. std::unique_ptr<opt::IRContext> ir_context =
  72. BuildModule(target_env_, consumer_, binary_in_.data(), binary_in_.size());
  73. assert(ir_context);
  74. // For replay validation, we track the last valid SPIR-V binary that was
  75. // observed. Initially this is the input binary.
  76. std::vector<uint32_t> last_valid_binary;
  77. if (validate_during_replay_) {
  78. last_valid_binary = binary_in_;
  79. }
  80. // We find the smallest id that is (a) not in use by the original module, and
  81. // (b) not used by any transformation in the sequence to be replayed. This
  82. // serves as a starting id from which to issue overflow ids if they are
  83. // required during replay.
  84. uint32_t first_overflow_id = ir_context->module()->id_bound();
  85. for (auto& transformation : transformation_sequence_in_.transformation()) {
  86. auto fresh_ids = Transformation::FromMessage(transformation)->GetFreshIds();
  87. if (!fresh_ids.empty()) {
  88. first_overflow_id =
  89. std::max(first_overflow_id,
  90. *std::max_element(fresh_ids.begin(), fresh_ids.end()) + 1);
  91. }
  92. }
  93. std::unique_ptr<TransformationContext> transformation_context =
  94. MakeUnique<TransformationContext>(
  95. MakeUnique<FactManager>(ir_context.get()), validator_options_,
  96. MakeUnique<CounterOverflowIdSource>(first_overflow_id));
  97. transformation_context->GetFactManager()->AddInitialFacts(consumer_,
  98. initial_facts_);
  99. // We track the largest id bound observed, to ensure that it only increases
  100. // as transformations are applied.
  101. uint32_t max_observed_id_bound = ir_context->module()->id_bound();
  102. (void)(max_observed_id_bound); // Keep release-mode compilers happy.
  103. protobufs::TransformationSequence transformation_sequence_out;
  104. // Consider the transformation proto messages in turn.
  105. uint32_t counter = 0;
  106. for (auto& message : transformation_sequence_in_.transformation()) {
  107. if (counter >= num_transformations_to_apply_) {
  108. break;
  109. }
  110. counter++;
  111. auto transformation = Transformation::FromMessage(message);
  112. // Check whether the transformation can be applied.
  113. if (transformation->IsApplicable(ir_context.get(),
  114. *transformation_context)) {
  115. // The transformation is applicable, so apply it, and copy it to the
  116. // sequence of transformations that were applied.
  117. transformation->Apply(ir_context.get(), transformation_context.get());
  118. *transformation_sequence_out.add_transformation() = message;
  119. assert(ir_context->module()->id_bound() >= max_observed_id_bound &&
  120. "The module's id bound should only increase due to applying "
  121. "transformations.");
  122. max_observed_id_bound = ir_context->module()->id_bound();
  123. if (validate_during_replay_) {
  124. std::vector<uint32_t> binary_to_validate;
  125. ir_context->module()->ToBinary(&binary_to_validate, false);
  126. // Check whether the latest transformation led to a valid binary.
  127. if (!tools.Validate(&binary_to_validate[0], binary_to_validate.size(),
  128. validator_options_)) {
  129. consumer_(SPV_MSG_INFO, nullptr, {},
  130. "Binary became invalid during replay (set a "
  131. "breakpoint to inspect); stopping.");
  132. return {Replayer::ReplayerResultStatus::kReplayValidationFailure,
  133. nullptr, nullptr, protobufs::TransformationSequence()};
  134. }
  135. // The binary was valid, so it becomes the latest valid binary.
  136. last_valid_binary = std::move(binary_to_validate);
  137. }
  138. }
  139. }
  140. return {Replayer::ReplayerResultStatus::kComplete, std::move(ir_context),
  141. std::move(transformation_context),
  142. std::move(transformation_sequence_out)};
  143. }
  144. } // namespace fuzz
  145. } // namespace spvtools