Skip to content

Commit 558785d

Browse files
committed
Move to JS implementation of proc_raise
1 parent 27983de commit 558785d

File tree

7 files changed

+121
-55
lines changed

7 files changed

+121
-55
lines changed

emcc.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2966,6 +2966,10 @@ def get_full_import_name(name):
29662966
# in standalone mode, crt1 will call the constructors from inside the wasm
29672967
settings.REQUIRED_EXPORTS.append('__wasm_call_ctors')
29682968

2969+
if not settings.MINIMAL_RUNTIME:
2970+
# For proc_raise to stringify signals.
2971+
settings.REQUIRED_EXPORTS.append('strsignal')
2972+
29692973
settings.PRE_JS_FILES = [os.path.abspath(f) for f in options.pre_js]
29702974
settings.POST_JS_FILES = [os.path.abspath(f) for f in options.post_js]
29712975

src/generated_struct_info32.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,8 +373,40 @@
373373
"SEEK_CUR": 1,
374374
"SEEK_END": 2,
375375
"SEEK_SET": 0,
376+
"SIGABRT": 6,
376377
"SIGALRM": 14,
378+
"SIGBUS": 7,
377379
"SIGCANCEL": 33,
380+
"SIGCHLD": 17,
381+
"SIGCONT": 18,
382+
"SIGFPE": 8,
383+
"SIGHUP": 1,
384+
"SIGILL": 4,
385+
"SIGINT": 2,
386+
"SIGIO": 29,
387+
"SIGIOT": 6,
388+
"SIGKILL": 9,
389+
"SIGPIPE": 13,
390+
"SIGPOLL": 29,
391+
"SIGPROF": 27,
392+
"SIGPWR": 30,
393+
"SIGQUIT": 3,
394+
"SIGSEGV": 11,
395+
"SIGSTKFLT": 16,
396+
"SIGSTOP": 19,
397+
"SIGSYS": 31,
398+
"SIGTERM": 15,
399+
"SIGTRAP": 5,
400+
"SIGTSTP": 20,
401+
"SIGTTIN": 21,
402+
"SIGTTOU": 22,
403+
"SIGURG": 23,
404+
"SIGUSR1": 10,
405+
"SIGUSR2": 12,
406+
"SIGVTALRM": 26,
407+
"SIGWINCH": 28,
408+
"SIGXCPU": 24,
409+
"SIGXFSZ": 25,
378410
"SOCK_CLOEXEC": 524288,
379411
"SOCK_DGRAM": 2,
380412
"SOCK_NONBLOCK": 2048,

src/library_wasi.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,48 @@ var WasiLibrary = {
563563
#endif // SYSCALLS_REQUIRE_FILESYSTEM
564564
},
565565
fd_sync__async: true,
566+
567+
proc_raise__deps: [
568+
#if !MINIMAL_RUNTIME
569+
'strsignal',
570+
#endif
571+
'proc_exit'
572+
],
573+
proc_raise: (sig) => {
574+
// Default handler actions ~auto-generated from https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/signal.h.html.
575+
// Note that stop and continue actions are not supported and treated as ignored.
576+
switch (sig) {
577+
case {{{ cDefs.SIGABRT }}}:
578+
case {{{ cDefs.SIGBUS }}}:
579+
case {{{ cDefs.SIGFPE }}}:
580+
case {{{ cDefs.SIGILL }}}:
581+
case {{{ cDefs.SIGQUIT }}}:
582+
case {{{ cDefs.SIGSEGV }}}:
583+
case {{{ cDefs.SIGSYS }}}:
584+
case {{{ cDefs.SIGTRAP }}}:
585+
case {{{ cDefs.SIGXCPU }}}:
586+
case {{{ cDefs.SIGXFSZ }}}:
587+
#if !MINIMAL_RUNTIME
588+
EXITSTATUS = 128 + sig;
589+
// TODO: abort should raise a signal (SIGABRT) instead of raise invoking abort.
590+
sig = 'Signal: ' + UTF8ToString(_strsignal(sig));
591+
#endif
592+
abort(sig);
593+
case {{{ cDefs.SIGALRM }}}:
594+
case {{{ cDefs.SIGHUP }}}:
595+
case {{{ cDefs.SIGINT }}}:
596+
case {{{ cDefs.SIGKILL }}}:
597+
case {{{ cDefs.SIGPIPE }}}:
598+
case {{{ cDefs.SIGTERM }}}:
599+
case {{{ cDefs.SIGUSR1 }}}:
600+
case {{{ cDefs.SIGUSR2 }}}:
601+
case {{{ cDefs.SIGPOLL }}}:
602+
case {{{ cDefs.SIGPROF }}}:
603+
case {{{ cDefs.SIGVTALRM }}}:
604+
_proc_exit(128 + sig);
605+
}
606+
return 0;
607+
},
566608
};
567609

