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 #include "test/fuzz/fuzz_test_util.h"
16
17 #include <fstream>
18 #include <iostream>
19
20 #include "tools/io.h"
21
22 namespace spvtools {
23 namespace fuzz {
24
IsEqual(const spv_target_env env,const std::vector<uint32_t> & expected_binary,const std::vector<uint32_t> & actual_binary)25 bool IsEqual(const spv_target_env env,
26 const std::vector<uint32_t>& expected_binary,
27 const std::vector<uint32_t>& actual_binary) {
28 if (expected_binary == actual_binary) {
29 return true;
30 }
31 SpirvTools t(env);
32 std::string expected_disassembled;
33 std::string actual_disassembled;
34 if (!t.Disassemble(expected_binary, &expected_disassembled,
35 kFuzzDisassembleOption)) {
36 return false;
37 }
38 if (!t.Disassemble(actual_binary, &actual_disassembled,
39 kFuzzDisassembleOption)) {
40 return false;
41 }
42 // Using expect gives us a string diff if the strings are not the same.
43 EXPECT_EQ(expected_disassembled, actual_disassembled);
44 // We then return the result of the equality comparison, to be used by an
45 // assertion in the test root function.
46 return expected_disassembled == actual_disassembled;
47 }
48
IsEqual(const spv_target_env env,const std::string & expected_text,const std::vector<uint32_t> & actual_binary)49 bool IsEqual(const spv_target_env env, const std::string& expected_text,
50 const std::vector<uint32_t>& actual_binary) {
51 std::vector<uint32_t> expected_binary;
52 SpirvTools t(env);
53 if (!t.Assemble(expected_text, &expected_binary, kFuzzAssembleOption)) {
54 return false;
55 }
56 return IsEqual(env, expected_binary, actual_binary);
57 }
58
IsEqual(const spv_target_env env,const std::string & expected_text,const opt::IRContext * actual_ir)59 bool IsEqual(const spv_target_env env, const std::string& expected_text,
60 const opt::IRContext* actual_ir) {
61 std::vector<uint32_t> actual_binary;
62 actual_ir->module()->ToBinary(&actual_binary, false);
63 return IsEqual(env, expected_text, actual_binary);
64 }
65
IsEqual(const spv_target_env env,const opt::IRContext * ir_1,const opt::IRContext * ir_2)66 bool IsEqual(const spv_target_env env, const opt::IRContext* ir_1,
67 const opt::IRContext* ir_2) {
68 std::vector<uint32_t> binary_1;
69 ir_1->module()->ToBinary(&binary_1, false);
70 std::vector<uint32_t> binary_2;
71 ir_2->module()->ToBinary(&binary_2, false);
72 return IsEqual(env, binary_1, binary_2);
73 }
74
IsValid(spv_target_env env,const opt::IRContext * ir)75 bool IsValid(spv_target_env env, const opt::IRContext* ir) {
76 std::vector<uint32_t> binary;
77 ir->module()->ToBinary(&binary, false);
78 SpirvTools t(env);
79 t.SetMessageConsumer(kConsoleMessageConsumer);
80 return t.Validate(binary);
81 }
82
ToString(spv_target_env env,const opt::IRContext * ir)83 std::string ToString(spv_target_env env, const opt::IRContext* ir) {
84 std::vector<uint32_t> binary;
85 ir->module()->ToBinary(&binary, false);
86 return ToString(env, binary);
87 }
88
ToString(spv_target_env env,const std::vector<uint32_t> & binary)89 std::string ToString(spv_target_env env, const std::vector<uint32_t>& binary) {
90 SpirvTools t(env);
91 std::string result;
92 t.Disassemble(binary, &result, kFuzzDisassembleOption);
93 return result;
94 }
95
DumpShader(opt::IRContext * context,const char * filename)96 void DumpShader(opt::IRContext* context, const char* filename) {
97 std::vector<uint32_t> binary;
98 context->module()->ToBinary(&binary, false);
99 DumpShader(binary, filename);
100 }
101
DumpShader(const std::vector<uint32_t> & binary,const char * filename)102 void DumpShader(const std::vector<uint32_t>& binary, const char* filename) {
103 auto write_file_succeeded =
104 WriteFile(filename, "wb", &binary[0], binary.size());
105 if (!write_file_succeeded) {
106 std::cerr << "Failed to dump shader" << std::endl;
107 }
108 }
109
DumpTransformationsJson(const protobufs::TransformationSequence & transformations,const char * filename)110 void DumpTransformationsJson(
111 const protobufs::TransformationSequence& transformations,
112 const char* filename) {
113 std::string json_string;
114 auto json_options = google::protobuf::util::JsonOptions();
115 json_options.add_whitespace = true;
116 auto json_generation_status = google::protobuf::util::MessageToJsonString(
117 transformations, &json_string, json_options);
118 if (json_generation_status == google::protobuf::util::Status::OK) {
119 std::ofstream transformations_json_file(filename);
120 transformations_json_file << json_string;
121 transformations_json_file.close();
122 }
123 }
124
125 } // namespace fuzz
126 } // namespace spvtools
127