diff --git a/lib/api/src/store.rs b/lib/api/src/store.rs index 452060146..5dbc9a704 100644 --- a/lib/api/src/store.rs +++ b/lib/api/src/store.rs @@ -39,28 +39,28 @@ impl Default for Store { // sure this function doesn't emit a compile error even if // more than one compiler is enabled. #[allow(unreachable_code)] - fn get_config() -> Arc { + fn get_config() -> Box { #[cfg(feature = "cranelift")] - return Arc::new(wasmer_compiler_cranelift::CraneliftConfig::default()); + return Box::new(wasmer_compiler_cranelift::CraneliftConfig::default()); #[cfg(feature = "llvm")] - return Arc::new(wasmer_compiler_llvm::LLVMConfig::default()); + return Box::new(wasmer_compiler_llvm::LLVMConfig::default()); #[cfg(feature = "singlepass")] - return Arc::new(wasmer_compiler_singlepass::SinglepassConfig::default()); + return Box::new(wasmer_compiler_singlepass::SinglepassConfig::default()); } #[allow(unreachable_code)] fn get_engine( - config: Arc, + config: Box, ) -> Arc { let tunables = Tunables::for_target(config.target().triple()); #[cfg(feature = "jit")] - return Arc::new(wasmer_engine_jit::JITEngine::new(&*config, tunables)); + return Arc::new(wasmer_engine_jit::JITEngine::new(config, tunables)); #[cfg(feature = "native")] - return Arc::new(wasmer_engine_native::NativeEngine::new(&*config, tunables)); + return Arc::new(wasmer_engine_native::NativeEngine::new(config, tunables)); } let config = get_config(); diff --git a/lib/compiler-cranelift/src/config.rs b/lib/compiler-cranelift/src/config.rs index 51cadb78b..f0eed6a6f 100644 --- a/lib/compiler-cranelift/src/config.rs +++ b/lib/compiler-cranelift/src/config.rs @@ -37,6 +37,8 @@ pub struct CraneliftConfig { /// The verifier assures that the generated Cranelift IR is valid. pub enable_verifier: bool, + enable_pic: bool, + /// The optimization levels when optimizing the IR. pub opt_level: OptLevel, @@ -52,6 +54,7 @@ impl CraneliftConfig { enable_nan_canonicalization: false, enable_verifier: false, opt_level: OptLevel::Speed, + enable_pic: false, features, target, } @@ -124,7 +127,9 @@ impl CraneliftConfig { .enable("avoid_div_traps") .expect("should be valid flag"); - flags.enable("is_pic").expect("should be a valid flag"); + if self.enable_pic { + flags.enable("is_pic").expect("should be a valid flag"); + } // Invert cranelift's default-on verification to instead default off. let enable_verifier = if self.enable_verifier { @@ -174,6 +179,10 @@ impl CompilerConfig for CraneliftConfig { &self.features } + fn enable_pic(&mut self) { + self.enable_pic = true; + } + /// Gets the target that we will use for compiling /// the WebAssembly module fn target(&self) -> &Target { diff --git a/lib/compiler-llvm/src/config.rs b/lib/compiler-llvm/src/config.rs index 581902aba..f0c6e7b4b 100644 --- a/lib/compiler-llvm/src/config.rs +++ b/lib/compiler-llvm/src/config.rs @@ -158,6 +158,10 @@ impl CompilerConfig for LLVMConfig { &self.features } + fn enable_pic(&mut self) { + unimplemented!("PIC is not yet implemented in LLVM"); + } + /// Gets the target that we will use for compiling /// the WebAssembly module fn target(&self) -> &Target { diff --git a/lib/compiler/src/compiler.rs b/lib/compiler/src/compiler.rs index f99422635..fb2530cfe 100644 --- a/lib/compiler/src/compiler.rs +++ b/lib/compiler/src/compiler.rs @@ -24,6 +24,13 @@ pub trait CompilerConfig { /// Gets the WebAssembly features fn features(&self) -> &Features; + /// Should Position Independen Code (PIC) be enabled. + /// + /// This is required for shared object generation (Native Engine), + /// but will make the JIT Engine to fail, since PIC is not yet + /// supported in the JIT linking phase. + fn enable_pic(&mut self); + /// Gets the target that we will use for compiling /// the WebAssembly module fn target(&self) -> &Target; diff --git a/lib/engine-jit/src/engine.rs b/lib/engine-jit/src/engine.rs index ec068a22a..49b275f41 100644 --- a/lib/engine-jit/src/engine.rs +++ b/lib/engine-jit/src/engine.rs @@ -28,13 +28,10 @@ impl JITEngine { /// Create a new `JITEngine` with the given config #[cfg(feature = "compiler")] - pub fn new( - config: &C, + pub fn new( + mut config: Box, tunables: impl Tunables + 'static + Send + Sync, - ) -> Self - where - C: ?Sized, - { + ) -> Self { let compiler = config.compiler(); Self { inner: Arc::new(Mutex::new(JITEngineInner { diff --git a/lib/engine-jit/src/link.rs b/lib/engine-jit/src/link.rs index 141eb349a..8e717ddb3 100644 --- a/lib/engine-jit/src/link.rs +++ b/lib/engine-jit/src/link.rs @@ -37,20 +37,26 @@ fn apply_relocation( }; match r.kind { + #[cfg(target_pointer_width = "64")] RelocationKind::Abs8 => unsafe { let (reloc_address, reloc_delta) = r.for_address(body, target_func_address as u64); write_unaligned(reloc_address as *mut u64, reloc_delta); }, + #[cfg(target_pointer_width = "32")] RelocationKind::X86PCRel4 => unsafe { let (reloc_address, reloc_delta) = r.for_address(body, target_func_address as u64); write_unaligned(reloc_address as *mut u32, reloc_delta as _); }, - RelocationKind::X86CallPCRel4 | RelocationKind::X86CallPLTRel4 => unsafe { + #[cfg(target_pointer_width = "32")] + RelocationKind::X86CallPCRel4 => unsafe { let (reloc_address, reloc_delta) = r.for_address(body, target_func_address as u64); write_unaligned(reloc_address as *mut u32, reloc_delta as _); }, RelocationKind::X86PCRelRodata4 => {} - _ => panic!("Relocation kind unsupported in the current architecture"), + kind => panic!( + "Relocation kind unsupported in the current architecture {}", + kind + ), } } diff --git a/lib/engine-native/src/engine.rs b/lib/engine-native/src/engine.rs index e606f7397..de74ac122 100644 --- a/lib/engine-native/src/engine.rs +++ b/lib/engine-native/src/engine.rs @@ -40,13 +40,11 @@ impl NativeEngine { /// Create a new `NativeEngine` with the given config #[cfg(feature = "compiler")] - pub fn new( - config: &C, + pub fn new( + mut config: Box, tunables: impl Tunables + 'static + Send + Sync, - ) -> Self - where - C: ?Sized, - { + ) -> Self { + config.enable_pic(); let compiler = config.compiler(); Self { inner: Arc::new(Mutex::new(NativeEngineInner { diff --git a/src/store.rs b/src/store.rs index 1d2fad6b0..3a30961e3 100644 --- a/src/store.rs +++ b/src/store.rs @@ -171,13 +171,12 @@ impl StoreOptions { let engine_type = self.get_engine()?; let engine: Arc = match engine_type { #[cfg(feature = "jit")] - EngineOptions::JIT => Arc::new(wasmer_engine_jit::JITEngine::new( - &*compiler_config, - tunables, - )), + EngineOptions::JIT => { + Arc::new(wasmer_engine_jit::JITEngine::new(compiler_config, tunables)) + } #[cfg(feature = "native")] EngineOptions::Native => Arc::new(wasmer_engine_native::NativeEngine::new( - &*compiler_config, + compiler_config, tunables, )), #[cfg(not(all(feature = "jit", feature = "native",)))] diff --git a/tests/lib/test-utils/src/lib.rs b/tests/lib/test-utils/src/lib.rs index bc737e636..2e44183a7 100644 --- a/tests/lib/test-utils/src/lib.rs +++ b/tests/lib/test-utils/src/lib.rs @@ -42,5 +42,5 @@ pub fn get_compiler_config_from_str( pub fn get_default_store() -> Store { let compiler_config = get_compiler_config_from_str("cranelift", false, Features::default()); let tunables = Tunables::for_target(compiler_config.target().triple()); - Store::new(Arc::new(JITEngine::new(&*compiler_config, tunables))) + Store::new(Arc::new(JITEngine::new(compiler_config, tunables))) } diff --git a/tests/wast.rs b/tests/wast.rs index 6e04e9f5d..d8c55f6fa 100644 --- a/tests/wast.rs +++ b/tests/wast.rs @@ -42,8 +42,8 @@ fn run_wast(wast_path: &str, compiler: &str) -> anyhow::Result<()> { let compiler_config = get_compiler_config_from_str(compiler, try_nan_canonicalization, features); let tunables = Tunables::for_target(compiler_config.target().triple()); - let store = Store::new(Arc::new(JITEngine::new(&*compiler_config, tunables))); - // let mut native = NativeEngine::new(&*compiler_config, tunables); + let store = Store::new(Arc::new(JITEngine::new(compiler_config, tunables))); + // let mut native = NativeEngine::new(compiler_config, tunables); // native.set_deterministic_prefixer(native_prefixer); // let store = Store::new(Arc::new(native)); let mut wast = Wast::new_with_spectest(store);