Skip to content

Commit

Permalink
Validate RefAsNonNull (#6785)
Browse files Browse the repository at this point in the history
Fixes #6781
  • Loading branch information
kripken committed Jul 24, 2024
1 parent 353e19e commit 017b473
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
13 changes: 11 additions & 2 deletions src/wasm/wasm-validator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2230,10 +2230,19 @@ void FunctionValidator::visitRefIsNull(RefIsNull* curr) {
}

void FunctionValidator::visitRefAs(RefAs* curr) {
if (curr->value->type != Type::unreachable &&
!shouldBeTrue(
curr->value->type.isRef(), curr, "ref.as value must be reference")) {
return;
}
switch (curr->op) {
default:
// TODO: validate all the other ref.as_*
case RefAsNonNull: {
shouldBeTrue(
getModule()->features.hasReferenceTypes(),
curr,
"ref.as requires reference-types [--enable-reference-types]");
break;
}
case AnyConvertExtern: {
shouldBeTrue(getModule()->features.hasGC(),
curr,
Expand Down
8 changes: 7 additions & 1 deletion src/wasm/wasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1223,7 +1223,13 @@ void ArrayInitElem::finalize() {
}

void RefAs::finalize() {
if (value->type == Type::unreachable) {
// An unreachable child means we are unreachable. Also set ourselves to
// unreachable when the child is invalid (say, it is an i32 or some other non-
// reference), which avoids getHeapType() erroring right after us (in this
// situation, the validator will report an error later).
// TODO: Remove that part when we solve the validation issue more generally,
// see https://github.com/WebAssembly/binaryen/issues/6781
if (!value->type.isRef()) {
type = Type::unreachable;
return;
}
Expand Down
13 changes: 13 additions & 0 deletions test/lit/validation/non-ref.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
;; RUN: not wasm-opt %s -all 2>&1 | filecheck %s

;; CHECK: ref.as value must be reference

(module
(func $test
(drop
(ref.as_non_null
(i32.const 42)
)
)
)
)

0 comments on commit 017b473

Please sign in to comment.