Unexpected exit code with std::process::ExitCode when compiling wasm32-wasip1 #130532
Description
When compiled as a standard binary on the host system, the exit codes are returned as expected. When compiled with wasm32-wasip1, the use of ExitCode results in the exit code of 1, regardless of the value provided to ExitCode::from.
The following are the minimal examples used while reproducing the bug:
/// This example main uses [`std::process::exit`], which works as expected when compiled against the
/// `wasm32-wasip1` target.
fn main() {
println!("Exiting with code 10");
std::process::exit(10);
}
The above example produces the expected error code of 10 when compiling for the the host system and wasm32-wasi
(executed with wasmtime 24.0.0).
/// This example main uses [`std::process::ExitCode`], which does not work as expected when
/// compiled using the `wasm32-wasip1` target.
use std::process::ExitCode;
fn main() -> ExitCode {
println!("Exiting with code 11`");
ExitCode::from(11)
}
The above example produces the exit code 11
when compiled for the host system but returns exit code 1 when compiled with wasm32-wasi
(again run in wasmtime 24.0.0).
Investigative Details
Looking at the resulting WASM (decompiled to WAT) we see the main function returns a i32.const 1
(here compiled with #[no_mangle] for ease of searching
):
(func $main (type 0) (result i32)
(local i32)
global.get $__stack_pointer
;; ...
global.set $__stack_pointer
i32.const 1)
For source code of the reproducer, see: peterschwarz/wasm32-wasip-exit-code-reproducer
Meta
Tested against stable, beta and nightly.
rustc --version --verbose
:
rustc 1.81.0 (eeb90cda1 2024-09-04)
binary: rustc
commit-hash: eeb90cda1969383f56a2637cbd3037bdf598841c
commit-date: 2024-09-04
host: x86_64-unknown-linux-gnu
release: 1.81.0
LLVM version: 18.1.7
rustc +beta --version --verbose
rustc 1.82.0-beta.3 (4976ae480 2024-09-09)
binary: rustc
commit-hash: 4976ae480e2b29cc46f44e1b9914469cc384fcc9
commit-date: 2024-09-09
host: x86_64-unknown-linux-gnu
release: 1.82.0-beta.3
LLVM version: 19.1.0
rustc +nightly --version --verbose
rustc 1.83.0-nightly (28e8f01c2 2024-09-17)
binary: rustc
commit-hash: 28e8f01c2a2f33fb4214925a704e3223b372cad5
commit-date: 2024-09-17
host: x86_64-unknown-linux-gnu
release: 1.83.0-nightly
LLVM version: 19.1.0