From 3c7dfe29f31f1597cd6c903f07d9e2456c4125ad Mon Sep 17 00:00:00 2001 From: Mark McCaskey Date: Mon, 19 Oct 2020 17:19:46 -0700 Subject: [PATCH 1/6] Add hacked together vertical slice of host env updating idea --- lib/api/src/externals/function.rs | 30 ++++++++++++++----- lib/api/src/instance.rs | 15 ++++++++-- lib/api/src/lib.rs | 14 +++++++++ lib/api/src/module.rs | 15 ++++++++-- lib/api/src/native.rs | 25 +++++++++++----- lib/api/src/types.rs | 6 +++- lib/cli/Cargo.toml | 2 +- lib/cli/src/commands/run/wasi.rs | 2 +- lib/engine/src/artifact.rs | 29 ++++++++++++++++-- lib/engine/src/resolver.rs | 4 ++- lib/vm/src/export.rs | 4 ++- lib/vm/src/instance.rs | 50 +++++++++++++++++++++---------- lib/vm/src/lib.rs | 8 ++--- lib/vm/src/trap/traphandlers.rs | 14 ++++----- lib/vm/src/vmcontext.rs | 39 ++++++++++++++++++++---- lib/vm/src/vmoffsets.rs | 2 +- lib/wasi/src/lib.rs | 29 ++++++++++++++---- tests/lib/wast/src/wasi_wast.rs | 2 +- 18 files changed, 222 insertions(+), 68 deletions(-) diff --git a/lib/api/src/externals/function.rs b/lib/api/src/externals/function.rs index fcf00b74342..ac0b5f26b4b 100644 --- a/lib/api/src/externals/function.rs +++ b/lib/api/src/externals/function.rs @@ -11,8 +11,8 @@ use std::cmp::max; use std::fmt; use wasmer_vm::{ raise_user_trap, resume_panic, wasmer_call_trampoline, Export, ExportFunction, - VMCallerCheckedAnyfunc, VMContext, VMDynamicFunctionContext, VMFunctionBody, VMFunctionKind, - VMTrampoline, + FunctionExtraData, VMCallerCheckedAnyfunc, VMContext, VMDynamicFunctionContext, VMFunctionBody, + VMFunctionKind, VMTrampoline, }; /// A function defined in the Wasm module @@ -85,7 +85,9 @@ impl Function { // The engine linker will replace the address with one pointing to a // generated dynamic trampoline. let address = std::ptr::null() as *const VMFunctionBody; - let vmctx = Box::into_raw(Box::new(dynamic_ctx)) as *mut VMContext; + let vmctx = FunctionExtraData { + host_env: Box::into_raw(Box::new(dynamic_ctx)) as *mut _, + }; Self { store: store.clone(), @@ -94,6 +96,7 @@ impl Function { address, kind: VMFunctionKind::Dynamic, vmctx, + function_ptr: 0, signature: ty.clone(), }, } @@ -134,7 +137,9 @@ impl Function { // The engine linker will replace the address with one pointing to a // generated dynamic trampoline. let address = std::ptr::null() as *const VMFunctionBody; - let vmctx = Box::into_raw(Box::new(dynamic_ctx)) as *mut VMContext; + let vmctx = FunctionExtraData { + host_env: Box::into_raw(Box::new(dynamic_ctx)) as *mut _, + }; Self { store: store.clone(), @@ -143,6 +148,8 @@ impl Function { address, kind: VMFunctionKind::Dynamic, vmctx, + // TODO: + function_ptr: 0, signature: ty.clone(), }, } @@ -174,7 +181,9 @@ impl Function { { let function = inner::Function::::new(func); let address = function.address() as *const VMFunctionBody; - let vmctx = std::ptr::null_mut() as *mut _ as *mut VMContext; + let vmctx = FunctionExtraData { + host_env: std::ptr::null_mut() as *mut _, + }; let signature = function.ty(); Self { @@ -184,6 +193,8 @@ impl Function { address, vmctx, signature, + // TODO: + function_ptr: 0, kind: VMFunctionKind::Static, }, } @@ -216,7 +227,7 @@ impl Function { F: HostFunction, Args: WasmTypeList, Rets: WasmTypeList, - Env: Sized + 'static, + Env: Sized + crate::WasmerPostInstantiate + 'static, { let function = inner::Function::::new(func); let address = function.address(); @@ -227,7 +238,11 @@ impl Function { // In the case of Host-defined functions `VMContext` is whatever environment // the user want to attach to the function. let box_env = Box::new(env); - let vmctx = Box::into_raw(box_env) as *mut _ as *mut VMContext; + let vmctx = FunctionExtraData { + host_env: Box::into_raw(box_env) as *mut _, + }; + let function_ptr = Env::finish as usize; + dbg!(function_ptr); let signature = function.ty(); Self { @@ -237,6 +252,7 @@ impl Function { address, kind: VMFunctionKind::Static, vmctx, + function_ptr, signature, }, } diff --git a/lib/api/src/instance.rs b/lib/api/src/instance.rs index cac647fce10..61f51a43ae1 100644 --- a/lib/api/src/instance.rs +++ b/lib/api/src/instance.rs @@ -73,7 +73,7 @@ impl Instance { pub fn new(module: &Module, resolver: &dyn Resolver) -> Result { let store = module.store(); - let handle = module.instantiate(resolver)?; + let (handle, thunks) = module.instantiate(resolver)?; let exports = module .exports() @@ -85,11 +85,20 @@ impl Instance { }) .collect::(); - Ok(Self { + let instance = Self { handle, module: module.clone(), exports, - }) + }; + + for (func, env) in thunks.iter() { + dbg!(func, env); + unsafe { + func(*env, (&instance) as *const _ as *const _); + } + } + + Ok(instance) } /// Gets the [`Module`] associated with this instance. diff --git a/lib/api/src/lib.rs b/lib/api/src/lib.rs index 2a1f42c246c..ce4d8204a83 100644 --- a/lib/api/src/lib.rs +++ b/lib/api/src/lib.rs @@ -118,3 +118,17 @@ pub use wasmer_engine_native::{Native, NativeArtifact, NativeEngine}; /// Version number of this crate. pub const VERSION: &str = env!("CARGO_PKG_VERSION"); + +// -------------------------------------------------------------- +// TODO: put this in a proper location, just prototyping for now: +// TODO: rename everything, all names are throw-away names + +/// Prototype trait for finishing envs. +pub trait WasmerPostInstantiate { + /// The function that Wasmer will call on your type to let it finish + /// instantiating. + fn finish(&mut self, instance: &Instance); + + /// Frees memory written to `self` so it can be dropped without any memory leaks. + fn free(&mut self); +} diff --git a/lib/api/src/module.rs b/lib/api/src/module.rs index 714991cba58..74b361a8d3f 100644 --- a/lib/api/src/module.rs +++ b/lib/api/src/module.rs @@ -261,9 +261,18 @@ impl Module { pub(crate) fn instantiate( &self, resolver: &dyn Resolver, - ) -> Result { + ) -> Result< + ( + InstanceHandle, + Vec<( + fn(*mut std::ffi::c_void, *const std::ffi::c_void), + *mut std::ffi::c_void, + )>, + ), + InstantiationError, + > { unsafe { - let instance_handle = + let (instance_handle, thunks) = self.artifact .instantiate(self.store.tunables(), resolver, Box::new(()))?; @@ -274,7 +283,7 @@ impl Module { // instance tables. self.artifact.finish_instantiation(&instance_handle)?; - Ok(instance_handle) + Ok((instance_handle, thunks)) } } diff --git a/lib/api/src/native.rs b/lib/api/src/native.rs index 5a73083f2cb..9957e23bb63 100644 --- a/lib/api/src/native.rs +++ b/lib/api/src/native.rs @@ -17,7 +17,8 @@ use crate::{FromToNativeWasmType, Function, FunctionType, RuntimeError, Store, W use std::panic::{catch_unwind, AssertUnwindSafe}; use wasmer_types::NativeWasmType; use wasmer_vm::{ - ExportFunction, VMContext, VMDynamicFunctionContext, VMFunctionBody, VMFunctionKind, + ExportFunction, FunctionExtraData, VMContext, VMDynamicFunctionContext, VMFunctionBody, + VMFunctionKind, }; /// A WebAssembly function that can be called natively @@ -26,7 +27,7 @@ pub struct NativeFunc<'a, Args = (), Rets = ()> { definition: FunctionDefinition, store: Store, address: *const VMFunctionBody, - vmctx: *mut VMContext, + vmctx: FunctionExtraData, arg_kind: VMFunctionKind, // exported: ExportFunction, _phantom: PhantomData<(&'a (), Args, Rets)>, @@ -42,7 +43,7 @@ where pub(crate) fn new( store: Store, address: *const VMFunctionBody, - vmctx: *mut VMContext, + vmctx: FunctionExtraData, arg_kind: VMFunctionKind, definition: FunctionDefinition, ) -> Self { @@ -68,6 +69,8 @@ where address: other.address, vmctx: other.vmctx, signature, + // TODO: + function_ptr: 0, kind: other.arg_kind, } } @@ -87,6 +90,8 @@ where address: other.address, vmctx: other.vmctx, signature, + // TODO: + function_ptr: 0, kind: other.arg_kind, }, } @@ -163,7 +168,7 @@ macro_rules! impl_native_traits { match self.arg_kind { VMFunctionKind::Static => { let results = catch_unwind(AssertUnwindSafe(|| unsafe { - let f = std::mem::transmute::<_, unsafe extern "C" fn( *mut VMContext, $( $x, )*) -> Rets::CStruct>(self.address); + let f = std::mem::transmute::<_, unsafe extern "C" fn( FunctionExtraData, $( $x, )*) -> Rets::CStruct>(self.address); // We always pass the vmctx f( self.vmctx, $( $x, )* ) })).map_err(|e| RuntimeError::new(format!("{:?}", e)))?; @@ -173,12 +178,16 @@ macro_rules! impl_native_traits { let params_list = [ $( $x.to_native().to_value() ),* ]; let results = if !has_env { type VMContextWithoutEnv = VMDynamicFunctionContext; - let ctx = self.vmctx as *mut VMContextWithoutEnv; - unsafe { (*ctx).ctx.call(¶ms_list)? } + unsafe { + let ctx = self.vmctx.host_env as *mut VMContextWithoutEnv; + (*ctx).ctx.call(¶ms_list)? + } } else { type VMContextWithEnv = VMDynamicFunctionContext>; - let ctx = self.vmctx as *mut VMContextWithEnv; - unsafe { (*ctx).ctx.call(¶ms_list)? } + unsafe { + let ctx = self.vmctx.host_env as *mut VMContextWithEnv; + (*ctx).ctx.call(¶ms_list)? + } }; let mut rets_list_array = Rets::empty_array(); let mut_rets = rets_list_array.as_mut() as *mut [i128] as *mut i128; diff --git a/lib/api/src/types.rs b/lib/api/src/types.rs index b085f6a2419..cea1336d60f 100644 --- a/lib/api/src/types.rs +++ b/lib/api/src/types.rs @@ -56,7 +56,9 @@ impl ValFuncRef for Val { Self::ExternRef(ExternRef::Null) => wasmer_vm::VMCallerCheckedAnyfunc { func_ptr: ptr::null(), type_index: wasmer_vm::VMSharedSignatureIndex::default(), - vmctx: ptr::null_mut(), + vmctx: wasmer_vm::FunctionExtraData { + host_env: ptr::null_mut(), + }, }, Self::FuncRef(f) => f.checked_anyfunc(), _ => return Err(RuntimeError::new("val is not funcref")), @@ -74,6 +76,8 @@ impl ValFuncRef for Val { let export = wasmer_vm::ExportFunction { address: item.func_ptr, signature, + // TODO: + function_ptr: 0, // All functions in tables are already Static (as dynamic functions // are converted to use the trampolines with static signatures). kind: wasmer_vm::VMFunctionKind::Static, diff --git a/lib/cli/Cargo.toml b/lib/cli/Cargo.toml index 2ffdede2d3c..9ec6f6f6868 100644 --- a/lib/cli/Cargo.toml +++ b/lib/cli/Cargo.toml @@ -58,7 +58,7 @@ default = [ "object-file", "cache", "wasi", - "emscripten", + # "emscripten", ] engine = [] jit = [ diff --git a/lib/cli/src/commands/run/wasi.rs b/lib/cli/src/commands/run/wasi.rs index 71f41c95291..d74dbd3f63a 100644 --- a/lib/cli/src/commands/run/wasi.rs +++ b/lib/cli/src/commands/run/wasi.rs @@ -58,7 +58,7 @@ impl Wasi { let import_object = wasi_env.import_object(&module)?; let instance = Instance::new(&module, &import_object)?; - wasi_env.set_memory(instance.exports.get_memory("memory")?.clone()); + //wasi_env.set_memory(instance.exports.get_memory("memory")?.clone()); let start = instance.exports.get_function("_start")?; let result = start.call(&[]); diff --git a/lib/engine/src/artifact.rs b/lib/engine/src/artifact.rs index f74fc4717bf..5719fa6d21e 100644 --- a/lib/engine/src/artifact.rs +++ b/lib/engine/src/artifact.rs @@ -86,7 +86,16 @@ pub trait Artifact: Send + Sync + Upcastable { tunables: &dyn Tunables, resolver: &dyn Resolver, host_state: Box, - ) -> Result { + ) -> Result< + ( + InstanceHandle, + Vec<( + fn(*mut std::ffi::c_void, *const std::ffi::c_void), + *mut std::ffi::c_void, + )>, + ), + InstantiationError, + > { self.preinstantiate()?; let module = self.module(); @@ -98,6 +107,19 @@ pub trait Artifact: Send + Sync + Upcastable { self.table_styles(), ) .map_err(InstantiationError::Link)?; + + let mut thunks = vec![]; + // ------------ + for func in imports.functions.values() { + unsafe { + let finish: fn(*mut std::ffi::c_void, instance: *const std::ffi::c_void) = + std::mem::transmute(func.function_ptr); + dbg!(func.extra_data.host_env); + thunks.push((finish, func.extra_data.host_env)); + } + } + // ------------ + let finished_memories = tunables .create_memories(&module, self.memory_styles()) .map_err(InstantiationError::Link)? @@ -113,7 +135,7 @@ pub trait Artifact: Send + Sync + Upcastable { self.register_frame_info(); - InstanceHandle::new( + let handle = InstanceHandle::new( module, self.finished_functions().clone(), finished_memories, @@ -123,7 +145,8 @@ pub trait Artifact: Send + Sync + Upcastable { self.signatures().clone(), host_state, ) - .map_err(|trap| InstantiationError::Start(RuntimeError::from_trap(trap))) + .map_err(|trap| InstantiationError::Start(RuntimeError::from_trap(trap)))?; + Ok((handle, thunks)) } /// Finishes the instantiation of a just created `InstanceHandle`. diff --git a/lib/engine/src/resolver.rs b/lib/engine/src/resolver.rs index 82b5df6251d..0e27426f60c 100644 --- a/lib/engine/src/resolver.rs +++ b/lib/engine/src/resolver.rs @@ -167,7 +167,9 @@ pub fn resolve_imports( }; function_imports.push(VMFunctionImport { body: address, - vmctx: f.vmctx, + extra_data: f.vmctx, + // TODO: + function_ptr: f.function_ptr, }); } Export::Table(ref t) => { diff --git a/lib/vm/src/export.rs b/lib/vm/src/export.rs index 7879c92d16a..23672aa8f17 100644 --- a/lib/vm/src/export.rs +++ b/lib/vm/src/export.rs @@ -30,7 +30,9 @@ pub struct ExportFunction { /// The address of the native-code function. pub address: *const VMFunctionBody, /// Pointer to the containing `VMContext`. - pub vmctx: *mut VMContext, + pub vmctx: crate::vmcontext::FunctionExtraData, + /// temp code to set vmctx for host functions + pub function_ptr: usize, /// The function type, used for compatibility checking. pub signature: FunctionType, /// The function kind (it defines how it's the signature that provided `address` have) diff --git a/lib/vm/src/instance.rs b/lib/vm/src/instance.rs index d5a674ceee3..055d57e2cfc 100644 --- a/lib/vm/src/instance.rs +++ b/lib/vm/src/instance.rs @@ -291,13 +291,19 @@ impl Instance { match export { ExportIndex::Function(index) => { let sig_index = &self.module.functions[*index]; - let (address, vmctx) = if let Some(def_index) = self.module.local_func_index(*index) - { - (self.functions[def_index].0 as *const _, self.vmctx_ptr()) - } else { - let import = self.imported_function(*index); - (import.body, import.vmctx) - }; + let (address, vmctx, function_ptr) = + if let Some(def_index) = self.module.local_func_index(*index) { + ( + self.functions[def_index].0 as *const _, + crate::vmcontext::FunctionExtraData { + vmctx: self.vmctx_ptr(), + }, + 0, + ) + } else { + let import = self.imported_function(*index); + (import.body, import.extra_data, import.function_ptr) + }; let signature = self.module.signatures[*sig_index].clone(); ExportFunction { address, @@ -308,6 +314,7 @@ impl Instance { kind: VMFunctionKind::Static, signature, vmctx, + function_ptr, } .into() } @@ -365,28 +372,34 @@ impl Instance { None => return Ok(()), }; - let (callee_address, callee_vmctx) = match self.module.local_func_index(start_index) { + let (callee_address, callee_extra_data) = match self.module.local_func_index(start_index) { Some(local_index) => { let body = self .functions .get(local_index) .expect("function index is out of bounds") .0; - (body as *const _, self.vmctx_ptr()) + ( + body as *const _, + crate::FunctionExtraData { + vmctx: self.vmctx_ptr(), + }, + ) } None => { assert_lt!(start_index.index(), self.module.num_imported_functions); let import = self.imported_function(start_index); - (import.body, import.vmctx) + (import.body, import.extra_data) } }; // Make the call. unsafe { - catch_traps(callee_vmctx, || { - mem::transmute::<*const VMFunctionBody, unsafe extern "C" fn(*mut VMContext)>( - callee_address, - )(callee_vmctx) + catch_traps(callee_extra_data, || { + mem::transmute::< + *const VMFunctionBody, + unsafe extern "C" fn(crate::FunctionExtraData), + >(callee_address)(callee_extra_data) }) } } @@ -556,10 +569,15 @@ impl Instance { let type_index = self.signature_id(sig); let (func_ptr, vmctx) = if let Some(def_index) = self.module.local_func_index(index) { - (self.functions[def_index].0 as *const _, self.vmctx_ptr()) + ( + self.functions[def_index].0 as *const _, + crate::FunctionExtraData { + vmctx: self.vmctx_ptr(), + }, + ) } else { let import = self.imported_function(index); - (import.body, import.vmctx) + (import.body, import.extra_data) }; VMCallerCheckedAnyfunc { func_ptr, diff --git a/lib/vm/src/lib.rs b/lib/vm/src/lib.rs index f8e649f591f..b9e9e39e153 100644 --- a/lib/vm/src/lib.rs +++ b/lib/vm/src/lib.rs @@ -48,10 +48,10 @@ pub use crate::sig_registry::SignatureRegistry; pub use crate::table::{LinearTable, Table, TableStyle}; pub use crate::trap::*; pub use crate::vmcontext::{ - VMBuiltinFunctionIndex, VMCallerCheckedAnyfunc, VMContext, VMDynamicFunctionContext, - VMFunctionBody, VMFunctionImport, VMFunctionKind, VMGlobalDefinition, VMGlobalImport, - VMMemoryDefinition, VMMemoryImport, VMSharedSignatureIndex, VMTableDefinition, VMTableImport, - VMTrampoline, + FunctionExtraData, VMBuiltinFunctionIndex, VMCallerCheckedAnyfunc, VMContext, + VMDynamicFunctionContext, VMFunctionBody, VMFunctionImport, VMFunctionKind, VMGlobalDefinition, + VMGlobalImport, VMMemoryDefinition, VMMemoryImport, VMSharedSignatureIndex, VMTableDefinition, + VMTableImport, VMTrampoline, }; pub use crate::vmoffsets::{TargetSharedSignatureIndex, VMOffsets}; diff --git a/lib/vm/src/trap/traphandlers.rs b/lib/vm/src/trap/traphandlers.rs index 613e8ec1035..a09b13bf537 100644 --- a/lib/vm/src/trap/traphandlers.rs +++ b/lib/vm/src/trap/traphandlers.rs @@ -429,13 +429,13 @@ impl Trap { /// Wildly unsafe because it calls raw function pointers and reads/writes raw /// function pointers. pub unsafe fn wasmer_call_trampoline( - vmctx: *mut VMContext, + vmctx: crate::FunctionExtraData, trampoline: VMTrampoline, callee: *const VMFunctionBody, values_vec: *mut u8, ) -> Result<(), Trap> { catch_traps(vmctx, || { - mem::transmute::<_, extern "C" fn(*mut VMContext, *const VMFunctionBody, *mut u8)>( + mem::transmute::<_, extern "C" fn(crate::FunctionExtraData, *const VMFunctionBody, *mut u8)>( trampoline, )(vmctx, callee, values_vec) }) @@ -447,7 +447,7 @@ pub unsafe fn wasmer_call_trampoline( /// # Safety /// /// Highly unsafe since `closure` won't have any destructors run. -pub unsafe fn catch_traps(vmctx: *mut VMContext, mut closure: F) -> Result<(), Trap> +pub unsafe fn catch_traps(vmctx: crate::FunctionExtraData, mut closure: F) -> Result<(), Trap> where F: FnMut(), { @@ -481,7 +481,7 @@ where /// /// Check [`catch_traps`]. pub unsafe fn catch_traps_with_result( - vmctx: *mut VMContext, + vmctx: crate::FunctionExtraData, mut closure: F, ) -> Result where @@ -501,7 +501,7 @@ pub struct CallThreadState { jmp_buf: Cell<*const u8>, reset_guard_page: Cell, prev: Option<*const CallThreadState>, - vmctx: *mut VMContext, + vmctx: crate::FunctionExtraData, handling_trap: Cell, } @@ -518,7 +518,7 @@ enum UnwindReason { } impl CallThreadState { - fn new(vmctx: *mut VMContext) -> Self { + fn new(vmctx: crate::FunctionExtraData) -> Self { Self { unwind: Cell::new(UnwindReason::None), vmctx, @@ -561,7 +561,7 @@ impl CallThreadState { fn any_instance(&self, func: impl Fn(&InstanceHandle) -> bool) -> bool { unsafe { - if func(&InstanceHandle::from_vmctx(self.vmctx)) { + if func(&InstanceHandle::from_vmctx(self.vmctx.vmctx)) { return true; } match self.prev { diff --git a/lib/vm/src/vmcontext.rs b/lib/vm/src/vmcontext.rs index 367a207629c..d1de6b3110b 100644 --- a/lib/vm/src/vmcontext.rs +++ b/lib/vm/src/vmcontext.rs @@ -15,6 +15,29 @@ use std::ptr::{self, NonNull}; use std::sync::Arc; use std::u32; +/// We stop lying about what this daat is +/// TODO: +#[derive(Copy, Clone)] +pub union FunctionExtraData { + /// Wasm function, it has a real VMContext: + pub vmctx: *mut VMContext, + /// Host functions can have custom environments + pub host_env: *mut std::ffi::c_void, +} + +impl std::fmt::Debug for FunctionExtraData { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + write!(f, "FunctionExtarData union") + } +} + +impl std::cmp::PartialEq for FunctionExtraData { + fn eq(&self, rhs: &Self) -> bool { + // TODO + false + } +} + /// An imported function. #[derive(Debug, Copy, Clone)] #[repr(C)] @@ -22,8 +45,12 @@ pub struct VMFunctionImport { /// A pointer to the imported function body. pub body: *const VMFunctionBody, - /// A pointer to the `VMContext` that owns the function. - pub vmctx: *mut VMContext, + /// A pointer to the `VMContext` that owns the function or host data. + pub extra_data: FunctionExtraData, + + /// temporary hack + /// Only used by host env + pub function_ptr: usize, } #[cfg(test)] @@ -46,7 +73,7 @@ mod test_vmfunction_import { usize::from(offsets.vmfunction_import_body()) ); assert_eq!( - offset_of!(VMFunctionImport, vmctx), + offset_of!(VMFunctionImport, extra_data), usize::from(offsets.vmfunction_import_vmctx()) ); } @@ -729,7 +756,7 @@ pub struct VMCallerCheckedAnyfunc { /// Function signature id. pub type_index: VMSharedSignatureIndex, /// Function `VMContext`. - pub vmctx: *mut VMContext, + pub vmctx: FunctionExtraData, // If more elements are added here, remember to add offset_of tests below! } @@ -768,7 +795,9 @@ impl Default for VMCallerCheckedAnyfunc { Self { func_ptr: ptr::null_mut(), type_index: Default::default(), - vmctx: ptr::null_mut(), + vmctx: FunctionExtraData { + vmctx: ptr::null_mut(), + }, } } } diff --git a/lib/vm/src/vmoffsets.rs b/lib/vm/src/vmoffsets.rs index b18408165d3..66406d0035f 100644 --- a/lib/vm/src/vmoffsets.rs +++ b/lib/vm/src/vmoffsets.rs @@ -109,7 +109,7 @@ impl VMOffsets { /// /// [`VMFunctionImport`]: crate::vmcontext::VMFunctionImport pub fn size_of_vmfunction_import(&self) -> u8 { - 2 * self.pointer_size + 3 * self.pointer_size } } diff --git a/lib/wasi/src/lib.rs b/lib/wasi/src/lib.rs index 74a0fab3819..d0733055f68 100644 --- a/lib/wasi/src/lib.rs +++ b/lib/wasi/src/lib.rs @@ -51,7 +51,25 @@ pub enum WasiError { #[derive(Debug, Clone)] pub struct WasiEnv { state: Arc>, - memory: Arc, + memory: *mut Memory, +} + +impl wasmer::WasmerPostInstantiate for WasiEnv { + fn finish(&mut self, instance: &wasmer::Instance) { + dbg!("in Wasi::Finish"); + let memory = instance.exports.get_memory("memory").unwrap(); + unsafe { + let heap_ptr = Box::into_raw(Box::new(memory.clone())); + self.memory = heap_ptr; + } + } + + fn free(&mut self) { + unsafe { + Box::from_raw(self.memory); + self.memory = std::ptr::null_mut(); + } + } } /// Wrapper type around `Memory` used to delay initialization of the memory. @@ -140,7 +158,7 @@ impl WasiEnv { pub fn new(state: WasiState) -> Self { Self { state: Arc::new(Mutex::new(state)), - memory: Arc::new(WasiMemory::new()), + memory: std::ptr::null_mut(), } } @@ -154,9 +172,9 @@ impl WasiEnv { } /// Set the memory - pub fn set_memory(&mut self, memory: Memory) -> bool { + /*pub fn set_memory(&mut self, memory: Memory) -> bool { self.memory.set_memory(memory) - } + }*/ /// Get the WASI state pub fn state(&self) -> MutexGuard { @@ -170,7 +188,8 @@ impl WasiEnv { /// Get a reference to the memory pub fn memory(&self) -> &Memory { - self.memory.get_memory().expect("The expected Memory is not attached to the `WasiEnv`. Did you forgot to call wasi_env.set_memory(...)?") + dbg!("Getting memory", &self.memory); + unsafe { &*self.memory } } pub(crate) fn get_memory_and_wasi_state( diff --git a/tests/lib/wast/src/wasi_wast.rs b/tests/lib/wast/src/wasi_wast.rs index 3c25dacfef9..866a68dabe8 100644 --- a/tests/lib/wast/src/wasi_wast.rs +++ b/tests/lib/wast/src/wasi_wast.rs @@ -84,7 +84,7 @@ impl<'a> WasiTest<'a> { let imports = self.get_imports(store, &module, env.clone())?; let instance = Instance::new(&module, &imports)?; let memory: &Memory = instance.exports.get("memory")?; - env.set_memory(memory.clone()); + //env.set_memory(memory.clone()); let start = instance.exports.get_function("_start")?; // TODO: handle errors here when the error fix gets shipped From ed1202181e42a7ff7a8158cbc45f637ab1eda231 Mon Sep 17 00:00:00 2001 From: Mark McCaskey Date: Tue, 20 Oct 2020 12:21:16 -0700 Subject: [PATCH 2/6] Clean up code a bit --- lib/api/src/externals/function.rs | 20 +++++++++------- lib/api/src/instance.rs | 11 ++++----- lib/api/src/module.rs | 15 +++--------- lib/api/src/native.rs | 4 ++-- lib/api/src/types.rs | 3 ++- lib/engine/src/artifact.rs | 32 +++++++++++-------------- lib/engine/src/resolver.rs | 8 +++++-- lib/vm/src/export.rs | 2 +- lib/vm/src/imports.rs | 12 ++++++++++ lib/vm/src/instance.rs | 40 +++++++++++++++++++++++++++++-- lib/vm/src/vmcontext.rs | 4 ---- lib/vm/src/vmoffsets.rs | 2 +- lib/wasi/src/lib.rs | 1 - 13 files changed, 96 insertions(+), 58 deletions(-) diff --git a/lib/api/src/externals/function.rs b/lib/api/src/externals/function.rs index ac0b5f26b4b..9a373c6b7e0 100644 --- a/lib/api/src/externals/function.rs +++ b/lib/api/src/externals/function.rs @@ -96,7 +96,7 @@ impl Function { address, kind: VMFunctionKind::Dynamic, vmctx, - function_ptr: 0, + function_ptr: None, signature: ty.clone(), }, } @@ -126,7 +126,7 @@ impl Function { pub fn new_with_env(store: &Store, ty: &FunctionType, env: Env, func: F) -> Self where F: Fn(&mut Env, &[Val]) -> Result, RuntimeError> + 'static, - Env: Sized + 'static, + Env: Sized + crate::WasmerPostInstantiate + 'static, { let dynamic_ctx = VMDynamicFunctionContext::from_context(VMDynamicFunctionWithEnv { env: RefCell::new(env), @@ -140,6 +140,9 @@ impl Function { let vmctx = FunctionExtraData { host_env: Box::into_raw(Box::new(dynamic_ctx)) as *mut _, }; + // TODO: look into removing transmute by changing API type signatures + let function_ptr = Some(unsafe { std::mem::transmute::(Env::finish) }); + //dbg!(function_ptr); Self { store: store.clone(), @@ -148,8 +151,7 @@ impl Function { address, kind: VMFunctionKind::Dynamic, vmctx, - // TODO: - function_ptr: 0, + function_ptr, signature: ty.clone(), }, } @@ -193,8 +195,9 @@ impl Function { address, vmctx, signature, - // TODO: - function_ptr: 0, + // TODO: figure out what's going on in this function: it takes an `Env` + // param but also marks itself as not having an env + function_ptr: None, kind: VMFunctionKind::Static, }, } @@ -241,8 +244,9 @@ impl Function { let vmctx = FunctionExtraData { host_env: Box::into_raw(box_env) as *mut _, }; - let function_ptr = Env::finish as usize; - dbg!(function_ptr); + // TODO: look into removing transmute by changing API type signatures + let function_ptr = Some(unsafe { std::mem::transmute::(Env::finish) }); + //dbg!(function_ptr as usize); let signature = function.ty(); Self { diff --git a/lib/api/src/instance.rs b/lib/api/src/instance.rs index 61f51a43ae1..c9dd1c6216b 100644 --- a/lib/api/src/instance.rs +++ b/lib/api/src/instance.rs @@ -73,7 +73,7 @@ impl Instance { pub fn new(module: &Module, resolver: &dyn Resolver) -> Result { let store = module.store(); - let (handle, thunks) = module.instantiate(resolver)?; + let handle = module.instantiate(resolver)?; let exports = module .exports() @@ -91,11 +91,10 @@ impl Instance { exports, }; - for (func, env) in thunks.iter() { - dbg!(func, env); - unsafe { - func(*env, (&instance) as *const _ as *const _); - } + unsafe { + instance + .handle + .initialize_host_envs(&instance as *const _ as *const _); } Ok(instance) diff --git a/lib/api/src/module.rs b/lib/api/src/module.rs index 74b361a8d3f..714991cba58 100644 --- a/lib/api/src/module.rs +++ b/lib/api/src/module.rs @@ -261,18 +261,9 @@ impl Module { pub(crate) fn instantiate( &self, resolver: &dyn Resolver, - ) -> Result< - ( - InstanceHandle, - Vec<( - fn(*mut std::ffi::c_void, *const std::ffi::c_void), - *mut std::ffi::c_void, - )>, - ), - InstantiationError, - > { + ) -> Result { unsafe { - let (instance_handle, thunks) = + let instance_handle = self.artifact .instantiate(self.store.tunables(), resolver, Box::new(()))?; @@ -283,7 +274,7 @@ impl Module { // instance tables. self.artifact.finish_instantiation(&instance_handle)?; - Ok((instance_handle, thunks)) + Ok(instance_handle) } } diff --git a/lib/api/src/native.rs b/lib/api/src/native.rs index 9957e23bb63..5a02ac8e3e0 100644 --- a/lib/api/src/native.rs +++ b/lib/api/src/native.rs @@ -70,7 +70,7 @@ where vmctx: other.vmctx, signature, // TODO: - function_ptr: 0, + function_ptr: None, kind: other.arg_kind, } } @@ -91,7 +91,7 @@ where vmctx: other.vmctx, signature, // TODO: - function_ptr: 0, + function_ptr: None, kind: other.arg_kind, }, } diff --git a/lib/api/src/types.rs b/lib/api/src/types.rs index cea1336d60f..07b0ea1f7ce 100644 --- a/lib/api/src/types.rs +++ b/lib/api/src/types.rs @@ -77,7 +77,8 @@ impl ValFuncRef for Val { address: item.func_ptr, signature, // TODO: - function_ptr: 0, + // figure out if we ever need a value here: need testing with complicated import patterns + function_ptr: None, // All functions in tables are already Static (as dynamic functions // are converted to use the trampolines with static signatures). kind: wasmer_vm::VMFunctionKind::Static, diff --git a/lib/engine/src/artifact.rs b/lib/engine/src/artifact.rs index 5719fa6d21e..215e8bc265a 100644 --- a/lib/engine/src/artifact.rs +++ b/lib/engine/src/artifact.rs @@ -86,16 +86,7 @@ pub trait Artifact: Send + Sync + Upcastable { tunables: &dyn Tunables, resolver: &dyn Resolver, host_state: Box, - ) -> Result< - ( - InstanceHandle, - Vec<( - fn(*mut std::ffi::c_void, *const std::ffi::c_void), - *mut std::ffi::c_void, - )>, - ), - InstantiationError, - > { + ) -> Result { self.preinstantiate()?; let module = self.module(); @@ -110,13 +101,17 @@ pub trait Artifact: Send + Sync + Upcastable { let mut thunks = vec![]; // ------------ - for func in imports.functions.values() { - unsafe { - let finish: fn(*mut std::ffi::c_void, instance: *const std::ffi::c_void) = - std::mem::transmute(func.function_ptr); - dbg!(func.extra_data.host_env); - thunks.push((finish, func.extra_data.host_env)); - } + for (func_init, func) in imports + .host_function_env_initializers + .values() + .cloned() + .zip(imports.functions.values()) + { + let host_env = unsafe { + //dbg!(func.extra_data.host_env); + func.extra_data.host_env + }; + thunks.push((func_init, host_env)); } // ------------ @@ -144,9 +139,10 @@ pub trait Artifact: Send + Sync + Upcastable { imports, self.signatures().clone(), host_state, + thunks, ) .map_err(|trap| InstantiationError::Start(RuntimeError::from_trap(trap)))?; - Ok((handle, thunks)) + Ok(handle) } /// Finishes the instantiation of a just created `InstanceHandle`. diff --git a/lib/engine/src/resolver.rs b/lib/engine/src/resolver.rs index 0e27426f60c..6ef8650d7a3 100644 --- a/lib/engine/src/resolver.rs +++ b/lib/engine/src/resolver.rs @@ -125,6 +125,9 @@ pub fn resolve_imports( _table_styles: &PrimaryMap, ) -> Result { let mut function_imports = PrimaryMap::with_capacity(module.num_imported_functions); + // TODO: account for imported functions without env / from other Wasm instances + let mut host_function_env_initializers = + PrimaryMap::with_capacity(module.num_imported_functions); let mut table_imports = PrimaryMap::with_capacity(module.num_imported_tables); let mut memory_imports = PrimaryMap::with_capacity(module.num_imported_memories); let mut global_imports = PrimaryMap::with_capacity(module.num_imported_globals); @@ -168,9 +171,9 @@ pub fn resolve_imports( function_imports.push(VMFunctionImport { body: address, extra_data: f.vmctx, - // TODO: - function_ptr: f.function_ptr, }); + + host_function_env_initializers.push(f.function_ptr); } Export::Table(ref t) => { table_imports.push(VMTableImport { @@ -224,6 +227,7 @@ pub fn resolve_imports( Ok(Imports::new( function_imports, + host_function_env_initializers, table_imports, memory_imports, global_imports, diff --git a/lib/vm/src/export.rs b/lib/vm/src/export.rs index 23672aa8f17..fde459e447c 100644 --- a/lib/vm/src/export.rs +++ b/lib/vm/src/export.rs @@ -32,7 +32,7 @@ pub struct ExportFunction { /// Pointer to the containing `VMContext`. pub vmctx: crate::vmcontext::FunctionExtraData, /// temp code to set vmctx for host functions - pub function_ptr: usize, + pub function_ptr: Option, /// The function type, used for compatibility checking. pub signature: FunctionType, /// The function kind (it defines how it's the signature that provided `address` have) diff --git a/lib/vm/src/imports.rs b/lib/vm/src/imports.rs index fb5da509907..80c7ce19cf7 100644 --- a/lib/vm/src/imports.rs +++ b/lib/vm/src/imports.rs @@ -11,6 +11,12 @@ pub struct Imports { /// Resolved addresses for imported functions. pub functions: BoxedSlice, + /// Initializers for host function environments. This is split out from `functions` + /// because the generated code never needs to touch this and the extra wasted + /// space may affect Wasm runtime performance due to increased cache pressure. + pub host_function_env_initializers: + BoxedSlice>, + /// Resolved addresses for imported tables. pub tables: BoxedSlice, @@ -25,12 +31,17 @@ impl Imports { /// Construct a new `Imports` instance. pub fn new( function_imports: PrimaryMap, + host_function_env_initializers: PrimaryMap< + FunctionIndex, + Option, + >, table_imports: PrimaryMap, memory_imports: PrimaryMap, global_imports: PrimaryMap, ) -> Self { Self { functions: function_imports.into_boxed_slice(), + host_function_env_initializers: host_function_env_initializers.into_boxed_slice(), tables: table_imports.into_boxed_slice(), memories: memory_imports.into_boxed_slice(), globals: global_imports.into_boxed_slice(), @@ -41,6 +52,7 @@ impl Imports { pub fn none() -> Self { Self { functions: PrimaryMap::new().into_boxed_slice(), + host_function_env_initializers: PrimaryMap::new().into_boxed_slice(), tables: PrimaryMap::new().into_boxed_slice(), memories: PrimaryMap::new().into_boxed_slice(), globals: PrimaryMap::new().into_boxed_slice(), diff --git a/lib/vm/src/instance.rs b/lib/vm/src/instance.rs index 055d57e2cfc..b77a5b6df62 100644 --- a/lib/vm/src/instance.rs +++ b/lib/vm/src/instance.rs @@ -100,6 +100,17 @@ pub(crate) struct Instance { /// Handler run when `SIGBUS`, `SIGFPE`, `SIGILL`, or `SIGSEGV` are caught by the instance thread. pub(crate) signal_handler: Cell>>, + /// TODO: document this + /// Functions to initialize the host environments in the imports. + /// Do we want to drain this? There's probably no reason to keep this memory + /// around once we've used it. + /// + /// Be sure to test with serialize/deserialize and imported functions from other Wasm modules. + import_initializers: Vec<( + Option, + *mut std::ffi::c_void, + )>, + /// Additional context used by compiled wasm code. This field is last, and /// represents a dynamically-sized array that extends beyond the nominal /// end of the struct (similar to a flexible array member). @@ -141,6 +152,14 @@ impl Instance { unsafe { &*self.imported_functions_ptr().add(index) } } + /// TODO: document this + fn imported_function_env_initializer( + &self, + index: FunctionIndex, + ) -> Option { + self.import_initializers[index.as_u32() as usize].0 + } + /// Return a pointer to the `VMFunctionImport`s. fn imported_functions_ptr(&self) -> *mut VMFunctionImport { unsafe { self.vmctx_plus_offset(self.offsets.vmctx_imported_functions_begin()) } @@ -298,11 +317,12 @@ impl Instance { crate::vmcontext::FunctionExtraData { vmctx: self.vmctx_ptr(), }, - 0, + None, ) } else { let import = self.imported_function(*index); - (import.body, import.extra_data, import.function_ptr) + let initializer = self.imported_function_env_initializer(*index); + (import.body, import.extra_data, initializer) }; let signature = self.module.signatures[*sig_index].clone(); ExportFunction { @@ -815,6 +835,10 @@ impl InstanceHandle { imports: Imports, vmshared_signatures: BoxedSlice, host_state: Box, + import_initializers: Vec<( + Option, + *mut std::ffi::c_void, + )>, ) -> Result { // TODO: investigate `vmctx_tables` and `vmctx_memories`: both of these // appear to be dropped in this function which may cause memory problems @@ -859,6 +883,7 @@ impl InstanceHandle { passive_data, host_state, signal_handler: Cell::new(None), + import_initializers, vmctx: VMContext {}, }; let layout = instance.alloc_layout(); @@ -1076,6 +1101,17 @@ impl InstanceHandle { unsafe { &*(self.instance as *const Instance) } } + /// TODO: document this + /// Initializes the host environments. + pub unsafe fn initialize_host_envs(&self, instance_ptr: *const std::ffi::c_void) { + for (func, env) in self.instance().import_initializers.iter() { + if let Some(f) = func { + dbg!(f, env); + f(*env, instance_ptr); + } + } + } + /// Deallocates memory associated with this instance. /// /// # Safety diff --git a/lib/vm/src/vmcontext.rs b/lib/vm/src/vmcontext.rs index d1de6b3110b..49a75c59869 100644 --- a/lib/vm/src/vmcontext.rs +++ b/lib/vm/src/vmcontext.rs @@ -47,10 +47,6 @@ pub struct VMFunctionImport { /// A pointer to the `VMContext` that owns the function or host data. pub extra_data: FunctionExtraData, - - /// temporary hack - /// Only used by host env - pub function_ptr: usize, } #[cfg(test)] diff --git a/lib/vm/src/vmoffsets.rs b/lib/vm/src/vmoffsets.rs index 66406d0035f..b18408165d3 100644 --- a/lib/vm/src/vmoffsets.rs +++ b/lib/vm/src/vmoffsets.rs @@ -109,7 +109,7 @@ impl VMOffsets { /// /// [`VMFunctionImport`]: crate::vmcontext::VMFunctionImport pub fn size_of_vmfunction_import(&self) -> u8 { - 3 * self.pointer_size + 2 * self.pointer_size } } diff --git a/lib/wasi/src/lib.rs b/lib/wasi/src/lib.rs index d0733055f68..70197795944 100644 --- a/lib/wasi/src/lib.rs +++ b/lib/wasi/src/lib.rs @@ -188,7 +188,6 @@ impl WasiEnv { /// Get a reference to the memory pub fn memory(&self) -> &Memory { - dbg!("Getting memory", &self.memory); unsafe { &*self.memory } } From 103ea035361c64c174e56ee1e77aebf606ce13d4 Mon Sep 17 00:00:00 2001 From: Mark McCaskey Date: Tue, 20 Oct 2020 15:40:51 -0700 Subject: [PATCH 3/6] Rename trait, get tests running --- examples/wasi.rs | 3 --- lib/api/src/externals/function.rs | 4 +-- lib/api/src/lib.rs | 2 +- lib/wasi/src/lib.rs | 2 +- tests/compilers/imports.rs | 42 ++++++++++++++++++++++++----- tests/compilers/native_functions.rs | 24 +++++++++++++++++ 6 files changed, 64 insertions(+), 13 deletions(-) diff --git a/examples/wasi.rs b/examples/wasi.rs index c9c779b7450..2a10b815f60 100644 --- a/examples/wasi.rs +++ b/examples/wasi.rs @@ -51,9 +51,6 @@ fn main() -> Result<(), Box> { let import_object = wasi_env.import_object(&module)?; let instance = Instance::new(&module, &import_object)?; - // WASI requires to explicitly set the memory for the `WasiEnv` - wasi_env.set_memory(instance.exports.get_memory("memory")?.clone()); - println!("Call WASI `_start` function..."); // And we just call the `_start` function! let start = instance.exports.get_function("_start")?; diff --git a/lib/api/src/externals/function.rs b/lib/api/src/externals/function.rs index 9a373c6b7e0..4a4081f9b11 100644 --- a/lib/api/src/externals/function.rs +++ b/lib/api/src/externals/function.rs @@ -126,7 +126,7 @@ impl Function { pub fn new_with_env(store: &Store, ty: &FunctionType, env: Env, func: F) -> Self where F: Fn(&mut Env, &[Val]) -> Result, RuntimeError> + 'static, - Env: Sized + crate::WasmerPostInstantiate + 'static, + Env: Sized + crate::WasmerEnv + 'static, { let dynamic_ctx = VMDynamicFunctionContext::from_context(VMDynamicFunctionWithEnv { env: RefCell::new(env), @@ -230,7 +230,7 @@ impl Function { F: HostFunction, Args: WasmTypeList, Rets: WasmTypeList, - Env: Sized + crate::WasmerPostInstantiate + 'static, + Env: Sized + crate::WasmerEnv + 'static, { let function = inner::Function::::new(func); let address = function.address(); diff --git a/lib/api/src/lib.rs b/lib/api/src/lib.rs index ce4d8204a83..122ea2daec1 100644 --- a/lib/api/src/lib.rs +++ b/lib/api/src/lib.rs @@ -124,7 +124,7 @@ pub const VERSION: &str = env!("CARGO_PKG_VERSION"); // TODO: rename everything, all names are throw-away names /// Prototype trait for finishing envs. -pub trait WasmerPostInstantiate { +pub trait WasmerEnv { /// The function that Wasmer will call on your type to let it finish /// instantiating. fn finish(&mut self, instance: &Instance); diff --git a/lib/wasi/src/lib.rs b/lib/wasi/src/lib.rs index 70197795944..b50a4cde56d 100644 --- a/lib/wasi/src/lib.rs +++ b/lib/wasi/src/lib.rs @@ -54,7 +54,7 @@ pub struct WasiEnv { memory: *mut Memory, } -impl wasmer::WasmerPostInstantiate for WasiEnv { +impl wasmer::WasmerEnv for WasiEnv { fn finish(&mut self, instance: &wasmer::Instance) { dbg!("in Wasi::Finish"); let memory = instance.exports.get_memory("memory").unwrap(); diff --git a/tests/compilers/imports.rs b/tests/compilers/imports.rs index b73501f4151..db43d2a9d3a 100644 --- a/tests/compilers/imports.rs +++ b/tests/compilers/imports.rs @@ -86,7 +86,22 @@ fn dynamic_function_with_env() -> Result<()> { let store = get_store(false); let module = get_module(&store)?; - let env: Arc = Arc::new(AtomicUsize::new(0)); + #[derive(Clone)] + struct Env(Arc); + + impl std::ops::Deref for Env { + type Target = Arc; + fn deref(&self) -> &Self::Target { + &self.0 + } + } + + impl WasmerEnv for Env { + fn finish(&mut self, _instance: &Instance) {} + fn free(&mut self) {} + } + + let env: Env = Env(Arc::new(AtomicUsize::new(0))); Instance::new( &module, &imports! { @@ -203,25 +218,40 @@ fn static_function_with_env() -> Result<()> { let store = get_store(false); let module = get_module(&store)?; - let env: Arc = Arc::new(AtomicUsize::new(0)); + #[derive(Clone)] + struct Env(Arc); + + impl std::ops::Deref for Env { + type Target = Arc; + fn deref(&self) -> &Self::Target { + &self.0 + } + } + + impl WasmerEnv for Env { + fn finish(&mut self, _instance: &Instance) {} + fn free(&mut self) {} + } + + let env: Env = Env(Arc::new(AtomicUsize::new(0))); Instance::new( &module, &imports! { "host" => { - "0" => Function::new_native_with_env(&store, env.clone(), |env: &mut Arc| { + "0" => Function::new_native_with_env(&store, env.clone(), |env: &mut Env| { assert_eq!(env.fetch_add(1, SeqCst), 0); }), - "1" => Function::new_native_with_env(&store, env.clone(), |env: &mut Arc, x: i32| -> i32 { + "1" => Function::new_native_with_env(&store, env.clone(), |env: &mut Env, x: i32| -> i32 { assert_eq!(x, 0); assert_eq!(env.fetch_add(1, SeqCst), 1); 1 }), - "2" => Function::new_native_with_env(&store, env.clone(), |env: &mut Arc, x: i32, y: i64| { + "2" => Function::new_native_with_env(&store, env.clone(), |env: &mut Env, x: i32, y: i64| { assert_eq!(x, 2); assert_eq!(y, 3); assert_eq!(env.fetch_add(1, SeqCst), 2); }), - "3" => Function::new_native_with_env(&store, env.clone(), |env: &mut Arc, a: i32, b: i64, c: i32, d: f32, e: f64| { + "3" => Function::new_native_with_env(&store, env.clone(), |env: &mut Env, a: i32, b: i64, c: i32, d: f32, e: f64| { assert_eq!(a, 100); assert_eq!(b, 200); assert_eq!(c, 300); diff --git a/tests/compilers/native_functions.rs b/tests/compilers/native_functions.rs index 66b3fada8f1..ed2e623a265 100644 --- a/tests/compilers/native_functions.rs +++ b/tests/compilers/native_functions.rs @@ -108,6 +108,18 @@ fn static_host_function_with_env() -> anyhow::Result<()> { #[derive(Clone)] struct Env(Rc>); + impl std::ops::Deref for Env { + type Target = Rc>; + fn deref(&self) -> &Self::Target { + &self.0 + } + } + + impl WasmerEnv for Env { + fn finish(&mut self, _instance: &Instance) {} + fn free(&mut self) {} + } + // Native static host function that returns a tuple. { let env = Env(Rc::new(RefCell::new(100))); @@ -175,6 +187,18 @@ fn dynamic_host_function_with_env() -> anyhow::Result<()> { #[derive(Clone)] struct Env(Rc>); + impl std::ops::Deref for Env { + type Target = Rc>; + fn deref(&self) -> &Self::Target { + &self.0 + } + } + + impl WasmerEnv for Env { + fn finish(&mut self, _instance: &Instance) {} + fn free(&mut self) {} + } + let env = Env(Rc::new(RefCell::new(100))); let f = Function::new_with_env( &store, From b78447f8a4f74079d222b6b1a6eb7abfa977fef2 Mon Sep 17 00:00:00 2001 From: Mark McCaskey Date: Tue, 20 Oct 2020 15:49:36 -0700 Subject: [PATCH 4/6] Fix up merge, use new union --- lib/api/src/externals/function.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/api/src/externals/function.rs b/lib/api/src/externals/function.rs index 683ca81b6f2..c0bff213658 100644 --- a/lib/api/src/externals/function.rs +++ b/lib/api/src/externals/function.rs @@ -385,7 +385,7 @@ impl Function { Self { store: store.clone(), definition: FunctionDefinition::Host(HostFunctionDefinition { - has_env: !wasmer_export.vmctx.is_null(), + has_env: !unsafe { wasmer_export.vmctx.host_env.is_null() }, }), exported: wasmer_export, } From d64908dc8302636c84af49f41d2fe93cbfa3fe84 Mon Sep 17 00:00:00 2001 From: Mark McCaskey Date: Tue, 20 Oct 2020 18:03:22 -0700 Subject: [PATCH 5/6] Fix package tests --- lib/api/src/externals/function.rs | 12 ++++++++++-- lib/api/tests/externals.rs | 9 +++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/api/src/externals/function.rs b/lib/api/src/externals/function.rs index c0bff213658..7f367fac355 100644 --- a/lib/api/src/externals/function.rs +++ b/lib/api/src/externals/function.rs @@ -108,12 +108,16 @@ impl Function { /// # Example /// /// ``` - /// # use wasmer::{Function, FunctionType, Type, Store, Value}; + /// # use wasmer::{Function, FunctionType, Type, Store, Value, WasmerEnv, Instance}; /// # let store = Store::default(); /// /// struct Env { /// multiplier: i32, /// }; + /// impl WasmerEnv for Env { + /// fn finish(&mut self, _instance: &Instance) {} + /// fn free(&mut self) {} + /// } /// let env = Env { multiplier: 2 }; /// /// let signature = FunctionType::new(vec![Type::I32, Type::I32], vec![Type::I32]); @@ -214,12 +218,16 @@ impl Function { /// # Example /// /// ``` - /// # use wasmer::{Store, Function}; + /// # use wasmer::{Store, Function, WasmerEnv, Instance}; /// # let store = Store::default(); /// /// struct Env { /// multiplier: i32, /// }; + /// impl WasmerEnv for Env { + /// fn finish(&mut self, _instance: &Instance) {} + /// fn free(&mut self) {} + /// } /// let env = Env { multiplier: 2 }; /// /// fn sum_and_multiply(env: &mut Env, a: i32, b: i32) -> i32 { diff --git a/lib/api/tests/externals.rs b/lib/api/tests/externals.rs index 49fdc6f0772..18ea8bf41e6 100644 --- a/lib/api/tests/externals.rs +++ b/lib/api/tests/externals.rs @@ -210,6 +210,11 @@ fn function_new_env() -> Result<()> { let store = Store::default(); #[derive(Clone)] struct MyEnv {}; + impl WasmerEnv for MyEnv { + fn finish(&mut self, _instance: &Instance) {} + fn free(&mut self) {} + } + let my_env = MyEnv {}; let function = Function::new_native_with_env(&store, my_env.clone(), |_env: &mut MyEnv| {}); assert_eq!(function.ty().clone(), FunctionType::new(vec![], vec![])); @@ -273,6 +278,10 @@ fn function_new_dynamic_env() -> Result<()> { #[derive(Clone)] struct MyEnv {}; let my_env = MyEnv {}; + impl WasmerEnv for MyEnv { + fn finish(&mut self, _instance: &Instance) {} + fn free(&mut self) {} + } let function_type = FunctionType::new(vec![], vec![]); let function = Function::new_with_env( From 149d0b937e39b9a81605a2a140e420bd8dc30340 Mon Sep 17 00:00:00 2001 From: Mark McCaskey Date: Wed, 21 Oct 2020 11:56:57 -0700 Subject: [PATCH 6/6] Fix up more tests --- lib/api/src/lib.rs | 9 ++ lib/c-api/src/deprecated/import/mod.rs | 5 + lib/c-api/src/deprecated/import/wasi.rs | 7 +- .../src/wasm_c_api/externals/function.rs | 15 ++- lib/c-api/src/wasm_c_api/wasi/mod.rs | 13 ++- lib/c-api/tests/wasm_c_api/test-wasi.c | 1 - lib/cli/src/commands/wasmer_create_exe_main.c | 4 - lib/deprecated/runtime-core/Cargo.lock | 104 +++++++++++------- lib/deprecated/runtime-core/src/module.rs | 8 +- lib/deprecated/runtime-core/src/typed_func.rs | 5 + lib/deprecated/runtime-core/src/vm.rs | 6 + lib/deprecated/runtime/Cargo.lock | 104 +++++++++++------- 12 files changed, 178 insertions(+), 103 deletions(-) diff --git a/lib/api/src/lib.rs b/lib/api/src/lib.rs index 122ea2daec1..4f3002b7430 100644 --- a/lib/api/src/lib.rs +++ b/lib/api/src/lib.rs @@ -132,3 +132,12 @@ pub trait WasmerEnv { /// Frees memory written to `self` so it can be dropped without any memory leaks. fn free(&mut self); } + +impl WasmerEnv for &'static mut T { + fn finish(&mut self, instance: &Instance) { + (*self).finish(instance) + } + fn free(&mut self) { + (*self).free() + } +} \ No newline at end of file diff --git a/lib/c-api/src/deprecated/import/mod.rs b/lib/c-api/src/deprecated/import/mod.rs index 09eb562c075..1c3bf0bf442 100644 --- a/lib/c-api/src/deprecated/import/mod.rs +++ b/lib/c-api/src/deprecated/import/mod.rs @@ -639,6 +639,11 @@ pub(crate) struct LegacyEnv { pub(crate) instance_ptr: Option>, } +impl wasmer::WasmerEnv for LegacyEnv { + fn finish(&mut self, _instance: &wasmer::Instance) {} + fn free(&mut self) {} +} + impl LegacyEnv { pub(crate) fn ctx_ptr(&self) -> *mut CAPIInstance { self.instance_ptr diff --git a/lib/c-api/src/deprecated/import/wasi.rs b/lib/c-api/src/deprecated/import/wasi.rs index 77196a60e20..aa94f523dab 100644 --- a/lib/c-api/src/deprecated/import/wasi.rs +++ b/lib/c-api/src/deprecated/import/wasi.rs @@ -4,7 +4,7 @@ use libc::c_uchar; use std::path::PathBuf; use std::ptr; use std::str; -use wasmer::{Memory, MemoryType, NamedResolver}; +use wasmer::NamedResolver; use wasmer_wasi as wasi; #[derive(Debug, PartialEq)] @@ -213,11 +213,6 @@ pub unsafe extern "C" fn wasmer_wasi_generate_default_import_object() -> *mut wa let mut wasi_state_builder = wasi::WasiState::new("wasmer-wasi-default-program-name"); let wasi_state = wasi_state_builder.build().unwrap(); let mut wasi_env = wasi::WasiEnv::new(wasi_state); - // this API will now leak a `Memory` - let memory_type = MemoryType::new(0, None, false); - let memory = Memory::new(store, memory_type).expect("create memory"); - wasi_env.set_memory(memory); - // TODO(mark): review lifetime of `Memory` here let import_object_inner: Box = Box::new( wasi::generate_import_object_from_env(store, wasi_env, wasi::WasiVersion::Latest), ); diff --git a/lib/c-api/src/wasm_c_api/externals/function.rs b/lib/c-api/src/wasm_c_api/externals/function.rs index 6f6319365b3..1ea6c8f5333 100644 --- a/lib/c-api/src/wasm_c_api/externals/function.rs +++ b/lib/c-api/src/wasm_c_api/externals/function.rs @@ -92,8 +92,17 @@ pub unsafe extern "C" fn wasm_func_new_with_env( // TODO: handle null pointers? let func_sig = ft.sig(); let num_rets = func_sig.results().len(); + + #[repr(transparent)] + struct WrapperEnv(*mut c_void); + + impl wasmer::WasmerEnv for WrapperEnv { + fn finish(&mut self, _instance: &wasmer::Instance) {} + fn free(&mut self) {} + } + let inner_callback = - move |env: &mut *mut c_void, args: &[Val]| -> Result, RuntimeError> { + move |env: &mut WrapperEnv, args: &[Val]| -> Result, RuntimeError> { let processed_args: wasm_val_vec_t = args .into_iter() .map(TryInto::try_into) @@ -110,7 +119,7 @@ pub unsafe extern "C" fn wasm_func_new_with_env( ] .into(); - let _traps = callback(*env, &processed_args, &mut results); + let _traps = callback(env.0, &processed_args, &mut results); // TODO: do something with `traps` let processed_results = results @@ -124,7 +133,7 @@ pub unsafe extern "C" fn wasm_func_new_with_env( Ok(processed_results) }; - let function = Function::new_with_env(&store.inner, &func_sig, env, inner_callback); + let function = Function::new_with_env(&store.inner, &func_sig, WrapperEnv(env), inner_callback); Some(Box::new(wasm_func_t { instance: None, diff --git a/lib/c-api/src/wasm_c_api/wasi/mod.rs b/lib/c-api/src/wasm_c_api/wasi/mod.rs index 68516c66a12..a5c8beddd8f 100644 --- a/lib/c-api/src/wasm_c_api/wasi/mod.rs +++ b/lib/c-api/src/wasm_c_api/wasi/mod.rs @@ -174,21 +174,28 @@ pub extern "C" fn wasi_env_new(mut config: Box) -> Option>) {} +// Dead code: deprecate or remove #[no_mangle] -pub extern "C" fn wasi_env_set_instance(env: &mut wasi_env_t, instance: &wasm_instance_t) -> bool { +pub extern "C" fn wasi_env_set_instance( + _env: &mut wasi_env_t, + _instance: &wasm_instance_t, +) -> bool { + /* let memory = if let Ok(memory) = instance.inner.exports.get_memory("memory") { memory } else { return false; }; env.inner.set_memory(memory.clone()); + */ true } +// Dead code: deprecate or remove #[no_mangle] -pub extern "C" fn wasi_env_set_memory(env: &mut wasi_env_t, memory: &wasm_memory_t) { - env.inner.set_memory(memory.inner.clone()); +pub extern "C" fn wasi_env_set_memory(_env: &mut wasi_env_t, _memory: &wasm_memory_t) { + //env.inner.set_memory(memory.inner.clone()); } #[no_mangle] diff --git a/lib/c-api/tests/wasm_c_api/test-wasi.c b/lib/c-api/tests/wasm_c_api/test-wasi.c index 1c7c244db91..60f89185c2f 100644 --- a/lib/c-api/tests/wasm_c_api/test-wasi.c +++ b/lib/c-api/tests/wasm_c_api/test-wasi.c @@ -103,7 +103,6 @@ int main(int argc, const char* argv[]) { } fprintf(stderr, "found %zu exports\n", exports.size); - wasi_env_set_instance(wasi_env, instance); wasm_func_t* run_func = wasi_get_start_function(instance); if (run_func == NULL) { printf("> Error accessing export!\n"); diff --git a/lib/cli/src/commands/wasmer_create_exe_main.c b/lib/cli/src/commands/wasmer_create_exe_main.c index 6624c31866c..73300da587a 100644 --- a/lib/cli/src/commands/wasmer_create_exe_main.c +++ b/lib/cli/src/commands/wasmer_create_exe_main.c @@ -159,10 +159,6 @@ int main(int argc, char* argv[]) { print_wasmer_error(); return -1; } - - #ifdef WASI - wasi_env_set_instance(wasi_env, instance); - #endif #ifdef WASI own wasm_func_t* start_function = wasi_get_start_function(instance); diff --git a/lib/deprecated/runtime-core/Cargo.lock b/lib/deprecated/runtime-core/Cargo.lock index 6ab9c79044c..a49b103f64f 100644 --- a/lib/deprecated/runtime-core/Cargo.lock +++ b/lib/deprecated/runtime-core/Cargo.lock @@ -107,9 +107,9 @@ checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" [[package]] name = "cloudabi" -version = "0.0.3" +version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" +checksum = "4344512281c643ae7638bbabc3af17a11307803ec8f0fcad9fae512a8bf36467" dependencies = [ "bitflags", ] @@ -455,8 +455,9 @@ dependencies = [ [[package]] name = "inkwell" -version = "0.1.0" -source = "git+https://github.com/theDan64/inkwell?rev=fdf895777e937c974204e879cf1102cf7a727c42#fdf895777e937c974204e879cf1102cf7a727c42" +version = "0.1.0-llvm10sample" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e079c12273d96e41481454a37ad968e607e1ce51b39b9facd3a802a12df6e9dc" dependencies = [ "either", "inkwell_internals", @@ -470,13 +471,23 @@ dependencies = [ [[package]] name = "inkwell_internals" version = "0.2.0" -source = "git+https://github.com/theDan64/inkwell?rev=fdf895777e937c974204e879cf1102cf7a727c42#fdf895777e937c974204e879cf1102cf7a727c42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b22cf4eda09069b48204cce4b7cd9a25311da813780e95a038524f2210fab44e" dependencies = [ "proc-macro2", "quote", "syn", ] +[[package]] +name = "instant" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63312a18f7ea8760cdd0a7c5aac1a619752a246b833545e3e36d1f81f7cd9e66" +dependencies = [ + "cfg-if", +] + [[package]] name = "itertools" version = "0.9.0" @@ -528,9 +539,9 @@ dependencies = [ [[package]] name = "lock_api" -version = "0.3.4" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c4da24a77a3d8a6d4862d95f72e6fdb9c09a643ecdb402d754004a557f2bec75" +checksum = "28247cc5a5be2f05fbcd76dd0cf2c7d3b5400cb978a28042abcd4fa0b3f8261c" dependencies = [ "scopeguard", ] @@ -642,22 +653,24 @@ checksum = "0b631f7e854af39a1739f401cf34a8a013dfe09eac4fa4dba91e9768bd28168d" [[package]] name = "parking_lot" -version = "0.10.2" +version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d3a704eb390aafdc107b0e392f56a82b668e3a71366993b5340f5833fd62505e" +checksum = "a4893845fa2ca272e647da5d0e46660a314ead9c2fdd9a883aabc32e481a8733" dependencies = [ + "instant", "lock_api", "parking_lot_core", ] [[package]] name = "parking_lot_core" -version = "0.7.2" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d58c7c768d4ba344e3e8d72518ac13e259d7c7ade24167003b8488e10b6740a3" +checksum = "c361aa727dd08437f2f1447be8b59a33b0edd15e0fcee698f935613d9efbca9b" dependencies = [ "cfg-if", "cloudabi", + "instant", "libc", "redox_syscall", "smallvec", @@ -1094,24 +1107,15 @@ version = "0.9.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" -[[package]] -name = "wasmer-types" -version = "1.0.0-alpha.1" -dependencies = [ - "cranelift-entity", - "serde", -] - [[package]] name = "wasmer" -version = "1.0.0-alpha.1" +version = "1.0.0-alpha4" dependencies = [ "cfg-if", "indexmap", "more-asserts", "target-lexicon", "thiserror", - "wasmer-types", "wasmer-compiler", "wasmer-compiler-cranelift", "wasmer-compiler-llvm", @@ -1119,6 +1123,7 @@ dependencies = [ "wasmer-engine", "wasmer-engine-jit", "wasmer-engine-native", + "wasmer-types", "wasmer-vm", "wat", "winapi", @@ -1126,7 +1131,7 @@ dependencies = [ [[package]] name = "wasmer-cache" -version = "1.0.0-alpha.1" +version = "1.0.0-alpha4" dependencies = [ "blake3", "hex", @@ -1137,7 +1142,7 @@ dependencies = [ [[package]] name = "wasmer-compiler" -version = "1.0.0-alpha.1" +version = "1.0.0-alpha4" dependencies = [ "enumset", "raw-cpuid", @@ -1153,7 +1158,7 @@ dependencies = [ [[package]] name = "wasmer-compiler-cranelift" -version = "1.0.0-alpha.1" +version = "1.0.0-alpha4" dependencies = [ "cranelift-codegen", "cranelift-frontend", @@ -1162,14 +1167,14 @@ dependencies = [ "rayon", "serde", "tracing", - "wasmer-types", "wasmer-compiler", + "wasmer-types", "wasmer-vm", ] [[package]] name = "wasmer-compiler-llvm" -version = "1.0.0-alpha.1" +version = "1.0.0-alpha4" dependencies = [ "byteorder", "cc", @@ -1184,14 +1189,14 @@ dependencies = [ "semver", "smallvec", "target-lexicon", - "wasmer-types", "wasmer-compiler", + "wasmer-types", "wasmer-vm", ] [[package]] name = "wasmer-compiler-singlepass" -version = "1.0.0-alpha.1" +version = "1.0.0-alpha4" dependencies = [ "byteorder", "dynasm", @@ -1201,14 +1206,14 @@ dependencies = [ "rayon", "serde", "smallvec", - "wasmer-types", "wasmer-compiler", + "wasmer-types", "wasmer-vm", ] [[package]] name = "wasmer-engine" -version = "1.0.0-alpha.1" +version = "1.0.0-alpha4" dependencies = [ "backtrace", "bincode", @@ -1219,55 +1224,54 @@ dependencies = [ "serde_bytes", "target-lexicon", "thiserror", - "wasmer-types", "wasmer-compiler", + "wasmer-types", "wasmer-vm", - "winapi", ] [[package]] name = "wasmer-engine-jit" -version = "1.0.0-alpha.1" +version = "1.0.0-alpha4" dependencies = [ "bincode", "cfg-if", "region", "serde", "serde_bytes", - "wasmer-types", "wasmer-compiler", "wasmer-engine", + "wasmer-types", "wasmer-vm", "winapi", ] [[package]] name = "wasmer-engine-native" -version = "1.0.0-alpha.1" +version = "1.0.0-alpha4" dependencies = [ "bincode", "cfg-if", "leb128", "libloading", "serde", - "serde_bytes", "tempfile", "tracing", - "wasmer-types", "wasmer-compiler", "wasmer-engine", "wasmer-object", + "wasmer-types", "wasmer-vm", + "which", ] [[package]] name = "wasmer-object" -version = "1.0.0-alpha.1" +version = "1.0.0-alpha4" dependencies = [ "object 0.19.0", "thiserror", - "wasmer-types", "wasmer-compiler", + "wasmer-types", ] [[package]] @@ -1276,7 +1280,6 @@ version = "0.18.0" dependencies = [ "blake3", "lazy_static", - "wasmer-types", "wasmer", "wasmer-cache", "wasmer-compiler", @@ -1285,12 +1288,21 @@ dependencies = [ "wasmer-compiler-singlepass", "wasmer-engine", "wasmer-engine-jit", + "wasmer-types", "wasmer-vm", ] +[[package]] +name = "wasmer-types" +version = "1.0.0-alpha4" +dependencies = [ + "cranelift-entity", + "serde", +] + [[package]] name = "wasmer-vm" -version = "1.0.0-alpha.1" +version = "1.0.0-alpha4" dependencies = [ "backtrace", "cc", @@ -1330,6 +1342,16 @@ dependencies = [ "wast", ] +[[package]] +name = "which" +version = "4.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87c14ef7e1b8b8ecfc75d5eca37949410046e66f15d185c01d70824f1f8111ef" +dependencies = [ + "libc", + "thiserror", +] + [[package]] name = "winapi" version = "0.3.9" diff --git a/lib/deprecated/runtime-core/src/module.rs b/lib/deprecated/runtime-core/src/module.rs index ac9b606d12d..7ef483dcb85 100644 --- a/lib/deprecated/runtime-core/src/module.rs +++ b/lib/deprecated/runtime-core/src/module.rs @@ -102,14 +102,14 @@ impl Module { // Properly drop the empty `vm::Ctx` // created by the host function. unsafe { - ptr::drop_in_place::(function.vmctx as _); + ptr::drop_in_place::(function.vmctx.host_env as _); } // Update the pointer to `VMContext`, // which is actually a `vm::Ctx` // pointer, to fallback on the // environment hack. - function.vmctx = pre_instance.vmctx_ptr() as _; + function.vmctx.host_env = pre_instance.vmctx_ptr() as _; } // `function` is a dynamic host function // constructed with @@ -147,13 +147,13 @@ impl Module { new::wasmer_vm::VMDynamicFunctionContext< VMDynamicFunctionWithEnv, >, - > = unsafe { Box::from_raw(function.vmctx as *mut _) }; + > = unsafe { Box::from_raw(function.vmctx.host_env as *mut _) }; // Replace the environment by ours. vmctx.ctx.env.borrow_mut().vmctx = pre_instance.vmctx(); // … without anyone noticing… - function.vmctx = Box::into_raw(vmctx) as _; + function.vmctx.host_env = Box::into_raw(vmctx) as _; } } diff --git a/lib/deprecated/runtime-core/src/typed_func.rs b/lib/deprecated/runtime-core/src/typed_func.rs index a1b8d74685e..c9b549f1093 100644 --- a/lib/deprecated/runtime-core/src/typed_func.rs +++ b/lib/deprecated/runtime-core/src/typed_func.rs @@ -236,6 +236,11 @@ pub(crate) struct DynamicCtx { pub(crate) vmctx: Rc>, } +impl new::wasmer::WasmerEnv for DynamicCtx { + fn finish(&mut self, _instance: &new::wasmer::Instance) {} + fn free(&mut self) {} +} + impl DynamicFunc { /// Create a new `DynamicFunc`. pub fn new(signature: &FuncSig, func: F) -> Self diff --git a/lib/deprecated/runtime-core/src/vm.rs b/lib/deprecated/runtime-core/src/vm.rs index 8b129e3c647..a07aecf6ea8 100644 --- a/lib/deprecated/runtime-core/src/vm.rs +++ b/lib/deprecated/runtime-core/src/vm.rs @@ -1,4 +1,5 @@ use crate::module::ModuleInfo; +use crate::new; use std::{ffi::c_void, ptr}; /// The context of the currently running WebAssembly instance. @@ -38,6 +39,11 @@ pub struct Ctx { pub data_finalizer: Option, } +impl new::wasmer::WasmerEnv for Ctx { + fn finish(&mut self, _instance: &new::wasmer::Instance) {} + fn free(&mut self) {} +} + impl Ctx { pub(crate) unsafe fn new_uninit() -> Self { Self { diff --git a/lib/deprecated/runtime/Cargo.lock b/lib/deprecated/runtime/Cargo.lock index 161e55b4649..3cb70a56205 100644 --- a/lib/deprecated/runtime/Cargo.lock +++ b/lib/deprecated/runtime/Cargo.lock @@ -107,9 +107,9 @@ checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" [[package]] name = "cloudabi" -version = "0.0.3" +version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" +checksum = "4344512281c643ae7638bbabc3af17a11307803ec8f0fcad9fae512a8bf36467" dependencies = [ "bitflags", ] @@ -455,8 +455,9 @@ dependencies = [ [[package]] name = "inkwell" -version = "0.1.0" -source = "git+https://github.com/theDan64/inkwell?rev=fdf895777e937c974204e879cf1102cf7a727c42#fdf895777e937c974204e879cf1102cf7a727c42" +version = "0.1.0-llvm10sample" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e079c12273d96e41481454a37ad968e607e1ce51b39b9facd3a802a12df6e9dc" dependencies = [ "either", "inkwell_internals", @@ -470,13 +471,23 @@ dependencies = [ [[package]] name = "inkwell_internals" version = "0.2.0" -source = "git+https://github.com/theDan64/inkwell?rev=fdf895777e937c974204e879cf1102cf7a727c42#fdf895777e937c974204e879cf1102cf7a727c42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b22cf4eda09069b48204cce4b7cd9a25311da813780e95a038524f2210fab44e" dependencies = [ "proc-macro2", "quote", "syn", ] +[[package]] +name = "instant" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63312a18f7ea8760cdd0a7c5aac1a619752a246b833545e3e36d1f81f7cd9e66" +dependencies = [ + "cfg-if", +] + [[package]] name = "itertools" version = "0.9.0" @@ -528,9 +539,9 @@ dependencies = [ [[package]] name = "lock_api" -version = "0.3.4" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c4da24a77a3d8a6d4862d95f72e6fdb9c09a643ecdb402d754004a557f2bec75" +checksum = "28247cc5a5be2f05fbcd76dd0cf2c7d3b5400cb978a28042abcd4fa0b3f8261c" dependencies = [ "scopeguard", ] @@ -642,22 +653,24 @@ checksum = "0b631f7e854af39a1739f401cf34a8a013dfe09eac4fa4dba91e9768bd28168d" [[package]] name = "parking_lot" -version = "0.10.2" +version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d3a704eb390aafdc107b0e392f56a82b668e3a71366993b5340f5833fd62505e" +checksum = "a4893845fa2ca272e647da5d0e46660a314ead9c2fdd9a883aabc32e481a8733" dependencies = [ + "instant", "lock_api", "parking_lot_core", ] [[package]] name = "parking_lot_core" -version = "0.7.2" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d58c7c768d4ba344e3e8d72518ac13e259d7c7ade24167003b8488e10b6740a3" +checksum = "c361aa727dd08437f2f1447be8b59a33b0edd15e0fcee698f935613d9efbca9b" dependencies = [ "cfg-if", "cloudabi", + "instant", "libc", "redox_syscall", "smallvec", @@ -1094,24 +1107,15 @@ version = "0.9.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" -[[package]] -name = "wasmer-types" -version = "1.0.0-alpha.1" -dependencies = [ - "cranelift-entity", - "serde", -] - [[package]] name = "wasmer" -version = "1.0.0-alpha.1" +version = "1.0.0-alpha4" dependencies = [ "cfg-if", "indexmap", "more-asserts", "target-lexicon", "thiserror", - "wasmer-types", "wasmer-compiler", "wasmer-compiler-cranelift", "wasmer-compiler-llvm", @@ -1119,6 +1123,7 @@ dependencies = [ "wasmer-engine", "wasmer-engine-jit", "wasmer-engine-native", + "wasmer-types", "wasmer-vm", "wat", "winapi", @@ -1126,7 +1131,7 @@ dependencies = [ [[package]] name = "wasmer-cache" -version = "1.0.0-alpha.1" +version = "1.0.0-alpha4" dependencies = [ "blake3", "hex", @@ -1137,7 +1142,7 @@ dependencies = [ [[package]] name = "wasmer-compiler" -version = "1.0.0-alpha.1" +version = "1.0.0-alpha4" dependencies = [ "enumset", "raw-cpuid", @@ -1153,7 +1158,7 @@ dependencies = [ [[package]] name = "wasmer-compiler-cranelift" -version = "1.0.0-alpha.1" +version = "1.0.0-alpha4" dependencies = [ "cranelift-codegen", "cranelift-frontend", @@ -1162,14 +1167,14 @@ dependencies = [ "rayon", "serde", "tracing", - "wasmer-types", "wasmer-compiler", + "wasmer-types", "wasmer-vm", ] [[package]] name = "wasmer-compiler-llvm" -version = "1.0.0-alpha.1" +version = "1.0.0-alpha4" dependencies = [ "byteorder", "cc", @@ -1184,14 +1189,14 @@ dependencies = [ "semver", "smallvec", "target-lexicon", - "wasmer-types", "wasmer-compiler", + "wasmer-types", "wasmer-vm", ] [[package]] name = "wasmer-compiler-singlepass" -version = "1.0.0-alpha.1" +version = "1.0.0-alpha4" dependencies = [ "byteorder", "dynasm", @@ -1201,14 +1206,14 @@ dependencies = [ "rayon", "serde", "smallvec", - "wasmer-types", "wasmer-compiler", + "wasmer-types", "wasmer-vm", ] [[package]] name = "wasmer-engine" -version = "1.0.0-alpha.1" +version = "1.0.0-alpha4" dependencies = [ "backtrace", "bincode", @@ -1219,55 +1224,54 @@ dependencies = [ "serde_bytes", "target-lexicon", "thiserror", - "wasmer-types", "wasmer-compiler", + "wasmer-types", "wasmer-vm", - "winapi", ] [[package]] name = "wasmer-engine-jit" -version = "1.0.0-alpha.1" +version = "1.0.0-alpha4" dependencies = [ "bincode", "cfg-if", "region", "serde", "serde_bytes", - "wasmer-types", "wasmer-compiler", "wasmer-engine", + "wasmer-types", "wasmer-vm", "winapi", ] [[package]] name = "wasmer-engine-native" -version = "1.0.0-alpha.1" +version = "1.0.0-alpha4" dependencies = [ "bincode", "cfg-if", "leb128", "libloading", "serde", - "serde_bytes", "tempfile", "tracing", - "wasmer-types", "wasmer-compiler", "wasmer-engine", "wasmer-object", + "wasmer-types", "wasmer-vm", + "which", ] [[package]] name = "wasmer-object" -version = "1.0.0-alpha.1" +version = "1.0.0-alpha4" dependencies = [ "object 0.19.0", "thiserror", - "wasmer-types", "wasmer-compiler", + "wasmer-types", ] [[package]] @@ -1283,7 +1287,6 @@ version = "0.18.0" dependencies = [ "blake3", "lazy_static", - "wasmer-types", "wasmer", "wasmer-cache", "wasmer-compiler", @@ -1292,12 +1295,21 @@ dependencies = [ "wasmer-compiler-singlepass", "wasmer-engine", "wasmer-engine-jit", + "wasmer-types", "wasmer-vm", ] +[[package]] +name = "wasmer-types" +version = "1.0.0-alpha4" +dependencies = [ + "cranelift-entity", + "serde", +] + [[package]] name = "wasmer-vm" -version = "1.0.0-alpha.1" +version = "1.0.0-alpha4" dependencies = [ "backtrace", "cc", @@ -1337,6 +1349,16 @@ dependencies = [ "wast", ] +[[package]] +name = "which" +version = "4.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87c14ef7e1b8b8ecfc75d5eca37949410046e66f15d185c01d70824f1f8111ef" +dependencies = [ + "libc", + "thiserror", +] + [[package]] name = "winapi" version = "0.3.9"