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