Skip to content

Commit

Permalink
Use trampolines for all libcalls in engine-universal and engine-dylib
Browse files Browse the repository at this point in the history
In both of these engines, the compiled code may be loaded in memory far
from the Wasmer runtime which means that libcalls may not be reachable
through the normal relocation types. Instead a trampoline is needed to
allow reaching any address in the 64-bit address space.

In the case of engine-dylib, this is even worse since the symbols are
not exported by the executable without some special linker flags. The
solution here is to manually patch in the addresses at load time into
a data table of function pointers.
  • Loading branch information
Amanieu committed Jan 19, 2022
1 parent ae66e76 commit 6e40e90
Show file tree
Hide file tree
Showing 29 changed files with 430 additions and 201 deletions.
5 changes: 0 additions & 5 deletions .cargo/config.toml

This file was deleted.

1 change: 1 addition & 0 deletions .github/workflows/test-sys.yaml
Expand Up @@ -166,6 +166,7 @@ jobs:
shell: bash
- name: Setup Rust target
run: |
mkdir -p .cargo
cat << EOF > .cargo/config.toml
[build]
target = "${{ matrix.target }}"
Expand Down
40 changes: 37 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions Makefile
Expand Up @@ -211,14 +211,13 @@ compilers_engines :=
ifeq ($(ENABLE_CRANELIFT), 1)
compilers_engines += cranelift-universal

ifneq (, $(filter 1, $(IS_DARWIN) $(IS_LINUX)))
ifneq (, $(filter 1, $(IS_WINDOWS) $(IS_DARWIN) $(IS_LINUX)))
ifeq ($(IS_AMD64), 1)
ifneq ($(LIBC), musl)
compilers_engines += cranelift-dylib
endif
else ifeq ($(IS_AARCH64), 1)
# The object crate doesn't support yet Darwin + Aarch64 relocations
ifneq ($(IS_DARWIN), 1)
ifneq ($(LIBC), musl)
compilers_engines += cranelift-dylib
endif
endif
Expand All @@ -230,7 +229,7 @@ endif
##

ifeq ($(ENABLE_LLVM), 1)
ifneq (, $(filter 1, $(IS_DARWIN) $(IS_LINUX)))
ifneq (, $(filter 1, $(IS_WINDOWS) $(IS_DARWIN) $(IS_LINUX)))
ifeq ($(IS_AMD64), 1)
compilers_engines += llvm-universal
compilers_engines += llvm-dylib
Expand All @@ -246,7 +245,7 @@ endif
##

ifeq ($(ENABLE_SINGLEPASS), 1)
ifneq (, $(filter 1, $(IS_DARWIN) $(IS_LINUX)))
ifneq (, $(filter 1, $(IS_WINDOWS) $(IS_DARWIN) $(IS_LINUX)))
ifeq ($(IS_AMD64), 1)
compilers_engines += singlepass-universal
endif
Expand Down
1 change: 1 addition & 0 deletions deny.toml
Expand Up @@ -179,6 +179,7 @@ skip = [
{ name = "semver-parser", version = "=0.7.0" },
{ name = "rustc_version", version = "=0.2.3" },
{ name = "itoa", version = "=0.4.8" },
{ name = "object", version = "=0.27.1" },
]
# Similarly to `skip` allows you to skip certain crates during duplicate
# detection. Unlike skip, it also includes the entire tree of transitive
Expand Down
1 change: 0 additions & 1 deletion lib/compiler-cranelift/src/compiler.rs
Expand Up @@ -295,7 +295,6 @@ impl Compiler for CraneliftCompiler {
function_call_trampolines,
dynamic_function_trampolines,
dwarf,
None,
))
}
}
6 changes: 6 additions & 0 deletions lib/compiler-cranelift/src/config.rs
Expand Up @@ -135,6 +135,12 @@ impl Cranelift {
flags.enable("is_pic").expect("should be a valid flag");
}

// We set up libcall trampolines in engine-dylib and engine-universal.
// These trampolines are always reachable through short jumps.
flags
.enable("use_colocated_libcalls")
.expect("should be a valid flag");

