Skip to content

Commit

Permalink
[tint][ir][val] Reject pointers and references to voids
Browse files Browse the repository at this point in the history
This prevents a variety of ways to try creating void values.
This implicitly prevents vars being created with void types.

Fixes: 382503561
Change-Id: I4e235b80691321a3c4df2a885f43620e239b9510
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/218394
Commit-Queue: Ryan Harrison <[email protected]>
Reviewed-by: James Price <[email protected]>
Auto-Submit: Ryan Harrison <[email protected]>
  • Loading branch information
zoddicus authored and Dawn LUCI CQ committed Dec 6, 2024
1 parent ba634ae commit a175095
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/tint/lang/core/ir/validator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1757,7 +1757,12 @@ void Validator::CheckType(const core::type::Type* root,
auto visit = [&](const core::type::Type* type) {
return tint::Switch(
type,
[&](const core::type::Reference*) {
[&](const core::type::Reference* ref) {
if (ref->StoreType()->Is<core::type::Void>()) {
diag() << "references to void are not permitted";
return false;
}

// Reference types are guarded by the AllowRefTypes capability.
if (!capabilities_.Contains(Capability::kAllowRefTypes) ||
ignore_caps.Contains(Capability::kAllowRefTypes)) {
Expand All @@ -1770,7 +1775,12 @@ void Validator::CheckType(const core::type::Type* root,
}
return true;
},
[&](const core::type::Pointer*) {
[&](const core::type::Pointer* ptr) {
if (ptr->StoreType()->Is<core::type::Void>()) {
diag() << "pointers to void are not permitted";
return false;
}

if (type != root) {
// Nesting pointer types inside structures is guarded by a capability.
if (!(root->Is<core::type::Struct>() &&
Expand Down
52 changes: 52 additions & 0 deletions src/tint/lang/core/ir/validator_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5203,6 +5203,27 @@ note: # Disassembly
)");
}

TEST_F(IR_ValidatorTest, Var_VoidType) {
mod.root_block->Append(b.Var(ty.ptr(private_, ty.void_())));

auto res = ir::Validate(mod);
ASSERT_NE(res, Success);
EXPECT_EQ(res.Failure().reason.Str(), R"(:2:3 error: var: pointers to void are not permitted
%1:ptr<private, void, read_write> = var
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
:1:1 note: in block
$B1: { # root
^^^
note: # Disassembly
$B1: { # root
%1:ptr<private, void, read_write> = var
}
)");
}

TEST_F(IR_ValidatorTest, Var_Function_NullResult) {
auto* v = mod.CreateInstruction<ir::Var>(nullptr);
v->SetInitializer(b.Constant(0_i));
Expand Down Expand Up @@ -9897,6 +9918,20 @@ TEST_F(IR_ValidatorTest, PointerToPointer) {
testing::HasSubstr("nested pointer types are not permitted"));
}

TEST_F(IR_ValidatorTest, PointerToVoid) {
auto* type = ty.ptr(AddressSpace::kFunction, ty.void_());
auto* fn = b.Function("my_func", ty.void_());
fn->SetParams(Vector{b.FunctionParam(type)});
b.Append(fn->Block(), [&] { //
b.Return(fn);
});

auto res = ir::Validate(mod);
ASSERT_NE(res, Success);
EXPECT_THAT(res.Failure().reason.Str(),
testing::HasSubstr("pointers to void are not permitted"));
}

TEST_F(IR_ValidatorTest, ReferenceToReference) {
auto* type = ty.ref<function>(ty.ref<function, i32>());
auto* fn = b.Function("my_func", ty.void_());
Expand All @@ -9914,6 +9949,23 @@ TEST_F(IR_ValidatorTest, ReferenceToReference) {
testing::HasSubstr("nested reference types are not permitted"));
}

TEST_F(IR_ValidatorTest, ReferenceToVoid) {
auto* type = ty.ref(AddressSpace::kFunction, ty.void_());
auto* fn = b.Function("my_func", ty.void_());
b.Append(fn->Block(), [&] { //
b.Var(type);
b.Return(fn);
});

Capabilities caps;
caps.Add(Capability::kAllowRefTypes);

auto res = ir::Validate(mod, caps);
ASSERT_NE(res, Success);
EXPECT_THAT(res.Failure().reason.Str(),
testing::HasSubstr("references to void are not permitted"));
}

TEST_F(IR_ValidatorTest, PointerInStructure_WithoutCapability) {
auto* str_ty =
ty.Struct(mod.symbols.New("S"), {
Expand Down

0 comments on commit a175095

Please sign in to comment.