fuzz_test_util.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 TEST_FUZZ_FUZZ_TEST_UTIL_H_
  15. #define TEST_FUZZ_FUZZ_TEST_UTIL_H_
  16. #include <vector>
  17. #include "source/fuzz/protobufs/spirvfuzz_protobufs.h"
  18. #include "source/fuzz/transformation.h"
  19. #include "source/fuzz/transformation_context.h"
  20. #include "source/opt/build_module.h"
  21. #include "source/opt/ir_context.h"
  22. #include "spirv-tools/libspirv.h"
  23. namespace spvtools {
  24. namespace fuzz {
  25. extern const spvtools::MessageConsumer kConsoleMessageConsumer;
  26. // Returns true if and only if the given binaries are bit-wise equal.
  27. bool IsEqual(spv_target_env env, const std::vector<uint32_t>& expected_binary,
  28. const std::vector<uint32_t>& actual_binary);
  29. // Assembles the given text and returns true if and only if the resulting binary
  30. // is bit-wise equal to the given binary.
  31. bool IsEqual(spv_target_env env, const std::string& expected_text,
  32. const std::vector<uint32_t>& actual_binary);
  33. // Assembles the given text and turns the given IR into binary, then returns
  34. // true if and only if the resulting binaries are bit-wise equal.
  35. bool IsEqual(spv_target_env env, const std::string& expected_text,
  36. const opt::IRContext* actual_ir);
  37. // Turns the given IRs into binaries, then returns true if and only if the
  38. // resulting binaries are bit-wise equal.
  39. bool IsEqual(spv_target_env env, const opt::IRContext* ir_1,
  40. const opt::IRContext* ir_2);
  41. // Turns |ir_2| into a binary, then returns true if and only if the resulting
  42. // binary is bit-wise equal to |binary_1|.
  43. bool IsEqual(spv_target_env env, const std::vector<uint32_t>& binary_1,
  44. const opt::IRContext* ir_2);
  45. // Assembles the given IR context, then returns its disassembly as a string.
  46. // Useful for debugging.
  47. std::string ToString(spv_target_env env, const opt::IRContext* ir);
  48. // Returns the disassembly of the given binary as a string.
  49. // Useful for debugging.
  50. std::string ToString(spv_target_env env, const std::vector<uint32_t>& binary);
  51. // Assembly options for writing fuzzer tests. It simplifies matters if
  52. // numeric ids do not change.
  53. const uint32_t kFuzzAssembleOption =
  54. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS;
  55. // Disassembly options for writing fuzzer tests.
  56. const uint32_t kFuzzDisassembleOption =
  57. SPV_BINARY_TO_TEXT_OPTION_NO_HEADER | SPV_BINARY_TO_TEXT_OPTION_INDENT;
  58. // Dumps the SPIRV-V module in |context| to file |filename|. Useful for
  59. // interactive debugging.
  60. void DumpShader(opt::IRContext* context, const char* filename);
  61. // Dumps |binary| to file |filename|. Useful for interactive debugging.
  62. void DumpShader(const std::vector<uint32_t>& binary, const char* filename);
  63. // Dumps |transformations| to file |filename| in binary format. Useful for
  64. // interactive debugging.
  65. void DumpTransformationsBinary(
  66. const protobufs::TransformationSequence& transformations,
  67. const char* filename);
  68. // Dumps |transformations| to file |filename| in JSON format. Useful for
  69. // interactive debugging.
  70. void DumpTransformationsJson(
  71. const protobufs::TransformationSequence& transformations,
  72. const char* filename);
  73. // Applies |transformation| to |ir_context| and |transformation_context|, and
  74. // asserts that any ids in |ir_context| that are only present post-
  75. // transformation are either contained in |transformation.GetFreshIds()|, or
  76. // in |issued_overflow_ids|.
  77. void ApplyAndCheckFreshIds(
  78. const Transformation& transformation, opt::IRContext* ir_context,
  79. TransformationContext* transformation_context,
  80. const std::unordered_set<uint32_t>& issued_overflow_ids = {{}});
  81. } // namespace fuzz
  82. } // namespace spvtools
  83. #endif // TEST_FUZZ_FUZZ_TEST_UTIL_H_