// Invert cranelift's default-on verification to instead default off.
let enable_verifier = if self.enable_verifier {
"true"
Expand Down
4 changes: 1 addition & 3 deletions lib/compiler-cranelift/src/func_environ.rs
Expand Up @@ -1132,9 +1132,7 @@ impl<'module_environment> BaseFuncEnvironment for FuncEnvironment<'module_enviro
Ok(func.import_function(ir::ExtFuncData {
name,
signature,
// We currently allocate all code segments independently, so nothing
// is colocated.
colocated: false,
colocated: true,
}))
}

Expand Down
1 change: 1 addition & 0 deletions lib/compiler-cranelift/src/translator/translation_utils.rs
Expand Up @@ -90,6 +90,7 @@ pub fn irreloc_to_relocationkind(reloc: Reloc) -> RelocationKind {
Reloc::X86CallPCRel4 => RelocationKind::X86CallPCRel4,
Reloc::X86CallPLTRel4 => RelocationKind::X86CallPLTRel4,
Reloc::X86GOTPCRel4 => RelocationKind::X86GOTPCRel4,
Reloc::Arm64Call => RelocationKind::Arm64Call,
_ => panic!("The relocation {} is not yet supported.", reloc),
}
}
Expand Down
6 changes: 4 additions & 2 deletions lib/compiler-llvm/Cargo.toml
Expand Up @@ -12,12 +12,14 @@ readme = "README.md"
edition = "2018"

[dependencies]
wasmer-compiler = { path = "../compiler", version = "2.1.1", features = ["translator"] }
wasmer-compiler = { path = "../compiler", version = "2.1.1", features = [
"translator",
] }
wasmer-vm = { path = "../vm", version = "2.1.1" }
wasmer-types = { path = "../types", version = "2.1.1" }
target-lexicon = { version = "0.12.2", default-features = false }
smallvec = "1.6"
object = { version = "0.27", default-features = false, features = ["read"] }
object = { version = "0.28.3", default-features = false, features = ["read"] }
libc = { version = "^0.2", default-features = false }
byteorder = "1"
itertools = "0.10"
Expand Down
39 changes: 3 additions & 36 deletions lib/compiler-llvm/src/compiler.rs
Expand Up @@ -12,10 +12,9 @@ use rayon::iter::ParallelBridge;
use rayon::prelude::{IntoParallelIterator, IntoParallelRefIterator, ParallelIterator};
use std::sync::Arc;
use wasmer_compiler::{
Architecture, Compilation, CompileError, CompileModuleInfo, Compiler, CustomSection,
CustomSectionProtection, Dwarf, FunctionBodyData, ModuleMiddleware, ModuleTranslationState,
RelocationTarget, SectionBody, SectionIndex, Symbol, SymbolRegistry, Target,
TrampolinesSection,
Compilation, CompileError, CompileModuleInfo, Compiler, CustomSection, CustomSectionProtection,
Dwarf, FunctionBodyData, ModuleMiddleware, ModuleTranslationState, RelocationTarget,
SectionBody, SectionIndex, Symbol, SymbolRegistry, Target,
};
use wasmer_types::entity::{EntityRef, PrimaryMap};
use wasmer_types::{FunctionIndex, LocalFunctionIndex, SignatureIndex};
Expand Down Expand Up @@ -305,37 +304,6 @@ impl Compiler for LLVMCompiler {
})
.collect::<PrimaryMap<LocalFunctionIndex, _>>();

let trampolines = match target.triple().architecture {
Architecture::Aarch64(_) => {
let nj = 16;
// We create a jump to an absolute 64bits address
// using x17 as a scratch register, SystemV declare both x16 and x17 as Intra-Procedural scratch register
// but Apple ask to just not use x16
// LDR x17, #8 51 00 00 58
// BR x17 20 02 1f d6
// JMPADDR 00 00 00 00 00 00 00 00
let onejump = [
0x51, 0x00, 0x00, 0x58, 0x20, 0x02, 0x1f, 0xd6, 0, 0, 0, 0, 0, 0, 0, 0,
];
let trampolines = Some(TrampolinesSection::new(
SectionIndex::from_u32(module_custom_sections.len() as u32),
nj,
onejump.len(),
));
let mut alljmps = vec![];
for _ in 0..nj {
alljmps.extend(onejump.iter().copied());
}
module_custom_sections.push(CustomSection {
protection: CustomSectionProtection::ReadExecute,
bytes: SectionBody::new_with_vec(alljmps),
relocations: vec![],
});
trampolines
}
_ => None,
};

