Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly validate ref.cast when lacking a common supertype #6741

Merged
merged 5 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/wasm/wasm-validator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2780,6 +2780,21 @@ void FunctionValidator::visitRefCast(RefCast* curr) {
curr->ref->type.isRef(), curr, "ref.cast ref must have ref type")) {
return;
}
// If the cast is unreachable but not the ref (we ruled out the former
// earlier), then the cast is unreachable because the cast type had no
// common supertype with the ref, which is invalid. This is the same as the
// check below us, but we must do it first (as getHeapType fails otherwise).
if (!shouldBeUnequal(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could tighten this up even more by checking that the result type is a reference type. So if it somehow was set to i32 or something, we would still avoid the assertion failure.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, I added that now as an extra test (it has a more generic error message, and I didn't want to lose the specific explanation for the supertype issue).

curr->type,
Type(Type::unreachable),
curr,
"ref.cast target type and ref type must have a common supertype")) {
return;
}
// Also error (more generically) on i32 and anything else invalid here.
if (!shouldBeTrue(curr->type.isRef(), curr, "ref.cast must have ref type")) {
return;
}
shouldBeEqual(
curr->type.getHeapType().getBottom(),
curr->ref->type.getHeapType().getBottom(),
Expand Down
10 changes: 10 additions & 0 deletions test/spec/ref_cast.wast
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,16 @@
"common supertype"
)

(assert_invalid
(module
(type $t0 (struct))
(func (export "test-ref-cast-extern") (result anyref)
(ref.cast (ref extern) (struct.new $t0))
)
)
"common supertype"
)

(assert_malformed
(module quote "(func (ref.cast i32 (unreachable)))")
"expected reftype"
Expand Down
Loading