diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index bf29f948b77..d8a3c2f7484 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -6898,6 +6898,9 @@ void WasmBinaryBuilder::visitRefAs(RefAs* curr, uint8_t code) { WASM_UNREACHABLE("invalid code for ref.as_*"); } curr->value = popNonVoidExpression(); + if (!curr->value->type.isRef() && curr->value->type != Type::unreachable) { + throwError("bad input type for ref.as: " + curr->value->type.toString()); + } curr->finalize(); } diff --git a/test/lit/binary/bad-ref-as.test b/test/lit/binary/bad-ref-as.test new file mode 100644 index 00000000000..2afa1990478 --- /dev/null +++ b/test/lit/binary/bad-ref-as.test @@ -0,0 +1,6 @@ +;; Test that we error properly on a file with a ref.as of a non-ref type. + +;; RUN: not wasm-opt %s.wasm 2>&1 | filecheck %s + +;; CHECK: parse exception: bad input type for ref.as: i32 + diff --git a/test/lit/binary/bad-ref-as.test.wasm b/test/lit/binary/bad-ref-as.test.wasm new file mode 100644 index 00000000000..637537dd22e Binary files /dev/null and b/test/lit/binary/bad-ref-as.test.wasm differ