Skip to content

Implement default signal handlers #20257

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

Merged
merged 8 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 51 additions & 7 deletions system/lib/libc/raise.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,39 @@ extern sigset_t __sig_pending;

bool __sig_is_blocked(int sig);

// Default handler actions ~auto-generated from https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/signal.h.html.
// Note that stop and continue actions are not supported and treated as ignored.

typedef enum {
ACTION_IGNORE = 0,
ACTION_TERMINATE,
ACTION_ABORT,
} default_action_t;

static default_action_t default_actions[_NSIG] = {
[SIGABRT] = ACTION_ABORT,
[SIGALRM] = ACTION_TERMINATE,
[SIGBUS] = ACTION_ABORT,
[SIGFPE] = ACTION_ABORT,
[SIGHUP] = ACTION_TERMINATE,
[SIGILL] = ACTION_ABORT,
[SIGINT] = ACTION_TERMINATE,
[SIGKILL] = ACTION_TERMINATE,
[SIGPIPE] = ACTION_TERMINATE,
[SIGQUIT] = ACTION_ABORT,
[SIGSEGV] = ACTION_ABORT,
[SIGTERM] = ACTION_TERMINATE,
[SIGUSR1] = ACTION_TERMINATE,
[SIGUSR2] = ACTION_TERMINATE,
[SIGPOLL] = ACTION_TERMINATE,
[SIGPROF] = ACTION_TERMINATE,
[SIGSYS] = ACTION_ABORT,
[SIGTRAP] = ACTION_ABORT,
[SIGVTALRM] = ACTION_TERMINATE,
[SIGXCPU] = ACTION_ABORT,
[SIGXFSZ] = ACTION_ABORT,
};

int raise(int sig) {
if (__sig_is_blocked(sig)) {
sigaddset(&__sig_pending, sig);
Expand All @@ -26,14 +59,25 @@ int raise(int sig) {
siginfo_t t = {0};
__sig_actions[sig].sa_sigaction(sig, &t, NULL);
} else {
if (__sig_actions[sig].sa_handler == SIG_DFL || __sig_actions[sig].sa_handler == SIG_IGN) {

return 0;
void (*handler)(int) = __sig_actions[sig].sa_handler;
if (handler == SIG_DFL) {
switch (default_actions[sig]) {
case ACTION_IGNORE:
break;
case ACTION_TERMINATE:
// Intentionally exiting via a function that doesn't call atexit handlers.
_Exit(128 + sig);
case ACTION_ABORT:
// TODO: should this integrate with `Module['onAbort'] once `abort` is migrated
// to raise SIGABRT as per spec?
__builtin_trap();
}
} else if (handler != SIG_IGN) {
// Avoid a direct call to the handler, and instead call via JS so we can
// avoid strict signature checking.
// https://github.com/emscripten-core/posixtestsuite/issues/6
__call_sighandler(__sig_actions[sig].sa_handler, sig);
}
// Avoid a direct call to the handler, and instead call via JS so we can
// avoid strict signature checking.
// https://github.com/emscripten-core/posixtestsuite/issues/6
__call_sighandler(__sig_actions[sig].sa_handler, sig);
}
return 0;
}
4 changes: 2 additions & 2 deletions test/other/test_standalone_syscalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ int main() {
// check we can call this, but the test doesn't check the output
emscripten_get_now();

// This doesn't do anything because we have not handler registered, but it
// This doesn't do anything because we have no handler registered, but it
// verifies that `raise` can be included in the build without any non-standard
// imports being generated.
raise(SIGUSR1);
raise(SIGCHLD);
return 0;
}
14 changes: 14 additions & 0 deletions test/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import random
import re
import shutil
import signal
import sys
import time
import unittest
Expand Down Expand Up @@ -6179,6 +6180,19 @@ def test_sigalrm(self):
def test_signals(self):
self.do_core_test(test_file('test_signals.c'))

@parameterized({
'sigint': (signal.SIGINT, 128 + signal.SIGINT, True),
'sigabrt': (signal.SIGABRT, 7, False)
})
def test_sigaction_default(self, signal, exit_code, assert_identical):
self.set_setting('EXIT_RUNTIME')
self.do_core_test(
test_file('test_sigaction_default.c'),
args=[str(int(signal))],
assert_identical=assert_identical,
assert_returncode=exit_code
)

@no_windows('https://github.com/emscripten-core/emscripten/issues/8882')
@requires_node
def test_unistd_access(self):
Expand Down
23 changes: 23 additions & 0 deletions test/test_sigaction_default.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2023 The Emscripten Authors. All rights reserved.
* Emscripten is available under two separate licenses, the MIT license and the
* University of Illinois/NCSA Open Source License. Both these licenses can be
* found in the LICENSE file.
*/

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <assert.h>

static void cleanExit() {
puts("3");
}

int main(int argc, char **argv) {
atexit(cleanExit);
puts("1");
raise(atoi(argv[1]));
puts("2");
return 0;
}
1 change: 1 addition & 0 deletions test/test_sigaction_default.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1