A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
gc: Avoid recursive drain drop (#14735)
Alex Crichtona55c4a5bff2b
diff --git a/crates/wasmtime/src/runtime/type_registry.rs b/crates/wasmtime/src/runtime/type_registry.rsindex b442818823..861683fcdd 100644--- a/crates/wasmtime/src/runtime/type_registry.rs+++ b/crates/wasmtime/src/runtime/type_registry.rs@@ -702,6 +702,11 @@ struct TypeRegistryInner {// to avoid recursion when dropping a type that is holding the last// reference to another type, etc...drop_stack: TryVec<RecGroupEntry>,++ #[cfg(debug_assertions)]+ /// Is set if we are already within a call to `drain_drop_stack`. Used to+ /// debug assert that `drain_drop_stock` does not recursively call itself.+ draining_drop_stack: bool,}impl TypeRegistryInner {@@ -1165,6 +1170,10 @@ impl TypeRegistryInner {}/// Remove the rec group's associated trampoline information.+ ///+ /// This function pushes onto the drop stack without calling+ /// `drain_drop_stack`. So it should only be called from within a+ /// `drain_drop_stack` loop.fn remove_entry_trampolines(&mut self, entry: &RecGroupEntry) {// We delay allocating space for this arena until we actually see a// function type that is not its own trampoline type, so early exit if@@ -1182,8 +1191,6 @@ impl TypeRegistryInner {}}}-- self.drain_drop_stack();}/// Insert the GC layout information for this rec group's types.@@ -1297,6 +1304,10 @@ impl TypeRegistryInner {/// For each cross-rec group type reference inside `entry`, decrement the/// referenced rec group's registration count.+ ///+ /// This function pushes to the drop stack without calling+ /// `drain_drop_stack`. So it should only be called from within a+ /// `drain_drop_stack` loop itself.fn decref_outgoing_edges(&mut self, entry: &RecGroupEntry) {let key = &entry.0.hash_consing_key;debug_assert!(key.is_canonicalized_for_hash_consing());@@ -1310,7 +1321,6 @@ impl TypeRegistryInner {Ok(())}).unwrap();- self.drain_drop_stack();}/// Pre-allocate capacity for a rec group of size `num_types` in our various@@ -1332,6 +1342,8 @@ impl TypeRegistryInner {type_to_supertypes: _,type_to_trampoline: _,type_to_gc_layout: _,+ #[cfg(debug_assertions)]+ draining_drop_stack: _,} = self;// This map contains rec groups, not types, so only reserve space for@@ -1590,8 +1602,9 @@ impl TypeRegistryInner {/// Enqueue an entry for dropping.///- /// Should always be followed by some call to `drain_drop_stack()`, although- /// it is fine to associate many `push_to_drop_stack()` calls with one final+ /// Should always be called within the `drain_drop_stack` loop *xor*+ /// followed by some call to `drain_drop_stack()`. Although it is fine to+ /// associate many `push_to_drop_stack()` calls with one final/// `drain_drop_stack()` call.fn push_to_drop_stack(&mut self, entry: RecGroupEntry) {log::trace!("Pushing entry to drop stack: {entry:?}");@@ -1607,17 +1620,34 @@ impl TypeRegistryInner {/// unregister that other type as well. However, we use an explicit drop/// stack to avoid recursion and the potential stack overflows that/// recursion implies.+ ///+ /// # Warning+ ///+ /// In order to avoid recursion and stack overflows, `drain_drop_stack` must+ /// not recursively call itself. Ensure this invariant is maintained+ /// whenever adding a call to `drain_drop_stack` or modifying its+ /// implementation.fn drain_drop_stack(&mut self) {- if self.drop_stack.is_empty() {- return;+ #[cfg(debug_assertions)]+ {+ debug_assert!(+ !self.draining_drop_stack,+ "`drain_drop_stack` must not recursively call itself"+ );+ self.draining_drop_stack = true;}- log::trace!("Draining drop stack");+while let Some(entry) = self.drop_stack.pop() {log::trace!("Begin unregistering {entry:?}");self.debug_assert_all_registered(&entry);self.remove_entry_impl(&entry, true);log::trace!("End unregistering {entry:?}");}++ #[cfg(debug_assertions)]+ {+ self.draining_drop_stack = false;+ }}}@@ -1634,6 +1664,7 @@ impl Drop for TypeRegistryInner {type_to_trampoline,type_to_gc_layout,drop_stack,+ draining_drop_stack: _,} = self;assert!(hash_consing_map.is_empty(),diff --git a/tests/all/types.rs b/tests/all/types.rsindex 7d97a3bd18..5fc6d16725 100644--- a/tests/all/types.rs+++ b/tests/all/types.rs@@ -738,3 +738,59 @@ fn heap_type_matches_noexn() -> Result<()> {Ok(())}++#[test]+#[cfg_attr(miri, ignore)]+/// Check that dropping a long guest-defined type from the registry doesn't+/// stack overflow.+fn drop_long_chain_of_guest_func_types() -> Result<()> {+ // A chain of 20,000 function types where each type's parameter is a+ // reference to the previous type.+ const N: usize = 20_000;++ let mut wat = String::from("(module\n (type $t0 (func))\n");+ for i in 1..N {+ wat.push_str(&format!(+ " (type $t{i} (func (param (ref null $t{}))))\n",+ i - 1+ ));+ }+ wat.push_str(&format!(+ " (func (export \"f\") (type $t{}) unreachable)\n)\n",+ N - 1+ ));++ let mut config = Config::new();+ config.wasm_function_references(true);+ let engine = Engine::new(&config)?;+ let module = Module::new(&engine, &wat)?;+ let mut store = Store::new(&engine, ());+ let instance = Instance::new(&mut store, &module, &[])?;++ let trap = instance+ .get_func(&mut store, "f")+ .unwrap()+ .call(&mut store, &[Val::FuncRef(None)], &mut [])+ .unwrap_err()+ .downcast::<Trap>()?;+ assert_eq!(trap, Trap::UnreachableCodeReached);++ Ok(())+}++#[test]+/// Check that dropping a long host-defined type from the registry doesn't stack+/// overflow.+fn drop_long_chain_of_host_func_types() -> Result<()> {+ // Dropping the last type in a long chain of rec groups, each of which+ // references the previous, should not overflow the stack.+ let engine = Engine::default();+ let mut ty = FuncType::new(&engine, [], []);+ let amt = if cfg!(miri) { 10 } else { 100_000 };+ for _ in 0..amt {+ let param = ValType::Ref(RefType::new(true, HeapType::ConcreteFunc(ty)));+ ty = FuncType::new(&engine, [param], []);+ }+ drop(ty);+ Ok(())+}