diff --git a/lib/api/src/exports.rs b/lib/api/src/exports.rs index 4bfa11074dd..1f1ecfbda72 100644 --- a/lib/api/src/exports.rs +++ b/lib/api/src/exports.rs @@ -7,7 +7,7 @@ use std::fmt; use std::iter::{ExactSizeIterator, FromIterator}; use std::sync::Arc; use thiserror::Error; -use wasmer_vm::Export; +use wasmer_engine::Export; /// The `ExportError` can happen when trying to get a specific /// export [`Extern`] from the [`Instance`] exports. diff --git a/lib/api/src/externals/function.rs b/lib/api/src/externals/function.rs index fe17f68e2ac..82425853a6d 100644 --- a/lib/api/src/externals/function.rs +++ b/lib/api/src/externals/function.rs @@ -12,9 +12,10 @@ pub use inner::{UnsafeMutableEnv, WithUnsafeMutableEnv}; use std::cmp::max; use std::fmt; +use wasmer_engine::{Export, ExportFunction}; use wasmer_vm::{ - raise_user_trap, resume_panic, wasmer_call_trampoline, Export, ExportFunction, - VMCallerCheckedAnyfunc, VMDynamicFunctionContext, VMFunctionBody, VMFunctionEnvironment, + raise_user_trap, resume_panic, wasmer_call_trampoline, VMCallerCheckedAnyfunc, + VMDynamicFunctionContext, VMExportFunction, VMFunctionBody, VMFunctionEnvironment, VMFunctionKind, VMTrampoline, }; @@ -96,12 +97,14 @@ impl Function { store: store.clone(), definition: FunctionDefinition::Host(HostFunctionDefinition { has_env: false }), exported: ExportFunction { - address, - kind: VMFunctionKind::Dynamic, - vmctx, - function_ptr: None, - signature: ty.clone(), - call_trampoline: None, + import_init_function_ptr: None, + vm_function: VMExportFunction { + address, + kind: VMFunctionKind::Dynamic, + vmctx, + signature: ty.clone(), + call_trampoline: None, + }, }, } } @@ -146,7 +149,7 @@ impl Function { 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 { + let import_init_function_ptr = Some(unsafe { std::mem::transmute:: Result<(), _>, fn(_, _) -> Result<(), _>>( Env::init_with_instance, ) @@ -156,12 +159,14 @@ impl Function { store: store.clone(), definition: FunctionDefinition::Host(HostFunctionDefinition { has_env: true }), exported: ExportFunction { - address, - kind: VMFunctionKind::Dynamic, - vmctx, - function_ptr, - signature: ty.clone(), - call_trampoline: None, + import_init_function_ptr, + vm_function: VMExportFunction { + address, + kind: VMFunctionKind::Dynamic, + vmctx, + signature: ty.clone(), + call_trampoline: None, + }, }, } } @@ -200,15 +205,18 @@ impl Function { Self { store: store.clone(), definition: FunctionDefinition::Host(HostFunctionDefinition { has_env: false }), + exported: ExportFunction { - address, - vmctx, - signature, // 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, - call_trampoline: None, + import_init_function_ptr: None, + vm_function: VMExportFunction { + address, + vmctx, + signature, + kind: VMFunctionKind::Static, + call_trampoline: None, + }, }, } } @@ -256,7 +264,7 @@ impl Function { host_env: Box::into_raw(box_env) as *mut _, }; // TODO: look into removing transmute by changing API type signatures - let function_ptr = Some(unsafe { + let import_init_function_ptr = Some(unsafe { std::mem::transmute:: Result<(), _>, fn(_, _) -> Result<(), _>>( Env::init_with_instance, ) @@ -267,12 +275,14 @@ impl Function { store: store.clone(), definition: FunctionDefinition::Host(HostFunctionDefinition { has_env: true }), exported: ExportFunction { - address, - kind: VMFunctionKind::Static, - vmctx, - function_ptr, - signature, - call_trampoline: None, + import_init_function_ptr, + vm_function: VMExportFunction { + address, + kind: VMFunctionKind::Static, + vmctx, + signature, + call_trampoline: None, + }, }, } } @@ -305,7 +315,7 @@ impl Function { }; let signature = function.ty(); // TODO: look into removing transmute by changing API type signatures - let function_ptr = Some(std::mem::transmute::< + let import_init_function_ptr = Some(std::mem::transmute::< fn(_, _) -> Result<(), _>, fn(_, _) -> Result<(), _>, >(Env::init_with_instance)); @@ -314,12 +324,14 @@ impl Function { store: store.clone(), definition: FunctionDefinition::Host(HostFunctionDefinition { has_env: true }), exported: ExportFunction { - address, - kind: VMFunctionKind::Static, - vmctx, - function_ptr, - signature, - call_trampoline: None, + import_init_function_ptr, + vm_function: VMExportFunction { + address, + kind: VMFunctionKind::Static, + vmctx, + signature, + call_trampoline: None, + }, }, } } @@ -342,7 +354,7 @@ impl Function { /// assert_eq!(f.ty().results(), vec![Type::I32]); /// ``` pub fn ty(&self) -> &FunctionType { - &self.exported.signature + &self.exported.vm_function.signature } /// Returns the [`Store`] where the `Function` belongs. @@ -399,9 +411,9 @@ impl Function { // Call the trampoline. if let Err(error) = unsafe { wasmer_call_trampoline( - self.exported.vmctx, + self.exported.vm_function.vmctx, func.trampoline, - self.exported.address, + self.exported.vm_function.address, values_vec.as_mut_ptr() as *mut u8, ) } { @@ -502,7 +514,7 @@ impl Function { } pub(crate) fn from_export(store: &Store, wasmer_export: ExportFunction) -> Self { - if let Some(trampoline) = wasmer_export.call_trampoline { + if let Some(trampoline) = wasmer_export.vm_function.call_trampoline { Self { store: store.clone(), definition: FunctionDefinition::Wasm(WasmFunctionDefinition { trampoline }), @@ -512,7 +524,7 @@ impl Function { Self { store: store.clone(), definition: FunctionDefinition::Host(HostFunctionDefinition { - has_env: !wasmer_export.vmctx.is_null(), + has_env: !wasmer_export.vm_function.vmctx.is_null(), }), exported: wasmer_export, } @@ -523,11 +535,11 @@ impl Function { let vmsignature = self .store .engine() - .register_signature(&self.exported.signature); + .register_signature(&self.exported.vm_function.signature); VMCallerCheckedAnyfunc { - func_ptr: self.exported.address, + func_ptr: self.exported.vm_function.address, type_index: vmsignature, - vmctx: self.exported.vmctx, + vmctx: self.exported.vm_function.vmctx, } } @@ -613,7 +625,7 @@ impl Function { { // type check { - let expected = self.exported.signature.params(); + let expected = self.exported.vm_function.signature.params(); let given = Args::wasm_types(); if expected != given { @@ -626,7 +638,7 @@ impl Function { } { - let expected = self.exported.signature.results(); + let expected = self.exported.vm_function.signature.results(); let given = Rets::wasm_types(); if expected != given { @@ -641,9 +653,9 @@ impl Function { Ok(NativeFunc::new( self.store.clone(), - self.exported.address, - self.exported.vmctx, - self.exported.kind, + self.exported.vm_function.address, + self.exported.vm_function.vmctx, + self.exported.vm_function.kind, self.definition.clone(), )) } diff --git a/lib/api/src/externals/global.rs b/lib/api/src/externals/global.rs index a306b9a94e5..9c0a73e7c57 100644 --- a/lib/api/src/externals/global.rs +++ b/lib/api/src/externals/global.rs @@ -7,7 +7,8 @@ use crate::Mutability; use crate::RuntimeError; use std::fmt; use std::sync::Arc; -use wasmer_vm::{Export, ExportGlobal, Global as RuntimeGlobal}; +use wasmer_engine::{Export, ExportGlobal}; +use wasmer_vm::{Global as RuntimeGlobal, VMExportGlobal}; /// A WebAssembly `global` instance. /// @@ -183,7 +184,7 @@ impl Global { pub(crate) fn from_export(store: &Store, wasmer_export: ExportGlobal) -> Self { Self { store: store.clone(), - global: wasmer_export.from, + global: wasmer_export.vm_global.from, } } @@ -217,7 +218,9 @@ impl fmt::Debug for Global { impl<'a> Exportable<'a> for Global { fn to_export(&self) -> Export { ExportGlobal { - from: self.global.clone(), + vm_global: VMExportGlobal { + from: self.global.clone(), + }, } .into() } diff --git a/lib/api/src/externals/memory.rs b/lib/api/src/externals/memory.rs index 32583c82fef..08d7744934b 100644 --- a/lib/api/src/externals/memory.rs +++ b/lib/api/src/externals/memory.rs @@ -5,8 +5,9 @@ use crate::{MemoryType, MemoryView}; use std::convert::TryInto; use std::slice; use std::sync::Arc; +use wasmer_engine::{Export, ExportMemory}; use wasmer_types::{Pages, ValueType}; -use wasmer_vm::{Export, ExportMemory, Memory as RuntimeMemory, MemoryError}; +use wasmer_vm::{Memory as RuntimeMemory, MemoryError, VMExportMemory}; /// A WebAssembly `memory` instance. /// @@ -223,7 +224,7 @@ impl Memory { pub(crate) fn from_export(store: &Store, wasmer_export: ExportMemory) -> Self { Self { store: store.clone(), - memory: wasmer_export.from, + memory: wasmer_export.vm_memory.from, } } @@ -247,7 +248,9 @@ impl Memory { impl<'a> Exportable<'a> for Memory { fn to_export(&self) -> Export { ExportMemory { - from: self.memory.clone(), + vm_memory: VMExportMemory { + from: self.memory.clone(), + }, } .into() } diff --git a/lib/api/src/externals/mod.rs b/lib/api/src/externals/mod.rs index c9521870059..ccdf974ab6f 100644 --- a/lib/api/src/externals/mod.rs +++ b/lib/api/src/externals/mod.rs @@ -17,7 +17,7 @@ use crate::exports::{ExportError, Exportable}; use crate::store::{Store, StoreObject}; use crate::ExternType; use std::fmt; -use wasmer_vm::Export; +use wasmer_engine::Export; /// An `Extern` is the runtime representation of an entity that /// can be imported or exported. diff --git a/lib/api/src/externals/table.rs b/lib/api/src/externals/table.rs index 4bcbf257584..ad2db9a0cfb 100644 --- a/lib/api/src/externals/table.rs +++ b/lib/api/src/externals/table.rs @@ -5,7 +5,8 @@ use crate::types::{Val, ValFuncRef}; use crate::RuntimeError; use crate::TableType; use std::sync::Arc; -use wasmer_vm::{Export, ExportTable, Table as RuntimeTable, VMCallerCheckedAnyfunc}; +use wasmer_engine::{Export, ExportTable}; +use wasmer_vm::{Table as RuntimeTable, VMCallerCheckedAnyfunc, VMExportTable}; /// A WebAssembly `table` instance. /// @@ -142,7 +143,7 @@ impl Table { pub(crate) fn from_export(store: &Store, wasmer_export: ExportTable) -> Self { Self { store: store.clone(), - table: wasmer_export.from, + table: wasmer_export.vm_table.from, } } @@ -155,7 +156,9 @@ impl Table { impl<'a> Exportable<'a> for Table { fn to_export(&self) -> Export { ExportTable { - from: self.table.clone(), + vm_table: VMExportTable { + from: self.table.clone(), + }, } .into() } diff --git a/lib/api/src/import_object.rs b/lib/api/src/import_object.rs index bf499fcc953..b38f4aa8d2b 100644 --- a/lib/api/src/import_object.rs +++ b/lib/api/src/import_object.rs @@ -6,8 +6,7 @@ use std::collections::VecDeque; use std::collections::{hash_map::Entry, HashMap}; use std::fmt; use std::sync::{Arc, Mutex}; -use wasmer_engine::NamedResolver; -use wasmer_vm::Export; +use wasmer_engine::{Export, NamedResolver}; /// The `LikeNamespace` trait represents objects that act as a namespace for imports. /// For example, an `Instance` or `Namespace` could be @@ -265,7 +264,6 @@ mod test { use crate::{Global, Store, Val}; use wasmer_engine::ChainableNamedResolver; use wasmer_types::Type; - use wasmer_vm::Export; #[test] fn chaining_works() { @@ -320,7 +318,7 @@ mod test { let happy_dog_entry = resolver.resolve_by_name("dog", "happy").unwrap(); assert!(if let Export::Global(happy_dog_global) = happy_dog_entry { - happy_dog_global.from.ty().ty == Type::I64 + happy_dog_global.vm_global.from.ty().ty == Type::I64 } else { false }); @@ -346,7 +344,7 @@ mod test { let happy_dog_entry = resolver.resolve_by_name("dog", "happy").unwrap(); assert!(if let Export::Global(happy_dog_global) = happy_dog_entry { - happy_dog_global.from.ty().ty == Type::I32 + happy_dog_global.vm_global.from.ty().ty == Type::I32 } else { false }); @@ -366,7 +364,7 @@ mod test { let happy_dog_entry = imports1.resolve_by_name("dog", "happy").unwrap(); assert!(if let Export::Global(happy_dog_global) = happy_dog_entry { - happy_dog_global.from.ty().ty == Type::I32 + happy_dog_global.vm_global.from.ty().ty == Type::I32 } else { false }); diff --git a/lib/api/src/instance.rs b/lib/api/src/instance.rs index c011be3703b..db2395ffac4 100644 --- a/lib/api/src/instance.rs +++ b/lib/api/src/instance.rs @@ -119,7 +119,7 @@ impl Instance { .map(|export| { let name = export.name().to_string(); let export = handle.lookup(&name).expect("export"); - let extern_ = Extern::from_export(store, export); + let extern_ = Extern::from_export(store, export.into()); (name, extern_) }) .collect::(); diff --git a/lib/api/src/lib.rs b/lib/api/src/lib.rs index f4cfb70f903..52c20ea7fea 100644 --- a/lib/api/src/lib.rs +++ b/lib/api/src/lib.rs @@ -80,7 +80,7 @@ pub use wasmer_compiler::{ }; pub use wasmer_compiler::{CpuFeature, Features, Target}; pub use wasmer_engine::{ - ChainableNamedResolver, DeserializeError, Engine, FrameInfo, LinkError, NamedResolver, + ChainableNamedResolver, DeserializeError, Engine, Export, FrameInfo, LinkError, NamedResolver, NamedResolverChain, Resolver, RuntimeError, SerializeError, }; pub use wasmer_types::{ @@ -89,7 +89,7 @@ pub use wasmer_types::{ }; // TODO: should those be moved into wasmer::vm as well? -pub use wasmer_vm::{raise_user_trap, Export, MemoryError}; +pub use wasmer_vm::{raise_user_trap, MemoryError, VMExport}; pub mod vm { //! We use the vm module for re-exporting wasmer-vm types diff --git a/lib/api/src/native.rs b/lib/api/src/native.rs index 8e82e41e2b2..77a8179cd12 100644 --- a/lib/api/src/native.rs +++ b/lib/api/src/native.rs @@ -15,9 +15,11 @@ use crate::externals::function::{ }; use crate::{FromToNativeWasmType, Function, FunctionType, RuntimeError, Store, WasmTypeList}; use std::panic::{catch_unwind, AssertUnwindSafe}; +use wasmer_engine::ExportFunction; use wasmer_types::NativeWasmType; use wasmer_vm::{ - ExportFunction, VMDynamicFunctionContext, VMFunctionBody, VMFunctionEnvironment, VMFunctionKind, + VMDynamicFunctionContext, VMExportFunction, VMFunctionBody, VMFunctionEnvironment, + VMFunctionKind, }; /// A WebAssembly function that can be called natively @@ -57,7 +59,8 @@ where } } -impl From<&NativeFunc> for ExportFunction +/* +impl From<&NativeFunc> for VMExportFunction where Args: WasmTypeList, Rets: WasmTypeList, @@ -68,12 +71,31 @@ where address: other.address, vmctx: other.vmctx, signature, - // TODO: - function_ptr: None, kind: other.arg_kind, call_trampoline: None, } } +}*/ + +impl From<&NativeFunc> for ExportFunction +where + Args: WasmTypeList, + Rets: WasmTypeList, +{ + fn from(other: &NativeFunc) -> Self { + let signature = FunctionType::new(Args::wasm_types(), Rets::wasm_types()); + Self { + // TODO: + import_init_function_ptr: None, + vm_function: VMExportFunction { + address: other.address, + vmctx: other.vmctx, + signature, + kind: other.arg_kind, + call_trampoline: None, + }, + } + } } impl From> for Function @@ -87,13 +109,15 @@ where store: other.store, definition: other.definition, exported: ExportFunction { - address: other.address, - vmctx: other.vmctx, - signature, // TODO: - function_ptr: None, - kind: other.arg_kind, - call_trampoline: None, + import_init_function_ptr: None, + vm_function: VMExportFunction { + address: other.address, + vmctx: other.vmctx, + signature, + kind: other.arg_kind, + call_trampoline: None, + }, }, } } diff --git a/lib/api/src/types.rs b/lib/api/src/types.rs index 5839b8f7723..d986e89b713 100644 --- a/lib/api/src/types.rs +++ b/lib/api/src/types.rs @@ -73,17 +73,19 @@ impl ValFuncRef for Val { .engine() .lookup_signature(item.type_index) .expect("Signature not found in store"); - let export = wasmer_vm::ExportFunction { - address: item.func_ptr, - signature, + let export = wasmer_engine::ExportFunction { // TODO: // 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, - vmctx: item.vmctx, - call_trampoline: None, + import_init_function_ptr: None, + vm_function: wasmer_vm::VMExportFunction { + address: item.func_ptr, + signature, + // All functions in tables are already Static (as dynamic functions + // are converted to use the trampolines with static signatures). + kind: wasmer_vm::VMFunctionKind::Static, + vmctx: item.vmctx, + call_trampoline: None, + }, }; let f = Function::from_export(store, export); Self::FuncRef(f) diff --git a/lib/c-api/src/ordered_resolver.rs b/lib/c-api/src/ordered_resolver.rs index 857714f1275..d35ac43fbf2 100644 --- a/lib/c-api/src/ordered_resolver.rs +++ b/lib/c-api/src/ordered_resolver.rs @@ -1,5 +1,5 @@ //! Ordered Resolvers are a custom kind of [`Resolver`] that retrieves -//! `Export`s based on the index of the import, and not the module or name. +//! `EngineExport`s based on the index of the import, and not the module or name. //! //! This resolver is used in the Wasm-C-API as the imports are provided //! by index and not by module and name. diff --git a/lib/c-api/wasmer_wasm.h b/lib/c-api/wasmer_wasm.h index ab3600f690e..3310984e901 100644 --- a/lib/c-api/wasmer_wasm.h +++ b/lib/c-api/wasmer_wasm.h @@ -29,9 +29,6 @@ # define DEPRECATED(message) __declspec(deprecated(message)) #endif -// The `jit` feature has been enabled for this build. -#define WASMER_JIT_ENABLED - // The `compiler` feature has been enabled for this build. #define WASMER_COMPILER_ENABLED diff --git a/lib/deprecated/runtime-core/src/export.rs b/lib/deprecated/runtime-core/src/export.rs index 359822b8d55..ddf6d8da6d1 100644 --- a/lib/deprecated/runtime-core/src/export.rs +++ b/lib/deprecated/runtime-core/src/export.rs @@ -1,4 +1,4 @@ pub use crate::new::{ + wasmer::Export as RuntimeExport, wasmer::{Exportable, Extern as Export}, - wasmer_vm::Export as RuntimeExport, }; diff --git a/lib/deprecated/runtime-core/src/global.rs b/lib/deprecated/runtime-core/src/global.rs index bf2aa45fc70..0f66a9918e1 100644 --- a/lib/deprecated/runtime-core/src/global.rs +++ b/lib/deprecated/runtime-core/src/global.rs @@ -87,7 +87,7 @@ impl From<&new::wasmer::Global> for Global { } impl<'a> new::wasmer::Exportable<'a> for Global { - fn to_export(&self) -> new::wasmer_vm::Export { + fn to_export(&self) -> new::wasmer::Export { self.new_global.to_export() } diff --git a/lib/deprecated/runtime-core/src/import.rs b/lib/deprecated/runtime-core/src/import.rs index b9af6e6c542..1c2dc88782d 100644 --- a/lib/deprecated/runtime-core/src/import.rs +++ b/lib/deprecated/runtime-core/src/import.rs @@ -30,11 +30,11 @@ impl Namespace { } impl LikeNamespace for Namespace { - fn get_namespace_export(&self, name: &str) -> Option { + fn get_namespace_export(&self, name: &str) -> Option { self.exports.new_exports.get_namespace_export(name) } - fn get_namespace_exports(&self) -> Vec<(String, new::wasmer_vm::Export)> { + fn get_namespace_exports(&self) -> Vec<(String, new::wasmer::Export)> { self.exports.new_exports.get_namespace_exports() } } diff --git a/lib/deprecated/runtime-core/src/instance.rs b/lib/deprecated/runtime-core/src/instance.rs index 8bc6bd4f567..7cf42b30936 100644 --- a/lib/deprecated/runtime-core/src/instance.rs +++ b/lib/deprecated/runtime-core/src/instance.rs @@ -222,11 +222,11 @@ impl Instance { } impl LikeNamespace for Instance { - fn get_namespace_export(&self, name: &str) -> Option { + fn get_namespace_export(&self, name: &str) -> Option { self.exports.new_exports.get_namespace_export(name) } - fn get_namespace_exports(&self) -> Vec<(String, new::wasmer_vm::Export)> { + fn get_namespace_exports(&self) -> Vec<(String, new::wasmer::Export)> { self.exports.new_exports.get_namespace_exports() } } @@ -256,11 +256,11 @@ impl Exports { } impl LikeNamespace for Exports { - fn get_namespace_export(&self, name: &str) -> Option { + fn get_namespace_export(&self, name: &str) -> Option { self.new_exports.get_namespace_export(name) } - fn get_namespace_exports(&self) -> Vec<(String, new::wasmer_vm::Export)> { + fn get_namespace_exports(&self) -> Vec<(String, new::wasmer::Export)> { self.new_exports.get_namespace_exports() } } diff --git a/lib/deprecated/runtime-core/src/memory.rs b/lib/deprecated/runtime-core/src/memory.rs index 5f0937da712..69074f400f1 100644 --- a/lib/deprecated/runtime-core/src/memory.rs +++ b/lib/deprecated/runtime-core/src/memory.rs @@ -9,8 +9,8 @@ pub mod ptr { pub use crate::new::wasmer::{Array, Item, WasmPtr}; } -pub use new::wasmer_types::MemoryType as MemoryDescriptor; pub use new::wasmer::{Atomically, MemoryView}; +pub use new::wasmer_types::MemoryType as MemoryDescriptor; pub use new::wasmer_vm::MemoryStyle as MemoryType; /// A Wasm linear memory. @@ -108,7 +108,7 @@ impl From<&new::wasmer::Memory> for Memory { } impl<'a> new::wasmer::Exportable<'a> for Memory { - fn to_export(&self) -> new::wasmer_vm::Export { + fn to_export(&self) -> new::wasmer::Export { self.new_memory.to_export() } diff --git a/lib/deprecated/runtime-core/src/module.rs b/lib/deprecated/runtime-core/src/module.rs index 7ef483dcb85..c7600d7224f 100644 --- a/lib/deprecated/runtime-core/src/module.rs +++ b/lib/deprecated/runtime-core/src/module.rs @@ -8,7 +8,7 @@ use crate::{ types::{FuncSig, Value}, vm, }; -use new::wasmer_vm::Export; +use new::wasmer::Export; use std::{ cell::RefCell, collections::HashMap, @@ -98,18 +98,20 @@ impl Module { // `function` is a static host function // constructed with // `new::wasmer::Function::new_env`. - if !function.address.is_null() { + if !function.vm_function.address.is_null() { // Properly drop the empty `vm::Ctx` // created by the host function. unsafe { - ptr::drop_in_place::(function.vmctx.host_env as _); + ptr::drop_in_place::( + function.vm_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.host_env = pre_instance.vmctx_ptr() as _; + function.vm_function.vmctx.host_env = pre_instance.vmctx_ptr() as _; } // `function` is a dynamic host function // constructed with @@ -147,13 +149,15 @@ impl Module { new::wasmer_vm::VMDynamicFunctionContext< VMDynamicFunctionWithEnv, >, - > = unsafe { Box::from_raw(function.vmctx.host_env as *mut _) }; + > = unsafe { + Box::from_raw(function.vm_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.host_env = Box::into_raw(vmctx) as _; + function.vm_function.vmctx.host_env = Box::into_raw(vmctx) as _; } } diff --git a/lib/deprecated/runtime-core/src/table.rs b/lib/deprecated/runtime-core/src/table.rs index ece88e5fb07..9b2c60d1d34 100644 --- a/lib/deprecated/runtime-core/src/table.rs +++ b/lib/deprecated/runtime-core/src/table.rs @@ -70,7 +70,7 @@ impl From<&new::wasmer::Table> for Table { } impl<'a> new::wasmer::Exportable<'a> for Table { - fn to_export(&self) -> new::wasmer_vm::Export { + fn to_export(&self) -> new::wasmer::Export { self.new_table.to_export() } diff --git a/lib/deprecated/runtime-core/src/typed_func.rs b/lib/deprecated/runtime-core/src/typed_func.rs index c44996cb310..0bfe47d61a5 100644 --- a/lib/deprecated/runtime-core/src/typed_func.rs +++ b/lib/deprecated/runtime-core/src/typed_func.rs @@ -201,7 +201,7 @@ where Args: WasmTypeList, Rets: WasmTypeList, { - fn to_export(&self) -> new::wasmer_vm::Export { + fn to_export(&self) -> new::wasmer::Export { self.new_function.to_export() } @@ -312,7 +312,7 @@ impl From<&new::wasmer::Function> for DynamicFunc { } impl<'a> new::wasmer::Exportable<'a> for DynamicFunc { - fn to_export(&self) -> new::wasmer_vm::Export { + fn to_export(&self) -> new::wasmer::Export { self.new_function.to_export() } diff --git a/lib/engine/src/export.rs b/lib/engine/src/export.rs new file mode 100644 index 00000000000..8f972c3f79f --- /dev/null +++ b/lib/engine/src/export.rs @@ -0,0 +1,103 @@ +use wasmer_vm::{ + ImportInitializerFuncPtr, VMExport, VMExportFunction, VMExportGlobal, VMExportMemory, + VMExportTable, +}; + +/// The value of an export passed from one instance to another. +#[derive(Debug, Clone)] +pub enum Export { + /// A function export value. + Function(ExportFunction), + + /// A table export value. + Table(ExportTable), + + /// A memory export value. + Memory(ExportMemory), + + /// A global export value. + Global(ExportGlobal), +} + +impl From for VMExport { + fn from(other: Export) -> Self { + match other { + Export::Function(ExportFunction { vm_function, .. }) => VMExport::Function(vm_function), + Export::Memory(ExportMemory { vm_memory }) => VMExport::Memory(vm_memory), + Export::Table(ExportTable { vm_table }) => VMExport::Table(vm_table), + Export::Global(ExportGlobal { vm_global }) => VMExport::Global(vm_global), + } + } +} + +impl From for Export { + fn from(other: VMExport) -> Self { + match other { + VMExport::Function(vm_function) => Export::Function(ExportFunction { + vm_function, + import_init_function_ptr: None, + }), + VMExport::Memory(vm_memory) => Export::Memory(ExportMemory { vm_memory }), + VMExport::Table(vm_table) => Export::Table(ExportTable { vm_table }), + VMExport::Global(vm_global) => Export::Global(ExportGlobal { vm_global }), + } + } +} + +/// A function export value with an extra function pointer to initialize +/// host environments. +#[derive(Debug, Clone, PartialEq)] +pub struct ExportFunction { + /// The VM function, containing most of the data. + pub vm_function: VMExportFunction, + /// Function pointer to `WasmerEnv::init_with_instance(&mut self, instance: &Instance)`. + /// + /// This function is called to finish setting up the environment after + /// we create the `api::Instance`. + pub import_init_function_ptr: Option, +} + +impl From for Export { + fn from(func: ExportFunction) -> Self { + Self::Function(func) + } +} + +/// A table export value. +#[derive(Debug, Clone)] +pub struct ExportTable { + /// The VM table, containing info about the table. + pub vm_table: VMExportTable, +} + +impl From for Export { + fn from(table: ExportTable) -> Self { + Self::Table(table) + } +} + +/// A memory export value. +#[derive(Debug, Clone)] +pub struct ExportMemory { + /// The VM memory, containing info about the table. + pub vm_memory: VMExportMemory, +} + +impl From for Export { + fn from(memory: ExportMemory) -> Self { + Self::Memory(memory) + } +} + +/// A global export value. +#[derive(Debug, Clone)] +pub struct ExportGlobal { + /// The VM global, containing info about the global. + pub vm_global: VMExportGlobal, +} + +impl From for Export { + fn from(global: ExportGlobal) -> Self { + Self::Global(global) + } +} diff --git a/lib/engine/src/lib.rs b/lib/engine/src/lib.rs index 66327e3a227..557b1b039d2 100644 --- a/lib/engine/src/lib.rs +++ b/lib/engine/src/lib.rs @@ -23,6 +23,7 @@ mod artifact; mod engine; mod error; +mod export; mod resolver; mod serialize; mod trap; @@ -33,6 +34,7 @@ pub use crate::engine::{Engine, EngineId}; pub use crate::error::{ DeserializeError, ImportError, InstantiationError, LinkError, SerializeError, }; +pub use crate::export::{Export, ExportFunction, ExportGlobal, ExportMemory, ExportTable}; pub use crate::resolver::{ resolve_imports, ChainableNamedResolver, NamedResolver, NamedResolverChain, NullResolver, Resolver, diff --git a/lib/engine/src/resolver.rs b/lib/engine/src/resolver.rs index 59de18d5f0d..7a0105e14e5 100644 --- a/lib/engine/src/resolver.rs +++ b/lib/engine/src/resolver.rs @@ -1,13 +1,13 @@ //! Define the `Resolver` trait, allowing custom resolution for external //! references. -use crate::{ImportError, LinkError}; +use crate::{Export, ImportError, LinkError}; use more_asserts::assert_ge; use wasmer_types::entity::{BoxedSlice, EntityRef, PrimaryMap}; use wasmer_types::{ExternType, FunctionIndex, ImportIndex, MemoryIndex, TableIndex}; use wasmer_vm::{ - Export, FunctionBodyPtr, Imports, MemoryStyle, ModuleInfo, TableStyle, VMFunctionBody, + FunctionBodyPtr, Imports, MemoryStyle, ModuleInfo, TableStyle, VMFunctionBody, VMFunctionImport, VMFunctionKind, VMGlobalImport, VMMemoryImport, VMTableImport, }; @@ -103,11 +103,11 @@ fn get_extern_from_import(module: &ModuleInfo, import_index: &ImportIndex) -> Ex /// Get an `ExternType` given an export (and Engine signatures in case is a function). fn get_extern_from_export(_module: &ModuleInfo, export: &Export) -> ExternType { match export { - Export::Function(ref f) => ExternType::Function(f.signature.clone()), - Export::Table(ref t) => ExternType::Table(*t.ty()), - Export::Memory(ref m) => ExternType::Memory(*m.ty()), + Export::Function(ref f) => ExternType::Function(f.vm_function.signature.clone()), + Export::Table(ref t) => ExternType::Table(*t.vm_table.ty()), + Export::Memory(ref m) => ExternType::Memory(*m.vm_memory.ty()), Export::Global(ref g) => { - let global = g.from.ty(); + let global = g.vm_global.from.ty(); ExternType::Global(*global) } } @@ -154,7 +154,7 @@ pub fn resolve_imports( } match resolved { Export::Function(ref f) => { - let address = match f.kind { + let address = match f.vm_function.kind { VMFunctionKind::Dynamic => { // If this is a dynamic imported function, // the address of the function is the address of the @@ -165,19 +165,19 @@ pub fn resolve_imports( // TODO: We should check that the f.vmctx actually matches // the shape of `VMDynamicFunctionImportContext` } - VMFunctionKind::Static => f.address, + VMFunctionKind::Static => f.vm_function.address, }; function_imports.push(VMFunctionImport { body: address, - environment: f.vmctx, + environment: f.vm_function.vmctx, }); - host_function_env_initializers.push(f.function_ptr); + host_function_env_initializers.push(f.import_init_function_ptr); } Export::Table(ref t) => { table_imports.push(VMTableImport { - definition: t.from.vmtable(), - from: t.from.clone(), + definition: t.vm_table.from.vmtable(), + from: t.vm_table.from.clone(), }); } Export::Memory(ref m) => { @@ -185,7 +185,7 @@ pub fn resolve_imports( ImportIndex::Memory(index) => { // Sanity-check: Ensure that the imported memory has at least // guard-page protections the importing module expects it to have. - let export_memory_style = m.style(); + let export_memory_style = m.vm_memory.style(); let import_memory_style = &memory_styles[*index]; if let ( MemoryStyle::Static { bound, .. }, @@ -210,15 +210,15 @@ pub fn resolve_imports( } memory_imports.push(VMMemoryImport { - definition: m.from.vmmemory(), - from: m.from.clone(), + definition: m.vm_memory.from.vmmemory(), + from: m.vm_memory.from.clone(), }); } Export::Global(ref g) => { global_imports.push(VMGlobalImport { - definition: g.from.vmglobal(), - from: g.from.clone(), + definition: g.vm_global.from.vmglobal(), + from: g.vm_global.from.clone(), }); } } diff --git a/lib/vm/src/export.rs b/lib/vm/src/export.rs index eb8956733ee..083210093bc 100644 --- a/lib/vm/src/export.rs +++ b/lib/vm/src/export.rs @@ -2,7 +2,6 @@ // Attributions: https://github.com/wasmerio/wasmer/blob/master/ATTRIBUTIONS.md use crate::global::Global; -use crate::instance::ImportInitializerFuncPtr; use crate::memory::{Memory, MemoryStyle}; use crate::table::{Table, TableStyle}; use crate::vmcontext::{VMFunctionBody, VMFunctionEnvironment, VMFunctionKind, VMTrampoline}; @@ -11,34 +10,27 @@ use wasmer_types::{FunctionType, MemoryType, TableType}; /// The value of an export passed from one instance to another. #[derive(Debug, Clone)] -pub enum Export { +pub enum VMExport { /// A function export value. - Function(ExportFunction), + Function(VMExportFunction), /// A table export value. - Table(ExportTable), + Table(VMExportTable), /// A memory export value. - Memory(ExportMemory), + Memory(VMExportMemory), /// A global export value. - Global(ExportGlobal), + Global(VMExportGlobal), } /// A function export value. #[derive(Debug, Clone, PartialEq)] -pub struct ExportFunction { +pub struct VMExportFunction { /// The address of the native-code function. pub address: *const VMFunctionBody, /// Pointer to the containing `VMContext`. pub vmctx: VMFunctionEnvironment, - /// Function pointer to `WasmerEnv::init_with_instance(&mut self, instance: &Instance)`. - /// - /// This function is called to finish setting up the environment after - /// we create the `api::Instance`. - // META: if you have a better idea of how to get this function to where it - // needs to be, please let me know. - pub function_ptr: Option, /// The function type, used for compatibility checking. pub signature: FunctionType, /// The function kind (specifies the calling convention for the function). @@ -51,20 +43,20 @@ pub struct ExportFunction { /// # Safety /// There is no non-threadsafe logic directly in this type. Calling the function /// may not be threadsafe. -unsafe impl Send for ExportFunction {} +unsafe impl Send for VMExportFunction {} /// # Safety -/// The members of an ExportFunction are immutable after construction. -unsafe impl Sync for ExportFunction {} +/// The members of an VMExportFunction are immutable after construction. +unsafe impl Sync for VMExportFunction {} -impl From for Export { - fn from(func: ExportFunction) -> Self { +impl From for VMExport { + fn from(func: VMExportFunction) -> Self { Self::Function(func) } } /// A table export value. #[derive(Debug, Clone)] -pub struct ExportTable { +pub struct VMExportTable { /// Pointer to the containing `Table`. pub from: Arc, } @@ -73,14 +65,14 @@ pub struct ExportTable { /// This is correct because there is no non-threadsafe logic directly in this type; /// correct use of the raw table from multiple threads via `definition` requires `unsafe` /// and is the responsibilty of the user of this type. -unsafe impl Send for ExportTable {} +unsafe impl Send for VMExportTable {} /// # Safety /// This is correct because the values directly in `definition` should be considered immutable /// and the type is both `Send` and `Clone` (thus marking it `Sync` adds no new behavior, it /// only makes this type easier to use) -unsafe impl Sync for ExportTable {} +unsafe impl Sync for VMExportTable {} -impl ExportTable { +impl VMExportTable { /// Get the table type for this exported table pub fn ty(&self) -> &TableType { self.from.ty() @@ -91,21 +83,21 @@ impl ExportTable { self.from.style() } - /// Returns whether or not the two `ExportTable`s refer to the same Memory. + /// Returns whether or not the two `VMExportTable`s refer to the same Memory. pub fn same(&self, other: &Self) -> bool { Arc::ptr_eq(&self.from, &other.from) } } -impl From for Export { - fn from(table: ExportTable) -> Self { +impl From for VMExport { + fn from(table: VMExportTable) -> Self { Self::Table(table) } } /// A memory export value. #[derive(Debug, Clone)] -pub struct ExportMemory { +pub struct VMExportMemory { /// Pointer to the containing `Memory`. pub from: Arc, } @@ -114,14 +106,14 @@ pub struct ExportMemory { /// This is correct because there is no non-threadsafe logic directly in this type; /// correct use of the raw memory from multiple threads via `definition` requires `unsafe` /// and is the responsibilty of the user of this type. -unsafe impl Send for ExportMemory {} +unsafe impl Send for VMExportMemory {} /// # Safety /// This is correct because the values directly in `definition` should be considered immutable /// and the type is both `Send` and `Clone` (thus marking it `Sync` adds no new behavior, it /// only makes this type easier to use) -unsafe impl Sync for ExportMemory {} +unsafe impl Sync for VMExportMemory {} -impl ExportMemory { +impl VMExportMemory { /// Get the type for this exported memory pub fn ty(&self) -> &MemoryType { self.from.ty() @@ -132,21 +124,21 @@ impl ExportMemory { self.from.style() } - /// Returns whether or not the two `ExportMemory`s refer to the same Memory. + /// Returns whether or not the two `VMExportMemory`s refer to the same Memory. pub fn same(&self, other: &Self) -> bool { Arc::ptr_eq(&self.from, &other.from) } } -impl From for Export { - fn from(memory: ExportMemory) -> Self { +impl From for VMExport { + fn from(memory: VMExportMemory) -> Self { Self::Memory(memory) } } /// A global export value. #[derive(Debug, Clone)] -pub struct ExportGlobal { +pub struct VMExportGlobal { /// The global declaration, used for compatibility checking. pub from: Arc, } @@ -155,22 +147,22 @@ pub struct ExportGlobal { /// This is correct because there is no non-threadsafe logic directly in this type; /// correct use of the raw global from multiple threads via `definition` requires `unsafe` /// and is the responsibilty of the user of this type. -unsafe impl Send for ExportGlobal {} +unsafe impl Send for VMExportGlobal {} /// # Safety /// This is correct because the values directly in `definition` should be considered immutable /// from the perspective of users of this type and the type is both `Send` and `Clone` (thus /// marking it `Sync` adds no new behavior, it only makes this type easier to use) -unsafe impl Sync for ExportGlobal {} +unsafe impl Sync for VMExportGlobal {} -impl ExportGlobal { - /// Returns whether or not the two `ExportGlobal`s refer to the same Global. +impl VMExportGlobal { + /// Returns whether or not the two `VMExportGlobal`s refer to the same Global. pub fn same(&self, other: &Self) -> bool { Arc::ptr_eq(&self.from, &other.from) } } -impl From for Export { - fn from(global: ExportGlobal) -> Self { +impl From for VMExport { + fn from(global: VMExportGlobal) -> Self { Self::Global(global) } } diff --git a/lib/vm/src/instance.rs b/lib/vm/src/instance.rs index 0b350617e4b..20225624544 100644 --- a/lib/vm/src/instance.rs +++ b/lib/vm/src/instance.rs @@ -4,7 +4,7 @@ //! An `Instance` contains all the runtime state used by execution of a //! wasm module (except its callstack and register state). An //! `InstanceHandle` is a reference-counting handle for an `Instance`. -use crate::export::Export; +use crate::export::VMExport; use crate::global::Global; use crate::imports::Imports; use crate::memory::{Memory, MemoryError}; @@ -16,8 +16,8 @@ use crate::vmcontext::{ VMMemoryDefinition, VMMemoryImport, VMSharedSignatureIndex, VMTableDefinition, VMTableImport, VMTrampoline, }; -use crate::{ExportFunction, ExportGlobal, ExportMemory, ExportTable}; use crate::{FunctionBodyPtr, ModuleInfo, VMOffsets}; +use crate::{VMExportFunction, VMExportGlobal, VMExportMemory, VMExportTable}; use memoffset::offset_of; use more_asserts::assert_lt; use std::alloc::{self, Layout}; @@ -65,7 +65,7 @@ cfg_if::cfg_if! { /// The function pointer to call with data and an [`Instance`] pointer to /// finish initializing the host env. -pub(crate) type ImportInitializerFuncPtr = +pub type ImportInitializerFuncPtr = fn(*mut std::ffi::c_void, *const std::ffi::c_void) -> Result<(), *mut std::ffi::c_void>; /// This type holds thunks (delayed computations) for initializing the imported @@ -308,18 +308,19 @@ impl Instance { } /// Lookup an export with the given name. - pub fn lookup(&self, field: &str) -> Option { + pub fn lookup(&self, field: &str) -> Option { let export = self.module.exports.get(field)?; Some(self.lookup_by_declaration(&export)) } /// Lookup an export with the given export declaration. - pub fn lookup_by_declaration(&self, export: &ExportIndex) -> Export { + // TODO: maybe EngineExport + pub fn lookup_by_declaration(&self, export: &ExportIndex) -> VMExport { match export { ExportIndex::Function(index) => { let sig_index = &self.module.functions[*index]; - let (address, vmctx, function_ptr) = + let (address, vmctx, _function_ptr) = if let Some(def_index) = self.module.local_func_index(*index) { ( self.functions[def_index].0 as *const _, @@ -335,7 +336,10 @@ impl Instance { }; let call_trampoline = Some(self.function_call_trampolines[*sig_index]); let signature = self.module.signatures[*sig_index].clone(); - ExportFunction { + /*EngineExportFunction { + function_ptr, + function: */ + VMExportFunction { address, // Any function received is already static at this point as: // 1. All locally defined functions in the Wasm have a static signature. @@ -344,9 +348,9 @@ impl Instance { kind: VMFunctionKind::Static, signature, vmctx, - function_ptr, call_trampoline, } + //} .into() } ExportIndex::Table(index) => { @@ -356,7 +360,7 @@ impl Instance { let import = self.imported_table(*index); import.from.clone() }; - ExportTable { from }.into() + VMExportTable { from }.into() } ExportIndex::Memory(index) => { let from = if let Some(def_index) = self.module.local_memory_index(*index) { @@ -365,7 +369,7 @@ impl Instance { let import = self.imported_memory(*index); import.from.clone() }; - ExportMemory { from }.into() + VMExportMemory { from }.into() } ExportIndex::Global(index) => { let from = { @@ -376,7 +380,7 @@ impl Instance { import.from.clone() } }; - ExportGlobal { from }.into() + VMExportGlobal { from }.into() } } } @@ -1068,12 +1072,12 @@ impl InstanceHandle { } /// Lookup an export with the given name. - pub fn lookup(&self, field: &str) -> Option { + pub fn lookup(&self, field: &str) -> Option { self.instance().lookup(field) } /// Lookup an export with the given export declaration. - pub fn lookup_by_declaration(&self, export: &ExportIndex) -> Export { + pub fn lookup_by_declaration(&self, export: &ExportIndex) -> VMExport { self.instance().lookup_by_declaration(export) } diff --git a/lib/vm/src/lib.rs b/lib/vm/src/lib.rs index 6a1af9f9afa..ecbadeca794 100644 --- a/lib/vm/src/lib.rs +++ b/lib/vm/src/lib.rs @@ -39,7 +39,7 @@ pub mod libcalls; pub use crate::export::*; pub use crate::global::*; pub use crate::imports::Imports; -pub use crate::instance::InstanceHandle; +pub use crate::instance::{ImportInitializerFuncPtr, InstanceHandle}; pub use crate::memory::{LinearMemory, Memory, MemoryError, MemoryStyle}; pub use crate::mmap::Mmap; pub use crate::module::{ExportsIterator, ImportsIterator, ModuleInfo};