Skip to content

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

Fix type registry leak when register_module_types fails midway through multiple rec groups (#13372)

Nick Fitzgeralda4e28b760f6a
diff --git a/crates/fuzzing/tests/oom/module.rs b/crates/fuzzing/tests/oom/module.rs
index e63186e30b..20c634a053 100644
--- a/crates/fuzzing/tests/oom/module.rs
+++ b/crates/fuzzing/tests/oom/module.rs
@@ -48,3 +48,35 @@ fn module_deserialize() -> Result<()> {
Ok(())
})
}
+
+#[test]
+fn module_deserialize_multiple_rec_groups() -> Result<()> {
+ let module_bytes = {
+ let mut config = Config::new();
+ config.concurrency_support(false);
+ let engine = Engine::new(&config)?;
+ Module::new(
+ &engine,
+ r#"
+ (module
+ (type (func (param i64 i64 i64 i64 i64 i64 i64 i64
+ i64 i64 i64 i64 i64 i64 i64 i64
+ i64 i64 i64 i64) (result i64)))
+ (type (func (param i64 i64 i64 i64 i64 i64 i64) (result i32)))
+ (global (mut i32) i32.const 1000)
+ )
+ "#,
+ )?
+ .serialize()?
+ };
+
+ let mut config = Config::new();
+ config.enable_compiler(false);
+ config.concurrency_support(false);
+ let engine = Engine::new(&config)?;
+
+ OomTest::new().allow_alloc_after_oom(true).test(|| unsafe {
+ let _ = Module::deserialize(&engine, &module_bytes)?;
+ Ok(())
+ })
+}
diff --git a/crates/wasmtime/src/runtime/type_registry.rs b/crates/wasmtime/src/runtime/type_registry.rs
index e9266d456e..d8bc147387 100644
--- a/crates/wasmtime/src/runtime/type_registry.rs
+++ b/crates/wasmtime/src/runtime/type_registry.rs
@@ -702,18 +702,40 @@ impl TypeRegistryInner {
log::trace!("Start registering module types");
// The engine's type registry entries for these module types.
- let mut entries = TryVec::with_capacity(types.rec_groups().len())?;
+ let mut entries = TryVec::new();
// The map from a module type index to an engine type index for these
// module types.
- let mut map = TryPrimaryMap::<ModuleInternedTypeIndex, VMSharedTypeIndex>::with_capacity(
- types.wasm_types().len(),
- )?;
+ let mut map = TryPrimaryMap::<ModuleInternedTypeIndex, VMSharedTypeIndex>::new();
+
+ if let Err(e) = self.register_module_types_impl(gc_runtime, types, &mut entries, &mut map) {
+ for entry in entries {
+ if entry.decref("register_module_types cleanup") {
+ self.unregister_entry(entry);
+ }
+ }
+ return Err(e);
+ }
+
+ log::trace!("End registering module types");
+
+ Ok((entries, map))
+ }
+
+ fn register_module_types_impl(
+ &mut self,
+ gc_runtime: Option<&dyn GcRuntime>,
+ types: &ModuleTypes,
+ entries: &mut TryVec<RecGroupEntry>,
+ map: &mut TryPrimaryMap<ModuleInternedTypeIndex, VMSharedTypeIndex>,
+ ) -> Result<(), OutOfMemory> {
+ entries.reserve(types.rec_groups().len())?;
+ map.reserve(types.wasm_types().len())?;
for (_rec_group_index, module_group) in types.rec_groups() {
let entry = self.register_rec_group(
gc_runtime,
- &map,
+ map,
module_group.clone(),
iter_entity_range(module_group.clone()).map(|ty| types[ty].try_clone()),
)?;
@@ -730,9 +752,7 @@ impl TypeRegistryInner {
entries.push(entry).expect("reserved capacity");
}
- log::trace!("End registering module types");
-
- Ok((entries, map))
+ Ok(())
}
/// Register a rec group in this registry.