replayer.h 3.6 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. #ifndef SOURCE_FUZZ_REPLAYER_H_
  15. #define SOURCE_FUZZ_REPLAYER_H_
  16. #include <memory>
  17. #include <vector>
  18. #include "source/fuzz/protobufs/spirvfuzz_protobufs.h"
  19. #include "source/fuzz/transformation_context.h"
  20. #include "source/opt/ir_context.h"
  21. #include "spirv-tools/libspirv.hpp"
  22. namespace spvtools {
  23. namespace fuzz {
  24. // Transforms a SPIR-V module into a semantically equivalent SPIR-V module by
  25. // applying a series of pre-defined transformations.
  26. class Replayer {
  27. public:
  28. // Possible statuses that can result from running the replayer.
  29. enum class ReplayerResultStatus {
  30. kComplete,
  31. kFailedToCreateSpirvToolsInterface,
  32. kInitialBinaryInvalid,
  33. kReplayValidationFailure,
  34. kTooManyTransformationsRequested,
  35. };
  36. struct ReplayerResult {
  37. ReplayerResultStatus status;
  38. std::unique_ptr<opt::IRContext> transformed_module;
  39. std::unique_ptr<TransformationContext> transformation_context;
  40. protobufs::TransformationSequence applied_transformations;
  41. };
  42. Replayer(spv_target_env target_env, MessageConsumer consumer,
  43. const std::vector<uint32_t>& binary_in,
  44. const protobufs::FactSequence& initial_facts,
  45. const protobufs::TransformationSequence& transformation_sequence_in,
  46. uint32_t num_transformations_to_apply, bool validate_during_replay,
  47. spv_validator_options validator_options);
  48. // Disables copy/move constructor/assignment operations.
  49. Replayer(const Replayer&) = delete;
  50. Replayer(Replayer&&) = delete;
  51. Replayer& operator=(const Replayer&) = delete;
  52. Replayer& operator=(Replayer&&) = delete;
  53. ~Replayer();
  54. // Attempts to apply the first |num_transformations_to_apply_| transformations
  55. // from |transformation_sequence_in_| to |binary_in_|. Initial facts about
  56. // the input binary and the context in which it will execute are provided via
  57. // |initial_facts_|.
  58. //
  59. // On success, returns a successful result status together with the
  60. // transformations that were applied, the IR for the transformed module, and
  61. // the transformation context that arises from applying these transformations.
  62. // Otherwise, returns an appropriate result status, an empty transformation
  63. // sequence, and null pointers for the IR context and transformation context.
  64. ReplayerResult Run();
  65. private:
  66. // Target environment.
  67. const spv_target_env target_env_;
  68. // Message consumer.
  69. MessageConsumer consumer_;
  70. // The binary to which transformations are to be applied.
  71. const std::vector<uint32_t>& binary_in_;
  72. // Initial facts known to hold in advance of applying any transformations.
  73. const protobufs::FactSequence& initial_facts_;
  74. // The transformations to be replayed.
  75. const protobufs::TransformationSequence& transformation_sequence_in_;
  76. // The number of transformations that should be replayed.
  77. const uint32_t num_transformations_to_apply_;
  78. // Controls whether the validator should be run after every replay step.
  79. const bool validate_during_replay_;
  80. // Options to control validation
  81. spv_validator_options validator_options_;
  82. };
  83. } // namespace fuzz
  84. } // namespace spvtools
  85. #endif // SOURCE_FUZZ_REPLAYER_H_