Wrap alternative stack functions properly.

This commit is contained in:
losfair 2019-06-26 20:44:51 +08:00
parent 73ca597d73
commit a792ac6a48
6 changed files with 107 additions and 56 deletions

View File

@ -1,5 +1,5 @@
.globl run_on_wasm_stack .globl run_on_alternative_stack
run_on_wasm_stack: run_on_alternative_stack:
# (stack_end, stack_begin) # (stack_end, stack_begin)
# We need to ensure 16-byte alignment here. # We need to ensure 16-byte alignment here.
pushq %r15 pushq %r15
@ -10,7 +10,7 @@ pushq %rbx
pushq %rbp pushq %rbp
movq %rsp, -16(%rdi) movq %rsp, -16(%rdi)
leaq run_on_wasm_stack.returning(%rip), %rax leaq run_on_alternative_stack.returning(%rip), %rax
movq %rax, -24(%rdi) movq %rax, -24(%rdi)
movq %rsi, %rsp movq %rsi, %rsp
@ -22,7 +22,7 @@ popq %r14
popq %r15 popq %r15
retq retq
run_on_wasm_stack.returning: run_on_alternative_stack.returning:
movq (%rsp), %rsp movq (%rsp), %rsp
popq %rbp popq %rbp
popq %rbx popq %rbx

View File

@ -1,5 +1,5 @@
.globl _run_on_wasm_stack .globl _run_on_alternative_stack
_run_on_wasm_stack: _run_on_alternative_stack:
# (stack_end, stack_begin) # (stack_end, stack_begin)
# We need to ensure 16-byte alignment here. # We need to ensure 16-byte alignment here.
pushq %r15 pushq %r15
@ -10,7 +10,7 @@ pushq %rbx
pushq %rbp pushq %rbp
movq %rsp, -16(%rdi) movq %rsp, -16(%rdi)
leaq _run_on_wasm_stack.returning(%rip), %rax leaq _run_on_alternative_stack.returning(%rip), %rax
movq %rax, -24(%rdi) movq %rax, -24(%rdi)
movq %rsi, %rsp movq %rsi, %rsp
@ -22,7 +22,7 @@ popq %r14
popq %r15 popq %r15
retq retq
_run_on_wasm_stack.returning: _run_on_alternative_stack.returning:
movq (%rsp), %rsp movq (%rsp), %rsp
popq %rbp popq %rbp
popq %rbx popq %rbx

View File

@ -0,0 +1,48 @@
mod raw {
extern "C" {
pub fn run_on_alternative_stack(
stack_end: *mut u64,
stack_begin: *mut u64,
userdata_arg2: *mut u8,
) -> u64;
}
}
pub(crate) unsafe fn run_on_alternative_stack(stack_end: *mut u64, stack_begin: *mut u64) -> u64 {
raw::run_on_alternative_stack(stack_end, stack_begin, ::std::ptr::null_mut())
}
pub fn allocate_and_run<R, F: FnOnce() -> R>(size: usize, f: F) -> R {
struct Context<F: FnOnce() -> R, R> {
f: Option<F>,
ret: Option<R>,
}
extern "C" fn invoke<F: FnOnce() -> R, R>(_: u64, _: u64, ctx: &mut Context<F, R>) {
let f = ctx.f.take().unwrap();
ctx.ret = Some(f());
}
unsafe {
let mut ctx = Context {
f: Some(f),
ret: None,
};
assert!(size % 16 == 0);
assert!(size >= 4096);
let mut stack: Vec<u64> = vec![0; size / 8];
let mut end_offset = stack.len();
stack[end_offset - 4] = invoke::<F, R> as usize as u64;
let stack_begin = stack.as_mut_ptr().offset((end_offset - 4 - 6) as isize);
let stack_end = stack.as_mut_ptr().offset(end_offset as isize);
raw::run_on_alternative_stack(
stack_end,
stack_begin,
&mut ctx as *mut Context<F, R> as *mut u8,
);
ctx.ret.take().unwrap()
}
}

View File

