Skip to content

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

Fix pooling allocator instance count leak on OOM during instantiation (#13374)

Nick Fitzgerald3503c4c8ecfd
diff --git a/crates/wasmtime/src/runtime/store.rs b/crates/wasmtime/src/runtime/store.rs
index fe93748a94..73adf8cd22 100644
--- a/crates/wasmtime/src/runtime/store.rs
+++ b/crates/wasmtime/src/runtime/store.rs
@@ -2289,6 +2289,8 @@ at https://bytecodealliance.org/security.
runtime_info: &ModuleRuntimeInfo,
imports: Imports<'_>,
) -> Result<InstanceId> {
+ self.instances.reserve(1)?;
+
let id = self.instances.next_key();
let allocator = match kind {
@@ -2315,20 +2317,24 @@ at https://bytecodealliance.org/security.
"Adding instance to store: store={:?}, module={module_id:?}, instance={id:?}",
self.id()
);
- self.instances.push(StoreInstance {
- handle,
- kind: StoreInstanceKind::Real { module_id },
- })?
+ self.instances
+ .push(StoreInstance {
+ handle,
+ kind: StoreInstanceKind::Real { module_id },
+ })
+ .expect("capacity was reserved above")
}
AllocateInstanceKind::Dummy { .. } => {
log::trace!(
"Adding dummy instance to store: store={:?}, instance={id:?}",
self.id()
);
- self.instances.push(StoreInstance {
- handle,
- kind: StoreInstanceKind::Dummy,
- })?
+ self.instances
+ .push(StoreInstance {
+ handle,
+ kind: StoreInstanceKind::Dummy,
+ })
+ .expect("capacity was reserved above")
}
};
diff --git a/crates/wasmtime/src/runtime/vm/instance/allocator.rs b/crates/wasmtime/src/runtime/vm/instance/allocator.rs
index 97b5b8fe32..41b7670dcd 100644
--- a/crates/wasmtime/src/runtime/vm/instance/allocator.rs
+++ b/crates/wasmtime/src/runtime/vm/instance/allocator.rs
@@ -345,17 +345,18 @@ impl dyn InstanceAllocator + '_ {
.await?;
self.allocate_tables(&mut request, &mut guard.tables)
.await?;
- guard.run_deallocate = false;
// SAFETY: memories/tables were just allocated from the store within
// `request` and this function's own contract requires that the
// imports are valid.
- return unsafe {
- Ok(Instance::new(
+ let handle = unsafe {
+ Instance::new(
request,
mem::take(&mut guard.memories),
mem::take(&mut guard.tables),
- )?)
+ )?
};
+ guard.run_deallocate = false;
+ return Ok(handle);
struct DeallocateOnDrop<'a> {
run_deallocate: bool,