Skip to content

Commit 8ec0143

Browse files
committed
[JSC] any.convert_extern should accept a subtype of externref
https://bugs.webkit.org/show_bug.cgi?id=328169 rdar://190498804 Reviewed by Sosuke Suzuki. any.convert_extern takes [(ref null? extern)], so a (ref null noextern) operand is valid. Currently we only accept exact externref type. This patch fixes it. * JSTests/wasm/stress/any-convert-extern.js: * Source/JavaScriptCore/wasm/WasmFunctionParser.h: (JSC::Wasm::FunctionParser<Context>::parseExpression): Canonical link: https://commits.webkit.org/324243@main
1 parent adade9c commit 8ec0143

2 files changed

Lines changed: 49 additions & 2 deletions

File tree

‎JSTests/wasm/stress/any-convert-extern.js‎

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { instantiate } from "../gc/wast-wrapper.js";
1+
import { compile, instantiate } from "../gc/wast-wrapper.js";
22
import * as assert from "../assert.js";
33

44
function test() {
@@ -33,4 +33,51 @@ function test() {
3333
assert.throws(() => toI31("foo"), WebAssembly.RuntimeError, "ref.cast failed to cast reference to target heap type");
3434
}
3535

36+
function testNoextern() {
37+
const instance = instantiate(`
38+
(module
39+
(global $g anyref (any.convert_extern (ref.null noextern)))
40+
(func (export "constant") (result anyref)
41+
(any.convert_extern (ref.null noextern)))
42+
(func (export "local") (result anyref) (local (ref null noextern))
43+
(any.convert_extern (local.get 0)))
44+
(func (export "nonNull") (result (ref any))
45+
(any.convert_extern (ref.as_non_null (ref.null noextern))))
46+
(func (export "global") (result anyref)
47+
(global.get $g))
48+
)
49+
`);
50+
const { constant, local, nonNull, global } = instance.exports;
51+
52+
for (let i = 0; i < wasmTestLoopCount; i++) {
53+
assert.eq(constant(), null);
54+
assert.eq(local(), null);
55+
assert.eq(global(), null);
56+
}
57+
assert.throws(() => nonNull(), WebAssembly.RuntimeError, "ref.as_non_null to a null reference");
58+
}
59+
60+
function testInvalid() {
61+
for (const heapType of ["nofunc", "func", "none", "any"]) {
62+
assert.throws(() => compile(`
63+
(module
64+
(func (result anyref)
65+
(any.convert_extern (ref.null ${heapType}))))
66+
`), WebAssembly.CompileError, `any.convert_extern reference to type (ref null ${heapType}) expected (ref null extern)`);
67+
}
68+
69+
assert.throws(() => compile(`
70+
(module
71+
(func (result (ref any))
72+
(any.convert_extern (ref.null noextern))))
73+
`), WebAssembly.CompileError, "control flow returns with unexpected type. (ref null any) is not a (ref any)");
74+
75+
assert.throws(() => compile(`
76+
(module
77+
(global (ref any) (any.convert_extern (ref.null noextern))))
78+
`), WebAssembly.CompileError, "control flow returns with unexpected type. (ref null any) is not a (ref any)");
79+
}
80+
3681
test();
82+
testNoextern();
83+
testInvalid();

‎Source/JavaScriptCore/wasm/WasmFunctionParser.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3059,7 +3059,7 @@ FOR_EACH_WASM_MEMORY_STORE_OP(CREATE_CASE)
30593059
case ExtGCOpType::AnyConvertExtern: {
30603060
TypedExpression reference;
30613061
WASM_TRY_POP_EXPRESSION_STACK_INTO(reference, "any.convert_extern"_s);
3062-
WASM_VALIDATOR_FAIL_IF(!isExternref(reference.type()), "any.convert_extern reference to type "_s, reference.type(), " expected "_s, TypeKind::Externref);
3062+
WASM_VALIDATOR_FAIL_IF(!isSubtype(reference.type(), externrefType()), "any.convert_extern reference to type "_s, reference.type(), " expected "_s, externrefType());
30633063

30643064
ExpressionType result;
30653065
WASM_TRY_ADD_TO_CONTEXT(addAnyConvertExtern(reference, result));

0 commit comments

Comments
 (0)