2020-12-01 18:04:17 +00:00
|
|
|
// Copyright 2020 The Tint Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
#include <unordered_set>
|
|
|
|
|
|
|
|
#include "src/reader/wgsl/parser_impl.h"
|
|
|
|
#include "src/writer/wgsl/generator.h"
|
|
|
|
|
|
|
|
#define ASSERT_EQ(A, B) \
|
|
|
|
do { \
|
|
|
|
decltype(A) assert_a = (A); \
|
|
|
|
decltype(B) assert_b = (B); \
|
|
|
|
if (assert_a != assert_b) { \
|
|
|
|
std::cerr << "ASSERT_EQ(" #A ", " #B ") failed:\n" \
|
|
|
|
<< #A << " was: " << assert_a << "\n" \
|
|
|
|
<< #B << " was: " << assert_b << "\n"; \
|
|
|
|
__builtin_trap(); \
|
|
|
|
} \
|
|
|
|
} while (false)
|
|
|
|
|
|
|
|
#define ASSERT_TRUE(A) \
|
|
|
|
do { \
|
|
|
|
decltype(A) assert_a = (A); \
|
|
|
|
if (!assert_a) { \
|
|
|
|
std::cerr << "ASSERT_TRUE(" #A ") failed:\n" \
|
|
|
|
<< #A << " was: " << assert_a << "\n"; \
|
|
|
|
__builtin_trap(); \
|
|
|
|
} \
|
|
|
|
} while (false)
|
|
|
|
|
2021-03-25 11:40:07 +00:00
|
|
|
[[noreturn]] void TintInternalCompilerErrorReporter(
|
|
|
|
const tint::diag::List& diagnostics) {
|
|
|
|
auto printer = tint::diag::Printer::create(stderr, true);
|
|
|
|
tint::diag::Formatter{}.format(diagnostics, printer.get());
|
|
|
|
__builtin_trap();
|
|
|
|
}
|
|
|
|
|
2020-12-01 18:04:17 +00:00
|
|
|
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
|
|
|
|
std::string str(reinterpret_cast<const char*>(data), size);
|
|
|
|
|
2021-03-25 11:40:07 +00:00
|
|
|
tint::SetInternalCompilerErrorReporter(&TintInternalCompilerErrorReporter);
|
|
|
|
|
2020-12-01 18:04:17 +00:00
|
|
|
tint::Source::File file("test.wgsl", str);
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
// Parse the wgsl, create the src program
|
2020-12-02 21:17:58 +00:00
|
|
|
tint::reader::wgsl::ParserImpl parser(&file);
|
2020-12-01 18:04:17 +00:00
|
|
|
parser.set_max_errors(1);
|
|
|
|
if (!parser.Parse()) {
|
|
|
|
return 0;
|
|
|
|
}
|
2021-01-26 16:57:10 +00:00
|
|
|
auto src = parser.program();
|
2021-01-27 18:49:05 +00:00
|
|
|
if (!src.IsValid()) {
|
|
|
|
return 0;
|
|
|
|
}
|
2020-12-01 18:04:17 +00:00
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
// Clone the src program to dst
|
2021-01-26 16:57:10 +00:00
|
|
|
tint::Program dst(src.Clone());
|
2020-12-01 18:04:17 +00:00
|
|
|
|
2021-01-06 14:30:11 +00:00
|
|
|
// Expect the demangled AST printed with to_str() to match
|
2021-01-29 11:22:40 +00:00
|
|
|
ASSERT_EQ(src.to_str(), dst.to_str());
|
2020-12-01 18:04:17 +00:00
|
|
|
|
|
|
|
// Check that none of the AST nodes or type pointers in dst are found in src
|
|
|
|
std::unordered_set<tint::ast::Node*> src_nodes;
|
2021-01-29 15:17:30 +00:00
|
|
|
for (auto* src_node : src.ASTNodes().Objects()) {
|
2021-01-21 15:30:10 +00:00
|
|
|
src_nodes.emplace(src_node);
|
2020-12-01 18:04:17 +00:00
|
|
|
}
|
2021-01-21 15:42:10 +00:00
|
|
|
std::unordered_set<tint::type::Type*> src_types;
|
2021-01-26 16:57:10 +00:00
|
|
|
for (auto* src_type : src.Types()) {
|
|
|
|
src_types.emplace(src_type);
|
2020-12-01 18:04:17 +00:00
|
|
|
}
|
2021-01-29 15:17:30 +00:00
|
|
|
for (auto* dst_node : dst.ASTNodes().Objects()) {
|
2021-01-21 15:30:10 +00:00
|
|
|
ASSERT_EQ(src_nodes.count(dst_node), 0u);
|
2020-12-01 18:04:17 +00:00
|
|
|
}
|
2021-01-26 16:57:10 +00:00
|
|
|
for (auto* dst_type : dst.Types()) {
|
|
|
|
ASSERT_EQ(src_types.count(dst_type), 0u);
|
2020-12-01 18:04:17 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
// Regenerate the wgsl for the src program. We use this instead of the
|
|
|
|
// original source so that reformatting doesn't impact the final wgsl
|
|
|
|
// comparison.
|
2020-12-01 18:04:17 +00:00
|
|
|
std::string src_wgsl;
|
|
|
|
{
|
2021-01-26 16:57:10 +00:00
|
|
|
tint::writer::wgsl::Generator src_gen(&src);
|
2020-12-01 18:04:17 +00:00
|
|
|
ASSERT_TRUE(src_gen.Generate());
|
|
|
|
src_wgsl = src_gen.result();
|
2021-01-26 16:57:10 +00:00
|
|
|
|
|
|
|
// Move the src program to a temporary that'll be dropped, so that the src
|
|
|
|
// program is released before we attempt to print the dst program. This
|
|
|
|
// guarantee that all the source program nodes and types are destructed and
|
|
|
|
// freed. ASAN should error if there's any remaining references in dst when
|
|
|
|
// we try to reconstruct the WGSL.
|
|
|
|
auto tmp = std::move(src);
|
2020-12-01 18:04:17 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
// Print the dst program, check it matches the original source
|
|
|
|
tint::writer::wgsl::Generator dst_gen(&dst);
|
2020-12-01 18:04:17 +00:00
|
|
|
ASSERT_TRUE(dst_gen.Generate());
|
|
|
|
auto dst_wgsl = dst_gen.result();
|
|
|
|
ASSERT_EQ(src_wgsl, dst_wgsl);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|