Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix probestack #2548

Merged
merged 15 commits into from
Sep 2, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 35 additions & 6 deletions lib/compiler-cranelift/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,15 @@ use wasmer_compiler::{
FunctionBodyData, MiddlewareBinaryReader, ModuleMiddleware, ModuleMiddlewareChain,
SectionIndex,
};
#[cfg(target_arch = "x86_64")]
use wasmer_compiler::{
CustomSection, CustomSectionProtection, Relocation, RelocationKind, RelocationTarget,
SectionBody,
};
use wasmer_types::entity::{EntityRef, PrimaryMap};
use wasmer_types::{FunctionIndex, LocalFunctionIndex, SignatureIndex};
#[cfg(target_arch = "x86_64")]
use wasmer_vm::libcalls::LibCall;

/// A compiler that compiles a WebAssembly module with Cranelift, translating the Wasm to Cranelift IR,
/// optimizing it and then translating to assembly.
Expand Down Expand Up @@ -102,6 +109,24 @@ impl Compiler for CraneliftCompiler {
}
};

let mut custom_sections = PrimaryMap::new();

#[cfg(target_arch = "x86_64")]
let probestack_trampoline = CustomSection {
protection: CustomSectionProtection::ReadExecute,
bytes: SectionBody::new_with_vec(vec![0xff, 0x25, 0x00, 0x00, 0x00, 0x00]),
ptitSeb marked this conversation as resolved.
Show resolved Hide resolved
relocations: vec![Relocation {
kind: RelocationKind::Abs8,
reloc_target: RelocationTarget::LibCall(LibCall::Probestack),
offset: 6,
ptitSeb marked this conversation as resolved.
Show resolved Hide resolved
addend: 0,
}],
};
#[cfg(target_arch = "x86_64")]
custom_sections.push(probestack_trampoline);
#[cfg(target_arch = "x86_64")]
let probestack_trampoline_relocation_target = SectionIndex::new(custom_sections.len() - 1);

