From cc5648f118947f64e15a25620fd8ed1c2ac28ca0 Mon Sep 17 00:00:00 2001 From: Mark McCaskey Date: Tue, 8 Dec 2020 17:22:41 -0800 Subject: [PATCH] Remove middle step --- lib/engine/src/artifact.rs | 8 ++-- lib/vm/src/instance.rs | 77 ++++++++++++-------------------------- lib/vm/src/lib.rs | 4 +- 3 files changed, 30 insertions(+), 59 deletions(-) diff --git a/lib/engine/src/artifact.rs b/lib/engine/src/artifact.rs index 98cb45763..70a462be4 100644 --- a/lib/engine/src/artifact.rs +++ b/lib/engine/src/artifact.rs @@ -95,7 +95,6 @@ pub trait Artifact: Send + Sync + Upcastable { self.preinstantiate()?; let module = self.module(); - let unprepared = InstanceHandle::allocate_instance(&*module); let (imports, import_initializers) = { let mut imports = resolve_imports( &module, @@ -115,8 +114,9 @@ pub trait Artifact: Send + Sync + Upcastable { // Get pointers to where metadata about local memories should live in VM memory. // Get pointers to where metadata about local tables should live in VM memory. - let (half_prepared, memory_definition_locations, table_definition_locations) = - unprepared.prepare(); + + let (unprepared, memory_definition_locations, table_definition_locations) = + InstanceHandle::allocate_instance(&*module); let finished_memories = tunables .create_memories(&module, self.memory_styles(), &memory_definition_locations) .map_err(InstantiationError::Link)? @@ -133,7 +133,7 @@ pub trait Artifact: Send + Sync + Upcastable { self.register_frame_info(); let handle = InstanceHandle::new( - half_prepared, + unprepared, module, self.finished_functions().clone(), self.finished_function_call_trampolines().clone(), diff --git a/lib/vm/src/instance.rs b/lib/vm/src/instance.rs index 8f086001f..c4a47eff8 100644 --- a/lib/vm/src/instance.rs +++ b/lib/vm/src/instance.rs @@ -714,7 +714,13 @@ impl UnpreparedInstanceAllocatorSetter { /// Allocate `Instance` (it is an uninitialized pointer). /// /// `offsets` is used to compute the layout with `Self::instance_layout`. - pub fn new(offsets: VMOffsets) -> Self { + pub fn new( + offsets: VMOffsets, + ) -> ( + Self, + Vec>, + Vec>, + ) { let instance_layout = Self::instance_layout(&offsets); #[allow(clippy::cast_ptr_alignment)] @@ -726,12 +732,17 @@ impl UnpreparedInstanceAllocatorSetter { alloc::handle_alloc_error(instance_layout); }; - Self { + let unprepared = Self { instance_ptr: instance_ptr.cast(), instance_layout, offsets: Some(offsets), consumed: false, - } + }; + + let memories = unsafe { unprepared.memory_definition_locations() }; + let tables = unsafe { unprepared.table_definition_locations() }; + + (unprepared, memories, tables) } /// Calculate the appropriate layout for `Instance`. @@ -749,31 +760,6 @@ impl UnpreparedInstanceAllocatorSetter { instance_layout.pad_to_align() } - /// Prepare the instance allocator setter by getting pointers into key - /// locations which can be used to initialize data. - pub fn prepare( - mut self, - ) -> ( - HalfPreparedInstanceAllocatorSetter, - Vec>, - Vec>, - ) { - let memories = unsafe { self.memory_definition_locations() }; - let tables = unsafe { self.table_definition_locations() }; - self.consumed = true; - let instance_ptr = self.instance_ptr; - let instance_layout = self.instance_layout; - let offsets = self.offsets.take(); - let prepared = HalfPreparedInstanceAllocatorSetter { - instance_ptr, - instance_layout, - offsets, - consumed: false, - }; - - (prepared, memories, tables) - } - /// Get the locations of where the local `VMMemoryDefinition`s should be stored. /// /// This function lets us create `Memory` objects on the host with backing @@ -818,7 +804,7 @@ impl UnpreparedInstanceAllocatorSetter { /// offsets in `offsets` point to valid locations in memory, /// i.e. `instance_ptr` must have been allocated by /// `InstanceHandle::allocate_instance`. - pub unsafe fn table_definition_locations(&self) -> Vec> { + unsafe fn table_definition_locations(&self) -> Vec> { let offsets = self.offsets.as_ref().unwrap(); let num_tables = offsets.num_local_tables; @@ -839,28 +825,7 @@ impl UnpreparedInstanceAllocatorSetter { } out } -} -/// TODO: rename -pub struct HalfPreparedInstanceAllocatorSetter { - instance_ptr: NonNull, - instance_layout: Layout, - offsets: Option, - consumed: bool, -} - -impl Drop for HalfPreparedInstanceAllocatorSetter { - fn drop(&mut self) { - if !self.consumed { - let instance_ptr = self.instance_ptr.as_ptr(); - unsafe { - std::alloc::dealloc(instance_ptr as *mut u8, self.instance_layout); - } - } - } -} - -impl HalfPreparedInstanceAllocatorSetter { /// Finish preparing by writing the `Instance` into memory. pub(crate) fn write_instance(mut self, instance: Instance) -> PreparedInstanceAllocatorSetter { unsafe { @@ -880,7 +845,7 @@ impl HalfPreparedInstanceAllocatorSetter { /// This function will return the offsets on the first time it's /// called and `None` on all subsequent calls. - fn take_offsets(&mut self) -> Option { + pub(crate) fn take_offsets(&mut self) -> Option { self.offsets.take() } } @@ -1152,7 +1117,13 @@ impl InstanceHandle { /// It should ideally return `NonNull` rather than /// `NonNull`, however `Instance` is private, and we want to /// keep it private. - pub fn allocate_instance(module: &ModuleInfo) -> UnpreparedInstanceAllocatorSetter { + pub fn allocate_instance( + module: &ModuleInfo, + ) -> ( + UnpreparedInstanceAllocatorSetter, + Vec>, + Vec>, + ) { let offsets = VMOffsets::new(mem::size_of::<*const u8>() as u8, module); UnpreparedInstanceAllocatorSetter::new(offsets) } @@ -1183,7 +1154,7 @@ impl InstanceHandle { /// all the local memories. #[allow(clippy::too_many_arguments)] pub unsafe fn new( - mut half_prepared: HalfPreparedInstanceAllocatorSetter, + mut half_prepared: UnpreparedInstanceAllocatorSetter, module: Arc, finished_functions: BoxedSlice, finished_function_call_trampolines: BoxedSlice, diff --git a/lib/vm/src/lib.rs b/lib/vm/src/lib.rs index 0fbddfdd5..4c8031c53 100644 --- a/lib/vm/src/lib.rs +++ b/lib/vm/src/lib.rs @@ -40,8 +40,8 @@ pub use crate::export::*; pub use crate::global::*; pub use crate::imports::Imports; pub use crate::instance::{ - HalfPreparedInstanceAllocatorSetter, ImportInitializerFuncPtr, InstanceHandle, - PreparedInstanceAllocatorSetter, UnpreparedInstanceAllocatorSetter, + ImportInitializerFuncPtr, InstanceHandle, PreparedInstanceAllocatorSetter, + UnpreparedInstanceAllocatorSetter, }; pub use crate::memory::{LinearMemory, Memory, MemoryError, MemoryStyle}; pub use crate::mmap::Mmap;