Skip to content

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

Fix `OwnedRooted<T>::wasm_ty_store` missing `i31ref` check (#12451)

Zijie Zhao43832481378b
diff --git a/crates/wasmtime/src/runtime/gc/enabled/rooting.rs b/crates/wasmtime/src/runtime/gc/enabled/rooting.rs
index e3503e9882..05bee99013 100644
--- a/crates/wasmtime/src/runtime/gc/enabled/rooting.rs
+++ b/crates/wasmtime/src/runtime/gc/enabled/rooting.rs
@@ -1827,7 +1827,15 @@ where
val_raw: impl Fn(u32) -> ValRaw,
) -> Result<()> {
let gc_ref = self.try_clone_gc_ref(store)?;
- let raw = store.require_gc_store_mut()?.expose_gc_ref_to_wasm(gc_ref);
+
+ let raw = match store.optional_gc_store_mut() {
+ Some(s) => s.expose_gc_ref_to_wasm(gc_ref),
+ None => {
+ debug_assert!(gc_ref.is_i31());
+ gc_ref.as_raw_non_zero_u32()
+ }
+ };
+
ptr.write(val_raw(raw.get()));
Ok(())
}
diff --git a/tests/all/i31ref.rs b/tests/all/i31ref.rs
index e49dfec536..0bb6c5d600 100644
--- a/tests/all/i31ref.rs
+++ b/tests/all/i31ref.rs
@@ -40,3 +40,41 @@ fn i31ref_to_raw_round_trip() -> Result<()> {
Ok(())
}
+
+#[test]
+#[cfg_attr(miri, ignore)]
+fn owned_rooted_i31ref_through_typed_wasm_func() -> Result<()> {
+ // OwnedRooted<AnyRef>::wasm_ty_store should handle i31ref values without
+ // requiring a GC heap to be allocated.
+
+ let mut config = Config::new();
+ config.wasm_function_references(true);
+ config.wasm_gc(true);
+
+ let engine = Engine::new(&config)?;
+ let mut store = Store::new(&engine, ());
+
+ let module = Module::new(
+ &engine,
+ r#"(module (func (export "f") (param (ref null any)) (result (ref null any)) local.get 0))"#,
+ )?;
+ let instance = Instance::new(&mut store, &module, &[])?;
+ let f = instance.get_typed_func::<Option<OwnedRooted<AnyRef>>, Option<OwnedRooted<AnyRef>>>(
+ &mut store, "f",
+ )?;
+
+ // No GC heap objects created; the store has no GcStore allocated yet.
+ let anyref = AnyRef::from_i31(&mut store, I31::wrapping_u32(42));
+ let owned = anyref.to_owned_rooted(&mut store)?;
+ let result = f.call(&mut store, Some(owned))?.unwrap();
+ assert_eq!(
+ result
+ .to_rooted(&mut store)
+ .as_i31(&store)?
+ .unwrap()
+ .get_u32(),
+ 42
+ );
+
+ Ok(())
+}