let functions = function_body_inputs
.iter()
.collect::<Vec<(LocalFunctionIndex, &FunctionBodyData<'_>)>>()
Expand Down Expand Up @@ -138,7 +163,12 @@ impl Compiler for CraneliftCompiler {
)?;

let mut code_buf: Vec<u8> = Vec::new();
let mut reloc_sink = RelocSink::new(&module, func_index);
let mut reloc_sink = RelocSink::new(
&module,
func_index,
#[cfg(target_arch = "x86_64")]
probestack_trampoline_relocation_target,
);
let mut trap_sink = TrapSink::new();
let mut stackmap_sink = binemit::NullStackMapSink {};
context
Expand Down Expand Up @@ -204,8 +234,7 @@ impl Compiler for CraneliftCompiler {
.collect::<PrimaryMap<LocalFunctionIndex, _>>();

#[cfg(feature = "unwind")]
let (custom_sections, dwarf) = {
let mut custom_sections = PrimaryMap::new();
let dwarf = {
let dwarf = if let Some((dwarf_frametable, _cie_id)) = dwarf_frametable {
let mut eh_frame = EhFrame(WriterRelocate::new(target.triple().endianness().ok()));
dwarf_frametable
Expand All @@ -216,14 +245,14 @@ impl Compiler for CraneliftCompiler {

let eh_frame_section = eh_frame.0.into_section();
custom_sections.push(eh_frame_section);
Some(Dwarf::new(SectionIndex::new(0)))
Some(Dwarf::new(SectionIndex::new(custom_sections.len() - 1)))
} else {
None
};
(custom_sections, dwarf)
dwarf
};
#[cfg(not(feature = "unwind"))]
let (custom_sections, dwarf) = (PrimaryMap::new(), None);
let dwarf = None;

// function call trampolines (only for local functions, by signature)
let function_call_trampolines = module
Expand Down
33 changes: 31 additions & 2 deletions lib/compiler-cranelift/src/sink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

use crate::translator::{irlibcall_to_libcall, irreloc_to_relocationkind};
use cranelift_codegen::binemit;
#[cfg(target_arch = "x86_64")]
use cranelift_codegen::ir::LibCall;
use cranelift_codegen::ir::{self, ExternalName};
use cranelift_entity::EntityRef as CraneliftEntityRef;
use wasmer_compiler::{JumpTable, Relocation, RelocationTarget, TrapInformation};
#[cfg(target_arch = "x86_64")]
use wasmer_compiler::{RelocationKind, SectionIndex};
use wasmer_types::entity::EntityRef;
use wasmer_types::{FunctionIndex, LocalFunctionIndex, ModuleInfo};
use wasmer_vm::TrapCode;
Expand All @@ -18,6 +22,10 @@ pub(crate) struct RelocSink<'a> {

/// Relocations recorded for the function.
pub func_relocs: Vec<Relocation>,

/// The section where the probestack trampoline call is located
#[cfg(target_arch = "x86_64")]
pub probestack_trampoline_relocation_target: SectionIndex,
}

impl<'a> binemit::RelocSink for RelocSink<'a> {
Expand All @@ -37,7 +45,22 @@ impl<'a> binemit::RelocSink for RelocSink<'a> {
.expect("The provided function should be local"),
)
} else if let ExternalName::LibCall(libcall) = *name {
RelocationTarget::LibCall(irlibcall_to_libcall(libcall))
match libcall {
#[cfg(target_arch = "x86_64")]
LibCall::Probestack => {
self.func_relocs.push(Relocation {
kind: RelocationKind::X86CallPCRel4,
reloc_target: RelocationTarget::CustomSection(
self.probestack_trampoline_relocation_target,
),
offset: offset,
addend: addend,
});
// Skip the default path
return;
}
_ => RelocationTarget::LibCall(irlibcall_to_libcall(libcall)),
}
} else {
panic!("unrecognized external name")
};
Expand Down Expand Up @@ -74,14 +97,20 @@ impl<'a> binemit::RelocSink for RelocSink<'a> {

impl<'a> RelocSink<'a> {
/// Return a new `RelocSink` instance.
pub fn new(module: &'a ModuleInfo, func_index: FunctionIndex) -> Self {
pub fn new(
module: &'a ModuleInfo,
func_index: FunctionIndex,
#[cfg(target_arch = "x86_64")] probestack_trampoline_relocation_target: SectionIndex,
) -> Self {
let local_func_index = module
.local_func_index(func_index)
.expect("The provided function should be local");
Self {
module,
local_func_index,
func_relocs: Vec::new(),
#[cfg(target_arch = "x86_64")]
probestack_trampoline_relocation_target,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/compiler-cranelift/src/translator/func_translator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ fn parse_local_decls<FE: FuncEnvironment + ?Sized>(

/// Declare `count` local variables of the same type, starting from `next_local`.
///
/// Fail of too many locals are declared in the function, or if the type is not valid for a local.
/// Fail if the type is not valid for a local.
fn declare_locals<FE: FuncEnvironment + ?Sized>(
builder: &mut FunctionBuilder,
count: u32,
Expand Down
3 changes: 2 additions & 1 deletion lib/vm/src/trap/traphandlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,8 @@ impl<'a> CallThreadState<'a> {
}

// If this fault wasn't in wasm code, then it's not our problem
if unsafe { !IS_WASM_PC(pc as _) } {
// except if it's a StackOverflow (see below)
if unsafe { !IS_WASM_PC(pc as _) } && signal_trap != Some(TrapCode::StackOverflow) {
return ptr::null();
}

Expand Down
6 changes: 0 additions & 6 deletions tests/ignores.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@ windows+cranelift spec::memory_grow
# LLVM/Universal doesn't work in macOS M1. Skip all tests
llvm+universal+macos+aarch64 *

# TODO: We need to fix this. The issue is caused by libunwind overflowing
# the stack while creating the stacktrace.
# https://github.com/rust-lang/backtrace-rs/issues/356
cranelift spec::skip_stack_guard_page
llvm spec::skip_stack_guard_page

# TODO(https://github.com/wasmerio/wasmer/issues/1727): Traps in dylib engine
cranelift+dylib spec::linking
cranelift+dylib spec::bulk
Expand Down