tint/writer: Do not attempt to use invalid programs

Attempting to emit shader code from an invalid program is unsafe.

The WGSL writer is used for dumping information which is valuable for debugging bad programs, so this has not been changed.

Bug: chromium:1327461
Change-Id: I4497fcb19d126ef6c872e2bcda8e9b79044aeb68
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/91163
Reviewed-by: David Neto <dneto@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
Ben Clayton 2022-05-20 19:41:50 +00:00 committed by Dawn LUCI CQ
parent 34f4e1ecd3
commit 8f4f449540
8 changed files with 52 additions and 0 deletions

View File

@ -30,6 +30,10 @@ Result::Result(const Result&) = default;
Result Generate(const Program* program, const Options& options, const std::string& entry_point) {
Result result;
if (!program->IsValid()) {
result.error = "input program is not valid";
return result;
}
// Sanitize the program.
auto sanitized_result = Sanitize(program, options, entry_point);

View File

@ -19,6 +19,15 @@ namespace {
using GlslGeneratorImplTest = TestHelper;
TEST_F(GlslGeneratorImplTest, InvalidProgram) {
Diagnostics().add_error(diag::System::Writer, "make the program invalid");
ASSERT_FALSE(IsValid());
auto program = std::make_unique<Program>(std::move(*this));
ASSERT_FALSE(program->IsValid());
auto result = Generate(program.get(), Options{}, "");
EXPECT_EQ(result.error, "input program is not valid");
}
TEST_F(GlslGeneratorImplTest, Generate) {
Func("my_func", ast::VariableList{}, ty.void_(), ast::StatementList{}, ast::AttributeList{});

View File

@ -29,6 +29,10 @@ Result::Result(const Result&) = default;
Result Generate(const Program* program, const Options& options) {
Result result;
if (!program->IsValid()) {
result.error = "input program is not valid";
return result;
}
// Sanitize the program.
auto sanitized_result = Sanitize(program, options);

View File

@ -19,6 +19,15 @@ namespace {
using HlslGeneratorImplTest = TestHelper;
TEST_F(HlslGeneratorImplTest, InvalidProgram) {
Diagnostics().add_error(diag::System::Writer, "make the program invalid");
ASSERT_FALSE(IsValid());
auto program = std::make_unique<Program>(std::move(*this));
ASSERT_FALSE(program->IsValid());
auto result = Generate(program.get(), Options{});
EXPECT_EQ(result.error, "input program is not valid");
}
TEST_F(HlslGeneratorImplTest, Generate) {
Func("my_func", ast::VariableList{}, ty.void_(), ast::StatementList{}, ast::AttributeList{});

View File

@ -31,6 +31,10 @@ Result::Result(const Result&) = default;
Result Generate(const Program* program, const Options& options) {
Result result;
if (!program->IsValid()) {
result.error = "input program is not valid";
return result;
}
// Sanitize the program.
auto sanitized_result = Sanitize(program, options);

View File

@ -22,6 +22,15 @@ namespace {
using MslGeneratorImplTest = TestHelper;
TEST_F(MslGeneratorImplTest, InvalidProgram) {
Diagnostics().add_error(diag::System::Writer, "make the program invalid");
ASSERT_FALSE(IsValid());
auto program = std::make_unique<Program>(std::move(*this));
ASSERT_FALSE(program->IsValid());
auto result = Generate(program.get(), Options{});
EXPECT_EQ(result.error, "input program is not valid");
}
TEST_F(MslGeneratorImplTest, Generate) {
Func("my_func", ast::VariableList{}, ty.void_(), ast::StatementList{},
ast::AttributeList{

View File

@ -20,6 +20,15 @@ namespace {
using BuilderTest = TestHelper;
TEST_F(BuilderTest, InvalidProgram) {
Diagnostics().add_error(diag::System::Writer, "make the program invalid");
ASSERT_FALSE(IsValid());
auto program = std::make_unique<Program>(std::move(*this));
ASSERT_FALSE(program->IsValid());
auto result = Generate(program.get(), Options{});
EXPECT_EQ(result.error, "input program is not valid");
}
TEST_F(BuilderTest, TracksIdBounds) {
spirv::Builder& b = Build();

View File

@ -26,6 +26,10 @@ Result::Result(const Result&) = default;
Result Generate(const Program* program, const Options& options) {
Result result;
if (!program->IsValid()) {
result.error = "input program is not valid";
return result;
}
// Sanitize the program.
auto sanitized_result = Sanitize(program, options);