Improved signal error messages

This commit is contained in:
Syrus Akbary 2018-11-15 01:22:44 -08:00
parent cdbd27275c
commit 459d5f376d
2 changed files with 21 additions and 7 deletions

5
examples/trap.wat Normal file
View File

@ -0,0 +1,5 @@
(module
(func $main (export "main") (result i32)
(i32.div_s (i32.const 0) (i32.const 0))
)
)

View File

@ -5,9 +5,11 @@
//! 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 nix::sys::signal::{
sigaction, SaFlags, SigAction, SigHandler, SigSet, SIGBUS, SIGFPE, SIGILL, SIGSEGV,
sigaction, Signal, SaFlags, SigAction, SigHandler, SigSet, SIGBUS, SIGFPE, SIGILL, SIGSEGV,
};
static mut SETJMP_BUFFER: [::nix::libc::c_int; 27] = [0; 27];
pub unsafe fn install_sighandler() {
let sa = SigAction::new(
SigHandler::Handler(signal_trap_handler),
@ -18,22 +20,29 @@ pub unsafe fn install_sighandler() {
sigaction(SIGILL, &sa).unwrap();
sigaction(SIGSEGV, &sa).unwrap();
sigaction(SIGBUS, &sa).unwrap();
let result = setjmp((&mut SETJMP_BUFFER[..]).as_mut_ptr() as *mut ::nix::libc::c_void);
if result != 0 {
panic!("Signal Error: {}", result);
let signum = setjmp((&mut SETJMP_BUFFER[..]).as_mut_ptr() as *mut ::nix::libc::c_void);
if signum != 0 {
let signal = Signal::from_c_int(signum).unwrap();
match signal {
SIGFPE => panic!("floating-point exception"),
SIGILL => panic!("illegal instruction"),
SIGSEGV => panic!("segmentation violation"),
SIGBUS => panic!("bus error"),
_ => panic!("signal error: {:?}", signal),
};
}
}
static mut SETJMP_BUFFER: [::nix::libc::c_int; 27] = [0; 27];
extern "C" {
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);
}
extern "C" fn signal_trap_handler(_: ::nix::libc::c_int) {
extern "C" fn signal_trap_handler(signum: ::nix::libc::c_int) {
unsafe {
longjmp(
(&mut SETJMP_BUFFER).as_mut_ptr() as *mut ::nix::libc::c_void,
3,
signum,
);
}
}