Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected exit code with std::process::ExitCode when compiling wasm32-wasip1 #130532

Closed
peterschwarz opened this issue Sep 18, 2024 · 6 comments · Fixed by #130554
Closed

Unexpected exit code with std::process::ExitCode when compiling wasm32-wasip1 #130532

peterschwarz opened this issue Sep 18, 2024 · 6 comments · Fixed by #130554
Assignees
Labels
C-bug Category: This is a bug. O-wasm Target: WASM (WebAssembly), http://webassembly.org/ T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@peterschwarz
Copy link

peterschwarz commented Sep 18, 2024

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
@peterschwarz peterschwarz added the C-bug Category: This is a bug. label Sep 18, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Sep 18, 2024
@workingjubilee
Copy link
Member

It seems that we just call libc::exit?

pub fn exit(code: i32) -> ! {
unsafe { libc::exit(code) }
}

@workingjubilee workingjubilee added O-wasm Target: WASM (WebAssembly), http://webassembly.org/ O-wasi Operating system: Wasi, Webassembly System Interface labels Sep 18, 2024
@ShE3py
Copy link
Contributor

ShE3py commented Sep 18, 2024

Minimized:

#![crate_type = "cdylib"]

use std::process::ExitCode;

#[no_mangle]
pub fn fun() -> ExitCode {
    ExitCode::from(2)
}
(func $fun (;0;) (type 0) (result i32)
  i32.const 1
)

impl From<u8> for ExitCode {
fn from(code: u8) -> Self {
match code {
0 => Self::SUCCESS,
1..=255 => Self::FAILURE,
}
}
}

@rustbot claim
@rustbot label -O-wasi

@rustbot rustbot removed the O-wasi Operating system: Wasi, Webassembly System Interface label Sep 18, 2024
@workingjubilee
Copy link
Member

workingjubilee commented Sep 18, 2024

Oh, "cool"!

@peterschwarz
Copy link
Author

@ShE3py that is so concise! Thanks.

I’d also be very interested in getting some insight on where to look in the code base for this sort of thing.

@saethlin saethlin added T-libs Relevant to the library team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Sep 19, 2024
@ShE3py
Copy link
Contributor

ShE3py commented Sep 19, 2024

I’d also be very interested in getting some insight on where to look in the code base for this sort of thing.

I'm kinda just abusing my IDE (RustRover)’s Go To Declaration & Find Usages shortcuts, you may need to tell yours to use the wasm target for name resolution/code indexing instead of your OS’ default one.

@bors bors closed this as completed in 553c20c Sep 19, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Sep 19, 2024
Rollup merge of rust-lang#130554 - ShE3py:unsupported-exitcode, r=Noratrieb

`pal::unsupported::process::ExitCode`: use an `u8` instead of a `bool`

`ExitCode` should “represents the status code the current process can return to its parent under normal termination”, but is currently represented as a `bool` on unsupported platforms, making the `impl From<u8> for ExitCode` lossy.

Fixes rust-lang#130532.

History: [IRLO thread](https://internals.rust-lang.org/t/mini-pre-rfc-redesigning-process-exitstatus/5426) (`ExitCode` as a `main` return), rust-lang#48618 (initial impl), rust-lang#93445 (`From<u8>` impl).
@peterschwarz
Copy link
Author

I ran the tests in my reproducer repo against the latest nightly (post merge of #130554) and the all pass now. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. O-wasm Target: WASM (WebAssembly), http://webassembly.org/ T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants