Skip to content

Commit b530caf

Browse files
authored
Port _Exit() to use wasip2 method (WebAssembly#611)
WARNING: This commit edits the auto-generated files wasip2.h and wasip2.c to add the _Noreturn attribute to `exit_exit`.
1 parent 2aaf351 commit b530caf

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

libc-bottom-half/cloudlibc/src/libc/stdlib/_Exit.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,22 @@
22
//
33
// SPDX-License-Identifier: BSD-2-Clause
44

5+
#ifdef __wasilibc_use_wasip2
6+
#include <wasi/wasip2.h>
7+
#else
58
#include <wasi/api.h>
9+
#endif
610
#include <_/cdefs.h>
711
#include <stdnoreturn.h>
812
#include <unistd.h>
913

1014
noreturn void _Exit(int status) {
15+
#ifdef __wasilibc_use_wasip2
16+
exit_result_void_void_t exit_status = { .is_err = status != 0 };
17+
exit_exit(&exit_status);
18+
#else
1119
__wasi_proc_exit(status);
20+
#endif
1221
}
1322

1423
__strong_reference(_Exit, _exit);

libc-bottom-half/crt/crt1-command.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
#include <stdatomic.h>
33
extern void __wasi_init_tp(void);
44
#endif
5+
#ifdef __wasilibc_use_wasip2
6+
#include <wasi/wasip2.h>
7+
#else
58
#include <wasi/api.h>
9+
#endif
610
extern void __wasm_call_ctors(void);
711
extern int __main_void(void);
812
extern void __wasm_call_dtors(void);
@@ -47,7 +51,14 @@ void _start(void) {
4751

4852
// If main exited successfully, just return, otherwise call
4953
// `__wasi_proc_exit`.
54+
#ifdef __wasilibc_use_wasip2
55+
if (r != 0) {
56+
exit_result_void_void_t status = { .is_err = true };
57+
exit_exit(&status);
58+
}
59+
#else
5060
if (r != 0) {
5161
__wasi_proc_exit(r);
5262
}
63+
#endif
5364
}

libc-bottom-half/headers/public/wasi/wasip2.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,10 @@ extern bool environment_initial_cwd(wasip2_string_t *ret);
10841084

10851085
// Imported Functions from `wasi:cli/[email protected]`
10861086
// Exit the current instance and any linked instances.
1087-
extern void exit_exit(exit_result_void_void_t *status);
1087+
// NOTE: This file has been manually edited to add the _Noreturn
1088+
// annotation on exit_exit(). If the file is re-generated, this
1089+
// annotation will have to be re-added.
1090+
_Noreturn extern void exit_exit(exit_result_void_void_t *status);
10881091

10891092
// Imported Functions from `wasi:io/[email protected]`
10901093
// Returns a string that is suitable to assist humans in debugging

libc-bottom-half/sources/wasip2.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ extern void __wasm_import_environment_get_arguments(int32_t);
1111
__attribute__((__import_module__("wasi:cli/[email protected]"), __import_name__("initial-cwd")))
1212
extern void __wasm_import_environment_initial_cwd(int32_t);
1313

14+
// NOTE: This file has been manually edited to add the _Noreturn
15+
// annotation on __wasm_import_exit_exit(). If the file is re-generated, this
16+
// annotation will have to be re-added.
1417
__attribute__((__import_module__("wasi:cli/[email protected]"), __import_name__("exit")))
15-
extern void __wasm_import_exit_exit(int32_t);
18+
_Noreturn extern void __wasm_import_exit_exit(int32_t);
1619

1720
__attribute__((__import_module__("wasi:io/[email protected]"), __import_name__("[method]error.to-debug-string")))
1821
extern void __wasm_import_io_error_method_error_to_debug_string(int32_t, int32_t);
@@ -1068,7 +1071,7 @@ bool environment_initial_cwd(wasip2_string_t *ret) {
10681071
return option.is_some;
10691072
}
10701073

1071-
void exit_exit(exit_result_void_void_t *status) {
1074+
_Noreturn void exit_exit(exit_result_void_void_t *status) {
10721075
int32_t result;
10731076
if ((*status).is_err) {
10741077
result = 1;

0 commit comments

Comments
 (0)