Make PIC optional for compilers

This commit is contained in:
Syrus
2020-05-19 16:56:56 -07:00
parent 78aa07fe57
commit ae8dcfcb27
10 changed files with 50 additions and 30 deletions

View File

@@ -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<dyn CompilerConfig + Send + Sync> {
fn get_config() -> Box<dyn CompilerConfig + Send + Sync> {
#[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<dyn CompilerConfig + Send + Sync>,
config: Box<dyn CompilerConfig + Send + Sync>,
) -> Arc<dyn Engine + Send + Sync> {
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();

View File

@@ -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");
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 {

View File

@@ -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 {

View File

@@ -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;

View File

@@ -28,13 +28,10 @@ impl JITEngine {
/// Create a new `JITEngine` with the given config
#[cfg(feature = "compiler")]
pub fn new<C: CompilerConfig>(
config: &C,
pub fn new(
mut config: Box<dyn CompilerConfig>,
tunables: impl Tunables + 'static + Send + Sync,
) -> Self
where
C: ?Sized,
{
) -> Self {
let compiler = config.compiler();
Self {
inner: Arc::new(Mutex::new(JITEngineInner {

View File

@@ -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
),
}
}

View File

@@ -40,13 +40,11 @@ impl NativeEngine {
/// Create a new `NativeEngine` with the given config
#[cfg(feature = "compiler")]
pub fn new<C: CompilerConfig>(
config: &C,
pub fn new(
mut config: Box<dyn CompilerConfig>,
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 {

View File

@@ -171,13 +171,12 @@ impl StoreOptions {
let engine_type = self.get_engine()?;
let engine: Arc<dyn Engine + Send + Sync> = 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",)))]

View File

@@ -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)))
}

View File

@@ -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);