Skip to content

Commit 353e19e

Browse files
authored
Properly validate ref.cast when lacking a common supertype (#6741)
When lacking a common supertype the GLB operation makes the type of the cast unreachable, which errors on getHeapType in the later code. Fixes #6738
1 parent 538c528 commit 353e19e

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/wasm/wasm-validator.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2780,6 +2780,21 @@ void FunctionValidator::visitRefCast(RefCast* curr) {
27802780
curr->ref->type.isRef(), curr, "ref.cast ref must have ref type")) {
27812781
return;
27822782
}
2783+
// If the cast is unreachable but not the ref (we ruled out the former
2784+
// earlier), then the cast is unreachable because the cast type had no
2785+
// common supertype with the ref, which is invalid. This is the same as the
2786+
// check below us, but we must do it first (as getHeapType fails otherwise).
2787+
if (!shouldBeUnequal(
2788+
curr->type,
2789+
Type(Type::unreachable),
2790+
curr,
2791+
"ref.cast target type and ref type must have a common supertype")) {
2792+
return;
2793+
}
2794+
// Also error (more generically) on i32 and anything else invalid here.
2795+
if (!shouldBeTrue(curr->type.isRef(), curr, "ref.cast must have ref type")) {
2796+
return;
2797+
}
27832798
shouldBeEqual(
27842799
curr->type.getHeapType().getBottom(),
27852800
curr->ref->type.getHeapType().getBottom(),

test/spec/ref_cast.wast

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,16 @@
170170
"common supertype"
171171
)
172172

173+
(assert_invalid
174+
(module
175+
(type $t0 (struct))
176+
(func (export "test-ref-cast-extern") (result anyref)
177+
(ref.cast (ref extern) (struct.new $t0))
178+
)
179+
)
180+
"common supertype"
181+
)
182+
173183
(assert_malformed
174184
(module quote "(func (ref.cast i32 (unreachable)))")
175185
"expected reftype"

0 commit comments

Comments
 (0)