1 // Copyright 2020 The Tint Authors.
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 <iostream>
16 #include <string>
17 #include <unordered_set>
18
19 #include "src/reader/wgsl/parser_impl.h"
20 #include "src/writer/wgsl/generator.h"
21
22 #define ASSERT_EQ(A, B) \
23 do { \
24 decltype(A) assert_a = (A); \
25 decltype(B) assert_b = (B); \
26 if (assert_a != assert_b) { \
27 std::cerr << "ASSERT_EQ(" #A ", " #B ") failed:\n" \
28 << #A << " was: " << assert_a << "\n" \
29 << #B << " was: " << assert_b << "\n"; \
30 __builtin_trap(); \
31 } \
32 } while (false)
33
34 #define ASSERT_TRUE(A) \
35 do { \
36 decltype(A) assert_a = (A); \
37 if (!assert_a) { \
38 std::cerr << "ASSERT_TRUE(" #A ") failed:\n" \
39 << #A << " was: " << assert_a << "\n"; \
40 __builtin_trap(); \
41 } \
42 } while (false)
43
TintInternalCompilerErrorReporter(const tint::diag::List & diagnostics)44 [[noreturn]] void TintInternalCompilerErrorReporter(
45 const tint::diag::List& diagnostics) {
46 auto printer = tint::diag::Printer::create(stderr, true);
47 tint::diag::Formatter{}.format(diagnostics, printer.get());
48 __builtin_trap();
49 }
50
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)51 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
52 std::string str(reinterpret_cast<const char*>(data), size);
53
54 tint::SetInternalCompilerErrorReporter(&TintInternalCompilerErrorReporter);
55
56 tint::Source::File file("test.wgsl", str);
57
58 // Parse the wgsl, create the src program
59 tint::reader::wgsl::ParserImpl parser(&file);
60 parser.set_max_errors(1);
61 if (!parser.Parse()) {
62 return 0;
63 }
64 auto src = parser.program();
65 if (!src.IsValid()) {
66 return 0;
67 }
68
69 // Clone the src program to dst
70 tint::Program dst(src.Clone());
71
72 // Expect the printed strings to match
73 ASSERT_EQ(tint::Program::printer(&src), tint::Program::printer(&dst));
74
75 // Check that none of the AST nodes or type pointers in dst are found in src
76 std::unordered_set<tint::ast::Node*> src_nodes;
77 for (auto* src_node : src.ASTNodes().Objects()) {
78 src_nodes.emplace(src_node);
79 }
80 std::unordered_set<tint::sem::Type*> src_types;
81 for (auto* src_type : src.Types()) {
82 src_types.emplace(src_type);
83 }
84 for (auto* dst_node : dst.ASTNodes().Objects()) {
85 ASSERT_EQ(src_nodes.count(dst_node), 0u);
86 }
87 for (auto* dst_type : dst.Types()) {
88 ASSERT_EQ(src_types.count(dst_type), 0u);
89 }
90
91 // Regenerate the wgsl for the src program. We use this instead of the
92 // original source so that reformatting doesn't impact the final wgsl
93 // comparison.
94 std::string src_wgsl;
95 tint::writer::wgsl::Options wgsl_options;
96 {
97 auto result = tint::writer::wgsl::Generate(&src, wgsl_options);
98 ASSERT_TRUE(result.success);
99 src_wgsl = result.wgsl;
100
101 // Move the src program to a temporary that'll be dropped, so that the src
102 // program is released before we attempt to print the dst program. This
103 // guarantee that all the source program nodes and types are destructed and
104 // freed. ASAN should error if there's any remaining references in dst when
105 // we try to reconstruct the WGSL.
106 auto tmp = std::move(src);
107 }
108
109 // Print the dst program, check it matches the original source
110 auto result = tint::writer::wgsl::Generate(&dst, wgsl_options);
111 ASSERT_TRUE(result.success);
112 auto dst_wgsl = result.wgsl;
113 ASSERT_EQ(src_wgsl, dst_wgsl);
114
115 return 0;
116 }
117