mirror of
https://github.com/fluencelabs/wasmer
synced 2025-06-24 22:21:32 +00:00
Starting to set up protected call in clif-backend
This commit is contained in:
125
lib/clif-backend/src/call/mod.rs
Normal file
125
lib/clif-backend/src/call/mod.rs
Normal file
@ -0,0 +1,125 @@
|
||||
mod recovery;
|
||||
mod sighandler;
|
||||
|
||||
use crate::call::recovery::call_protected;
|
||||
use wasmer_runtime::{
|
||||
backend::{Token, ProtectedCaller},
|
||||
types::{FuncIndex, Value, Type, FuncSig, LocalOrImport},
|
||||
module::ModuleInner,
|
||||
error::{RuntimeResult},
|
||||
export::Context,
|
||||
vm::{self, ImportBacking},
|
||||
};
|
||||
use libffi::high::{arg as libffi_arg, call as libffi_call, CodePtr};
|
||||
use hashbrown::HashSet;
|
||||
use std::iter;
|
||||
|
||||
pub struct Caller {
|
||||
func_export_set: HashSet<FuncIndex>,
|
||||
}
|
||||
|
||||
impl Caller {
|
||||
pub fn new(module: &ModuleInner) -> Self {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
impl ProtectedCaller for Caller {
|
||||
fn call(
|
||||
&self,
|
||||
module: &ModuleInner,
|
||||
func_index: FuncIndex,
|
||||
params: &[Value],
|
||||
returns: &mut [Value],
|
||||
import_backing: &ImportBacking,
|
||||
vmctx: *mut vm::Ctx,
|
||||
_: Token,
|
||||
) -> RuntimeResult<()> {
|
||||
let (func_ptr, ctx, signature) = get_func_from_index(&module, import_backing, func_index);
|
||||
|
||||
let vmctx_ptr = match ctx {
|
||||
Context::External(external_vmctx) => external_vmctx,
|
||||
Context::Internal => vmctx,
|
||||
};
|
||||
|
||||
assert!(self.func_export_set.contains(&func_index));
|
||||
|
||||
assert!(
|
||||
returns.len() == signature.returns.len() && signature.returns.len() <= 1,
|
||||
"multi-value returns not yet supported"
|
||||
);
|
||||
|
||||
assert!(signature.check_sig(params), "incorrect signature");
|
||||
|
||||
let libffi_args: Vec<_> = params
|
||||
.iter()
|
||||
.map(|val| match val {
|
||||
Value::I32(ref x) => libffi_arg(x),
|
||||
Value::I64(ref x) => libffi_arg(x),
|
||||
Value::F32(ref x) => libffi_arg(x),
|
||||
Value::F64(ref x) => libffi_arg(x),
|
||||
})
|
||||
.chain(iter::once(libffi_arg(&vmctx_ptr)))
|
||||
.collect();
|
||||
|
||||
let code_ptr = CodePtr::from_ptr(func_ptr as _);
|
||||
|
||||
call_protected(|| {
|
||||
// Only supports zero or one return values for now.
|
||||
// To support multiple returns, we will have to
|
||||
// generate trampolines instead of using libffi.
|
||||
match signature.returns.first() {
|
||||
Some(ty) => {
|
||||
let val = match ty {
|
||||
Type::I32 => Value::I32(unsafe { libffi_call(code_ptr, &libffi_args) }),
|
||||
Type::I64 => Value::I64(unsafe { libffi_call(code_ptr, &libffi_args) }),
|
||||
Type::F32 => Value::F32(unsafe { libffi_call(code_ptr, &libffi_args) }),
|
||||
Type::F64 => Value::F64(unsafe { libffi_call(code_ptr, &libffi_args) }),
|
||||
};
|
||||
returns[0] = val;
|
||||
},
|
||||
// call with no returns
|
||||
None => unsafe {
|
||||
libffi_call::<()>(code_ptr, &libffi_args);
|
||||
},
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn get_func_from_index<'a>(
|
||||
module: &'a ModuleInner,
|
||||
import_backing: &ImportBacking,
|
||||
func_index: FuncIndex,
|
||||
) -> (*const vm::Func, Context, &'a FuncSig) {
|
||||
let sig_index = *module
|
||||
.func_assoc
|
||||
.get(func_index)
|
||||
.expect("broken invariant, incorrect func index");
|
||||
|
||||
let (func_ptr, ctx) = match func_index.local_or_import(module) {
|
||||
LocalOrImport::Local(local_func_index) => {
|
||||
(
|
||||
module
|
||||
.func_resolver
|
||||
.get(&module, local_func_index)
|
||||
.expect("broken invariant, func resolver not synced with module.exports")
|
||||
.cast()
|
||||
.as_ptr() as *const _,
|
||||
Context::Internal,
|
||||
)
|
||||
}
|
||||
LocalOrImport::Import(imported_func_index) => {
|
||||
let imported_func = import_backing.imported_func(imported_func_index);
|
||||
(
|
||||
imported_func.func as *const _,
|
||||
Context::External(imported_func.vmctx),
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
let signature = module.sig_registry.lookup_func_sig(sig_index);
|
||||
|
||||
(func_ptr, ctx, signature)
|
||||
}
|
79
lib/clif-backend/src/call/recovery.rs
Normal file
79
lib/clif-backend/src/call/recovery.rs
Normal file
@ -0,0 +1,79 @@
|
||||
//! When a WebAssembly module triggers any traps, we perform recovery here.
|
||||
//!
|
||||
//! This module uses TLS (thread-local storage) to track recovery information. Since the four signals we're handling
|
||||
//! are very special, the async signal unsafety of Rust's TLS implementation generally does not affect the correctness here
|
||||
//! unless you have memory unsafety elsewhere in your code.
|
||||
|
||||
use wasmer_runtime::{
|
||||
error::{RuntimeError, RuntimeResult},
|
||||
};
|
||||
use crate::call::sighandler::install_sighandler;
|
||||
use nix::libc::siginfo_t;
|
||||
use nix::sys::signal::{Signal, SIGBUS, SIGFPE, SIGILL, SIGSEGV};
|
||||
use std::cell::{Cell, UnsafeCell};
|
||||
use std::sync::Once;
|
||||
|
||||
extern "C" {
|
||||
pub fn setjmp(env: *mut ::nix::libc::c_void) -> ::nix::libc::c_int;
|
||||
fn longjmp(env: *mut ::nix::libc::c_void, val: ::nix::libc::c_int) -> !;
|
||||
}
|
||||
|
||||
const SETJMP_BUFFER_LEN: usize = 27;
|
||||
pub static SIGHANDLER_INIT: Once = Once::new();
|
||||
|
||||
thread_local! {
|
||||
pub static SETJMP_BUFFER: UnsafeCell<[::nix::libc::c_int; SETJMP_BUFFER_LEN]> = UnsafeCell::new([0; SETJMP_BUFFER_LEN]);
|
||||
pub static CAUGHT_ADDRESS: Cell<usize> = Cell::new(0);
|
||||
}
|
||||
|
||||
pub fn call_protected<T>(f: impl FnOnce() -> T) -> RuntimeResult<T> {
|
||||
unsafe {
|
||||
let jmp_buf = SETJMP_BUFFER.with(|buf| buf.get());
|
||||
let prev_jmp_buf = *jmp_buf;
|
||||
|
||||
SIGHANDLER_INIT.call_once(|| {
|
||||
install_sighandler();
|
||||
});
|
||||
|
||||
let signum = setjmp(jmp_buf as *mut ::nix::libc::c_void);
|
||||
if signum != 0 {
|
||||
*jmp_buf = prev_jmp_buf;
|
||||
let addr = CAUGHT_ADDRESS.with(|cell| cell.get());
|
||||
|
||||
let signal = match Signal::from_c_int(signum) {
|
||||
Ok(SIGFPE) => "floating-point exception",
|
||||
Ok(SIGILL) => "illegal instruction",
|
||||
Ok(SIGSEGV) => "segmentation violation",
|
||||
Ok(SIGBUS) => "bus error",
|
||||
Err(_) => "error while getting the Signal",
|
||||
_ => "unkown trapped signal",
|
||||
};
|
||||
// When the trap-handler is fully implemented, this will return more information.
|
||||
Err(RuntimeError::Unknown {
|
||||
msg: format!("trap at {:#x} - {}", addr, signal),
|
||||
}
|
||||
.into())
|
||||
} else {
|
||||
let ret = f(); // TODO: Switch stack?
|
||||
*jmp_buf = prev_jmp_buf;
|
||||
Ok(ret)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Unwinds to last protected_call.
|
||||
pub unsafe fn do_unwind(signum: i32, siginfo: *mut siginfo_t) -> ! {
|
||||
// Since do_unwind is only expected to get called from WebAssembly code which doesn't hold any host resources (locks etc.)
|
||||
// itself, accessing TLS here is safe. In case any other code calls this, it often indicates a memory safety bug and you should
|
||||
// temporarily disable the signal handlers to debug it.
|
||||
|
||||
let jmp_buf = SETJMP_BUFFER.with(|buf| buf.get());
|
||||
if *jmp_buf == [0; SETJMP_BUFFER_LEN] {
|
||||
::std::process::abort();
|
||||
}
|
||||
// We only target macos at the moment as other ones might not have si_addr field
|
||||
#[cfg(target_os = "macos")]
|
||||
CAUGHT_ADDRESS.with(|cell| cell.set((*siginfo).si_addr as _));
|
||||
|
||||
longjmp(jmp_buf as *mut ::nix::libc::c_void, signum)
|
||||
}
|
33
lib/clif-backend/src/call/sighandler.rs
Normal file
33
lib/clif-backend/src/call/sighandler.rs
Normal file
@ -0,0 +1,33 @@
|
||||
//! We install signal handlers to handle WebAssembly traps within
|
||||
//! our Rust code. Otherwise we will have errors that stop the Rust process
|
||||
//! such as `process didn't exit successfully: ... (signal: 8, SIGFPE: erroneous arithmetic operation)`
|
||||
//!
|
||||
//! Please read more about this here: https://github.com/CraneStation/wasmtime/issues/15
|
||||
//! This code is inspired by: https://github.com/pepyakin/wasmtime/commit/625a2b6c0815b21996e111da51b9664feb174622
|
||||
use crate::call::recovery;
|
||||
use nix::libc::{c_void, siginfo_t};
|
||||
use nix::sys::signal::{
|
||||
sigaction, SaFlags, SigAction, SigHandler, SigSet, SIGBUS, SIGFPE, SIGILL, SIGSEGV,
|
||||
};
|
||||
|
||||
pub unsafe fn install_sighandler() {
|
||||
let sa = SigAction::new(
|
||||
SigHandler::SigAction(signal_trap_handler),
|
||||
SaFlags::SA_ONSTACK,
|
||||
SigSet::empty(),
|
||||
);
|
||||
sigaction(SIGFPE, &sa).unwrap();
|
||||
sigaction(SIGILL, &sa).unwrap();
|
||||
sigaction(SIGSEGV, &sa).unwrap();
|
||||
sigaction(SIGBUS, &sa).unwrap();
|
||||
}
|
||||
|
||||
extern "C" fn signal_trap_handler(
|
||||
signum: ::nix::libc::c_int,
|
||||
siginfo: *mut siginfo_t,
|
||||
_ucontext: *mut c_void,
|
||||
) {
|
||||
unsafe {
|
||||
recovery::do_unwind(signum, siginfo);
|
||||
}
|
||||
}
|
@ -5,6 +5,7 @@ mod module;
|
||||
mod module_env;
|
||||
mod relocation;
|
||||
mod resolver;
|
||||
mod call;
|
||||
|
||||
use cranelift_codegen::{
|
||||
isa,
|
||||
|
Reference in New Issue
Block a user