diff --git a/crates/wasmtime/src/instance.rs b/crates/wasmtime/src/instance.rs index 87936fd535..7048d93338 100644 --- a/crates/wasmtime/src/instance.rs +++ b/crates/wasmtime/src/instance.rs @@ -520,7 +520,7 @@ impl<'a> Instantiator<'a> { // initializers may have run which placed elements into other instance's // tables. This means that from this point on, regardless of whether // initialization is successful, we need to keep the instance alive. - let instance = self.store.add_instance(instance); + let instance = self.store.add_instance(instance, false); allocator .initialize( &instance.handle, diff --git a/crates/wasmtime/src/store.rs b/crates/wasmtime/src/store.rs index dbaa5eb44c..9445f852a2 100644 --- a/crates/wasmtime/src/store.rs +++ b/crates/wasmtime/src/store.rs @@ -18,10 +18,19 @@ use std::task::{Context, Poll}; use wasmtime_environ::wasm; use wasmtime_jit::{CompiledModule, ModuleCode, TypeTables}; use wasmtime_runtime::{ - InstanceHandle, SignalHandler, StackMapRegistry, TrapInfo, VMContext, VMExternRef, - VMExternRefActivationsTable, VMInterrupts, VMSharedSignatureIndex, + InstanceAllocator, InstanceHandle, SignalHandler, StackMapRegistry, TrapInfo, VMContext, + VMExternRef, VMExternRefActivationsTable, VMInterrupts, VMSharedSignatureIndex, }; +/// Used to associate instances with the store. +/// +/// This is needed to track if the instance was allocated expliclty with the default +/// instance allocator. +struct StoreInstance { + handle: InstanceHandle, + use_default_allocator: bool, +} + /// A `Store` is a collection of WebAssembly instances and host-defined items. /// /// All WebAssembly instances and items will be attached to and refer to a @@ -63,7 +72,7 @@ pub(crate) struct StoreInner { engine: Engine, interrupts: Arc, signatures: RefCell, - instances: RefCell>, + instances: RefCell>, signal_handler: RefCell>>>, externref_activations_table: VMExternRefActivationsTable, stack_map_registry: StackMapRegistry, @@ -374,8 +383,15 @@ impl Store { Ok(()) } - pub(crate) unsafe fn add_instance(&self, handle: InstanceHandle) -> StoreInstanceHandle { - self.inner.instances.borrow_mut().push(handle.clone()); + pub(crate) unsafe fn add_instance( + &self, + handle: InstanceHandle, + use_default_allocator: bool, + ) -> StoreInstanceHandle { + self.inner.instances.borrow_mut().push(StoreInstance { + handle: handle.clone(), + use_default_allocator, + }); StoreInstanceHandle { store: self.clone(), handle, @@ -388,7 +404,7 @@ impl Store { .instances .borrow() .iter() - .any(|i| i.vmctx_ptr() == handle.vmctx_ptr())); + .any(|i| i.handle.vmctx_ptr() == handle.vmctx_ptr())); StoreInstanceHandle { store: self.clone(), handle, @@ -963,7 +979,14 @@ impl Drop for StoreInner { let allocator = self.engine.config().instance_allocator(); for instance in self.instances.borrow().iter() { unsafe { - allocator.deallocate(instance); + if instance.use_default_allocator { + self.engine + .config() + .default_instance_allocator + .deallocate(&instance.handle); + } else { + allocator.deallocate(&instance.handle); + } } } } diff --git a/crates/wasmtime/src/trampoline/create_handle.rs b/crates/wasmtime/src/trampoline/create_handle.rs index ff088b9aa1..71595ff729 100644 --- a/crates/wasmtime/src/trampoline/create_handle.rs +++ b/crates/wasmtime/src/trampoline/create_handle.rs @@ -44,6 +44,6 @@ pub(crate) fn create_handle( stack_map_registry: store.stack_map_registry() as *const StackMapRegistry as *mut _, })?; - Ok(store.add_instance(handle)) + Ok(store.add_instance(handle, true)) } }