Skip to content

wasip2/wasip3: report main's exit status via _Exit - #925

Merged
alexcrichton merged 3 commits into
WebAssembly:mainfrom
avrabe:fix-main-exit-code
Dec 1, 2026
Merged

alexcrichton merged 3 commits into
WebAssembly:mainfrom
avrabe:fix-main-exit-code

Conversation

@avrabe

@avrabe avrabe commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Fixes #924.

On wasip2 and wasip3, _start (exported as wasi:cli/run#run) returned r != 0, so any nonzero status returned from main reached the host as 1. This calls _Exit(r) for a nonzero r instead. _Exit uses wasi:cli/exit#exit-with-code for 0-255 since #860, and falls back to exit(err) otherwise. It is _Exit rather than exit because __wasm_call_dtors() has already run the atexit handlers and destructors. __wasilibc_assert_no_descriptor_leaks() still runs first. This mirrors the wasip1 branch, which calls __wasi_proc_exit(r) after the destructors.

Results: wasi-libc main 238d349, with and without this patch, built for wasm32-wasip2 with the wasi-sdk-34 clang (-DBUILD_TESTS=OFF -DBUILD_SHARED=OFF).

program unpatched, wasmtime 51.0.0 patched, wasmtime 50.0.3 patched, wasmtime 51.0.0
return 42; 1 42 42
exit(42); 42 42 42
return 0; 0 0 0
return 300; (outside 0-255) 1 1 1
atexit(f); return 7; 1, f ran once 7, f ran once 7, f ran once

With the patch on wasm32-wasip3 under wasmtime 51.0.0: return 42 gives 42, exit(42) gives 42, return 0 gives 0, and atexit(f); return 7 gives 7 with f running once.

There is no new test. The test harness checks pass/fail (exit 0, or an output regex), so asserting a specific status like 42 would need a small wrapper around the engine invocation. I'm happy to add one if you'd like it in a particular shape.

Disclosure: this patch and its description were prepared with an AI coding assistant (Claude Code), which also built both sysroots and ran the programs above.

`_start` returned `r != 0` as the `wasi:cli/run` result, so any nonzero
status returned from `main` reached the host as 1. Call `_Exit(r)` for a
nonzero `r` instead; since WebAssembly#860 it uses `exit-with-code` for 0-255.
Destructors and atexit handlers have already run, hence `_Exit` rather
than `exit`. This matches the wasip1 branch, which calls
`__wasi_proc_exit(r)`.

Fixes WebAssembly#924

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread libc-bottom-half/crt/crt1-command.c Outdated
return r != 0;
// If main exited successfully, just return, otherwise report its exit
// status via `_Exit`, which uses `exit-with-code` for 0-255. Returning
// `r != 0` from `run` would report every nonzero status as 1. Destructors

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for a comment about "Returning .." as that's just describing the old code.

Comment thread libc-bottom-half/crt/crt1-command.c Outdated
#include <wasi/report-error.h>
#if defined(__wasip2__) || defined(__wasip3__)
#include <stdlib.h>
#endif

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to make this conditional, it's ok to just include the header

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@avrabe

avrabe commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review! Both are done in e5e9c60: <stdlib.h> is now included unconditionally, and the comment about returning r != 0 is gone. I rebuilt the wasm32-wasip2 and wasm32-wasip3 sysroots and re-ran the reproduction under wasmtime 51.0.0: return 0/1/2/42 exit with 0/1/2/42, exit(42) with 42, and atexit(f); return 7 with 7 and f running once.

@alexcrichton

Copy link
Copy Markdown
Collaborator

I think this'll need a clang-format, and you can ignore the macos failures that's just CI weather

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@avrabe

avrabe commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

Thanks! clang-format applied in 8085c5a (it only moves the <stdlib.h> include into sorted order).

@alexcrichton
alexcrichton merged commit 304dc04 into WebAssembly:main Dec 1, 2026
72 of 74 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

wasip2/wasip3: a nonzero status returned from main is reported as exit 1

2 participants