Skip to content

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

Fix subtype check of functions (#14941)

Alex Crichton0bd8e1c1e125
diff --git a/crates/wasmtime/src/runtime/types.rs b/crates/wasmtime/src/runtime/types.rs
index 2d4023269e..807460989b 100644
--- a/crates/wasmtime/src/runtime/types.rs
+++ b/crates/wasmtime/src/runtime/types.rs
@@ -2676,18 +2676,9 @@ impl FuncType {
pub fn matches(&self, other: &FuncType) -> bool {
assert!(self.comes_from_same_engine(other.engine()));
- // Avoid matching on structure for subtyping checks when we have
- // precisely the same type.
- if self.type_index() == other.type_index() {
- return true;
- }
-
- Self::matches_impl(
- self.params(),
- other.params(),
- self.results(),
- other.results(),
- )
+ self.engine()
+ .signatures()
+ .is_subtype(self.type_index(), other.type_index())
}
fn matches_impl(
diff --git a/tests/all/func.rs b/tests/all/func.rs
index 3a1f42f750..39a2f2e8e3 100644
--- a/tests/all/func.rs
+++ b/tests/all/func.rs
@@ -1846,10 +1846,9 @@ fn wrap_supertype_result() -> wasmtime::Result<()> {
Ok(())
}
-#[wasmtime_test(wasm_features(function_references))]
+#[wasmtime_test(wasm_features(gc, function_references))]
#[cfg_attr(miri, ignore)]
fn call_wasm_passing_subtype_func_param(config: &mut Config) -> wasmtime::Result<()> {
- config.wasm_function_references(true);
let engine = Engine::new(&config)?;
let mut store = Store::new(&engine, ());
@@ -1857,7 +1856,7 @@ fn call_wasm_passing_subtype_func_param(config: &mut Config) -> wasmtime::Result
&engine,
r#"
(module
- (type $ty (func (result funcref)))
+ (type $ty (sub (func (result funcref))))
(func (export "f") (param (ref null $ty)) (result funcref)
;; Return null if the funcref is null.
ref.null func
@@ -1876,6 +1875,8 @@ fn call_wasm_passing_subtype_func_param(config: &mut Config) -> wasmtime::Result
let instance = Instance::new(&mut store, &module, &[])?;
let f = instance.get_func(&mut store, "f").unwrap();
+ let ty = f.ty(&store).params().next().unwrap();
+ let ty = ty.unwrap_ref().heap_type().unwrap_concrete_func();
let g_ty = FuncType::new(&engine, None, Some(ValType::I32));
let g = Func::new(&mut store, g_ty.clone(), |_caller, _params, results| {
@@ -1883,17 +1884,19 @@ fn call_wasm_passing_subtype_func_param(config: &mut Config) -> wasmtime::Result
Ok(())
});
- // h's type is a subtype of the Wasm-defined `$ty`:
+ // h's type is declared as a subtype of the Wasm-defined `$ty`:
//
// (func (result (ref null g_ty))) <: (func (result funcref))
- let h_ty = FuncType::new(
+ let h_ty = FuncType::with_finality_and_supertype(
&engine,
- None,
- Some(ValType::Ref(RefType::new(
+ Finality::Final,
+ Some(ty),
+ [],
+ [ValType::Ref(RefType::new(
true,
HeapType::ConcreteFunc(g_ty),
- ))),
- );
+ ))],
+ )?;
let h = Func::new(&mut store, h_ty, move |_caller, _params, results| {
results[0] = Val::FuncRef(Some(g));
Ok(())
diff --git a/tests/all/types.rs b/tests/all/types.rs
index 5fc6d16725..baf53d6266 100644
--- a/tests/all/types.rs
+++ b/tests/all/types.rs
@@ -704,6 +704,7 @@ fn func_subtyping() -> Result<()> {
(false, "i", &i, "a", &a),
] {
eprintln!("expect that `{sub_name} <: {sup_name}` is `{expected}`");
+ assert_eq!(expected, sub.matches(sup));
let sub = HeapType::ConcreteFunc(sub.clone());
let sup = HeapType::ConcreteFunc(sup.clone());
let actual = sub.matches(&sup);