From 5b0a9261f29f90d574e57129d4cecce91ecb9f35 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Tue, 20 Dec 2022 14:49:32 +0900 Subject: [PATCH 1/2] crt1-command.c: always call wasi_proc_exit for _REENTRANT Note: a return from main() is an equivalent of exit(), which should terminate sibling threads. --- libc-bottom-half/crt/crt1-command.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/libc-bottom-half/crt/crt1-command.c b/libc-bottom-half/crt/crt1-command.c index fb9ee71fb..3badc2e30 100644 --- a/libc-bottom-half/crt/crt1-command.c +++ b/libc-bottom-half/crt/crt1-command.c @@ -45,9 +45,14 @@ void _start(void) { // Call atexit functions, destructors, stdio cleanup, etc. __wasm_call_dtors(); +#ifdef _REENTRANT + // `__wasi_proc_exit` terminates sibling threads. + __wasi_proc_exit(r); +#else // If main exited successfully, just return, otherwise call // `__wasi_proc_exit`. if (r != 0) { __wasi_proc_exit(r); } +#endif } From 43cc8ffdf107e8fee8ea4d53b5c1749db1a7e1fd Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Tue, 20 Dec 2022 14:58:30 +0900 Subject: [PATCH 2/2] crt1-commant.c: add a comment --- libc-bottom-half/crt/crt1-command.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/libc-bottom-half/crt/crt1-command.c b/libc-bottom-half/crt/crt1-command.c index 3badc2e30..5877577b4 100644 --- a/libc-bottom-half/crt/crt1-command.c +++ b/libc-bottom-half/crt/crt1-command.c @@ -42,6 +42,14 @@ void _start(void) { // arguments and calls `__main_argv_argc`. int r = __main_void(); + // The following code is basically an inlined copy of exit(). + // It's mandated by the C standard: + // + // > If the return type of the main function is a type compatible + // > with int, a return from the initial call to the main function + // > is equivalent to calling the exit function with the value + // > returned by the main function as its argument, + // Call atexit functions, destructors, stdio cleanup, etc. __wasm_call_dtors();