From 2cb5cefb6392619d908ce2ab683815d7e22ac9a5 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Thu, 8 Dec 2022 09:03:48 -0800 Subject: Validate ref.as_* argument is a reference (#5330) Without this we hit an assert with no line number info (or in a no-asserts build, bad things can happen). With this: $ bin/wasm-opt -all ~/Downloads/crash.wat --nominal [parse exception: Invalid ref for ref.as (at 155065:119)] Fatal: error parsing wasm (That can only happen for ref.as_non_null, as all the others do not have that assert - their types do not depend on the child's type, so their finalize does not error. Still, it is nice to validate earlier for them as well, so this PR handles them all.) --- src/wasm/wasm-s-parser.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'src/wasm/wasm-s-parser.cpp') diff --git a/src/wasm/wasm-s-parser.cpp b/src/wasm/wasm-s-parser.cpp index ee9fdde72..507cf3b6c 100644 --- a/src/wasm/wasm-s-parser.cpp +++ b/src/wasm/wasm-s-parser.cpp @@ -2945,7 +2945,11 @@ Expression* SExpressionWasmBuilder::makeArrayCopy(Element& s) { } Expression* SExpressionWasmBuilder::makeRefAs(Element& s, RefAsOp op) { - return Builder(wasm).makeRefAs(op, parseExpression(s[1])); + auto* value = parseExpression(s[1]); + if (!value->type.isRef() && value->type != Type::unreachable) { + throw ParseException("ref.as child must be a ref", s.line, s.col); + } + return Builder(wasm).makeRefAs(op, value); } Expression* SExpressionWasmBuilder::makeStringNew(Element& s, StringNewOp op) { -- cgit v1.2.3