Ensure default allocator is used for instance deallocation.

Handles created with `create_handle` need to be deallocated with the default
(on-demand) instance allocator.

This commit changes Store such that handles can be added with a flag that is
used to force deallocation via the default instance allocator when the Store is
dropped.
This commit is contained in:
Peter Huene
2020-12-14 17:12:54 -08:00
parent 5beb81d02a
commit 8457261cfe
3 changed files with 32 additions and 9 deletions

View File

@@ -520,7 +520,7 @@ impl<'a> Instantiator<'a> {
// initializers may have run which placed elements into other instance's // initializers may have run which placed elements into other instance's
// tables. This means that from this point on, regardless of whether // tables. This means that from this point on, regardless of whether
// initialization is successful, we need to keep the instance alive. // 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 allocator
.initialize( .initialize(
&instance.handle, &instance.handle,

View File

@@ -18,10 +18,19 @@ use std::task::{Context, Poll};
use wasmtime_environ::wasm; use wasmtime_environ::wasm;
use wasmtime_jit::{CompiledModule, ModuleCode, TypeTables}; use wasmtime_jit::{CompiledModule, ModuleCode, TypeTables};
use wasmtime_runtime::{ use wasmtime_runtime::{
InstanceHandle, SignalHandler, StackMapRegistry, TrapInfo, VMContext, VMExternRef, InstanceAllocator, InstanceHandle, SignalHandler, StackMapRegistry, TrapInfo, VMContext,
VMExternRefActivationsTable, VMInterrupts, VMSharedSignatureIndex, 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. /// 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 /// All WebAssembly instances and items will be attached to and refer to a
@@ -63,7 +72,7 @@ pub(crate) struct StoreInner {
engine: Engine, engine: Engine,
interrupts: Arc<VMInterrupts>, interrupts: Arc<VMInterrupts>,
signatures: RefCell<SignatureRegistry>, signatures: RefCell<SignatureRegistry>,
instances: RefCell<Vec<InstanceHandle>>, instances: RefCell<Vec<StoreInstance>>,
signal_handler: RefCell<Option<Box<SignalHandler<'static>>>>, signal_handler: RefCell<Option<Box<SignalHandler<'static>>>>,
externref_activations_table: VMExternRefActivationsTable, externref_activations_table: VMExternRefActivationsTable,
stack_map_registry: StackMapRegistry, stack_map_registry: StackMapRegistry,
@@ -374,8 +383,15 @@ impl Store {
Ok(()) Ok(())
} }
pub(crate) unsafe fn add_instance(&self, handle: InstanceHandle) -> StoreInstanceHandle { pub(crate) unsafe fn add_instance(
self.inner.instances.borrow_mut().push(handle.clone()); &self,
handle: InstanceHandle,
use_default_allocator: bool,
) -> StoreInstanceHandle {
self.inner.instances.borrow_mut().push(StoreInstance {
handle: handle.clone(),
use_default_allocator,
});
StoreInstanceHandle { StoreInstanceHandle {
store: self.clone(), store: self.clone(),
handle, handle,
@@ -388,7 +404,7 @@ impl Store {
.instances .instances
.borrow() .borrow()
.iter() .iter()
.any(|i| i.vmctx_ptr() == handle.vmctx_ptr())); .any(|i| i.handle.vmctx_ptr() == handle.vmctx_ptr()));
StoreInstanceHandle { StoreInstanceHandle {
store: self.clone(), store: self.clone(),
handle, handle,
@@ -963,7 +979,14 @@ impl Drop for StoreInner {
let allocator = self.engine.config().instance_allocator(); let allocator = self.engine.config().instance_allocator();
for instance in self.instances.borrow().iter() { for instance in self.instances.borrow().iter() {
unsafe { unsafe {
allocator.deallocate(instance); if instance.use_default_allocator {
self.engine
.config()
.default_instance_allocator
.deallocate(&instance.handle);
} else {
allocator.deallocate(&instance.handle);
}
} }
} }
} }

View File

@@ -44,6 +44,6 @@ pub(crate) fn create_handle(
stack_map_registry: store.stack_map_registry() as *const StackMapRegistry as *mut _, stack_map_registry: store.stack_map_registry() as *const StackMapRegistry as *mut _,
})?; })?;
Ok(store.add_instance(handle)) Ok(store.add_instance(handle, true))
} }
} }