@ -43,6 +43,8 @@ pub mod vm;
pub mod vmcalls; pub mod vmcalls;
#[cfg(all(unix, target_arch = "x86_64"))] #[cfg(all(unix, target_arch = "x86_64"))]
pub use trampoline_x64 as trampoline; pub use trampoline_x64 as trampoline;
#[cfg(all(unix, target_arch = "x86_64"))]
pub mod alternative_stack;
pub mod state; pub mod state;
#[cfg(all(unix, target_arch = "x86_64"))] #[cfg(all(unix, target_arch = "x86_64"))]
pub mod suspend; pub mod suspend;

View File

@ -321,10 +321,8 @@ impl ExecutionStateImage {
#[cfg(all(unix, target_arch = "x86_64"))] #[cfg(all(unix, target_arch = "x86_64"))]
pub mod x64 { pub mod x64 {
extern "C" {
fn run_on_wasm_stack(stack_end: *mut u64, stack_begin: *mut u64) -> u64;
}
use super::*; use super::*;
use crate::alternative_stack::run_on_alternative_stack;
use crate::structures::TypedIndex; use crate::structures::TypedIndex;
use crate::types::LocalGlobalIndex; use crate::types::LocalGlobalIndex;
use crate::vm::Ctx; use crate::vm::Ctx;
@ -515,7 +513,7 @@ pub mod x64 {
image.globals[i]; image.globals[i];
} }
run_on_wasm_stack( run_on_alternative_stack(
stack.as_mut_ptr().offset(stack.len() as isize), stack.as_mut_ptr().offset(stack.len() as isize),
stack.as_mut_ptr().offset(stack_offset as isize), stack.as_mut_ptr().offset(stack_offset as isize),
) )

View File

@ -20,6 +20,7 @@ use std::collections::HashMap;
use std::ptr; use std::ptr;
use std::sync::Arc; use std::sync::Arc;
use std::sync::Once; use std::sync::Once;
use wasmer_runtime_core::alternative_stack::allocate_and_run;
use wasmer_runtime_core::codegen::BkptInfo; use wasmer_runtime_core::codegen::BkptInfo;
use wasmer_runtime_core::state::x64::{read_stack, X64Register, GPR}; use wasmer_runtime_core::state::x64::{read_stack, X64Register, GPR};
use wasmer_runtime_core::typed_func::WasmTrapInfo; use wasmer_runtime_core::typed_func::WasmTrapInfo;
@ -46,6 +47,7 @@ extern "C" fn signal_trap_handler(
_ => {} _ => {}
} }
allocate_and_run(65536, || {
// TODO: make this safer // TODO: make this safer
let ctx = &*(fault.known_registers[X64Register::GPR(GPR::R15).to_index().0].unwrap() let ctx = &*(fault.known_registers[X64Register::GPR(GPR::R15).to_index().0].unwrap()
as *mut vm::Ctx); as *mut vm::Ctx);
@ -72,6 +74,8 @@ extern "C" fn signal_trap_handler(
.red() .red()
); );
image.print_backtrace_if_needed(); image.print_backtrace_if_needed();
});
do_unwind(signum, siginfo as _, ucontext); do_unwind(signum, siginfo as _, ucontext);
} }
} }
@ -191,8 +195,8 @@ pub struct FaultInfo {
#[cfg(all(target_os = "linux", target_arch = "x86_64"))] #[cfg(all(target_os = "linux", target_arch = "x86_64"))]
unsafe fn get_fault_info(siginfo: *const c_void, ucontext: *const c_void) -> FaultInfo { unsafe fn get_fault_info(siginfo: *const c_void, ucontext: *const c_void) -> FaultInfo {
use libc::{ use libc::{
ucontext_t, R10, R11, R12, R13, R14, R15, R8, R9, RAX, RBP, RBX, RCX, RDI, RDX, RIP, RSI, ucontext_t, REG_R10, REG_R11, REG_R12, REG_R13, REG_R14, REG_R15, REG_R8, REG_R9, REG_RAX,
RSP, REG_RBP, REG_RBX, REG_RCX, REG_RDI, REG_RDX, REG_RIP, REG_RSI, REG_RSP,
}; };
#[allow(dead_code)] #[allow(dead_code)]
@ -212,30 +216,29 @@ unsafe fn get_fault_info(siginfo: *const c_void, ucontext: *const c_void) -> Fau
let gregs = &(*ucontext).uc_mcontext.gregs; let gregs = &(*ucontext).uc_mcontext.gregs;
let mut known_registers: [Option<u64>; 24] = [None; 24]; let mut known_registers: [Option<u64>; 24] = [None; 24];
known_registers[X64Register::GPR(GPR::R15).to_index().0] = Some(gregs[REG_R15 as usize] as _);
known_registers[X64Register::GPR(GPR::R14).to_index().0] = Some(gregs[REG_R14 as usize] as _);
known_registers[X64Register::GPR(GPR::R13).to_index().0] = Some(gregs[REG_R13 as usize] as _);
known_registers[X64Register::GPR(GPR::R12).to_index().0] = Some(gregs[REG_R12 as usize] as _);
known_registers[X64Register::GPR(GPR::R11).to_index().0] = Some(gregs[REG_R11 as usize] as _);
known_registers[X64Register::GPR(GPR::R10).to_index().0] = Some(gregs[REG_R10 as usize] as _);
known_registers[X64Register::GPR(GPR::R9).to_index().0] = Some(gregs[REG_R9 as usize] as _);
known_registers[X64Register::GPR(GPR::R8).to_index().0] = Some(gregs[REG_R8 as usize] as _);
known_registers[X64Register::GPR(GPR::RSI).to_index().0] = Some(gregs[REG_RSI as usize] as _);
known_registers[X64Register::GPR(GPR::RDI).to_index().0] = Some(gregs[REG_RDI as usize] as _);
known_registers[X64Register::GPR(GPR::RDX).to_index().0] = Some(gregs[REG_RDX as usize] as _);
known_registers[X64Register::GPR(GPR::RCX).to_index().0] = Some(gregs[REG_RCX as usize] as _);
known_registers[X64Register::GPR(GPR::RBX).to_index().0] = Some(gregs[REG_RBX as usize] as _);
known_registers[X64Register::GPR(GPR::RAX).to_index().0] = Some(gregs[REG_RAX as usize] as _);
known_registers[X64Register::GPR(GPR::R15).to_index().0] = Some(gregs[R15 as usize] as _); known_registers[X64Register::GPR(GPR::RBP).to_index().0] = Some(gregs[REG_RBP as usize] as _);
known_registers[X64Register::GPR(GPR::R14).to_index().0] = Some(gregs[R14 as usize] as _); known_registers[X64Register::GPR(GPR::RSP).to_index().0] = Some(gregs[REG_RSP as usize] as _);
known_registers[X64Register::GPR(GPR::R13).to_index().0] = Some(gregs[R13 as usize] as _);
known_registers[X64Register::GPR(GPR::R12).to_index().0] = Some(gregs[R12 as usize] as _);
known_registers[X64Register::GPR(GPR::R11).to_index().0] = Some(gregs[R11 as usize] as _);
known_registers[X64Register::GPR(GPR::R10).to_index().0] = Some(gregs[R10 as usize] as _);
known_registers[X64Register::GPR(GPR::R9).to_index().0] = Some(gregs[R9 as usize] as _);
known_registers[X64Register::GPR(GPR::R8).to_index().0] = Some(gregs[R8 as usize] as _);
known_registers[X64Register::GPR(GPR::RSI).to_index().0] = Some(gregs[RSI as usize] as _);
known_registers[X64Register::GPR(GPR::RDI).to_index().0] = Some(gregs[RDI as usize] as _);
known_registers[X64Register::GPR(GPR::RDX).to_index().0] = Some(gregs[RDX as usize] as _);
known_registers[X64Register::GPR(GPR::RCX).to_index().0] = Some(gregs[RCX as usize] as _);
known_registers[X64Register::GPR(GPR::RBX).to_index().0] = Some(gregs[RBX as usize] as _);
known_registers[X64Register::GPR(GPR::RAX).to_index().0] = Some(gregs[RAX as usize] as _);
known_registers[X64Register::GPR(GPR::RBP).to_index().0] = Some(gregs[RBP as usize] as _);
known_registers[X64Register::GPR(GPR::RSP).to_index().0] = Some(gregs[RSP as usize] as _);
// TODO: XMM registers // TODO: XMM registers
FaultInfo { FaultInfo {
faulting_addr: si_addr as usize as _, faulting_addr: si_addr as usize as _,
ip: gregs[RIP as usize] as _, ip: gregs[REG_RIP as usize] as _,
known_registers, known_registers,
} }
} }