Skip to content

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

Ensure trace info is available for `exnref` (#13326)

Alex Crichton0d31c5d28608
diff --git a/crates/wasmtime/src/runtime/externals/global.rs b/crates/wasmtime/src/runtime/externals/global.rs
index f08fea2526..31c8dfba91 100644
--- a/crates/wasmtime/src/runtime/externals/global.rs
+++ b/crates/wasmtime/src/runtime/externals/global.rs
@@ -310,7 +310,7 @@ impl Global {
return;
}
- if let Some(gc_ref) = unsafe { self.definition(store).as_ref().as_gc_ref() } {
+ if let Some(gc_ref) = unsafe { self.definition(store).as_mut().as_gc_ref_mut() } {
unsafe {
gc_roots_list.add_vmgcref_root(gc_ref.into(), "Wasm global");
}
diff --git a/crates/wasmtime/src/runtime/gc/enabled/arrayref.rs b/crates/wasmtime/src/runtime/gc/enabled/arrayref.rs
index 266d066de9..fb8b2d8126 100644
--- a/crates/wasmtime/src/runtime/gc/enabled/arrayref.rs
+++ b/crates/wasmtime/src/runtime/gc/enabled/arrayref.rs
@@ -76,14 +76,6 @@ impl ArrayRefPre {
pub(crate) fn _new(store: &mut StoreOpaque, ty: ArrayType) -> Self {
store.insert_gc_host_alloc_type(ty.registered_type().clone());
-
- // If a GC heap is already allocated, eagerly register trace info
- // now. Otherwise, trace info will be registered when the GC heap
- // is allocated in `StoreOpaque::allocate_gc_store`.
- if let Some(gc_store) = store.optional_gc_store_mut() {
- gc_store.ensure_trace_info(ty.registered_type().index());
- }
-
let store_id = store.id();
ArrayRefPre { store_id, ty }
}
diff --git a/crates/wasmtime/src/runtime/gc/enabled/exnref.rs b/crates/wasmtime/src/runtime/gc/enabled/exnref.rs
index a456a1b2fe..4374492cf7 100644
--- a/crates/wasmtime/src/runtime/gc/enabled/exnref.rs
+++ b/crates/wasmtime/src/runtime/gc/enabled/exnref.rs
@@ -80,7 +80,6 @@ impl ExnRefPre {
pub(crate) fn _new(store: &mut StoreOpaque, ty: ExnType) -> Self {
store.insert_gc_host_alloc_type(ty.registered_type().clone());
let store_id = store.id();
-
ExnRefPre { store_id, ty }
}
diff --git a/crates/wasmtime/src/runtime/gc/enabled/structref.rs b/crates/wasmtime/src/runtime/gc/enabled/structref.rs
index d9d5dc5c8b..74294788cc 100644
--- a/crates/wasmtime/src/runtime/gc/enabled/structref.rs
+++ b/crates/wasmtime/src/runtime/gc/enabled/structref.rs
@@ -76,14 +76,6 @@ impl StructRefPre {
pub(crate) fn _new(store: &mut StoreOpaque, ty: StructType) -> Self {
store.insert_gc_host_alloc_type(ty.registered_type().clone());
-
- // If a GC heap is already allocated, eagerly register trace info
- // now. Otherwise, trace info will be registered when the GC heap
- // is allocated in `StoreOpaque::allocate_gc_store`.
- if let Some(gc_store) = store.optional_gc_store_mut() {
- gc_store.ensure_trace_info(ty.registered_type().index());
- }
-
let store_id = store.id();
StructRefPre { store_id, ty }
}
diff --git a/crates/wasmtime/src/runtime/store.rs b/crates/wasmtime/src/runtime/store.rs
index 20465faeab..a478446a51 100644
--- a/crates/wasmtime/src/runtime/store.rs
+++ b/crates/wasmtime/src/runtime/store.rs
@@ -2363,6 +2363,12 @@ impl StoreOpaque {
/// store are holding it alive).
#[cfg(feature = "gc")]
pub(crate) fn insert_gc_host_alloc_type(&mut self, ty: crate::type_registry::RegisteredType) {
+ // If a GC heap is already allocated, eagerly register trace info
+ // now. Otherwise, trace info will be registered when the GC heap
+ // is allocated in `StoreOpaque::allocate_gc_store`.
+ if let Some(gc_store) = self.optional_gc_store_mut() {
+ gc_store.ensure_trace_info(ty.index());
+ }
self.gc_host_alloc_types.insert(ty);
}
diff --git a/crates/wasmtime/src/runtime/vm/vmcontext.rs b/crates/wasmtime/src/runtime/vm/vmcontext.rs
index 447391c6fd..d2791a1498 100644
--- a/crates/wasmtime/src/runtime/vm/vmcontext.rs
+++ b/crates/wasmtime/src/runtime/vm/vmcontext.rs
@@ -795,6 +795,14 @@ impl VMGlobalDefinition {
ret
}
+ /// Return a reference to the global value as a borrowed GC reference.
+ pub unsafe fn as_gc_ref_mut(&mut self) -> Option<&mut VMGcRef> {
+ let raw_ptr = self.storage.as_mut().as_mut_ptr().cast::<Option<VMGcRef>>();
+ let ret = unsafe { (*raw_ptr).as_mut() };
+ assert!(cfg!(feature = "gc") || ret.is_none());
+ ret
+ }
+
/// Initialize a global to the given GC reference.
pub unsafe fn init_gc_ref(&mut self, store: &mut StoreOpaque, gc_ref: Option<&VMGcRef>) {
let dest = unsafe {
diff --git a/tests/all/arrays.rs b/tests/all/arrays.rs
index 5658c80fe2..31f68b306e 100644
--- a/tests/all/arrays.rs
+++ b/tests/all/arrays.rs
@@ -1000,3 +1000,45 @@ fn issue_13034_array_layout_overflow() -> Result<()> {
}
Ok(())
}
+
+#[test]
+fn host_arrayref_has_trace_info_for_gc() -> Result<()> {
+ for collector in [Collector::Copying, Collector::DeferredReferenceCounting] {
+ println!("Using GC collector: {collector:?}");
+
+ let mut config = Config::new();
+ config.wasm_exceptions(true).wasm_gc(true);
+ config.collector(collector);
+
+ let engine = Engine::new(&config)?;
+
+ // Create a store and allocate the GC store by instantiating a module
+ let mut store = Store::new(&engine, ());
+ let module = Module::new(
+ &engine,
+ r#"(module
+ (global (export "g") (mut arrayref) (ref.null array))
+ (table 1 anyref)
+ )"#,
+ )?;
+ let instance = Instance::new(&mut store, &module, &[])?;
+ let g = instance.get_global(&mut store, "g").unwrap();
+
+ // Allocate a host arrayref object in a nested scope and put it into the
+ // module that was just allocated.
+ let array_ty = ArrayType::new(&engine, FieldType::new(Mutability::Const, StorageType::I8));
+ let arraypre = ArrayRefPre::new(&mut store, array_ty.clone());
+ {
+ let mut scope = RootScope::new(&mut store);
+ let array = ArrayRef::new(&mut scope, &arraypre, &Val::I32(29), 1)?;
+ g.set(&mut scope, Val::AnyRef(Some(array.into())))?;
+ }
+
+ // The arrayref should stay alive and be traced properly...
+ store.gc(None)?;
+
+ assert!(matches!(g.get(&mut store), Val::AnyRef(Some(_))));
+ }
+
+ Ok(())
+}
diff --git a/tests/all/exnrefs.rs b/tests/all/exnrefs.rs
index e1586242b7..6251d01ff6 100644
--- a/tests/all/exnrefs.rs
+++ b/tests/all/exnrefs.rs
@@ -83,3 +83,46 @@ fn exn_objects() -> Result<()> {
Ok(())
}
+
+#[test]
+fn host_exnref_has_trace_info_for_gc() -> Result<()> {
+ for collector in [Collector::Copying, Collector::DeferredReferenceCounting] {
+ println!("Using GC collector: {collector:?}");
+
+ let mut config = Config::new();
+ config.wasm_exceptions(true).wasm_gc(true);
+ config.collector(collector);
+
+ let engine = Engine::new(&config)?;
+
+ // Create a store and allocate the GC store by instantiating a module
+ let mut store = Store::new(&engine, ());
+ let module = Module::new(
+ &engine,
+ r#"(module
+ (global (export "g") (mut exnref) (ref.null exn))
+ (table 1 anyref)
+ )"#,
+ )?;
+ let instance = Instance::new(&mut store, &module, &[])?;
+ let g = instance.get_global(&mut store, "g").unwrap();
+
+ // Allocate a host exnref object in a nested scope and put it into the
+ // module that was just allocated.
+ let exn_ty = ExnType::new(&engine, [ValType::I32])?;
+ let exnpre = ExnRefPre::new(&mut store, exn_ty.clone());
+ let tag = Tag::new(&mut store, &exn_ty.tag_type())?;
+ {
+ let mut scope = RootScope::new(&mut store);
+ let exn = ExnRef::new(&mut scope, &exnpre, &tag, &[Val::I32(43)])?;
+ g.set(&mut scope, Val::ExnRef(Some(exn)))?;
+ }
+
+ // The exnref should stay alive and be traced properly...
+ store.gc(None)?;
+
+ assert!(matches!(g.get(&mut store), Val::ExnRef(Some(_))));
+ }
+
+ Ok(())
+}
diff --git a/tests/all/structs.rs b/tests/all/structs.rs
index 0de1f378a2..59e14ae087 100644
--- a/tests/all/structs.rs
+++ b/tests/all/structs.rs
@@ -910,3 +910,48 @@ fn issue_9714(config: &mut Config) -> Result<()> {
Ok(())
}
+
+#[test]
+fn host_structref_has_trace_info_for_gc() -> Result<()> {
+ for collector in [Collector::Copying, Collector::DeferredReferenceCounting] {
+ println!("Using GC collector: {collector:?}");
+
+ let mut config = Config::new();
+ config.wasm_exceptions(true).wasm_gc(true);
+ config.collector(collector);
+
+ let engine = Engine::new(&config)?;
+
+ // Create a store and allocate the GC store by instantiating a module
+ let mut store = Store::new(&engine, ());
+ let module = Module::new(
+ &engine,
+ r#"(module
+ (global (export "g") (mut structref) (ref.null struct))
+ (table 1 anyref)
+ )"#,
+ )?;
+ let instance = Instance::new(&mut store, &module, &[])?;
+ let g = instance.get_global(&mut store, "g").unwrap();
+
+ // Allocate a host structref object in a nested scope and put it into the
+ // module that was just allocated.
+ let struct_ty = StructType::new(
+ &engine,
+ [FieldType::new(Mutability::Const, StorageType::I8)],
+ )?;
+ let structpre = StructRefPre::new(&mut store, struct_ty.clone());
+ {
+ let mut scope = RootScope::new(&mut store);
+ let s = StructRef::new(&mut scope, &structpre, &[Val::I32(29)])?;
+ g.set(&mut scope, Val::AnyRef(Some(s.into())))?;
+ }
+
+ // The structref should stay alive and be traced properly...
+ store.gc(None)?;
+
+ assert!(matches!(g.get(&mut store), Val::AnyRef(Some(_))));
+ }
+
+ Ok(())
+}