• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
15 #ifndef TEST_FUZZ_FUZZ_TEST_UTIL_H_
16 #define TEST_FUZZ_FUZZ_TEST_UTIL_H_
17 
18 #include "gtest/gtest.h"
19 
20 #include <vector>
21 
22 #include "source/fuzz/protobufs/spirvfuzz_protobufs.h"
23 #include "source/opt/build_module.h"
24 #include "source/opt/ir_context.h"
25 #include "spirv-tools/libspirv.h"
26 
27 namespace spvtools {
28 namespace fuzz {
29 
30 // Returns true if and only if the given binaries are bit-wise equal.
31 bool IsEqual(spv_target_env env, const std::vector<uint32_t>& expected_binary,
32              const std::vector<uint32_t>& actual_binary);
33 
34 // Assembles the given text and returns true if and only if the resulting binary
35 // is bit-wise equal to the given binary.
36 bool IsEqual(spv_target_env env, const std::string& expected_text,
37              const std::vector<uint32_t>& actual_binary);
38 
39 // Assembles the given text and turns the given IR into binary, then returns
40 // true if and only if the resulting binaries are bit-wise equal.
41 bool IsEqual(spv_target_env env, const std::string& expected_text,
42              const opt::IRContext* actual_ir);
43 
44 // Turns the given IRs into binaries, then returns true if and only if the
45 // resulting binaries are bit-wise equal.
46 bool IsEqual(spv_target_env env, const opt::IRContext* ir_1,
47              const opt::IRContext* ir_2);
48 
49 // Assembles the given IR context and returns true if and only if
50 // the resulting binary is valid.
51 bool IsValid(spv_target_env env, const opt::IRContext* ir);
52 
53 // Assembles the given IR context, then returns its disassembly as a string.
54 // Useful for debugging.
55 std::string ToString(spv_target_env env, const opt::IRContext* ir);
56 
57 // Returns the disassembly of the given binary as a string.
58 // Useful for debugging.
59 std::string ToString(spv_target_env env, const std::vector<uint32_t>& binary);
60 
61 // Assembly options for writing fuzzer tests.  It simplifies matters if
62 // numeric ids do not change.
63 const uint32_t kFuzzAssembleOption =
64     SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS;
65 // Disassembly options for writing fuzzer tests.
66 const uint32_t kFuzzDisassembleOption =
67     SPV_BINARY_TO_TEXT_OPTION_NO_HEADER | SPV_BINARY_TO_TEXT_OPTION_INDENT;
68 
69 // A silent message consumer.
70 const spvtools::MessageConsumer kSilentConsumer =
71     [](spv_message_level_t, const char*, const spv_position_t&,
72        const char*) -> void {};
73 
74 const spvtools::MessageConsumer kConsoleMessageConsumer =
75     [](spv_message_level_t level, const char*, const spv_position_t& position,
76        const char* message) -> void {
77   switch (level) {
78     case SPV_MSG_FATAL:
79     case SPV_MSG_INTERNAL_ERROR:
80     case SPV_MSG_ERROR:
81       std::cerr << "error: line " << position.index << ": " << message
82                 << std::endl;
83       break;
84     case SPV_MSG_WARNING:
85       std::cout << "warning: line " << position.index << ": " << message
86                 << std::endl;
87       break;
88     case SPV_MSG_INFO:
89       std::cout << "info: line " << position.index << ": " << message
90                 << std::endl;
91       break;
92     default:
93       break;
94   }
95 };
96 
97 // Dumps the SPIRV-V module in |context| to file |filename|. Useful for
98 // interactive debugging.
99 void DumpShader(opt::IRContext* context, const char* filename);
100 
101 // Dumps |binary| to file |filename|. Useful for interactive debugging.
102 void DumpShader(const std::vector<uint32_t>& binary, const char* filename);
103 
104 // Dumps |transformations| to file |filename| in JSON format. Useful for
105 // interactive debugging.
106 void DumpTransformationsJson(
107     const protobufs::TransformationSequence& transformations,
108     const char* filename);
109 
110 }  // namespace fuzz
111 }  // namespace spvtools
112 
113 #endif  // TEST_FUZZ_FUZZ_TEST_UTIL_H_
114