2018-11-06 11:15:40 +01:00
|
|
|
//! 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
|
2018-11-24 19:40:29 +08:00
|
|
|
use super::recovery;
|
2018-11-28 13:25:56 -08:00
|
|
|
use nix::libc::{c_void, siginfo_t};
|
2018-11-06 11:15:40 +01:00
|
|
|
use nix::sys::signal::{
|
2018-11-24 19:40:29 +08:00
|
|
|
sigaction, SaFlags, SigAction, SigHandler, SigSet, SIGBUS, SIGFPE, SIGILL, SIGSEGV,
|
2018-11-06 11:15:40 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
pub unsafe fn install_sighandler() {
|
|
|
|
let sa = SigAction::new(
|
2018-11-27 19:10:43 -05:00
|
|
|
SigHandler::SigAction(signal_trap_handler),
|
2018-11-26 13:13:52 +08:00
|
|
|
SaFlags::SA_ONSTACK,
|
2018-11-06 11:15:40 +01:00
|
|
|
SigSet::empty(),
|
|
|
|
);
|
|
|
|
sigaction(SIGFPE, &sa).unwrap();
|
|
|
|
sigaction(SIGILL, &sa).unwrap();
|
|
|
|
sigaction(SIGSEGV, &sa).unwrap();
|
|
|
|
sigaction(SIGBUS, &sa).unwrap();
|
|
|
|
}
|
2018-11-15 01:22:44 -08:00
|
|
|
|
2018-11-28 13:25:56 -08:00
|
|
|
extern "C" fn signal_trap_handler(
|
|
|
|
signum: ::nix::libc::c_int,
|
|
|
|
siginfo: *mut siginfo_t,
|
|
|
|
_ucontext: *mut c_void,
|
|
|
|
) {
|
2018-11-06 11:15:40 +01:00
|
|
|
unsafe {
|
2018-11-27 19:10:43 -05:00
|
|
|
recovery::do_unwind(signum, siginfo);
|
2018-11-06 11:15:40 +01:00
|
|
|
}
|
|
|
|
}
|