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

Interrupt signal propagation #1496

Closed
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## **[Unreleased]**
- [#1496](https://github.com/wasmerio/wasmer/pull/1496) Propagate interrupt signal(SIGINT) to previous signal handler

## 0.17.1 - 2020-06-24

Expand Down
45 changes: 39 additions & 6 deletions lib/runtime-core/src/fault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,23 @@ pub fn allocate_and_run<R, F: FnOnce() -> R>(size: usize, f: F) -> R {
}
}

unsafe fn call_signal_handler(
sig: Signal,
siginfo: *mut siginfo_t,
ucontext: *mut c_void,
sig_action: &SigAction,
) {
match sig_action.handler() {
SigHandler::SigDfl => {
sigaction(sig, sig_action).unwrap();
return;
}
SigHandler::SigIgn => return,
SigHandler::Handler(handler) => handler(sig as _),
SigHandler::SigAction(handler) => handler(sig as _, siginfo as _, ucontext),
}
}

extern "C" fn signal_trap_handler(
signum: ::nix::libc::c_int,
siginfo: *mut siginfo_t,
Expand Down Expand Up @@ -439,17 +456,33 @@ extern "C" fn signal_trap_handler(
}
}

static mut SIGINT_SYS_HANDLER: Option<SigAction> = None;

extern "C" fn sigint_handler(
_signum: ::nix::libc::c_int,
_siginfo: *mut siginfo_t,
_ucontext: *mut c_void,
) {
if INTERRUPT_SIGNAL_DELIVERED.swap(true, Ordering::SeqCst) {
eprintln!("Got another SIGINT before trap is triggered on WebAssembly side, aborting");
process::abort();
}
unsafe {
set_wasm_interrupt();
match SIGINT_SYS_HANDLER {
Some(prev_handler) => {
if !INTERRUPT_SIGNAL_DELIVERED.swap(true, Ordering::SeqCst) {
set_wasm_interrupt();
}

call_signal_handler(SIGINT, _siginfo, _ucontext, &prev_handler);
}
None => {
if !INTERRUPT_SIGNAL_DELIVERED.swap(true, Ordering::SeqCst) {
eprintln!(
"Got another SIGINT before trap is triggered on WebAssembly side, aborting"
);
process::abort();
}

set_wasm_interrupt();
}
}
}
}

Expand Down Expand Up @@ -479,7 +512,7 @@ unsafe fn install_sighandler() {
SaFlags::SA_ONSTACK,
SigSet::empty(),
);
sigaction(SIGINT, &sa_interrupt).unwrap();
SIGINT_SYS_HANDLER = Some(sigaction(SIGINT, &sa_interrupt).unwrap());
}

#[derive(Debug, Clone)]
Expand Down