fuzz_test_util.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 "test/fuzz/fuzz_test_util.h"
  15. #include <iostream>
  16. #include "tools/io.h"
  17. namespace spvtools {
  18. namespace fuzz {
  19. bool IsEqual(const spv_target_env env,
  20. const std::vector<uint32_t>& expected_binary,
  21. const std::vector<uint32_t>& actual_binary) {
  22. if (expected_binary == actual_binary) {
  23. return true;
  24. }
  25. SpirvTools t(env);
  26. std::string expected_disassembled;
  27. std::string actual_disassembled;
  28. if (!t.Disassemble(expected_binary, &expected_disassembled,
  29. kFuzzDisassembleOption)) {
  30. return false;
  31. }
  32. if (!t.Disassemble(actual_binary, &actual_disassembled,
  33. kFuzzDisassembleOption)) {
  34. return false;
  35. }
  36. // Using expect gives us a string diff if the strings are not the same.
  37. EXPECT_EQ(expected_disassembled, actual_disassembled);
  38. // We then return the result of the equality comparison, to be used by an
  39. // assertion in the test root function.
  40. return expected_disassembled == actual_disassembled;
  41. }
  42. bool IsEqual(const spv_target_env env, const std::string& expected_text,
  43. const std::vector<uint32_t>& actual_binary) {
  44. std::vector<uint32_t> expected_binary;
  45. SpirvTools t(env);
  46. if (!t.Assemble(expected_text, &expected_binary, kFuzzAssembleOption)) {
  47. return false;
  48. }
  49. return IsEqual(env, expected_binary, actual_binary);
  50. }
  51. bool IsEqual(const spv_target_env env, const std::string& expected_text,
  52. const opt::IRContext* actual_ir) {
  53. std::vector<uint32_t> actual_binary;
  54. actual_ir->module()->ToBinary(&actual_binary, false);
  55. return IsEqual(env, expected_text, actual_binary);
  56. }
  57. bool IsEqual(const spv_target_env env, const opt::IRContext* ir_1,
  58. const opt::IRContext* ir_2) {
  59. std::vector<uint32_t> binary_1;
  60. ir_1->module()->ToBinary(&binary_1, false);
  61. std::vector<uint32_t> binary_2;
  62. ir_2->module()->ToBinary(&binary_2, false);
  63. return IsEqual(env, binary_1, binary_2);
  64. }
  65. bool IsValid(spv_target_env env, const opt::IRContext* ir) {
  66. std::vector<uint32_t> binary;
  67. ir->module()->ToBinary(&binary, false);
  68. SpirvTools t(env);
  69. return t.Validate(binary);
  70. }
  71. std::string ToString(spv_target_env env, const opt::IRContext* ir) {
  72. std::vector<uint32_t> binary;
  73. ir->module()->ToBinary(&binary, false);
  74. return ToString(env, binary);
  75. }
  76. std::string ToString(spv_target_env env, const std::vector<uint32_t>& binary) {
  77. SpirvTools t(env);
  78. std::string result;
  79. t.Disassemble(binary, &result, kFuzzDisassembleOption);
  80. return result;
  81. }
  82. void DumpShader(opt::IRContext* context, const char* filename) {
  83. std::vector<uint32_t> binary;
  84. context->module()->ToBinary(&binary, false);
  85. DumpShader(binary, filename);
  86. }
  87. void DumpShader(const std::vector<uint32_t>& binary, const char* filename) {
  88. auto write_file_succeeded =
  89. WriteFile(filename, "wb", &binary[0], binary.size());
  90. if (!write_file_succeeded) {
  91. std::cerr << "Failed to dump shader" << std::endl;
  92. }
  93. }
  94. } // namespace fuzz
  95. } // namespace spvtools