568610
for (var x in WasiLibrary) {

src/struct_info.json

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,39 @@
274274
{
275275
"file": "signal.h",
276276
"defines": [
277-
"SIGALRM"
277+
"SIGHUP",
278+
"SIGINT",
279+
"SIGQUIT",
280+
"SIGILL",
281+
"SIGTRAP",
282+
"SIGABRT",
283+
"SIGIOT",
284+
"SIGBUS",
285+
"SIGFPE",
286+
"SIGKILL",
287+
"SIGUSR1",
288+
"SIGSEGV",
289+
"SIGUSR2",
290+
"SIGPIPE",
291+
"SIGALRM",
292+
"SIGTERM",
293+
"SIGSTKFLT",
294+
"SIGCHLD",
295+
"SIGCONT",
296+
"SIGSTOP",
297+
"SIGTSTP",
298+
"SIGTTIN",
299+
"SIGTTOU",
300+
"SIGURG",
301+
"SIGXCPU",
302+
"SIGXFSZ",
303+
"SIGVTALRM",
304+
"SIGPROF",
305+
"SIGWINCH",
306+
"SIGIO",
307+
"SIGPOLL",
308+
"SIGPWR",
309+
"SIGSYS"
278310
]
279311
},
280312
{

system/lib/libc/raise.c

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <stdbool.h>
1010
#include <stddef.h>
1111
#include <signal.h>
12+
#include "syscall.h"
1213

1314
#include "emscripten_internal.h"
1415

@@ -17,39 +18,6 @@ extern sigset_t __sig_pending;
1718

1819
bool __sig_is_blocked(int sig);
1920

20-
// Default handler actions ~auto-generated from https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/signal.h.html.
21-
// Note that stop and continue actions are not supported and treated as ignored.
22-
23-
typedef enum {
24-
ACTION_IGNORE = 0,
25-
ACTION_TERMINATE,
26-
ACTION_ABORT,
27-
} default_action_t;
28-
29-
static default_action_t default_actions[_NSIG] = {
30-
[SIGABRT] = ACTION_ABORT,
31-
[SIGALRM] = ACTION_TERMINATE,
32-
[SIGBUS] = ACTION_ABORT,
33-
[SIGFPE] = ACTION_ABORT,
34-
[SIGHUP] = ACTION_TERMINATE,
35-
[SIGILL] = ACTION_ABORT,
36-
[SIGINT] = ACTION_TERMINATE,
37-
[SIGKILL] = ACTION_TERMINATE,
38-
[SIGPIPE] = ACTION_TERMINATE,
39-
[SIGQUIT] = ACTION_ABORT,
40-
[SIGSEGV] = ACTION_ABORT,
41-
[SIGTERM] = ACTION_TERMINATE,
42-
[SIGUSR1] = ACTION_TERMINATE,
43-
[SIGUSR2] = ACTION_TERMINATE,
44-
[SIGPOLL] = ACTION_TERMINATE,
45-
[SIGPROF] = ACTION_TERMINATE,
46-
[SIGSYS] = ACTION_ABORT,
47-
[SIGTRAP] = ACTION_ABORT,
48-
[SIGVTALRM] = ACTION_TERMINATE,
49-
[SIGXCPU] = ACTION_ABORT,
50-
[SIGXFSZ] = ACTION_ABORT,
51-
};
52-
5321
int raise(int sig) {
5422
if (__sig_is_blocked(sig)) {
5523
sigaddset(&__sig_pending, sig);
@@ -61,18 +29,9 @@ int raise(int sig) {
6129
} else {
6230
void (*handler)(int) = __sig_actions[sig].sa_handler;
6331
if (handler == SIG_DFL) {
64-
switch (default_actions[sig]) {
65-
case ACTION_IGNORE:
66-
break;
67-
case ACTION_TERMINATE:
68-
// Intentionally exiting via a function that doesn't call atexit handlers.
69-
_Exit(128 + sig);
70-
case ACTION_ABORT:
71-
// TODO: should this integrate with `Module['onAbort'] once `abort` is migrated
72-
// to raise SIGABRT as per spec?
73-
__builtin_trap();
74-
}
75-
} else if (handler != SIG_IGN) {
32+
return __wasi_syscall_ret(__wasi_proc_raise(sig));
33+
}
34+
if (handler != SIG_IGN) {
7635
// Avoid a direct call to the handler, and instead call via JS so we can
7736
// avoid strict signature checking.
7837
// https://github.com/emscripten-core/posixtestsuite/issues/6

test/test_core.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6181,15 +6181,11 @@ def test_signals(self):
61816181
self.do_core_test(test_file('test_signals.c'))
61826182

61836183
@parameterized({
6184-
'sigint': ('sigint', signal.SIGINT, 128 + signal.SIGINT, True, ['EXIT_RUNTIME']),
6185-
'sigabrt': ('sigabrt', signal.SIGABRT, NON_ZERO, False, ['EXIT_RUNTIME']),
6186-
# TODO: @also_with_standalone_wasm() should work with @parameterized() as a matrix.
6187-
'sigint_standalone': ('sigint', signal.SIGINT, 128 + signal.SIGINT, True, ['PURE_WASI']),
6188-
'sigabrt_standalone': ('sigabrt', signal.SIGABRT, NON_ZERO, False, ['PURE_WASI']),
6184+
'sigint': ('sigint', signal.SIGINT, 128 + signal.SIGINT, True),
6185+
'sigabrt': ('sigabrt', signal.SIGABRT, NON_ZERO, False),
61896186
})
6190-
def test_sig_default(self, suffix, signal, exit_code, assert_identical, settings):
6191-
for setting in settings:
6192-
self.set_setting(setting)
6187+
def test_sig_default(self, suffix, signal, exit_code, assert_identical):
6188+
self.set_setting('EXIT_RUNTIME')
61936189
self.do_run_from_file(
61946190
test_file('test_sig_default.c'),
61956191
test_file(f'test_sig_default_{suffix}.out'),

test/test_sig_default_sigabrt.out

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
RuntimeError: unreachable
1+
1
2+
Aborted(Signal: Aborted)

0 commit comments

Comments
 (0)