A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Fix DRC leak when passing same object to Wasm multiple times (#13166)
Nick Fitzgerald5b149b152cef
diff --git a/crates/wasmtime/src/runtime/vm/gc/enabled/drc.rs b/crates/wasmtime/src/runtime/vm/gc/enabled/drc.rsindex efe0f0cc21..7437021e19 100644--- a/crates/wasmtime/src/runtime/vm/gc/enabled/drc.rs+++ b/crates/wasmtime/src/runtime/vm/gc/enabled/drc.rs@@ -930,12 +930,21 @@ unsafe impl GcHeap for DrcHeap {let next = (*self.over_approximated_stack_roots).as_ref().map(|r| r.unchecked_copy());+let header = self.index_mut(drc_ref(&gc_ref));if header.is_in_over_approximated_stack_roots() {- // Already in the over-approximated-stack-roots list, nothing more- // to do here.+ // Already in the over-approximated-stack-roots list. Decrement the+ // object's ref count because the OASR list can't hold multiple+ // copies of the same GC reference.+ let ref_count_is_zero = header.dec_ref();+ debug_assert!(+ !ref_count_is_zero,+ "should not have reached refcount == 0 because the OASR list \+ is holding a reference"+ );return;}+// Push this object onto the head of the over-approximated-stack-roots// list using a single index_mut call.header.set_in_over_approximated_stack_roots_bit(true);diff --git a/tests/all/gc.rs b/tests/all/gc.rsindex 0f7d3e2f87..7204f897f6 100644--- a/tests/all/gc.rs+++ b/tests/all/gc.rs@@ -1,5 +1,6 @@use super::ref_types_module;use std::sync::Arc;+use std::sync::atomic::Ordering;use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering::SeqCst};use wasmtime::*;@@ -1927,3 +1928,37 @@ fn gc_heap_guard_pages_but_no_memory_guard_pages() -> Result<()> {Ok(())}++#[test]+#[cfg_attr(miri, ignore)]+fn issue_13037_drc_leak_passing_objects_already_in_over_approx_stack_roots_list() -> Result<()> {+ let _ = env_logger::try_init();++ let mut config = Config::new();+ config.collector(Collector::DeferredReferenceCounting);+ let engine = Engine::new(&config)?;+ let mut store = Store::new(&engine, ());++ let module = Module::new(+ &engine,+ r#"(module (func (export "nop") (param externref)))"#,+ )?;++ let instance = Instance::new(&mut store, &module, &[])?;+ let nop = instance.get_typed_func::<Option<Rooted<ExternRef>>, ()>(&mut store, "nop")?;++ let dropped = Arc::new(AtomicBool::new(false));+ {+ let mut scope = RootScope::new(&mut store);+ let ext = ExternRef::new(&mut scope, SetFlagOnDrop(dropped.clone()))?;++ nop.call(&mut scope, Some(ext))?;+ nop.call(&mut scope, Some(ext))?;+ }++ store.gc(None)?;++ assert!(dropped.load(Ordering::SeqCst));++ Ok(())+}