Skip to content

Commit 5472428

Browse files
authored
Disallow non-portable and signal values as exit statuses. (#235)
* Disallow non-portable and signal values as exit statuses. Exit codes of at least 256 aren't portable to [POSIX exit], so programs expecting to return full 32-bit [Windows System Error Codes] aren't practically portable. And on POSIX, error codes of at least 128 are reserved for reporting program exits via signals, and 127 and 126 are reserved for POSIX-style shells. While it's theoretically possible for POSIX applications to return these explicitly, this is very rare, not often useful, particularly in programs intended to be portable, and could potentially be confusing to users. If a need arrises for programs to return values in [126,256), or to provide other kinds of information upon program exit, we can look at relaxing these restrictions or adding new APIs to WASI for program termination, but for now it makes sense to start with something simple. With that, this PR proposes: - The WASI `exit` function takes a `u8`, but if the value is at least 126, it traps. Otherwise it is provided to the environment. - WASI libc's `exit` will map from `int` to `u8` by applying the mask as specified in [POSIX exit]. No other WASI syscalls trap right now, but `exit` has no other way to indicate errors. [POSIX exit]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/_Exit.html [Windows System Error Codes]: https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes?redirectedfrom=MSDN#system-error-codes * Update the docs. * Say "or greater" instead of "at least". Co-authored-by: Dan Gohman <[email protected]>
1 parent 1b9709f commit 5472428

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

phases/ephemeral/docs.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2389,9 +2389,12 @@ The number of events stored.
23892389
---
23902390

23912391
#### <a href="#exit" name="exit"></a> `exit(rval: exitcode)`
2392-
Terminate the process normally. An exit code of 0 indicates successful
2393-
termination of the program. The meanings of other values is dependent on
2394-
the environment.
2392+
Terminate the process normally. An exit code of `$exitcode::success`
2393+
reports successful completion of the program. An exit code of
2394+
`$exitcode::failure` or any other value less than 126 reports a
2395+
failure, and the value is provided to the environment. If a value
2396+
of 126 or greater is given, this function behaves as if it were
2397+
implemented by an `unreachable` instruction.
23952398

23962399
##### Params
23972400
- <a href="#exit.rval" name="exit.rval"></a> `rval`: [`exitcode`](#exitcode)

phases/ephemeral/witx/typenames.witx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -638,8 +638,18 @@
638638
)
639639
)
640640

641-
;;; Exit code generated by a process when exiting.
642-
(typename $exitcode u32)
641+
;;; Exit code generated by a program when exiting.
642+
(typename $exitcode u8)
643+
644+
;;; Indicate the program exited successfully.
645+
;;;
646+
;;; Note: This is similar to `EXIT_SUCCESS` in POSIX.
647+
(@witx const $exitcode $success 0)
648+
649+
;;; Indicate the program exited unsuccessfully.
650+
;;;
651+
;;; Note: This is similar to `EXIT_FAILURE` in POSIX.
652+
(@witx const $exitcode $failure 1)
643653

644654
;;; Flags provided to `sock_recv`.
645655
(typename $riflags

phases/ephemeral/witx/wasi_ephemeral_proc.witx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
(use "typenames.witx")
99

1010
(module $wasi_ephemeral_proc
11-
;;; Terminate the process normally. An exit code of 0 indicates successful
12-
;;; termination of the program. The meanings of other values is dependent on
13-
;;; the environment.
11+
;;; Terminate the process normally. An exit code of `$exitcode::success`
12+
;;; reports successful completion of the program. An exit code of
13+
;;; `$exitcode::failure` or any other value less than 126 reports a
14+
;;; failure, and the value is provided to the environment. If a value
15+
;;; of 126 or greater is given, this function behaves as if it were
16+
;;; implemented by an `unreachable` instruction.
1417
(@interface func (export "exit")
1518
;;; The exit code returned by the process.
1619
(param $rval $exitcode)

0 commit comments

Comments
 (0)