Skip to content

Commit

Permalink
Allow using custom signal handler from non-main thread (#2551)
Browse files Browse the repository at this point in the history
Remove thread local attribute of prev_sig_act_SIGSEGV/SIGBUS to allow using
custom signal handler from non-main thread since in a thread spawned by
embedder, embedder may be unable to call wasm_runtime_init_thread_env to
initialize them.

And fix the handling of prev_sig_act when its sa_handler is SIG_DFL, SIG_IGN,
or a user customized handler.
  • Loading branch information
eloparco authored Sep 15, 2023
1 parent 9c34fc3 commit 132378f
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions core/shared/platform/common/posix/posix_thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -509,8 +509,8 @@ mask_signals(int how)
pthread_sigmask(how, &set, NULL);
}

static os_thread_local_attribute struct sigaction prev_sig_act_SIGSEGV;
static os_thread_local_attribute struct sigaction prev_sig_act_SIGBUS;
static struct sigaction prev_sig_act_SIGSEGV;
static struct sigaction prev_sig_act_SIGBUS;

/* ASAN is not designed to work with custom stack unwind or other low-level \
things. > Ignore a function that does some low-level magic. (e.g. walking \
Expand Down Expand Up @@ -540,11 +540,16 @@ signal_callback(int sig_num, siginfo_t *sig_info, void *sig_ucontext)
if (prev_sig_act && (prev_sig_act->sa_flags & SA_SIGINFO)) {
prev_sig_act->sa_sigaction(sig_num, sig_info, sig_ucontext);
}
else if (prev_sig_act
&& ((void *)prev_sig_act->sa_sigaction == SIG_DFL
|| (void *)prev_sig_act->sa_sigaction == SIG_IGN)) {
else if (prev_sig_act && (void *)prev_sig_act->sa_handler == SIG_DFL) {
/* Default action */
sigaction(sig_num, prev_sig_act, NULL);
}
else if (prev_sig_act && (void *)prev_sig_act->sa_handler == SIG_IGN) {
/* Ignore this signal */
}
else if (prev_sig_act && prev_sig_act->sa_handler) {
prev_sig_act->sa_handler(sig_num);
}
/* Output signal info and then crash if signal is unhandled */
else {
switch (sig_num) {
Expand Down

0 comments on commit 132378f

Please sign in to comment.