Skip to content

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

Fix Table::grow panic on unrooted GC init reference (#14931)

Viktor Kravetsc50128f314a7
diff --git a/crates/wasmtime/src/runtime/externals/table.rs b/crates/wasmtime/src/runtime/externals/table.rs
index 841384fe8a..da4b71ded1 100644
--- a/crates/wasmtime/src/runtime/externals/table.rs
+++ b/crates/wasmtime/src/runtime/externals/table.rs
@@ -696,4 +696,25 @@ mod tests {
_assert_send(table.grow(store, 0, init))
}
}
+
+ #[test]
+ #[cfg(feature = "gc")]
+ fn grow_with_unrooted_ref_returns_err() -> Result<()> {
+ let mut config = crate::Config::new();
+ config.wasm_gc(true);
+ let engine = crate::Engine::new(&config)?;
+ let mut store = Store::new(&engine, ());
+ let ty = crate::TableType::new(crate::RefType::EXTERNREF, 2, None)?;
+ let table = Table::new(&mut store, ty, Ref::Extern(None))?;
+
+ let unrooted = {
+ let mut scope = crate::RootScope::new(&mut store);
+ ExternRef::new(&mut scope, 1u32)?
+ };
+
+ let res = table.grow(&mut store, 1, Ref::Extern(Some(unrooted)));
+ assert!(res.is_err());
+ assert_eq!(table.size(&store), 2);
+ Ok(())
+ }
}
diff --git a/crates/wasmtime/src/runtime/values.rs b/crates/wasmtime/src/runtime/values.rs
index 3813279e42..ddab81ebcd 100644
--- a/crates/wasmtime/src/runtime/values.rs
+++ b/crates/wasmtime/src/runtime/values.rs
@@ -1039,7 +1039,10 @@ impl Ref {
// `HeapType::Func`.
match self {
Ref::Extern(None) => HeapType::NoExtern,
- Ref::Extern(Some(_)) => HeapType::Extern,
+ Ref::Extern(Some(e)) => {
+ e.try_gc_ref(store)?;
+ HeapType::Extern
+ }
Ref::Func(None) => HeapType::NoFunc,
Ref::Func(Some(f)) => HeapType::ConcreteFunc(f.load_ty(store)),
@@ -1071,7 +1074,11 @@ impl Ref {
return Ok(false);
}
Ok(match (self, ty.heap_type()) {
- (Ref::Extern(_), HeapType::Extern) => true,
+ (Ref::Extern(Some(e)), HeapType::Extern) => {
+ e.try_gc_ref(store)?;
+ true
+ }
+ (Ref::Extern(None), HeapType::Extern) => true,
(Ref::Extern(None), HeapType::NoExtern) => true,
(Ref::Extern(_), _) => false,
@@ -1080,7 +1087,11 @@ impl Ref {
(Ref::Func(Some(f)), HeapType::ConcreteFunc(func_ty)) => f._matches_ty(store, func_ty),
(Ref::Func(_), _) => false,
- (Ref::Any(_), HeapType::Any) => true,
+ (Ref::Any(Some(a)), HeapType::Any) => {
+ a.try_gc_ref(store)?;
+ true
+ }
+ (Ref::Any(None), HeapType::Any) => true,
(Ref::Any(Some(a)), HeapType::I31) => a._is_i31(store)?,
(Ref::Any(Some(a)), HeapType::Struct) => a._is_struct(store)?,
(Ref::Any(Some(a)), HeapType::ConcreteStruct(_ty)) => match a._as_struct(store)? {
@@ -1105,7 +1116,11 @@ impl Ref {
) => true,
(Ref::Any(_), _) => false,
- (Ref::Exn(_), HeapType::Exn) => true,
+ (Ref::Exn(Some(e)), HeapType::Exn) => {
+ e.try_gc_ref(store)?;
+ true
+ }
+ (Ref::Exn(None), HeapType::Exn) => true,
(Ref::Exn(None), HeapType::NoExn | HeapType::ConcreteExn(_)) => true,
(Ref::Exn(Some(e)), HeapType::ConcreteExn(_)) => {
e._matches_ty(store, &ty.heap_type())?