From 447de2139019b18da974e13b4eff642652ee5f64 Mon Sep 17 00:00:00 2001 From: Nick Lewycky Date: Fri, 2 Oct 2020 14:58:20 -0700 Subject: [PATCH 01/18] A mess of changes. --- lib/api/src/externals/function.rs | 20 +++++++++++++++----- lib/cli/Cargo.toml | 14 +++++++------- lib/compiler-llvm/src/translator/abi.rs | 5 +---- lib/engine-native/src/artifact.rs | 21 ++++++++++++++------- lib/engine-native/src/engine.rs | 8 ++++---- lib/engine/src/artifact.rs | 7 ++++++- lib/engine/src/engine.rs | 2 ++ 7 files changed, 49 insertions(+), 28 deletions(-) diff --git a/lib/api/src/externals/function.rs b/lib/api/src/externals/function.rs index fcf00b74342..4b6b39e5bf0 100644 --- a/lib/api/src/externals/function.rs +++ b/lib/api/src/externals/function.rs @@ -10,16 +10,26 @@ use std::cell::RefCell; 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, + raise_user_trap, + resume_panic, + wasmer_call_trampoline, + Export, + ExportFunction, + VMCallerCheckedAnyfunc, + VMContext, + VMDynamicFunctionContext, + VMFunctionBody, + VMFunctionKind, + VMSharedSignatureIndex, //VMTrampoline, }; /// A function defined in the Wasm module #[derive(Clone, PartialEq)] pub struct WasmFunctionDefinition { // The trampoline to do the call - pub(crate) trampoline: VMTrampoline, + //pub(crate) trampoline: VMTrampoline, + // The signature to look up the trampoline to make the call + pub(crate) signature: VMSharedSignatureIndex, } /// A function defined in the Host @@ -351,7 +361,7 @@ impl Function { } pub(crate) fn from_export(store: &Store, wasmer_export: ExportFunction) -> Self { - let vmsignature = store.engine().register_signature(&wasmer_export.signature); + let vmsignature = store.engine().register(&wasmer_export.signature); let trampoline = store .engine() .function_call_trampoline(vmsignature) diff --git a/lib/cli/Cargo.toml b/lib/cli/Cargo.toml index 1d11775273c..76c23b74172 100644 --- a/lib/cli/Cargo.toml +++ b/lib/cli/Cargo.toml @@ -24,7 +24,7 @@ wasmer-compiler-singlepass = { version = "1.0.0-alpha3", path = "../compiler-sin wasmer-compiler-llvm = { version = "1.0.0-alpha3", path = "../compiler-llvm", optional = true } wasmer-emscripten = { version = "1.0.0-alpha3", path = "../emscripten", optional = true } wasmer-engine = { version = "1.0.0-alpha3", path = "../engine" } -wasmer-engine-jit = { version = "1.0.0-alpha3", path = "../engine-jit", optional = true } +#wasmer-engine-jit = { version = "1.0.0-alpha3", path = "../engine-jit", optional = true } wasmer-engine-native = { version = "1.0.0-alpha3", path = "../engine-native", optional = true } wasmer-wasi = { version = "1.0.0-alpha3", path = "../wasi", optional = true } wasmer-wasi-experimental-io-devices = { version = "1.0.0-alpha3", path = "../wasi-experimental-io-devices", optional = true } @@ -50,17 +50,17 @@ log = { version = "0.4", optional = true } default = [ "wat", "wast", - "jit", +# "jit", "native", "cache", "wasi", "emscripten", ] engine = [] -jit = [ - "wasmer-engine-jit", - "engine", -] +#jit = [ +# "wasmer-engine-jit", +# "engine", +#] native = [ "wasmer-engine-native", "engine", @@ -72,7 +72,7 @@ emscripten = ["wasmer-emscripten"] wat = ["wasmer/wat"] compiler = [ "wasmer-compiler/translator", - "wasmer-engine-jit/compiler", +# "wasmer-engine-jit/compiler", "wasmer-engine-native/compiler" ] experimental-io-devices = [ diff --git a/lib/compiler-llvm/src/translator/abi.rs b/lib/compiler-llvm/src/translator/abi.rs index 66343c75889..6e26a026b68 100644 --- a/lib/compiler-llvm/src/translator/abi.rs +++ b/lib/compiler-llvm/src/translator/abi.rs @@ -5,10 +5,7 @@ // // So far, this is an implementation of the SysV AMD64 ABI. -#![deny( - dead_code, - missing_docs, -)] +#![deny(dead_code, missing_docs)] use crate::translator::intrinsics::{type_to_llvm, Intrinsics}; use inkwell::{ diff --git a/lib/engine-native/src/artifact.rs b/lib/engine-native/src/artifact.rs index cefde4d8d6a..37861673939 100644 --- a/lib/engine-native/src/artifact.rs +++ b/lib/engine-native/src/artifact.rs @@ -4,6 +4,7 @@ use crate::engine::{NativeEngine, NativeEngineInner}; use crate::serialize::ModuleMetadata; use libloading::{Library, Symbol as LibrarySymbol}; +use std::collections::HashMap; use std::error::Error; use std::fs::File; use std::io::{Read, Write}; @@ -30,12 +31,12 @@ use wasmer_types::entity::{BoxedSlice, PrimaryMap}; #[cfg(feature = "compiler")] use wasmer_types::DataInitializer; use wasmer_types::{ - FunctionIndex, LocalFunctionIndex, MemoryIndex, OwnedDataInitializer, SignatureIndex, - TableIndex, + FunctionIndex, FunctionType, LocalFunctionIndex, MemoryIndex, OwnedDataInitializer, + SignatureIndex, TableIndex, }; use wasmer_vm::{ - FunctionBodyPtr, MemoryStyle, ModuleInfo, TableStyle, VMFunctionBody, VMSharedSignatureIndex, - VMTrampoline, + FunctionBodyPtr, MemoryStyle, ModuleInfo, SignatureRegistry, TableStyle, VMFunctionBody, + VMSharedSignatureIndex, VMTrampoline, }; /// A compiled wasm module, ready to be instantiated. @@ -47,6 +48,8 @@ pub struct NativeArtifact { finished_functions: BoxedSlice, finished_dynamic_function_trampolines: BoxedSlice, signatures: BoxedSlice, + /// Pointers to trampoline functions used to enter particular signatures. + trampolines: HashMap, } fn to_compile_error(err: impl Error) -> CompileError { @@ -335,6 +338,7 @@ impl NativeArtifact { finished_dynamic_function_trampolines: finished_dynamic_function_trampolines .into_boxed_slice(), signatures: signatures.into_boxed_slice(), + trampolines: HashMap::new(), }) } @@ -367,6 +371,8 @@ impl NativeArtifact { } } + let mut trampolines = HashMap::new(); + // Retrieve function call trampolines (for all signatures in the module) for (sig_index, func_type) in metadata.compile_info.module.signatures.iter() { let function_name = metadata.symbol_to_name(Symbol::FunctionCallTrampoline(sig_index)); @@ -374,7 +380,8 @@ impl NativeArtifact { let trampoline: LibrarySymbol = lib .get(function_name.as_bytes()) .map_err(to_compile_error)?; - engine_inner.add_trampoline(&func_type, *trampoline); + let index = engine_inner.signatures().register(&func_type); + trampolines.insert(index, *trampoline); } } @@ -418,13 +425,12 @@ impl NativeArtifact { // Compute indices into the shared signature table. let signatures = { - let signature_registry = engine_inner.signatures(); metadata .compile_info .module .signatures .values() - .map(|sig| signature_registry.register(sig)) + .map(|sig| engine_inner.signatures().register(sig)) .collect::>() }; @@ -436,6 +442,7 @@ impl NativeArtifact { finished_dynamic_function_trampolines: finished_dynamic_function_trampolines .into_boxed_slice(), signatures: signatures.into_boxed_slice(), + trampolines, }) } diff --git a/lib/engine-native/src/engine.rs b/lib/engine-native/src/engine.rs index 5e19af64e18..0d63a262f62 100644 --- a/lib/engine-native/src/engine.rs +++ b/lib/engine-native/src/engine.rs @@ -49,7 +49,6 @@ impl NativeEngine { Self { inner: Arc::new(Mutex::new(NativeEngineInner { compiler: Some(compiler), - trampolines: HashMap::new(), signatures: SignatureRegistry::new(), prefixer: None, features, @@ -81,7 +80,6 @@ impl NativeEngine { compiler: None, #[cfg(feature = "compiler")] features: Features::default(), - trampolines: HashMap::new(), signatures: SignatureRegistry::new(), prefixer: None, is_cross_compiling: false, @@ -125,6 +123,7 @@ impl Engine for NativeEngine { &self.target } + /* /// Register a signature fn register_signature(&self, func_type: &FunctionType) -> VMSharedSignatureIndex { let compiler = self.inner(); @@ -141,6 +140,7 @@ impl Engine for NativeEngine { fn function_call_trampoline(&self, sig: VMSharedSignatureIndex) -> Option { self.inner().trampoline(sig) } + */ /// Validates a WebAssembly module fn validate(&self, binary: &[u8]) -> Result<(), CompileError> { @@ -222,8 +222,6 @@ pub struct NativeEngineInner { /// The WebAssembly features to use #[cfg(feature = "compiler")] features: Features, - /// Pointers to trampoline functions used to enter particular signatures - trampolines: HashMap, /// The signature registry is used mainly to operate with trampolines /// performantly. signatures: SignatureRegistry, @@ -283,6 +281,7 @@ impl NativeEngineInner { &self.signatures } + /* /// Gets the trampoline pre-registered for a particular signature pub fn trampoline(&self, sig: VMSharedSignatureIndex) -> Option { self.trampolines.get(&sig).cloned() @@ -295,6 +294,7 @@ impl NativeEngineInner { // where they belong become unallocated. self.trampolines.insert(index, trampoline); } + */ pub(crate) fn is_cross_compiling(&self) -> bool { self.is_cross_compiling diff --git a/lib/engine/src/artifact.rs b/lib/engine/src/artifact.rs index eedf406e5a0..85e427d0d58 100644 --- a/lib/engine/src/artifact.rs +++ b/lib/engine/src/artifact.rs @@ -54,8 +54,13 @@ pub trait Artifact: Send + Sync { /// ready to be run. fn finished_functions(&self) -> &BoxedSlice; + /// Returns the static typed function trampolines allocated in memory of + /// this `Artifact`, ready to be run. + fn finished_function_trampolines(&self) + -> &BoxedSlice; + /// Returns the dynamic function trampolines allocated in memory - /// for this `Artifact`, ready to be run. + /// of this `Artifact`, ready to be run. fn finished_dynamic_function_trampolines(&self) -> &BoxedSlice; /// Returns the associated VM signatures for this `Artifact`. diff --git a/lib/engine/src/engine.rs b/lib/engine/src/engine.rs index 17978b351f2..200c2064357 100644 --- a/lib/engine/src/engine.rs +++ b/lib/engine/src/engine.rs @@ -19,6 +19,7 @@ pub trait Engine { /// Gets the target fn target(&self) -> &Target; + /* /// Register a signature fn register_signature(&self, func_type: &FunctionType) -> VMSharedSignatureIndex; @@ -27,6 +28,7 @@ pub trait Engine { /// Retrieves a trampoline given a signature fn function_call_trampoline(&self, sig: VMSharedSignatureIndex) -> Option; + */ /// Validates a WebAssembly module fn validate(&self, binary: &[u8]) -> Result<(), CompileError>; From e05e0ef2b4a46727d1a6a24bfc7373d20b35fd17 Mon Sep 17 00:00:00 2001 From: Nick Lewycky Date: Wed, 7 Oct 2020 17:01:56 -0700 Subject: [PATCH 02/18] Add a 'trampoline' field to ExportFunction. Work in progress. --- lib/api/src/externals/function.rs | 22 ++++++++++------------ lib/c-api/wasmer_wasm.h | 3 +++ lib/cli/Cargo.toml | 14 +++++++------- lib/engine-native/src/artifact.rs | 20 ++++++++++++++++++++ lib/engine/src/artifact.rs | 9 +++++---- lib/vm/src/export.rs | 4 +++- lib/vm/src/instance.rs | 9 ++++++++- 7 files changed, 56 insertions(+), 25 deletions(-) diff --git a/lib/api/src/externals/function.rs b/lib/api/src/externals/function.rs index 4b6b39e5bf0..6d3cc228676 100644 --- a/lib/api/src/externals/function.rs +++ b/lib/api/src/externals/function.rs @@ -20,16 +20,15 @@ use wasmer_vm::{ VMDynamicFunctionContext, VMFunctionBody, VMFunctionKind, - VMSharedSignatureIndex, //VMTrampoline, + VMSharedSignatureIndex, + VMTrampoline, }; /// A function defined in the Wasm module #[derive(Clone, PartialEq)] pub struct WasmFunctionDefinition { - // The trampoline to do the call - //pub(crate) trampoline: VMTrampoline, - // The signature to look up the trampoline to make the call - pub(crate) signature: VMSharedSignatureIndex, + // Address of the trampoline to do the call. + pub(crate) trampoline: VMTrampoline, } /// A function defined in the Host @@ -105,6 +104,7 @@ impl Function { kind: VMFunctionKind::Dynamic, vmctx, signature: ty.clone(), + trampoline: std::ptr::null_mut(), }, } } @@ -154,6 +154,7 @@ impl Function { kind: VMFunctionKind::Dynamic, vmctx, signature: ty.clone(), + trampoline: std::ptr::null(), }, } } @@ -195,6 +196,7 @@ impl Function { vmctx, signature, kind: VMFunctionKind::Static, + trampoline: std::ptr::null(), }, } } @@ -248,6 +250,7 @@ impl Function { kind: VMFunctionKind::Static, vmctx, signature, + trampoline: std::ptr::null(), }, } } @@ -311,7 +314,7 @@ impl Function { if let Err(error) = unsafe { wasmer_call_trampoline( self.exported.vmctx, - func.trampoline, + *func.trampoline, self.exported.address, values_vec.as_mut_ptr() as *mut u8, ) @@ -361,14 +364,9 @@ impl Function { } pub(crate) fn from_export(store: &Store, wasmer_export: ExportFunction) -> Self { - let vmsignature = store.engine().register(&wasmer_export.signature); - let trampoline = store - .engine() - .function_call_trampoline(vmsignature) - .expect("Can't get call trampoline for the function"); Self { store: store.clone(), - definition: FunctionDefinition::Wasm(WasmFunctionDefinition { trampoline }), + definition: FunctionDefinition::Wasm(WasmFunctionDefinition { trampoline: wasmer_export.trampoline }), exported: wasmer_export, } } diff --git a/lib/c-api/wasmer_wasm.h b/lib/c-api/wasmer_wasm.h index 88510282739..b9eea99ae71 100644 --- a/lib/c-api/wasmer_wasm.h +++ b/lib/c-api/wasmer_wasm.h @@ -28,6 +28,9 @@ # 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/cli/Cargo.toml b/lib/cli/Cargo.toml index b9195284e6a..ee8e865ae79 100644 --- a/lib/cli/Cargo.toml +++ b/lib/cli/Cargo.toml @@ -26,7 +26,7 @@ wasmer-emscripten = { version = "1.0.0-alpha3", path = "../emscripten", optional wasmer-engine = { version = "1.0.0-alpha3", path = "../engine" } #wasmer-engine-jit = { version = "1.0.0-alpha3", path = "../engine-jit", optional = true } wasmer-engine-native = { version = "1.0.0-alpha3", path = "../engine-native", optional = true } -wasmer-engine-object-file = { version = "1.0.0-alpha3", path = "../engine-object-file", optional = true } +#wasmer-engine-object-file = { version = "1.0.0-alpha3", path = "../engine-object-file", optional = true } wasmer-vm = { version = "1.0.0-alpha3", path = "../vm" } wasmer-wasi = { version = "1.0.0-alpha3", path = "../wasi", optional = true } wasmer-wasi-experimental-io-devices = { version = "1.0.0-alpha3", path = "../wasi-experimental-io-devices", optional = true } @@ -54,7 +54,7 @@ default = [ "wast", # "jit", "native", - "object-file", +# "object-file", "cache", "wasi", "emscripten", @@ -68,10 +68,10 @@ native = [ "wasmer-engine-native", "engine", ] -object-file = [ - "wasmer-engine-object-file", - "engine", -] +#object-file = [ +# "wasmer-engine-object-file", +# "engine", +#] cache = ["wasmer-cache"] wast = ["wasmer-wast"] wasi = ["wasmer-wasi"] @@ -81,7 +81,7 @@ compiler = [ "wasmer-compiler/translator", # "wasmer-engine-jit/compiler", "wasmer-engine-native/compiler", - "wasmer-engine-object-file/compiler", +# "wasmer-engine-object-file/compiler", ] experimental-io-devices = [ "wasmer-wasi-experimental-io-devices", diff --git a/lib/engine-native/src/artifact.rs b/lib/engine-native/src/artifact.rs index 37861673939..24dff814902 100644 --- a/lib/engine-native/src/artifact.rs +++ b/lib/engine-native/src/artifact.rs @@ -46,6 +46,7 @@ pub struct NativeArtifact { #[allow(dead_code)] library: Option, finished_functions: BoxedSlice, + finished_function_call_trampolines: BoxedSlice, finished_dynamic_function_trampolines: BoxedSlice, signatures: BoxedSlice, /// Pointers to trampoline functions used to enter particular signatures. @@ -327,6 +328,8 @@ impl NativeArtifact { sharedobject_path: PathBuf, ) -> Result { let finished_functions: PrimaryMap = PrimaryMap::new(); + let finished_function_call_trampolines: PrimaryMap = + PrimaryMap::new(); let finished_dynamic_function_trampolines: PrimaryMap = PrimaryMap::new(); let signatures: PrimaryMap = PrimaryMap::new(); @@ -335,6 +338,7 @@ impl NativeArtifact { metadata, library: None, finished_functions: finished_functions.into_boxed_slice(), + finished_function_call_trampolines: finished_function_call_trampolines.into_boxed_slice(), finished_dynamic_function_trampolines: finished_dynamic_function_trampolines .into_boxed_slice(), signatures: signatures.into_boxed_slice(), @@ -385,6 +389,17 @@ impl NativeArtifact { } } + // Retrieve function call trampolines + let mut finished_function_call_trampolines: PrimaryMap = PrimaryMap::with_capacity(metadata.compile_info.module.signatures.len()); + for sig_index in metadata.compile_info.module.signatures.keys() { + let function_name = metadata.symbol_to_name(Symbol::FunctionCallTrampoline(sig_index)); + unsafe { + let trampoline: LibrarySymbol = lib.get(function_name.as_bytes()).map_err(to_compile_error)?; + let raw = *trampoline.into_raw(); + finished_function_call_trampolines.push(raw); + } + } + // Retrieve dynamic function trampolines (only for imported functions) let mut finished_dynamic_function_trampolines: PrimaryMap = PrimaryMap::with_capacity(metadata.compile_info.module.num_imported_functions); @@ -439,6 +454,7 @@ impl NativeArtifact { metadata, library: Some(lib), finished_functions: finished_functions.into_boxed_slice(), + finished_function_call_trampolines: finished_function_call_trampolines.into_boxed_slice(), finished_dynamic_function_trampolines: finished_dynamic_function_trampolines .into_boxed_slice(), signatures: signatures.into_boxed_slice(), @@ -578,6 +594,10 @@ impl Artifact for NativeArtifact { &self.finished_functions } + fn finished_function_call_trampolines(&self) -> &BoxedSlice { + &self.finished_function_call_trampolines + } + fn finished_dynamic_function_trampolines(&self) -> &BoxedSlice { &self.finished_dynamic_function_trampolines } diff --git a/lib/engine/src/artifact.rs b/lib/engine/src/artifact.rs index 9c22b08cc47..ee9e4d41530 100644 --- a/lib/engine/src/artifact.rs +++ b/lib/engine/src/artifact.rs @@ -13,6 +13,7 @@ use wasmer_types::{ }; use wasmer_vm::{ FunctionBodyPtr, InstanceHandle, MemoryStyle, ModuleInfo, TableStyle, VMSharedSignatureIndex, + VMTrampoline, }; /// An `Artifact` is the product that the `Engine` @@ -54,10 +55,9 @@ pub trait Artifact: Send + Sync + Upcastable { /// ready to be run. fn finished_functions(&self) -> &BoxedSlice; - /// Returns the static typed function trampolines allocated in memory of - /// this `Artifact`, ready to be run. - fn finished_function_trampolines(&self) - -> &BoxedSlice; + /// Returns the function call trampolines allocated in memory of this + /// `Artifact`, ready to be run. + fn finished_function_call_trampolines(&self) -> &BoxedSlice; /// Returns the dynamic function trampolines allocated in memory /// of this `Artifact`, ready to be run. @@ -121,6 +121,7 @@ pub trait Artifact: Send + Sync + Upcastable { InstanceHandle::new( module, self.finished_functions().clone(), + self.finished_function_call_trampolines().clone(), finished_memories, finished_tables, finished_globals, diff --git a/lib/vm/src/export.rs b/lib/vm/src/export.rs index 7879c92d16a..c309117e5f3 100644 --- a/lib/vm/src/export.rs +++ b/lib/vm/src/export.rs @@ -4,7 +4,7 @@ use crate::global::Global; use crate::memory::{Memory, MemoryStyle}; use crate::table::{Table, TableStyle}; -use crate::vmcontext::{VMContext, VMFunctionBody, VMFunctionKind}; +use crate::vmcontext::{VMContext, VMFunctionBody, VMFunctionKind, VMTrampoline}; use std::sync::Arc; use wasmer_types::{FunctionType, MemoryType, TableType}; @@ -35,6 +35,8 @@ pub struct ExportFunction { pub signature: FunctionType, /// The function kind (it defines how it's the signature that provided `address` have) pub kind: VMFunctionKind, + /// Address of the function call trampoline owned by the same VMContext that owns the VMFunctionBody. May be nullptr when FunctionType == Dynamic or vmctx == nullptr. + pub trampoline: VMTrampoline, } /// # Safety diff --git a/lib/vm/src/instance.rs b/lib/vm/src/instance.rs index b38e9594a18..25bcef9f1a7 100644 --- a/lib/vm/src/instance.rs +++ b/lib/vm/src/instance.rs @@ -13,7 +13,7 @@ use crate::trap::{catch_traps, init_traps, Trap, TrapCode}; use crate::vmcontext::{ VMBuiltinFunctionsArray, VMCallerCheckedAnyfunc, VMContext, VMFunctionBody, VMFunctionImport, VMFunctionKind, VMGlobalDefinition, VMGlobalImport, VMMemoryDefinition, VMMemoryImport, - VMSharedSignatureIndex, VMTableDefinition, VMTableImport, + VMSharedSignatureIndex, VMTableDefinition, VMTableImport, VMTrampoline, }; use crate::{ExportFunction, ExportGlobal, ExportMemory, ExportTable}; use crate::{FunctionBodyPtr, ModuleInfo, VMOffsets}; @@ -85,6 +85,9 @@ pub(crate) struct Instance { /// Pointers to functions in executable memory. functions: BoxedSlice, + /// Pointers to function call trampolines in executable memory. + function_call_trampolines: BoxedSlice, + /// Passive elements in this instantiation. As `elem.drop`s happen, these /// entries get removed. A missing entry is considered equivalent to an /// empty slice. @@ -298,6 +301,7 @@ impl Instance { let import = self.imported_function(*index); (import.body, import.vmctx) }; + let trampoline = self.function_call_trampolines[*sig_index]; let signature = self.module.signatures[*sig_index].clone(); ExportFunction { address, @@ -308,6 +312,7 @@ impl Instance { kind: VMFunctionKind::Static, signature, vmctx, + trampoline, } .into() } @@ -791,6 +796,7 @@ impl InstanceHandle { pub unsafe fn new( module: Arc, finished_functions: BoxedSlice, + finished_function_call_trampolines: BoxedSlice, finished_memories: BoxedSlice>, finished_tables: BoxedSlice>, finished_globals: BoxedSlice>, @@ -837,6 +843,7 @@ impl InstanceHandle { tables: finished_tables, globals: finished_globals, functions: finished_functions, + function_call_trampolines: finished_function_call_trampolines, passive_elements: Default::default(), passive_data, host_state, From 5d147f8236655cf8cb81b0796874e7ad7f607635 Mon Sep 17 00:00:00 2001 From: Nick Lewycky Date: Thu, 8 Oct 2020 15:09:37 -0700 Subject: [PATCH 03/18] WIP checkpoint. Code that builds. Use Option which should be ffi-compatible. Put the register_signature and lookup_signature functions back on the Engine. --- lib/api/src/externals/function.rs | 28 ++++++++++------------------ lib/api/src/native.rs | 2 ++ lib/api/src/types.rs | 1 + lib/engine-native/src/artifact.rs | 13 +++++++++---- lib/engine-native/src/engine.rs | 2 +- lib/engine/src/engine.rs | 2 +- lib/vm/src/export.rs | 4 ++-- lib/vm/src/instance.rs | 2 +- 8 files changed, 27 insertions(+), 27 deletions(-) diff --git a/lib/api/src/externals/function.rs b/lib/api/src/externals/function.rs index 6d3cc228676..f4df7a2702e 100644 --- a/lib/api/src/externals/function.rs +++ b/lib/api/src/externals/function.rs @@ -10,18 +10,9 @@ use std::cell::RefCell; 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, - VMSharedSignatureIndex, - VMTrampoline, + raise_user_trap, resume_panic, wasmer_call_trampoline, Export, ExportFunction, + VMCallerCheckedAnyfunc, VMContext, VMDynamicFunctionContext, VMFunctionBody, VMFunctionKind, + VMSharedSignatureIndex, VMTrampoline, }; /// A function defined in the Wasm module @@ -104,7 +95,7 @@ impl Function { kind: VMFunctionKind::Dynamic, vmctx, signature: ty.clone(), - trampoline: std::ptr::null_mut(), + trampoline: None, }, } } @@ -154,7 +145,7 @@ impl Function { kind: VMFunctionKind::Dynamic, vmctx, signature: ty.clone(), - trampoline: std::ptr::null(), + trampoline: None, }, } } @@ -196,7 +187,7 @@ impl Function { vmctx, signature, kind: VMFunctionKind::Static, - trampoline: std::ptr::null(), + trampoline: None, }, } } @@ -250,7 +241,7 @@ impl Function { kind: VMFunctionKind::Static, vmctx, signature, - trampoline: std::ptr::null(), + trampoline: None, }, } } @@ -314,7 +305,7 @@ impl Function { if let Err(error) = unsafe { wasmer_call_trampoline( self.exported.vmctx, - *func.trampoline, + func.trampoline, self.exported.address, values_vec.as_mut_ptr() as *mut u8, ) @@ -364,9 +355,10 @@ impl Function { } pub(crate) fn from_export(store: &Store, wasmer_export: ExportFunction) -> Self { + let trampoline = wasmer_export.trampoline.unwrap(); Self { store: store.clone(), - definition: FunctionDefinition::Wasm(WasmFunctionDefinition { trampoline: wasmer_export.trampoline }), + definition: FunctionDefinition::Wasm(WasmFunctionDefinition { trampoline }), exported: wasmer_export, } } diff --git a/lib/api/src/native.rs b/lib/api/src/native.rs index 5a73083f2cb..01bd411cb9b 100644 --- a/lib/api/src/native.rs +++ b/lib/api/src/native.rs @@ -69,6 +69,7 @@ where vmctx: other.vmctx, signature, kind: other.arg_kind, + trampoline: None, } } } @@ -88,6 +89,7 @@ where vmctx: other.vmctx, signature, kind: other.arg_kind, + trampoline: None, }, } } diff --git a/lib/api/src/types.rs b/lib/api/src/types.rs index de27ede3eb6..7c1d2a9a288 100644 --- a/lib/api/src/types.rs +++ b/lib/api/src/types.rs @@ -78,6 +78,7 @@ impl ValFuncRef for Val { // are converted to use the trampolines with static signatures). kind: wasmer_vm::VMFunctionKind::Static, vmctx: item.vmctx, + trampoline: None, }; let f = Function::from_export(store, export); Val::FuncRef(f) diff --git a/lib/engine-native/src/artifact.rs b/lib/engine-native/src/artifact.rs index 24dff814902..f08095babea 100644 --- a/lib/engine-native/src/artifact.rs +++ b/lib/engine-native/src/artifact.rs @@ -338,7 +338,8 @@ impl NativeArtifact { metadata, library: None, finished_functions: finished_functions.into_boxed_slice(), - finished_function_call_trampolines: finished_function_call_trampolines.into_boxed_slice(), + finished_function_call_trampolines: finished_function_call_trampolines + .into_boxed_slice(), finished_dynamic_function_trampolines: finished_dynamic_function_trampolines .into_boxed_slice(), signatures: signatures.into_boxed_slice(), @@ -390,11 +391,14 @@ impl NativeArtifact { } // Retrieve function call trampolines - let mut finished_function_call_trampolines: PrimaryMap = PrimaryMap::with_capacity(metadata.compile_info.module.signatures.len()); + let mut finished_function_call_trampolines: PrimaryMap = + PrimaryMap::with_capacity(metadata.compile_info.module.signatures.len()); for sig_index in metadata.compile_info.module.signatures.keys() { let function_name = metadata.symbol_to_name(Symbol::FunctionCallTrampoline(sig_index)); unsafe { - let trampoline: LibrarySymbol = lib.get(function_name.as_bytes()).map_err(to_compile_error)?; + let trampoline: LibrarySymbol = lib + .get(function_name.as_bytes()) + .map_err(to_compile_error)?; let raw = *trampoline.into_raw(); finished_function_call_trampolines.push(raw); } @@ -454,7 +458,8 @@ impl NativeArtifact { metadata, library: Some(lib), finished_functions: finished_functions.into_boxed_slice(), - finished_function_call_trampolines: finished_function_call_trampolines.into_boxed_slice(), + finished_function_call_trampolines: finished_function_call_trampolines + .into_boxed_slice(), finished_dynamic_function_trampolines: finished_dynamic_function_trampolines .into_boxed_slice(), signatures: signatures.into_boxed_slice(), diff --git a/lib/engine-native/src/engine.rs b/lib/engine-native/src/engine.rs index 0d63a262f62..0d036a82203 100644 --- a/lib/engine-native/src/engine.rs +++ b/lib/engine-native/src/engine.rs @@ -123,7 +123,6 @@ impl Engine for NativeEngine { &self.target } - /* /// Register a signature fn register_signature(&self, func_type: &FunctionType) -> VMSharedSignatureIndex { let compiler = self.inner(); @@ -136,6 +135,7 @@ impl Engine for NativeEngine { compiler.signatures().lookup(sig) } + /* /// Retrieves a trampoline given a signature fn function_call_trampoline(&self, sig: VMSharedSignatureIndex) -> Option { self.inner().trampoline(sig) diff --git a/lib/engine/src/engine.rs b/lib/engine/src/engine.rs index 200c2064357..3147fd48178 100644 --- a/lib/engine/src/engine.rs +++ b/lib/engine/src/engine.rs @@ -19,13 +19,13 @@ pub trait Engine { /// Gets the target fn target(&self) -> &Target; - /* /// Register a signature fn register_signature(&self, func_type: &FunctionType) -> VMSharedSignatureIndex; /// Lookup a signature fn lookup_signature(&self, sig: VMSharedSignatureIndex) -> Option; + /* /// Retrieves a trampoline given a signature fn function_call_trampoline(&self, sig: VMSharedSignatureIndex) -> Option; */ diff --git a/lib/vm/src/export.rs b/lib/vm/src/export.rs index c309117e5f3..edca422cfdf 100644 --- a/lib/vm/src/export.rs +++ b/lib/vm/src/export.rs @@ -35,8 +35,8 @@ pub struct ExportFunction { pub signature: FunctionType, /// The function kind (it defines how it's the signature that provided `address` have) pub kind: VMFunctionKind, - /// Address of the function call trampoline owned by the same VMContext that owns the VMFunctionBody. May be nullptr when FunctionType == Dynamic or vmctx == nullptr. - pub trampoline: VMTrampoline, + /// Address of the function call trampoline owned by the same VMContext that owns the VMFunctionBody. May be None when FunctionType == Dynamic or vmctx == nullptr. + pub trampoline: Option, } /// # Safety diff --git a/lib/vm/src/instance.rs b/lib/vm/src/instance.rs index 25bcef9f1a7..de99190f396 100644 --- a/lib/vm/src/instance.rs +++ b/lib/vm/src/instance.rs @@ -301,7 +301,7 @@ impl Instance { let import = self.imported_function(*index); (import.body, import.vmctx) }; - let trampoline = self.function_call_trampolines[*sig_index]; + let trampoline = Some(self.function_call_trampolines[*sig_index]); let signature = self.module.signatures[*sig_index].clone(); ExportFunction { address, From c9ea2fadcdb2b1d9a2da897574594957595a61d8 Mon Sep 17 00:00:00 2001 From: Nick Lewycky Date: Thu, 8 Oct 2020 16:09:08 -0700 Subject: [PATCH 04/18] cargo lint cleanups. --- lib/engine-native/src/artifact.rs | 26 ++++---------------------- lib/engine-native/src/engine.rs | 3 +-- lib/engine/src/engine.rs | 2 +- 3 files changed, 6 insertions(+), 25 deletions(-) diff --git a/lib/engine-native/src/artifact.rs b/lib/engine-native/src/artifact.rs index f08095babea..a98b7363732 100644 --- a/lib/engine-native/src/artifact.rs +++ b/lib/engine-native/src/artifact.rs @@ -31,12 +31,12 @@ use wasmer_types::entity::{BoxedSlice, PrimaryMap}; #[cfg(feature = "compiler")] use wasmer_types::DataInitializer; use wasmer_types::{ - FunctionIndex, FunctionType, LocalFunctionIndex, MemoryIndex, OwnedDataInitializer, - SignatureIndex, TableIndex, + FunctionIndex, LocalFunctionIndex, MemoryIndex, OwnedDataInitializer, SignatureIndex, + TableIndex, }; use wasmer_vm::{ - FunctionBodyPtr, MemoryStyle, ModuleInfo, SignatureRegistry, TableStyle, VMFunctionBody, - VMSharedSignatureIndex, VMTrampoline, + FunctionBodyPtr, MemoryStyle, ModuleInfo, TableStyle, VMFunctionBody, VMSharedSignatureIndex, + VMTrampoline, }; /// A compiled wasm module, ready to be instantiated. @@ -49,8 +49,6 @@ pub struct NativeArtifact { finished_function_call_trampolines: BoxedSlice, finished_dynamic_function_trampolines: BoxedSlice, signatures: BoxedSlice, - /// Pointers to trampoline functions used to enter particular signatures. - trampolines: HashMap, } fn to_compile_error(err: impl Error) -> CompileError { @@ -343,7 +341,6 @@ impl NativeArtifact { finished_dynamic_function_trampolines: finished_dynamic_function_trampolines .into_boxed_slice(), signatures: signatures.into_boxed_slice(), - trampolines: HashMap::new(), }) } @@ -376,20 +373,6 @@ impl NativeArtifact { } } - let mut trampolines = HashMap::new(); - - // Retrieve function call trampolines (for all signatures in the module) - for (sig_index, func_type) in metadata.compile_info.module.signatures.iter() { - let function_name = metadata.symbol_to_name(Symbol::FunctionCallTrampoline(sig_index)); - unsafe { - let trampoline: LibrarySymbol = lib - .get(function_name.as_bytes()) - .map_err(to_compile_error)?; - let index = engine_inner.signatures().register(&func_type); - trampolines.insert(index, *trampoline); - } - } - // Retrieve function call trampolines let mut finished_function_call_trampolines: PrimaryMap = PrimaryMap::with_capacity(metadata.compile_info.module.signatures.len()); @@ -463,7 +446,6 @@ impl NativeArtifact { finished_dynamic_function_trampolines: finished_dynamic_function_trampolines .into_boxed_slice(), signatures: signatures.into_boxed_slice(), - trampolines, }) } diff --git a/lib/engine-native/src/engine.rs b/lib/engine-native/src/engine.rs index 0d036a82203..4be5606d891 100644 --- a/lib/engine-native/src/engine.rs +++ b/lib/engine-native/src/engine.rs @@ -1,7 +1,6 @@ //! Native Engine. use crate::NativeArtifact; -use std::collections::HashMap; use std::path::Path; use std::sync::Arc; use std::sync::Mutex; @@ -12,7 +11,7 @@ use wasmer_engine::{Artifact, DeserializeError, Engine, EngineId, Tunables}; #[cfg(feature = "compiler")] use wasmer_types::Features; use wasmer_types::FunctionType; -use wasmer_vm::{SignatureRegistry, VMSharedSignatureIndex, VMTrampoline}; +use wasmer_vm::{SignatureRegistry, VMSharedSignatureIndex}; #[cfg(feature = "compiler")] use which::which; diff --git a/lib/engine/src/engine.rs b/lib/engine/src/engine.rs index 3147fd48178..84b00d2facc 100644 --- a/lib/engine/src/engine.rs +++ b/lib/engine/src/engine.rs @@ -7,7 +7,7 @@ use std::sync::atomic::{AtomicUsize, Ordering::SeqCst}; use std::sync::Arc; use wasmer_compiler::{CompileError, Target}; use wasmer_types::FunctionType; -use wasmer_vm::{VMSharedSignatureIndex, VMTrampoline}; +use wasmer_vm::VMSharedSignatureIndex; /// A unimplemented Wasmer `Engine`. /// From a6ad7af22b3c3a23448c5548d6e0bbd03ded1adc Mon Sep 17 00:00:00 2001 From: Nick Lewycky Date: Thu, 8 Oct 2020 16:30:59 -0700 Subject: [PATCH 05/18] Move function call trampolines from engine inner to artifact in engine-object-file. --- lib/engine-object-file/src/artifact.rs | 23 ++++++++++++++++------- lib/engine-object-file/src/engine.rs | 2 ++ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/lib/engine-object-file/src/artifact.rs b/lib/engine-object-file/src/artifact.rs index 72f158870c3..19131f59ddc 100644 --- a/lib/engine-object-file/src/artifact.rs +++ b/lib/engine-object-file/src/artifact.rs @@ -34,6 +34,7 @@ pub struct ObjectFileArtifact { metadata: ModuleMetadata, module_bytes: Vec, finished_functions: BoxedSlice, + finished_function_call_trampolines: BoxedSlice, finished_dynamic_function_trampolines: BoxedSlice, signatures: BoxedSlice, /// Length of the serialized metadata @@ -247,6 +248,8 @@ impl ObjectFileArtifact { metadata_length: usize, ) -> Result { let finished_functions: PrimaryMap = PrimaryMap::new(); + let finished_function_call_trampolines: PrimaryMap = + PrimaryMap::new(); let finished_dynamic_function_trampolines: PrimaryMap = PrimaryMap::new(); let signature_registry = engine_inner.signatures(); @@ -262,6 +265,8 @@ impl ObjectFileArtifact { metadata, module_bytes, finished_functions: finished_functions.into_boxed_slice(), + finished_function_call_trampolines: finished_function_call_trampolines + .into_boxed_slice(), finished_dynamic_function_trampolines: finished_dynamic_function_trampolines .into_boxed_slice(), signatures: signatures.into_boxed_slice(), @@ -309,7 +314,7 @@ impl ObjectFileArtifact { len: usize, } - let mut engine_inner = engine.inner_mut(); + let engine_inner = engine.inner(); let signature_registry = engine_inner.signatures(); let mut sig_map: BTreeMap = BTreeMap::new(); @@ -342,23 +347,21 @@ impl ObjectFileArtifact { } // read trampolines in order + let mut finished_function_call_trampolines = PrimaryMap::new(); for i in 0..WORD_SIZE { byte_buffer[i] = bytes[cur_offset + i]; } cur_offset += WORD_SIZE; let num_function_trampolines = usize::from_ne_bytes(byte_buffer); - for i in 0..num_function_trampolines { + for _ in 0..num_function_trampolines { for j in 0..WORD_SIZE { byte_buffer[j] = bytes[cur_offset + j]; } cur_offset += WORD_SIZE; let trampoline_ptr_bytes = usize::from_ne_bytes(byte_buffer); let trampoline = mem::transmute::(trampoline_ptr_bytes); - - let func_type = &metadata.compile_info.module.signatures[SignatureIndex::new(i)]; - - engine_inner.add_trampoline(func_type, trampoline); - // TODO: we can read back the length here if we serialize it. This will improve debug output. + finished_function_call_trampolines.push(trampoline); + // TODO: we can read back the length here if we serialize it. This will improve debug output. } // read dynamic function trampolines in order now... @@ -386,6 +389,8 @@ impl ObjectFileArtifact { metadata, module_bytes: bytes.to_owned(), finished_functions: finished_functions.into_boxed_slice(), + finished_function_call_trampolines: finished_function_call_trampolines + .into_boxed_slice(), finished_dynamic_function_trampolines: finished_dynamic_function_trampolines .into_boxed_slice(), signatures: signatures.into_boxed_slice(), @@ -441,6 +446,10 @@ impl Artifact for ObjectFileArtifact { &self.finished_functions } + fn finished_function_call_trampolines(&self) -> &BoxedSlice { + &self.finished_function_call_trampolines + } + fn finished_dynamic_function_trampolines(&self) -> &BoxedSlice { &self.finished_dynamic_function_trampolines } diff --git a/lib/engine-object-file/src/engine.rs b/lib/engine-object-file/src/engine.rs index 3c83a275fde..421a86021f8 100644 --- a/lib/engine-object-file/src/engine.rs +++ b/lib/engine-object-file/src/engine.rs @@ -112,10 +112,12 @@ impl Engine for ObjectFileEngine { compiler.signatures().lookup(sig) } + /* /// Retrieves a trampoline given a signature fn function_call_trampoline(&self, sig: VMSharedSignatureIndex) -> Option { self.inner().trampoline(sig) } + */ /// Validates a WebAssembly module fn validate(&self, binary: &[u8]) -> Result<(), CompileError> { From a484765fd6d84246a64c34bc6af2a23dc411007b Mon Sep 17 00:00:00 2001 From: Nick Lewycky Date: Thu, 8 Oct 2020 17:33:16 -0700 Subject: [PATCH 06/18] Move trampolines to artifact in engine-{jit,object-file,dummy}. Some cleanup per cargo clippy. --- lib/api/src/externals/function.rs | 2 +- lib/cli/Cargo.toml | 24 ++++++++-------- lib/engine-jit/src/artifact.rs | 14 +++++++-- lib/engine-jit/src/engine.rs | 39 ++++++++------------------ lib/engine-native/src/artifact.rs | 1 - lib/engine-object-file/src/engine.rs | 27 +----------------- tests/lib/engine-dummy/src/artifact.rs | 20 +++++++++++-- tests/lib/engine-dummy/src/engine.rs | 9 +----- 8 files changed, 56 insertions(+), 80 deletions(-) diff --git a/lib/api/src/externals/function.rs b/lib/api/src/externals/function.rs index f4df7a2702e..e1914cdc106 100644 --- a/lib/api/src/externals/function.rs +++ b/lib/api/src/externals/function.rs @@ -12,7 +12,7 @@ use std::fmt; use wasmer_vm::{ raise_user_trap, resume_panic, wasmer_call_trampoline, Export, ExportFunction, VMCallerCheckedAnyfunc, VMContext, VMDynamicFunctionContext, VMFunctionBody, VMFunctionKind, - VMSharedSignatureIndex, VMTrampoline, + VMTrampoline, }; /// A function defined in the Wasm module diff --git a/lib/cli/Cargo.toml b/lib/cli/Cargo.toml index ee8e865ae79..fea527b5778 100644 --- a/lib/cli/Cargo.toml +++ b/lib/cli/Cargo.toml @@ -24,9 +24,9 @@ wasmer-compiler-singlepass = { version = "1.0.0-alpha3", path = "../compiler-sin wasmer-compiler-llvm = { version = "1.0.0-alpha3", path = "../compiler-llvm", optional = true } wasmer-emscripten = { version = "1.0.0-alpha3", path = "../emscripten", optional = true } wasmer-engine = { version = "1.0.0-alpha3", path = "../engine" } -#wasmer-engine-jit = { version = "1.0.0-alpha3", path = "../engine-jit", optional = true } +wasmer-engine-jit = { version = "1.0.0-alpha3", path = "../engine-jit", optional = true } wasmer-engine-native = { version = "1.0.0-alpha3", path = "../engine-native", optional = true } -#wasmer-engine-object-file = { version = "1.0.0-alpha3", path = "../engine-object-file", optional = true } +wasmer-engine-object-file = { version = "1.0.0-alpha3", path = "../engine-object-file", optional = true } wasmer-vm = { version = "1.0.0-alpha3", path = "../vm" } wasmer-wasi = { version = "1.0.0-alpha3", path = "../wasi", optional = true } wasmer-wasi-experimental-io-devices = { version = "1.0.0-alpha3", path = "../wasi-experimental-io-devices", optional = true } @@ -52,26 +52,26 @@ log = { version = "0.4", optional = true } default = [ "wat", "wast", -# "jit", + "jit", "native", -# "object-file", + "object-file", "cache", "wasi", "emscripten", ] engine = [] -#jit = [ -# "wasmer-engine-jit", -# "engine", -#] +jit = [ + "wasmer-engine-jit", + "engine", +] native = [ "wasmer-engine-native", "engine", ] -#object-file = [ -# "wasmer-engine-object-file", -# "engine", -#] +object-file = [ + "wasmer-engine-object-file", + "engine", +] cache = ["wasmer-cache"] wast = ["wasmer-wast"] wasi = ["wasmer-wasi"] diff --git a/lib/engine-jit/src/artifact.rs b/lib/engine-jit/src/artifact.rs index b4c9af239e8..f7bca8910a5 100644 --- a/lib/engine-jit/src/artifact.rs +++ b/lib/engine-jit/src/artifact.rs @@ -20,12 +20,15 @@ use wasmer_types::{ FunctionIndex, LocalFunctionIndex, MemoryIndex, OwnedDataInitializer, SignatureIndex, TableIndex, }; -use wasmer_vm::{FunctionBodyPtr, MemoryStyle, ModuleInfo, TableStyle, VMSharedSignatureIndex}; +use wasmer_vm::{ + FunctionBodyPtr, MemoryStyle, ModuleInfo, TableStyle, VMSharedSignatureIndex, VMTrampoline, +}; /// A compiled wasm module, ready to be instantiated. pub struct JITArtifact { serializable: SerializableModule, finished_functions: BoxedSlice, + finished_function_call_trampolines: BoxedSlice, finished_dynamic_function_trampolines: BoxedSlice, signatures: BoxedSlice, frame_info_registration: Mutex>, @@ -150,7 +153,7 @@ impl JITArtifact { ) -> Result { let ( finished_functions, - _finished_function_call_trampolines, + finished_function_call_trampolines, finished_dynamic_function_trampolines, custom_sections, ) = inner_jit.allocate( @@ -201,6 +204,8 @@ impl JITArtifact { inner_jit.publish_eh_frame(eh_frame)?; let finished_functions = finished_functions.into_boxed_slice(); + let finished_function_call_trampolines = + finished_function_call_trampolines.into_boxed_slice(); let finished_dynamic_function_trampolines = finished_dynamic_function_trampolines.into_boxed_slice(); let signatures = signatures.into_boxed_slice(); @@ -208,6 +213,7 @@ impl JITArtifact { Ok(Self { serializable, finished_functions, + finished_function_call_trampolines, finished_dynamic_function_trampolines, signatures, frame_info_registration: Mutex::new(None), @@ -270,6 +276,10 @@ impl Artifact for JITArtifact { &self.finished_functions } + fn finished_function_call_trampolines(&self) -> &BoxedSlice { + &self.finished_function_call_trampolines + } + // TODO: return *const instead of *mut fn finished_dynamic_function_trampolines(&self) -> &BoxedSlice { &self.finished_dynamic_function_trampolines diff --git a/lib/engine-jit/src/engine.rs b/lib/engine-jit/src/engine.rs index 0a4dbdf83ad..0396ac2bdd6 100644 --- a/lib/engine-jit/src/engine.rs +++ b/lib/engine-jit/src/engine.rs @@ -1,7 +1,6 @@ //! JIT compilation. use crate::{CodeMemory, JITArtifact}; -use std::collections::HashMap; use std::sync::{Arc, Mutex}; #[cfg(feature = "compiler")] use wasmer_compiler::Compiler; @@ -33,7 +32,6 @@ impl JITEngine { Self { inner: Arc::new(Mutex::new(JITEngineInner { compiler: Some(compiler), - function_call_trampolines: HashMap::new(), code_memory: vec![], signatures: SignatureRegistry::new(), features, @@ -61,7 +59,6 @@ impl JITEngine { inner: Arc::new(Mutex::new(JITEngineInner { #[cfg(feature = "compiler")] compiler: None, - function_call_trampolines: HashMap::new(), code_memory: vec![], signatures: SignatureRegistry::new(), features: Features::default(), @@ -98,11 +95,6 @@ impl Engine for JITEngine { compiler.signatures().lookup(sig) } - /// Retrieves a trampoline given a signature - fn function_call_trampoline(&self, sig: VMSharedSignatureIndex) -> Option { - self.inner().function_call_trampoline(sig) - } - /// Validates a WebAssembly module fn validate(&self, binary: &[u8]) -> Result<(), CompileError> { self.inner().validate(binary) @@ -150,8 +142,6 @@ pub struct JITEngineInner { /// The compiler #[cfg(feature = "compiler")] compiler: Option>, - /// Pointers to trampoline functions used to enter particular signatures - function_call_trampolines: HashMap, /// The features to compile the Wasm module with features: Features, /// The code memory is responsible of publishing the compiled @@ -196,7 +186,7 @@ impl JITEngineInner { #[allow(clippy::type_complexity)] pub(crate) fn allocate( &mut self, - module: &ModuleInfo, + _module: &ModuleInfo, functions: &PrimaryMap, function_call_trampolines: &PrimaryMap, dynamic_function_trampolines: &PrimaryMap, @@ -204,7 +194,7 @@ impl JITEngineInner { ) -> Result< ( PrimaryMap, - PrimaryMap, + PrimaryMap, PrimaryMap, PrimaryMap, ), @@ -241,20 +231,20 @@ impl JITEngineInner { .map(|slice| FunctionBodyPtr(slice as *mut [_])) .collect::>(); - let mut allocated_function_call_trampolines: PrimaryMap = + let mut allocated_function_call_trampolines: PrimaryMap = PrimaryMap::new(); - for ((sig_index, _), ptr) in function_call_trampolines.iter().zip( + for ((_sig_index, _), ptr) in function_call_trampolines.iter().zip( allocated_functions .drain(0..function_call_trampolines.len()) - .map(|slice| FunctionBodyPtr(slice as *mut [_])), + .map(|slice| slice.as_ptr()), ) { - let func_type = &module.signatures[sig_index]; - let index = self.signatures.register(&func_type); - allocated_function_call_trampolines.push(ptr); - let trampoline = unsafe { - std::mem::transmute::<*const VMFunctionBody, VMTrampoline>((**ptr).as_ptr()) - }; - self.function_call_trampolines.insert(index, trampoline); + // TODO: make certain that we're visiting sigindex strictly increasing + //let func_type = &module.signatures[sig_index]; + //let index = self.signatures.register(&func_type); + + let trampoline = + unsafe { std::mem::transmute::<*const VMFunctionBody, VMTrampoline>(ptr) }; + allocated_function_call_trampolines.push(trampoline); } let allocated_dynamic_function_trampolines = allocated_functions @@ -309,9 +299,4 @@ impl JITEngineInner { pub fn signatures(&self) -> &SignatureRegistry { &self.signatures } - - /// Gets the trampoline pre-registered for a particular signature - pub fn function_call_trampoline(&self, sig: VMSharedSignatureIndex) -> Option { - self.function_call_trampolines.get(&sig).cloned() - } } diff --git a/lib/engine-native/src/artifact.rs b/lib/engine-native/src/artifact.rs index a98b7363732..cc8665156ca 100644 --- a/lib/engine-native/src/artifact.rs +++ b/lib/engine-native/src/artifact.rs @@ -4,7 +4,6 @@ use crate::engine::{NativeEngine, NativeEngineInner}; use crate::serialize::ModuleMetadata; use libloading::{Library, Symbol as LibrarySymbol}; -use std::collections::HashMap; use std::error::Error; use std::fs::File; use std::io::{Read, Write}; diff --git a/lib/engine-object-file/src/engine.rs b/lib/engine-object-file/src/engine.rs index 421a86021f8..a24fe358b63 100644 --- a/lib/engine-object-file/src/engine.rs +++ b/lib/engine-object-file/src/engine.rs @@ -1,5 +1,4 @@ use crate::ObjectFileArtifact; -use std::collections::HashMap; use std::io::Read; use std::path::Path; use std::sync::{Arc, Mutex}; @@ -10,7 +9,7 @@ use wasmer_engine::{Artifact, DeserializeError, Engine, EngineId, Tunables}; #[cfg(feature = "compiler")] use wasmer_types::Features; use wasmer_types::FunctionType; -use wasmer_vm::{SignatureRegistry, VMSharedSignatureIndex, VMTrampoline}; +use wasmer_vm::{SignatureRegistry, VMSharedSignatureIndex}; /// A WebAssembly `ObjectFile` Engine. #[derive(Clone)] @@ -28,7 +27,6 @@ impl ObjectFileEngine { Self { inner: Arc::new(Mutex::new(ObjectFileEngineInner { compiler: Some(compiler), - trampolines: HashMap::new(), signatures: SignatureRegistry::new(), prefixer: None, features, @@ -58,7 +56,6 @@ impl ObjectFileEngine { compiler: None, #[cfg(feature = "compiler")] features: Features::default(), - trampolines: HashMap::new(), signatures: SignatureRegistry::new(), prefixer: None, })), @@ -112,13 +109,6 @@ impl Engine for ObjectFileEngine { compiler.signatures().lookup(sig) } - /* - /// Retrieves a trampoline given a signature - fn function_call_trampoline(&self, sig: VMSharedSignatureIndex) -> Option { - self.inner().trampoline(sig) - } - */ - /// Validates a WebAssembly module fn validate(&self, binary: &[u8]) -> Result<(), CompileError> { self.inner().validate(binary) @@ -182,8 +172,6 @@ pub struct ObjectFileEngineInner { /// The WebAssembly features to use #[cfg(feature = "compiler")] features: Features, - /// Pointers to trampoline functions used to enter particular signatures - trampolines: HashMap, /// The signature registry is used mainly to operate with trampolines /// performantly. signatures: SignatureRegistry, @@ -238,17 +226,4 @@ impl ObjectFileEngineInner { pub fn signatures(&self) -> &SignatureRegistry { &self.signatures } - - /// Gets the trampoline pre-registered for a particular signature - pub fn trampoline(&self, sig: VMSharedSignatureIndex) -> Option { - self.trampolines.get(&sig).cloned() - } - - pub(crate) fn add_trampoline(&mut self, func_type: &FunctionType, trampoline: VMTrampoline) { - let index = self.signatures.register(&func_type); - // We always use (for now) the latest trampoline compiled - // TODO: we need to deallocate trampolines as the compiled modules - // where they belong become unallocated. - self.trampolines.insert(index, trampoline); - } } diff --git a/tests/lib/engine-dummy/src/artifact.rs b/tests/lib/engine-dummy/src/artifact.rs index 42cda621a0d..e8e693661bb 100644 --- a/tests/lib/engine-dummy/src/artifact.rs +++ b/tests/lib/engine-dummy/src/artifact.rs @@ -16,7 +16,7 @@ use wasmer_types::{ }; use wasmer_vm::{ FunctionBodyPtr, MemoryStyle, ModuleInfo, TableStyle, VMContext, VMFunctionBody, - VMSharedSignatureIndex, + VMSharedSignatureIndex, VMTrampoline, }; /// Serializable struct for the artifact @@ -38,6 +38,7 @@ pub struct DummyArtifact { metadata: DummyArtifactMetadata, finished_functions: BoxedSlice, + finished_function_call_trampolines: BoxedSlice, finished_dynamic_function_trampolines: BoxedSlice, signatures: BoxedSlice, } @@ -131,7 +132,7 @@ impl DummyArtifact { ) -> Result { let num_local_functions = metadata.module.functions.len() - metadata.module.num_imported_functions; - // We prepare the pointers for the finished functions + // We prepare the pointers for the finished functions. let finished_functions: PrimaryMap = (0 ..num_local_functions) .map(|_| unsafe { @@ -141,7 +142,13 @@ impl DummyArtifact { }) .collect::>(); - // We prepare the pointers for the finished dynamic function trampolines + // We prepare the pointers for the finished function call trampolines. + let finished_function_call_trampolines: PrimaryMap = (0 + ..metadata.module.signatures.len()) + .map(|_| unsafe { std::mem::transmute::<_, VMTrampoline>(&dummy_function) }) + .collect::>(); + + // We prepare the pointers for the finished dynamic function trampolines. let finished_dynamic_function_trampolines: PrimaryMap = (0 ..metadata.module.num_imported_functions) .map(|_| { @@ -165,6 +172,8 @@ impl DummyArtifact { }; let finished_functions = finished_functions.into_boxed_slice(); + let finished_function_call_trampolines = + finished_function_call_trampolines.into_boxed_slice(); let finished_dynamic_function_trampolines = finished_dynamic_function_trampolines.into_boxed_slice(); let signatures = signatures.into_boxed_slice(); @@ -172,6 +181,7 @@ impl DummyArtifact { Ok(Self { metadata, finished_functions, + finished_function_call_trampolines, finished_dynamic_function_trampolines, signatures, }) @@ -215,6 +225,10 @@ impl Artifact for DummyArtifact { &self.finished_functions } + fn finished_function_call_trampolines(&self) -> &BoxedSlice { + &self.finished_function_call_trampolines + } + fn finished_dynamic_function_trampolines(&self) -> &BoxedSlice { &self.finished_dynamic_function_trampolines } diff --git a/tests/lib/engine-dummy/src/engine.rs b/tests/lib/engine-dummy/src/engine.rs index 0a8a3e255c5..14c5c0beceb 100644 --- a/tests/lib/engine-dummy/src/engine.rs +++ b/tests/lib/engine-dummy/src/engine.rs @@ -5,9 +5,7 @@ use std::sync::Arc; use wasmer_compiler::{CompileError, Features, Target}; use wasmer_engine::{Artifact, DeserializeError, Engine, EngineId, Tunables}; use wasmer_types::FunctionType; -use wasmer_vm::{ - SignatureRegistry, VMContext, VMFunctionBody, VMSharedSignatureIndex, VMTrampoline, -}; +use wasmer_vm::{SignatureRegistry, VMContext, VMFunctionBody, VMSharedSignatureIndex}; extern "C" fn dummy_trampoline( _context: *mut VMContext, @@ -58,11 +56,6 @@ impl Engine for DummyEngine { self.signatures.lookup(sig) } - /// Retrieves a trampoline given a signature - fn function_call_trampoline(&self, _sig: VMSharedSignatureIndex) -> Option { - Some(dummy_trampoline) - } - #[cfg(feature = "compiler")] /// Validates a WebAssembly module fn validate(&self, binary: &[u8]) -> Result<(), CompileError> { From 37615b1ae5f31bf5d8e6459129fd2d1f98946d09 Mon Sep 17 00:00:00 2001 From: Nick Lewycky Date: Thu, 8 Oct 2020 19:07:19 -0700 Subject: [PATCH 07/18] Re-enable features that were temporarily disabled for development. Clean up some commented out code. --- lib/cli/Cargo.toml | 4 ++-- lib/engine-native/src/engine.rs | 22 ---------------------- lib/engine/src/engine.rs | 5 ----- 3 files changed, 2 insertions(+), 29 deletions(-) diff --git a/lib/cli/Cargo.toml b/lib/cli/Cargo.toml index 33077c7db24..d7705771944 100644 --- a/lib/cli/Cargo.toml +++ b/lib/cli/Cargo.toml @@ -79,9 +79,9 @@ emscripten = ["wasmer-emscripten"] wat = ["wasmer/wat"] compiler = [ "wasmer-compiler/translator", -# "wasmer-engine-jit/compiler", + "wasmer-engine-jit/compiler", "wasmer-engine-native/compiler", -# "wasmer-engine-object-file/compiler", + "wasmer-engine-object-file/compiler", ] experimental-io-devices = [ "wasmer-wasi-experimental-io-devices", diff --git a/lib/engine-native/src/engine.rs b/lib/engine-native/src/engine.rs index 4be5606d891..af798ed088e 100644 --- a/lib/engine-native/src/engine.rs +++ b/lib/engine-native/src/engine.rs @@ -134,13 +134,6 @@ impl Engine for NativeEngine { compiler.signatures().lookup(sig) } - /* - /// Retrieves a trampoline given a signature - fn function_call_trampoline(&self, sig: VMSharedSignatureIndex) -> Option { - self.inner().trampoline(sig) - } - */ - /// Validates a WebAssembly module fn validate(&self, binary: &[u8]) -> Result<(), CompileError> { self.inner().validate(binary) @@ -280,21 +273,6 @@ impl NativeEngineInner { &self.signatures } - /* - /// Gets the trampoline pre-registered for a particular signature - pub fn trampoline(&self, sig: VMSharedSignatureIndex) -> Option { - self.trampolines.get(&sig).cloned() - } - - pub(crate) fn add_trampoline(&mut self, func_type: &FunctionType, trampoline: VMTrampoline) { - let index = self.signatures.register(&func_type); - // We always use (for now) the latest trampoline compiled - // TODO: we need to deallocate trampolines as the compiled modules - // where they belong become unallocated. - self.trampolines.insert(index, trampoline); - } - */ - pub(crate) fn is_cross_compiling(&self) -> bool { self.is_cross_compiling } diff --git a/lib/engine/src/engine.rs b/lib/engine/src/engine.rs index 84b00d2facc..2828bfc52a3 100644 --- a/lib/engine/src/engine.rs +++ b/lib/engine/src/engine.rs @@ -25,11 +25,6 @@ pub trait Engine { /// Lookup a signature fn lookup_signature(&self, sig: VMSharedSignatureIndex) -> Option; - /* - /// Retrieves a trampoline given a signature - fn function_call_trampoline(&self, sig: VMSharedSignatureIndex) -> Option; - */ - /// Validates a WebAssembly module fn validate(&self, binary: &[u8]) -> Result<(), CompileError>; From e6801ddeb2feaecda2c29ef7c2623ae7e633ea89 Mon Sep 17 00:00:00 2001 From: Nick Lewycky Date: Mon, 12 Oct 2020 10:31:11 -0700 Subject: [PATCH 08/18] DO NOT MERGE TO MASTER. Remove parts of this test not relevant to the failure. --- lib/deprecated/runtime/examples/call.rs | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/lib/deprecated/runtime/examples/call.rs b/lib/deprecated/runtime/examples/call.rs index b56914dd90a..9443f3015c4 100644 --- a/lib/deprecated/runtime/examples/call.rs +++ b/lib/deprecated/runtime/examples/call.rs @@ -5,14 +5,6 @@ static WAT: &'static str = r#" (module (type (;0;) (func (result i32))) (import "env" "do_panic" (func $do_panic (type 0))) - (func $dbz (result i32) - call $do_panic - drop - i32.const 42 - i32.const 0 - i32.div_u - ) - (export "dbz" (func $dbz)) ) "#; @@ -43,16 +35,13 @@ fn main() -> Result<(), Box> { println!("instantiating"); - let instance = module.instantiate(&imports! { + let imports = imports! { "env" => { "do_panic" => Func::new(do_panic), }, - })?; + }; - let foo: Func<(), i32> = instance.exports.get("dbz")?; - let result = foo.call(); - - println!("result: {:?}", result.unwrap_err().message()); + let instance = module.instantiate(&imports)?; Ok(()) } From fed7541c60ccf4a217ee6ebacd909e1eb1fc35a9 Mon Sep 17 00:00:00 2001 From: Nick Lewycky Date: Mon, 12 Oct 2020 11:14:17 -0700 Subject: [PATCH 09/18] Revert "DO NOT MERGE TO MASTER." This reverts commit e6801ddeb2feaecda2c29ef7c2623ae7e633ea89. --- lib/deprecated/runtime/examples/call.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/deprecated/runtime/examples/call.rs b/lib/deprecated/runtime/examples/call.rs index 9443f3015c4..b56914dd90a 100644 --- a/lib/deprecated/runtime/examples/call.rs +++ b/lib/deprecated/runtime/examples/call.rs @@ -5,6 +5,14 @@ static WAT: &'static str = r#" (module (type (;0;) (func (result i32))) (import "env" "do_panic" (func $do_panic (type 0))) + (func $dbz (result i32) + call $do_panic + drop + i32.const 42 + i32.const 0 + i32.div_u + ) + (export "dbz" (func $dbz)) ) "#; @@ -35,13 +43,16 @@ fn main() -> Result<(), Box> { println!("instantiating"); - let imports = imports! { + let instance = module.instantiate(&imports! { "env" => { "do_panic" => Func::new(do_panic), }, - }; + })?; - let instance = module.instantiate(&imports)?; + let foo: Func<(), i32> = instance.exports.get("dbz")?; + let result = foo.call(); + + println!("result: {:?}", result.unwrap_err().message()); Ok(()) } From c2646c12591595840d1e945fa030eace7019f308 Mon Sep 17 00:00:00 2001 From: Nick Lewycky Date: Mon, 12 Oct 2020 11:17:45 -0700 Subject: [PATCH 10/18] Permit exported functions to be host functions. At the moment this only happens from lib/deprecated. --- lib/api/src/externals/function.rs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/api/src/externals/function.rs b/lib/api/src/externals/function.rs index e1914cdc106..4edbf676e79 100644 --- a/lib/api/src/externals/function.rs +++ b/lib/api/src/externals/function.rs @@ -355,11 +355,20 @@ impl Function { } pub(crate) fn from_export(store: &Store, wasmer_export: ExportFunction) -> Self { - let trampoline = wasmer_export.trampoline.unwrap(); - Self { - store: store.clone(), - definition: FunctionDefinition::Wasm(WasmFunctionDefinition { trampoline }), - exported: wasmer_export, + if let Some(trampoline) = wasmer_export.trampoline { + Self { + store: store.clone(), + definition: FunctionDefinition::Wasm(WasmFunctionDefinition { trampoline }), + exported: wasmer_export, + } + } else { + Self { + store: store.clone(), + definition: FunctionDefinition::Host(HostFunctionDefinition { + has_env: !wasmer_export.vmctx.is_null(), + }), + exported: wasmer_export, + } } } From f28b1944d35ccf55354a7dee6dddb5b9e5c2426d Mon Sep 17 00:00:00 2001 From: Nick Lewycky Date: Mon, 12 Oct 2020 11:19:22 -0700 Subject: [PATCH 11/18] Add changelog entry. --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 41c7a1d46ed..c8e9890aabd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## **[Unreleased]** +- [#1710](https://github.com/wasmerio/wasmer/pull/1710) Memory for function call trampolines is now owned by the Artifact. - [#1700](https://github.com/wasmerio/wasmer/pull/1700) Implement `wasm_externtype_copy` in the Wasm C API. ## 1.0.0-alpha4 - 2020-10-08 From df3b1f2ae441f0f5443206bb74dc9e5cd664a357 Mon Sep 17 00:00:00 2001 From: Nick Lewycky Date: Mon, 12 Oct 2020 11:29:05 -0700 Subject: [PATCH 12/18] Simplify this loop. --- lib/engine-jit/src/engine.rs | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/lib/engine-jit/src/engine.rs b/lib/engine-jit/src/engine.rs index 0396ac2bdd6..82624fec6ca 100644 --- a/lib/engine-jit/src/engine.rs +++ b/lib/engine-jit/src/engine.rs @@ -233,15 +233,10 @@ impl JITEngineInner { let mut allocated_function_call_trampolines: PrimaryMap = PrimaryMap::new(); - for ((_sig_index, _), ptr) in function_call_trampolines.iter().zip( - allocated_functions - .drain(0..function_call_trampolines.len()) - .map(|slice| slice.as_ptr()), - ) { - // TODO: make certain that we're visiting sigindex strictly increasing - //let func_type = &module.signatures[sig_index]; - //let index = self.signatures.register(&func_type); - + for ptr in allocated_functions + .drain(0..function_call_trampolines.len()) + .map(|slice| slice.as_ptr()) + { let trampoline = unsafe { std::mem::transmute::<*const VMFunctionBody, VMTrampoline>(ptr) }; allocated_function_call_trampolines.push(trampoline); From 6ede21c173106612dd0539c753b221d75a181175 Mon Sep 17 00:00:00 2001 From: nlewycky Date: Mon, 12 Oct 2020 17:22:59 -0700 Subject: [PATCH 13/18] Update lib/vm/src/export.rs Correct a comment. Co-authored-by: Syrus Akbary --- lib/vm/src/export.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/vm/src/export.rs b/lib/vm/src/export.rs index edca422cfdf..1d4ede555ab 100644 --- a/lib/vm/src/export.rs +++ b/lib/vm/src/export.rs @@ -35,7 +35,8 @@ pub struct ExportFunction { pub signature: FunctionType, /// The function kind (it defines how it's the signature that provided `address` have) pub kind: VMFunctionKind, - /// Address of the function call trampoline owned by the same VMContext that owns the VMFunctionBody. May be None when FunctionType == Dynamic or vmctx == nullptr. + /// Address of the function call trampoline owned by the same VMContext that owns the VMFunctionBody. + /// May be None when the function is an host-function (FunctionType == Dynamic or vmctx == nullptr) pub trampoline: Option, } From 8350c7b3495883bb6a267ed4955bb6eb7de0b528 Mon Sep 17 00:00:00 2001 From: Nick Lewycky Date: Mon, 12 Oct 2020 17:23:42 -0700 Subject: [PATCH 14/18] Comment ends in a full stop. --- lib/vm/src/export.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/vm/src/export.rs b/lib/vm/src/export.rs index 1d4ede555ab..3c9bb3d9cbb 100644 --- a/lib/vm/src/export.rs +++ b/lib/vm/src/export.rs @@ -36,7 +36,7 @@ pub struct ExportFunction { /// The function kind (it defines how it's the signature that provided `address` have) pub kind: VMFunctionKind, /// Address of the function call trampoline owned by the same VMContext that owns the VMFunctionBody. - /// May be None when the function is an host-function (FunctionType == Dynamic or vmctx == nullptr) + /// May be None when the function is an host-function (FunctionType == Dynamic or vmctx == nullptr). pub trampoline: Option, } From 62b9c8b627cb4729fae8f80842a370587937d8a6 Mon Sep 17 00:00:00 2001 From: Nick Lewycky Date: Tue, 13 Oct 2020 14:24:28 -0700 Subject: [PATCH 15/18] Move Library from artifact to engine inner. This way the code stays alive as long as the store is alive. Fixes segfault in traps::present_after_module_drop. --- lib/engine-native/src/artifact.rs | 19 +++++++++---------- lib/engine-native/src/engine.rs | 9 +++++++++ 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/lib/engine-native/src/artifact.rs b/lib/engine-native/src/artifact.rs index cc8665156ca..7145a636576 100644 --- a/lib/engine-native/src/artifact.rs +++ b/lib/engine-native/src/artifact.rs @@ -20,7 +20,7 @@ use wasmer_compiler::{ CompileModuleInfo, FunctionBodyData, ModuleEnvironment, ModuleTranslationState, }; use wasmer_engine::{ - Artifact, DeserializeError, InstantiationError, LinkError, RuntimeError, SerializeError, + Artifact, DeserializeError, InstantiationError, SerializeError, }; #[cfg(feature = "compiler")] use wasmer_engine::{Engine, Tunables}; @@ -42,14 +42,18 @@ use wasmer_vm::{ pub struct NativeArtifact { sharedobject_path: PathBuf, metadata: ModuleMetadata, - #[allow(dead_code)] - library: Option, finished_functions: BoxedSlice, finished_function_call_trampolines: BoxedSlice, finished_dynamic_function_trampolines: BoxedSlice, signatures: BoxedSlice, } +impl Drop for NativeArtifact { + fn drop(&mut self) { + println!("Dropping NativeArtifact!"); + } +} + fn to_compile_error(err: impl Error) -> CompileError { CompileError::Codegen(format!("{}", err)) } @@ -333,7 +337,6 @@ impl NativeArtifact { Ok(Self { sharedobject_path, metadata, - library: None, finished_functions: finished_functions.into_boxed_slice(), finished_function_call_trampolines: finished_function_call_trampolines .into_boxed_slice(), @@ -435,10 +438,11 @@ impl NativeArtifact { .collect::>() }; + engine_inner.add_library(lib); + Ok(Self { sharedobject_path, metadata, - library: Some(lib), finished_functions: finished_functions.into_boxed_slice(), finished_function_call_trampolines: finished_function_call_trampolines .into_boxed_slice(), @@ -593,11 +597,6 @@ impl Artifact for NativeArtifact { } fn preinstantiate(&self) -> Result<(), InstantiationError> { - if self.library.is_none() { - return Err(InstantiationError::Link(LinkError::Trap( - RuntimeError::new("Cross compiled artifacts can't be instantiated."), - ))); - } Ok(()) } diff --git a/lib/engine-native/src/engine.rs b/lib/engine-native/src/engine.rs index 830c57e629a..220609c025e 100644 --- a/lib/engine-native/src/engine.rs +++ b/lib/engine-native/src/engine.rs @@ -1,6 +1,7 @@ //! Native Engine. use crate::NativeArtifact; +use libloading::Library; use std::path::Path; use std::sync::Arc; use std::sync::Mutex; @@ -53,6 +54,7 @@ impl NativeEngine { features, is_cross_compiling, linker, + libraries: vec![], })), target: Arc::new(target), engine_id: EngineId::default(), @@ -83,6 +85,7 @@ impl NativeEngine { prefixer: None, is_cross_compiling: false, linker: Linker::None, + libraries: vec![], })), target: Arc::new(Target::default()), engine_id: EngineId::default(), @@ -225,6 +228,8 @@ pub struct NativeEngineInner { is_cross_compiling: bool, /// The linker to use. linker: Linker, + /// List of libraries loaded by this engine. + libraries: Vec, } impl NativeEngineInner { @@ -280,4 +285,8 @@ impl NativeEngineInner { pub(crate) fn linker(&self) -> Linker { self.linker } + + pub(crate) fn add_library(&mut self, library: Library) { + self.libraries.push(library); + } } From 83caef68fc86b18330a97821c5713e48dc463550 Mon Sep 17 00:00:00 2001 From: Nick Lewycky Date: Tue, 13 Oct 2020 14:33:16 -0700 Subject: [PATCH 16/18] cargo fmt --- lib/engine-native/src/artifact.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/engine-native/src/artifact.rs b/lib/engine-native/src/artifact.rs index 7145a636576..42ba8a6f9e0 100644 --- a/lib/engine-native/src/artifact.rs +++ b/lib/engine-native/src/artifact.rs @@ -19,9 +19,7 @@ use wasmer_compiler::{CompileError, Features, OperatingSystem, Symbol, SymbolReg use wasmer_compiler::{ CompileModuleInfo, FunctionBodyData, ModuleEnvironment, ModuleTranslationState, }; -use wasmer_engine::{ - Artifact, DeserializeError, InstantiationError, SerializeError, -}; +use wasmer_engine::{Artifact, DeserializeError, InstantiationError, SerializeError}; #[cfg(feature = "compiler")] use wasmer_engine::{Engine, Tunables}; #[cfg(feature = "compiler")] From 90efd5333a43f4541ffb14ed822415ef02c3019d Mon Sep 17 00:00:00 2001 From: Nick Lewycky Date: Tue, 13 Oct 2020 14:37:47 -0700 Subject: [PATCH 17/18] Remove accidentally added debug code. --- lib/engine-native/src/artifact.rs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/lib/engine-native/src/artifact.rs b/lib/engine-native/src/artifact.rs index 42ba8a6f9e0..54e4fb5d6b4 100644 --- a/lib/engine-native/src/artifact.rs +++ b/lib/engine-native/src/artifact.rs @@ -46,12 +46,6 @@ pub struct NativeArtifact { signatures: BoxedSlice, } -impl Drop for NativeArtifact { - fn drop(&mut self) { - println!("Dropping NativeArtifact!"); - } -} - fn to_compile_error(err: impl Error) -> CompileError { CompileError::Codegen(format!("{}", err)) } From ff15be3a7528b16dba804ae7b4b6a73bb62d2886 Mon Sep 17 00:00:00 2001 From: Nick Lewycky Date: Thu, 15 Oct 2020 17:05:21 -0700 Subject: [PATCH 18/18] Un-ignore these fixed tests. --- tests/compilers/traps.rs | 1 - tests/ignores.txt | 2 -- 2 files changed, 3 deletions(-) diff --git a/tests/compilers/traps.rs b/tests/compilers/traps.rs index 7b1944ba369..f9798399026 100644 --- a/tests/compilers/traps.rs +++ b/tests/compilers/traps.rs @@ -495,7 +495,6 @@ RuntimeError: unreachable } #[test] -#[cfg_attr(feature = "test-native", ignore)] fn present_after_module_drop() -> Result<()> { let store = get_store(); let module = Module::new(&store, r#"(func (export "foo") unreachable)"#)?; diff --git a/tests/ignores.txt b/tests/ignores.txt index f8b426a02a8..6fcd597d8ed 100644 --- a/tests/ignores.txt +++ b/tests/ignores.txt @@ -24,8 +24,6 @@ llvm::spec::skip_stack_guard_page on darwin # TODO(https://github.com/wasmerio/wasmer/issues/1727): Traps in native engine cranelift::spec::linking on native -# TODO(https://github.com/wasmerio/wasmer/pull/1710): Library is dropped too soon. -llvm::spec::linking on native # https://github.com/wasmerio/wasmer/issues/1722 llvm::spec::skip_stack_guard_page on native