Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Fix `unreachable!` in `I31` and `NoneRef` type-checks (#15076)

Alex Crichtond5b5ccc68db3
diff --git a/crates/wasmtime/src/runtime/func/typed.rs b/crates/wasmtime/src/runtime/func/typed.rs
index 4b1c79b69c..89d36b67df 100644
--- a/crates/wasmtime/src/runtime/func/typed.rs
+++ b/crates/wasmtime/src/runtime/func/typed.rs
@@ -334,9 +334,11 @@ pub unsafe trait WasmTy: Send {
// Dynamic checks that `self <: actual` for concrete type arguments. See the
// comment above in `WasmTy::typecheck`.
//
- // Only ever called for concrete reference type arguments, so any type which
- // is not in a type hierarchy with concrete reference types can implement
- // this with `unreachable!()`.
+ // This is only ever called when `actual` is verified to be in the same type
+ // hierarchy as `Self::valtype()`. For example if `Self` is part of the
+ // `any` type hierarchy then `actual` will never be a concrete function
+ // reference. Additionally `actual` here is guaranteed to be a concrete
+ // heap type.
#[doc(hidden)]
fn dynamic_concrete_type_check(
&self,
diff --git a/crates/wasmtime/src/runtime/gc/enabled/i31.rs b/crates/wasmtime/src/runtime/gc/enabled/i31.rs
index 85c4bce6e9..cd129c6a61 100644
--- a/crates/wasmtime/src/runtime/gc/enabled/i31.rs
+++ b/crates/wasmtime/src/runtime/gc/enabled/i31.rs
@@ -4,6 +4,7 @@
//! in order to get doc comments with example code that shows the use of `I31`
//! with typed functions and such, we need to wrap it in a newtype.
+use crate::prelude::*;
use crate::runtime::vm::{VMGcRef, ValRaw};
use crate::{
HeapType, RefType, Result, ValType, WasmTy,
@@ -235,9 +236,11 @@ unsafe impl WasmTy for I31 {
&self,
_store: &StoreOpaque,
_nullable: bool,
- _actual: &HeapType,
+ actual: &HeapType,
) -> Result<()> {
- unreachable!()
+ // The only concrete types in the `any` hierarchy are struct and array
+ // types, and an `i31` is never an instance of either.
+ bail!("argument type mismatch: expected (ref {actual}), found (ref i31)")
}
fn store(self, _store: &mut AutoAssertNoGc<'_>, ptr: &mut MaybeUninit<ValRaw>) -> Result<()> {
@@ -266,11 +269,20 @@ unsafe impl WasmTy for Option<I31> {
fn dynamic_concrete_type_check(
&self,
- _store: &StoreOpaque,
- _nullable: bool,
- _actual: &HeapType,
+ store: &StoreOpaque,
+ nullable: bool,
+ actual: &HeapType,
) -> Result<()> {
- unreachable!()
+ match self {
+ Some(i) => i.dynamic_concrete_type_check(store, nullable, actual),
+ None => {
+ ensure!(
+ nullable,
+ "argument type mismatch: expected non-nullable (ref {actual}), found null reference"
+ );
+ Ok(())
+ }
+ }
}
fn store(self, store: &mut AutoAssertNoGc<'_>, ptr: &mut MaybeUninit<ValRaw>) -> Result<()> {
diff --git a/crates/wasmtime/src/runtime/gc/none_ref.rs b/crates/wasmtime/src/runtime/gc/none_ref.rs
index 47638b2b07..0b238afa45 100644
--- a/crates/wasmtime/src/runtime/gc/none_ref.rs
+++ b/crates/wasmtime/src/runtime/gc/none_ref.rs
@@ -1,3 +1,4 @@
+use crate::prelude::*;
use crate::{
HeapType, Ref, RefType, Result, Val, ValRaw, ValType, WasmTy,
store::{AutoAssertNoGc, StoreOpaque},
@@ -135,10 +136,18 @@ unsafe impl WasmTy for Option<NoneRef> {
fn dynamic_concrete_type_check(
&self,
_store: &StoreOpaque,
- _nullable: bool,
- _ty: &HeapType,
+ nullable: bool,
+ ty: &HeapType,
) -> Result<()> {
- unreachable!()
+ if nullable {
+ // `(ref null none) <: (ref null $t)` for all struct and array
+ // types `$t`, and this method is only ever invoked with concrete
+ // types in the `any` type hierarchy, so there's no need to check
+ // for concrete function references or such here.
+ Ok(())
+ } else {
+ bail!("argument type mismatch: expected non-nullable (ref {ty}), found null reference")
+ }
}
#[inline]
diff --git a/tests/all/func.rs b/tests/all/func.rs
index e76fdc9f8a..6a215d41d8 100644
--- a/tests/all/func.rs
+++ b/tests/all/func.rs
@@ -1720,6 +1720,72 @@ fn typed_concrete_param(config: &mut Config) -> wasmtime::Result<()> {
Ok(())
}
+#[wasmtime_test(wasm_features(gc, function_references))]
+#[cfg_attr(miri, ignore)]
+fn typed_concrete_gc_param_with_i31_and_none(config: &mut Config) -> wasmtime::Result<()> {
+ let engine = Engine::new(&config)?;
+ let module = Module::new(
+ &engine,
+ r#"
+ (module
+ (type $s (struct (field i32)))
+ (type $a (array i32))
+ (type $f (func))
+ (func (export "nullable-struct") (param (ref null $s)))
+ (func (export "struct") (param (ref $s)))
+ (func (export "array") (param (ref $a)))
+ (func (export "func") (param (ref $f)))
+ )
+ "#,
+ )?;
+ let mut store = Store::new(&engine, ());
+ let instance = Instance::new(&mut store, &module, &[])?;
+
+ // `(ref null none)` is a subtype of every nullable struct and array type,
+ // so passing null is fine.
+ let f = instance.get_typed_func::<Option<NoneRef>, ()>(&mut store, "nullable-struct")?;
+ f.call(&mut store, None)?;
+
+ // however while `i31ref` is in the same type hierarchy as `(ref null
+ // $struct)` to get `f` here it fails the type-check at runtime.
+ let f = instance.get_typed_func::<Option<I31>, ()>(&mut store, "nullable-struct")?;
+ f.call(&mut store, None)?;
+ assert!(f.call(&mut store, I31::new_i32(7)).is_err());
+
+ // Similar to above, these types are all in the same hierarchy so getting
+ // `f` works but actually calling it should fail.
+ for name in ["struct", "array"] {
+ let f = instance.get_typed_func::<I31, ()>(&mut store, name)?;
+ assert!(f.call(&mut store, I31::new_i32(7).unwrap()).is_err());
+
+ let f = instance.get_typed_func::<Option<I31>, ()>(&mut store, name)?;
+ assert!(f.call(&mut store, None).is_err());
+
+ let f = instance.get_typed_func::<Option<NoneRef>, ()>(&mut store, name)?;
+ assert!(f.call(&mut store, None).is_err());
+ }
+
+ // outside of the type hierarchy entirely, however, just fails to get `f` to
+ // begin with.
+ assert!(
+ instance
+ .get_typed_func::<I31, ()>(&mut store, "func")
+ .is_err()
+ );
+ assert!(
+ instance
+ .get_typed_func::<Option<I31>, ()>(&mut store, "func")
+ .is_err()
+ );
+ assert!(
+ instance
+ .get_typed_func::<Option<NoneRef>, ()>(&mut store, "func")
+ .is_err()
+ );
+
+ Ok(())
+}
+
#[wasmtime_test(wasm_features(function_references, bulk_memory))]
#[cfg_attr(miri, ignore)]
fn typed_concrete_result(config: &mut Config) -> wasmtime::Result<()> {