mirror of
https://github.com/mii443/wasmer.git
synced 2025-12-07 05:08:19 +00:00
Make PIC optional for compilers
This commit is contained in:
@@ -39,28 +39,28 @@ impl Default for Store {
|
|||||||
// sure this function doesn't emit a compile error even if
|
// sure this function doesn't emit a compile error even if
|
||||||
// more than one compiler is enabled.
|
// more than one compiler is enabled.
|
||||||
#[allow(unreachable_code)]
|
#[allow(unreachable_code)]
|
||||||
fn get_config() -> Arc<dyn CompilerConfig + Send + Sync> {
|
fn get_config() -> Box<dyn CompilerConfig + Send + Sync> {
|
||||||
#[cfg(feature = "cranelift")]
|
#[cfg(feature = "cranelift")]
|
||||||
return Arc::new(wasmer_compiler_cranelift::CraneliftConfig::default());
|
return Box::new(wasmer_compiler_cranelift::CraneliftConfig::default());
|
||||||
|
|
||||||
#[cfg(feature = "llvm")]
|
#[cfg(feature = "llvm")]
|
||||||
return Arc::new(wasmer_compiler_llvm::LLVMConfig::default());
|
return Box::new(wasmer_compiler_llvm::LLVMConfig::default());
|
||||||
|
|
||||||
#[cfg(feature = "singlepass")]
|
#[cfg(feature = "singlepass")]
|
||||||
return Arc::new(wasmer_compiler_singlepass::SinglepassConfig::default());
|
return Box::new(wasmer_compiler_singlepass::SinglepassConfig::default());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(unreachable_code)]
|
#[allow(unreachable_code)]
|
||||||
fn get_engine(
|
fn get_engine(
|
||||||
config: Arc<dyn CompilerConfig + Send + Sync>,
|
config: Box<dyn CompilerConfig + Send + Sync>,
|
||||||
) -> Arc<dyn Engine + Send + Sync> {
|
) -> Arc<dyn Engine + Send + Sync> {
|
||||||
let tunables = Tunables::for_target(config.target().triple());
|
let tunables = Tunables::for_target(config.target().triple());
|
||||||
|
|
||||||
#[cfg(feature = "jit")]
|
#[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")]
|
#[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();
|
let config = get_config();
|
||||||
|
|||||||
@@ -37,6 +37,8 @@ pub struct CraneliftConfig {
|
|||||||
/// The verifier assures that the generated Cranelift IR is valid.
|
/// The verifier assures that the generated Cranelift IR is valid.
|
||||||
pub enable_verifier: bool,
|
pub enable_verifier: bool,
|
||||||
|
|
||||||
|
enable_pic: bool,
|
||||||
|
|
||||||
/// The optimization levels when optimizing the IR.
|
/// The optimization levels when optimizing the IR.
|
||||||
pub opt_level: OptLevel,
|
pub opt_level: OptLevel,
|
||||||
|
|
||||||
@@ -52,6 +54,7 @@ impl CraneliftConfig {
|
|||||||
enable_nan_canonicalization: false,
|
enable_nan_canonicalization: false,
|
||||||
enable_verifier: false,
|
enable_verifier: false,
|
||||||
opt_level: OptLevel::Speed,
|
opt_level: OptLevel::Speed,
|
||||||
|
enable_pic: false,
|
||||||
features,
|
features,
|
||||||
target,
|
target,
|
||||||
}
|
}
|
||||||
@@ -124,7 +127,9 @@ impl CraneliftConfig {
|
|||||||
.enable("avoid_div_traps")
|
.enable("avoid_div_traps")
|
||||||
.expect("should be valid flag");
|
.expect("should be valid flag");
|
||||||
|
|
||||||
|
if self.enable_pic {
|
||||||
flags.enable("is_pic").expect("should be a valid flag");
|
flags.enable("is_pic").expect("should be a valid flag");
|
||||||
|
}
|
||||||
|
|
||||||
// Invert cranelift's default-on verification to instead default off.
|
// Invert cranelift's default-on verification to instead default off.
|
||||||
let enable_verifier = if self.enable_verifier {
|
let enable_verifier = if self.enable_verifier {
|
||||||
@@ -174,6 +179,10 @@ impl CompilerConfig for CraneliftConfig {
|
|||||||
&self.features
|
&self.features
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn enable_pic(&mut self) {
|
||||||
|
self.enable_pic = true;
|
||||||
|
}
|
||||||
|
|
||||||
/// Gets the target that we will use for compiling
|
/// Gets the target that we will use for compiling
|
||||||
/// the WebAssembly module
|
/// the WebAssembly module
|
||||||
fn target(&self) -> &Target {
|
fn target(&self) -> &Target {
|
||||||
|
|||||||
@@ -158,6 +158,10 @@ impl CompilerConfig for LLVMConfig {
|
|||||||
&self.features
|
&self.features
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn enable_pic(&mut self) {
|
||||||
|
unimplemented!("PIC is not yet implemented in LLVM");
|
||||||
|
}
|
||||||
|
|
||||||
/// Gets the target that we will use for compiling
|
/// Gets the target that we will use for compiling
|
||||||
/// the WebAssembly module
|
/// the WebAssembly module
|
||||||
fn target(&self) -> &Target {
|
fn target(&self) -> &Target {
|
||||||
|
|||||||
@@ -24,6 +24,13 @@ pub trait CompilerConfig {
|
|||||||
/// Gets the WebAssembly features
|
/// Gets the WebAssembly features
|
||||||
fn features(&self) -> &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
|
/// Gets the target that we will use for compiling
|
||||||
/// the WebAssembly module
|
/// the WebAssembly module
|
||||||
fn target(&self) -> &Target;
|
fn target(&self) -> &Target;
|
||||||
|
|||||||
@@ -28,13 +28,10 @@ impl JITEngine {
|
|||||||
|
|
||||||
/// Create a new `JITEngine` with the given config
|
/// Create a new `JITEngine` with the given config
|
||||||
#[cfg(feature = "compiler")]
|
#[cfg(feature = "compiler")]
|
||||||
pub fn new<C: CompilerConfig>(
|
pub fn new(
|
||||||
config: &C,
|
mut config: Box<dyn CompilerConfig>,
|
||||||
tunables: impl Tunables + 'static + Send + Sync,
|
tunables: impl Tunables + 'static + Send + Sync,
|
||||||
) -> Self
|
) -> Self {
|
||||||
where
|
|
||||||
C: ?Sized,
|
|
||||||
{
|
|
||||||
let compiler = config.compiler();
|
let compiler = config.compiler();
|
||||||
Self {
|
Self {
|
||||||
inner: Arc::new(Mutex::new(JITEngineInner {
|
inner: Arc::new(Mutex::new(JITEngineInner {
|
||||||
|
|||||||
@@ -37,20 +37,26 @@ fn apply_relocation(
|
|||||||
};
|
};
|
||||||
|
|
||||||
match r.kind {
|
match r.kind {
|
||||||
|
#[cfg(target_pointer_width = "64")]
|
||||||
RelocationKind::Abs8 => unsafe {
|
RelocationKind::Abs8 => unsafe {
|
||||||
let (reloc_address, reloc_delta) = r.for_address(body, target_func_address as u64);
|
let (reloc_address, reloc_delta) = r.for_address(body, target_func_address as u64);
|
||||||
write_unaligned(reloc_address as *mut u64, reloc_delta);
|
write_unaligned(reloc_address as *mut u64, reloc_delta);
|
||||||
},
|
},
|
||||||
|
#[cfg(target_pointer_width = "32")]
|
||||||
RelocationKind::X86PCRel4 => unsafe {
|
RelocationKind::X86PCRel4 => unsafe {
|
||||||
let (reloc_address, reloc_delta) = r.for_address(body, target_func_address as u64);
|
let (reloc_address, reloc_delta) = r.for_address(body, target_func_address as u64);
|
||||||
write_unaligned(reloc_address as *mut u32, reloc_delta as _);
|
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);
|
let (reloc_address, reloc_delta) = r.for_address(body, target_func_address as u64);
|
||||||
write_unaligned(reloc_address as *mut u32, reloc_delta as _);
|
write_unaligned(reloc_address as *mut u32, reloc_delta as _);
|
||||||
},
|
},
|
||||||
RelocationKind::X86PCRelRodata4 => {}
|
RelocationKind::X86PCRelRodata4 => {}
|
||||||
_ => panic!("Relocation kind unsupported in the current architecture"),
|
kind => panic!(
|
||||||
|
"Relocation kind unsupported in the current architecture {}",
|
||||||
|
kind
|
||||||
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -40,13 +40,11 @@ impl NativeEngine {
|
|||||||
|
|
||||||
/// Create a new `NativeEngine` with the given config
|
/// Create a new `NativeEngine` with the given config
|
||||||
#[cfg(feature = "compiler")]
|
#[cfg(feature = "compiler")]
|
||||||
pub fn new<C: CompilerConfig>(
|
pub fn new(
|
||||||
config: &C,
|
mut config: Box<dyn CompilerConfig>,
|
||||||
tunables: impl Tunables + 'static + Send + Sync,
|
tunables: impl Tunables + 'static + Send + Sync,
|
||||||
) -> Self
|
) -> Self {
|
||||||
where
|
config.enable_pic();
|
||||||
C: ?Sized,
|
|
||||||
{
|
|
||||||
let compiler = config.compiler();
|
let compiler = config.compiler();
|
||||||
Self {
|
Self {
|
||||||
inner: Arc::new(Mutex::new(NativeEngineInner {
|
inner: Arc::new(Mutex::new(NativeEngineInner {
|
||||||
|
|||||||
@@ -171,13 +171,12 @@ impl StoreOptions {
|
|||||||
let engine_type = self.get_engine()?;
|
let engine_type = self.get_engine()?;
|
||||||
let engine: Arc<dyn Engine + Send + Sync> = match engine_type {
|
let engine: Arc<dyn Engine + Send + Sync> = match engine_type {
|
||||||
#[cfg(feature = "jit")]
|
#[cfg(feature = "jit")]
|
||||||
EngineOptions::JIT => Arc::new(wasmer_engine_jit::JITEngine::new(
|
EngineOptions::JIT => {
|
||||||
&*compiler_config,
|
Arc::new(wasmer_engine_jit::JITEngine::new(compiler_config, tunables))
|
||||||
tunables,
|
}
|
||||||
)),
|
|
||||||
#[cfg(feature = "native")]
|
#[cfg(feature = "native")]
|
||||||
EngineOptions::Native => Arc::new(wasmer_engine_native::NativeEngine::new(
|
EngineOptions::Native => Arc::new(wasmer_engine_native::NativeEngine::new(
|
||||||
&*compiler_config,
|
compiler_config,
|
||||||
tunables,
|
tunables,
|
||||||
)),
|
)),
|
||||||
#[cfg(not(all(feature = "jit", feature = "native",)))]
|
#[cfg(not(all(feature = "jit", feature = "native",)))]
|
||||||
|
|||||||
@@ -42,5 +42,5 @@ pub fn get_compiler_config_from_str(
|
|||||||
pub fn get_default_store() -> Store {
|
pub fn get_default_store() -> Store {
|
||||||
let compiler_config = get_compiler_config_from_str("cranelift", false, Features::default());
|
let compiler_config = get_compiler_config_from_str("cranelift", false, Features::default());
|
||||||
let tunables = Tunables::for_target(compiler_config.target().triple());
|
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)))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,8 +42,8 @@ fn run_wast(wast_path: &str, compiler: &str) -> anyhow::Result<()> {
|
|||||||
let compiler_config =
|
let compiler_config =
|
||||||
get_compiler_config_from_str(compiler, try_nan_canonicalization, features);
|
get_compiler_config_from_str(compiler, try_nan_canonicalization, features);
|
||||||
let tunables = Tunables::for_target(compiler_config.target().triple());
|
let tunables = Tunables::for_target(compiler_config.target().triple());
|
||||||
let store = Store::new(Arc::new(JITEngine::new(&*compiler_config, tunables)));
|
let store = Store::new(Arc::new(JITEngine::new(compiler_config, tunables)));
|
||||||
// let mut native = NativeEngine::new(&*compiler_config, tunables);
|
// let mut native = NativeEngine::new(compiler_config, tunables);
|
||||||
// native.set_deterministic_prefixer(native_prefixer);
|
// native.set_deterministic_prefixer(native_prefixer);
|
||||||
// let store = Store::new(Arc::new(native));
|
// let store = Store::new(Arc::new(native));
|
||||||
let mut wast = Wast::new_with_spectest(store);
|
let mut wast = Wast::new_with_spectest(store);
|
||||||
|
|||||||
Reference in New Issue
Block a user