let dwarf = if !frame_section_bytes.is_empty() {
let dwarf = Some(Dwarf::new(SectionIndex::from_u32(
module_custom_sections.len() as u32,
Expand Down Expand Up @@ -400,7 +368,6 @@ impl Compiler for LLVMCompiler {
function_call_trampolines,
dynamic_function_trampolines,
dwarf,
trampolines,
))
}
}
1 change: 0 additions & 1 deletion lib/compiler-singlepass/src/compiler.rs
Expand Up @@ -187,7 +187,6 @@ impl Compiler for SinglepassCompiler {
function_call_trampolines,
dynamic_function_trampolines,
None,
None,
))
}
}
Expand Down
37 changes: 0 additions & 37 deletions lib/compiler/src/function.rs
Expand Up @@ -109,33 +109,6 @@ impl Dwarf {
}
}

/// Trampolines section used by ARM short jump (26bits)
#[cfg_attr(feature = "enable-serde", derive(Deserialize, Serialize))]
#[cfg_attr(
feature = "enable-rkyv",
derive(RkyvSerialize, RkyvDeserialize, Archive)
)]
#[derive(Debug, PartialEq, Eq, Clone, MemoryUsage)]
pub struct TrampolinesSection {
/// SectionIndex for the actual Trampolines code
pub section_index: SectionIndex,
/// Number of jump slots in the section
pub slots: usize,
/// Slot size
pub size: usize,
}

impl TrampolinesSection {
/// Creates a `Trampolines` struct with the indice for its section, and number of slots and size of slot
pub fn new(section_index: SectionIndex, slots: usize, size: usize) -> Self {
Self {
section_index,
slots,
size,
}
}
}

/// The result of compiling a WebAssembly module's functions.
#[cfg_attr(feature = "enable-serde", derive(Deserialize, Serialize))]
#[derive(Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -182,9 +155,6 @@ pub struct Compilation {

/// Section ids corresponding to the Dwarf debug info
debug: Option<Dwarf>,

/// Trampolines for the arch that needs it
trampolines: Option<TrampolinesSection>,
}

impl Compilation {
Expand All @@ -195,15 +165,13 @@ impl Compilation {
function_call_trampolines: PrimaryMap<SignatureIndex, FunctionBody>,
dynamic_function_trampolines: PrimaryMap<FunctionIndex, FunctionBody>,
debug: Option<Dwarf>,
trampolines: Option<TrampolinesSection>,
) -> Self {
Self {
functions,
custom_sections,
function_call_trampolines,
dynamic_function_trampolines,
debug,
trampolines,
}
}

Expand Down Expand Up @@ -281,11 +249,6 @@ impl Compilation {
pub fn get_debug(&self) -> Option<Dwarf> {
self.debug.clone()
}

/// Returns the Trampilines info.
pub fn get_trampolines(&self) -> Option<TrampolinesSection> {
self.trampolines.clone()
}
}

impl<'a> IntoIterator for &'a Compilation {
Expand Down
2 changes: 1 addition & 1 deletion lib/compiler/src/lib.rs
Expand Up @@ -74,7 +74,7 @@ pub use crate::error::{
};
pub use crate::function::{
Compilation, CompiledFunction, CompiledFunctionFrameInfo, CustomSections, Dwarf, FunctionBody,
Functions, TrampolinesSection,
Functions,
};
pub use crate::jump_table::{JumpTable, JumpTableOffsets};
pub use crate::module::CompileModuleInfo;
Expand Down
2 changes: 2 additions & 0 deletions lib/engine-dylib/Cargo.toml
Expand Up @@ -26,6 +26,8 @@ which = "4.0"
rkyv = "0.7.20"
loupe = "0.1"
enumset = "1.0"
enum-iterator = "0.7.0"
object = { version = "0.28.3", default-features = false, features = ["write"] }

[features]
# Enable the `compiler` feature if you want the engine to compile
Expand Down

0 comments on commit 6e40e90

Please sign in to comment.