Repository navigation
wasip2/wasip3: report main's exit status via _Exit - #925
Conversation
`_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>
| 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 |
There was a problem hiding this comment.
No need for a comment about "Returning .." as that's just describing the old code.
| #include <wasi/report-error.h> | ||
| #if defined(__wasip2__) || defined(__wasip3__) | ||
| #include <stdlib.h> | ||
| #endif |
There was a problem hiding this comment.
No need to make this conditional, it's ok to just include the header
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks for the review! Both are done in e5e9c60: |
|
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>
|
Thanks! clang-format applied in 8085c5a (it only moves the |
Fixes #924.
On wasip2 and wasip3,
_start(exported aswasi:cli/run#run) returnedr != 0, so any nonzero status returned frommainreached the host as 1. This calls_Exit(r)for a nonzerorinstead._Exituseswasi:cli/exit#exit-with-codefor 0-255 since #860, and falls back toexit(err)otherwise. It is_Exitrather thanexitbecause__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-wasip2with the wasi-sdk-34 clang (-DBUILD_TESTS=OFF -DBUILD_SHARED=OFF).return 42;exit(42);return 0;return 300;(outside 0-255)atexit(f); return 7;fran oncefran oncefran onceWith the patch on
wasm32-wasip3under wasmtime 51.0.0:return 42gives 42,exit(42)gives 42,return 0gives 0, andatexit(f); return 7gives 7 withfrunning 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.