diff --git a/.github/workflows/wasi.yml b/.github/workflows/wasi.yml index bbafa7178d9..71eab13559e 100644 --- a/.github/workflows/wasi.yml +++ b/.github/workflows/wasi.yml @@ -51,7 +51,7 @@ jobs: run: | cargo test --target ${{ matrix.job.target }} --no-default-features \ $(cargo tree --depth 1 --format "{lib}" --features=feat_wasm --prefix none | sed -n 's/^uu_/-p &/p') - - name: Run uucore fs unit tests + - name: Run uucore unit tests env: # The fs tests touch the filesystem, so wasmtime must preopen a # directory; map the host cwd to the guest root so the tests' relative @@ -59,7 +59,8 @@ jobs: CARGO_TARGET_WASM32_WASIP1_RUNNER: wasmtime --dir=.::/ -- CARGO_TARGET_WASM32_WASIP2_RUNNER: wasmtime --dir=.::/ -- run: | - cargo test --target ${{ matrix.job.target }} --no-default-features --features fs -p uucore fs::tests + cargo test --target ${{ matrix.job.target }} --no-default-features --features fs -p uucore \ + -- fs::tests mods::error::tests - name: Run integration tests via wasmtime if: matrix.job.target == 'wasm32-wasip1' env: @@ -87,9 +88,8 @@ jobs: test_arch:: test_b2sum:: test_base32:: test_base64:: test_basenc:: \ test_basename:: test_cat:: test_cksum:: test_comm:: test_cp:: test_cut:: test_date:: \ test_dir:: test_dircolors:: test_dirname:: test_echo:: test_expand:: test_expr:: \ - test_factor:: test_false:: test_fmt:: test_fold:: test_head:: test_join:: test_link:: test_ln:: \ - test_ls:: \ - test_md5sum:: test_mkdir:: test_mktemp:: test_mv:: test_nl:: test_nproc:: test_numfmt:: test_od:: \ + test_factor:: test_false:: test_fmt:: test_fold:: test_head:: test_join:: test_link:: test_ln:: test_ls:: \ + test_md5sum:: test_mkdir:: test_mktemp:: test_mv:: test_nice:: test_nl:: test_nproc:: test_numfmt:: test_od:: \ test_paste:: test_pathchk:: test_pr:: test_printenv:: test_printf:: test_ptx:: \ test_pwd:: test_readlink:: test_realpath:: test_rmdir:: test_seq:: test_sha1sum:: test_sha224sum:: test_sha256sum:: \ test_sha384sum:: test_sha512sum:: test_shred:: test_shuf:: test_sleep:: \ diff --git a/src/bin/coreutils.rs b/src/bin/coreutils.rs index 39efff43202..c28b01648cc 100644 --- a/src/bin/coreutils.rs +++ b/src/bin/coreutils.rs @@ -147,7 +147,13 @@ fn main() -> ExitCode { // #[cfg(not(feature = "only_english"))] validation::setup_localization_or_exit(util); let code = uumain(vec![util_os].into_iter().chain(args)); + // `exit` remaps the code for WASI's `proc_exit`; preview 2 also maps + // every nonzero `ExitCode` returned from main to 1, so WASI always + // has to go through `exit` rather than return. + #[cfg(target_os = "wasi")] + exit(code); // ExitCode only holds a u8; larger codes (Windows) go through exit. + #[cfg(not(target_os = "wasi"))] return u8::try_from(code).map_or_else(|_| exit(code), ExitCode::from); } // GNU coreutils --help string shows help for coreutils diff --git a/src/common/validation.rs b/src/common/validation.rs index 1dbc141db16..733cdf2791d 100644 --- a/src/common/validation.rs +++ b/src/common/validation.rs @@ -28,6 +28,8 @@ unsafe extern "C" { /// Terminates the process with `code`, flushing the PGO counters when the /// binary was built for profile training on Windows. pub fn exit(code: i32) -> ! { + #[cfg(target_os = "wasi")] + let code = uucore::error::wasi_safe_exit_code(code); #[cfg(all(pgo_training, windows))] // SAFETY: `__llvm_profile_write_file` is provided by the LLVM profiling // runtime, which is linked in whenever this cfg is set (the same build diff --git a/src/uucore/src/lib/lib.rs b/src/uucore/src/lib/lib.rs index 0495dbc692c..62d106d1701 100644 --- a/src/uucore/src/lib/lib.rs +++ b/src/uucore/src/lib/lib.rs @@ -211,6 +211,9 @@ macro_rules! bin_inner { let code = $util::uumain(uucore::args_os()); $post + // WASI's proc_exit traps on exit statuses outside [0, 126); remap first. + #[cfg(target_os = "wasi")] + let code = uucore::error::wasi_safe_exit_code(code); std::process::exit(code); } }; diff --git a/src/uucore/src/lib/mods/error.rs b/src/uucore/src/lib/mods/error.rs index aeb95e21073..9e95b2efeb8 100644 --- a/src/uucore/src/lib/mods/error.rs +++ b/src/uucore/src/lib/mods/error.rs @@ -92,6 +92,14 @@ pub fn set_exit_code(code: i32) { EXIT_CODE.store(code, Ordering::SeqCst); } +/// Remaps an exit code into the range WASI's `proc_exit` accepts. +/// +/// `proc_exit` traps the whole module for any status outside `[0, 126)`; +/// remap the rest to 125 so the exit stays non-zero without crashing it. +pub fn wasi_safe_exit_code(code: i32) -> i32 { + if (0..126).contains(&code) { code } else { 125 } +} + /// Print `: ` to stderr, used by [`crate::show!`] and friends. /// /// stderr is unbuffered, so the line is formatted first and written at once. @@ -793,7 +801,7 @@ impl Display for ClapErrorWrapper { #[cfg(test)] mod tests { - use super::{USimpleError, UUsageError, quiet_if_reported}; + use super::{USimpleError, UUsageError, quiet_if_reported, wasi_safe_exit_code}; /// A quieted error keeps its code but says nothing: the report already did. #[test] @@ -851,4 +859,15 @@ mod tests { .to_string() ); } + + #[test] + fn test_wasi_safe_exit_code() { + assert_eq!(wasi_safe_exit_code(0), 0); + assert_eq!(wasi_safe_exit_code(1), 1); + assert_eq!(wasi_safe_exit_code(125), 125); + assert_eq!(wasi_safe_exit_code(126), 125); + assert_eq!(wasi_safe_exit_code(127), 125); + assert_eq!(wasi_safe_exit_code(128 + 9), 125); + assert_eq!(wasi_safe_exit_code(255), 125); + } } diff --git a/tests/by-util/test_nice.rs b/tests/by-util/test_nice.rs index c1d9c112789..d3ce1388bff 100644 --- a/tests/by-util/test_nice.rs +++ b/tests/by-util/test_nice.rs @@ -12,11 +12,17 @@ use uutests::new_ucmd; #[cfg(unix)] #[cfg(not(target_os = "android"))] fn test_get_current_niceness() { - // Test that the nice command with no arguments returns the default nice value + // Test that the nice command with no arguments returns the default nice value. + // WASI has no getpriority syscall, so nice always reports a fixed niceness of 0 + // there, regardless of the host's actual value. + #[cfg(wasi_runner)] + let nice = 0; + #[cfg(not(wasi_runner))] let nice = rustix::process::getpriority_process(None).unwrap(); new_ucmd!().succeeds().stdout_is(format!("{nice}\n")); } +#[uutests::wasi_ignore(NoSubprocessSpawning)] #[test] #[cfg(unix)] #[cfg(not(target_os = "android"))] @@ -41,12 +47,14 @@ fn test_adjustment_with_no_command_should_error() { .usage_error("A command must be given with an adjustment."); } +#[uutests::wasi_ignore(NoSubprocessSpawning)] #[test] #[cfg(unix)] fn test_command_with_no_adjustment() { new_ucmd!().args(&["echo", "a"]).succeeds().stdout_is("a\n"); } +#[uutests::wasi_ignore(NoSubprocessSpawning)] #[test] #[cfg(unix)] fn test_command_with_no_args() { @@ -56,6 +64,7 @@ fn test_command_with_no_args() { .stdout_is("\n"); } +#[uutests::wasi_ignore(NoSubprocessSpawning)] #[test] #[cfg(unix)] fn test_command_with_args() { @@ -65,6 +74,7 @@ fn test_command_with_args() { .stdout_is("a b c\n"); } +#[uutests::wasi_ignore(NoSubprocessSpawning)] #[test] #[cfg(unix)] fn test_command_where_command_takes_n_flag() { @@ -74,6 +84,7 @@ fn test_command_where_command_takes_n_flag() { .stdout_is("a"); } +#[uutests::wasi_ignore(NonUtf8ArgsUnsupported)] #[test] #[cfg(unix)] fn test_command_with_non_utf8_args() { @@ -87,6 +98,7 @@ fn test_command_with_non_utf8_args() { .stdout_only_bytes(b"a\xffb\n"); } +#[uutests::wasi_ignore(NoSubprocessSpawning)] #[test] #[cfg(unix)] fn test_non_utf8_command_not_found() { @@ -127,6 +139,7 @@ fn test_invalid_argument() { new_ucmd!().arg("--invalid").fails_with_code(125); } +#[uutests::wasi_ignore(NoSubprocessSpawning)] #[test] #[cfg(unix)] fn test_bare_adjustment() { @@ -148,6 +161,7 @@ fn test_trailing_empty_adjustment() { ); } +#[uutests::wasi_ignore(NoSubprocessSpawning)] #[test] #[cfg(unix)] fn test_nice_huge() { @@ -161,6 +175,7 @@ fn test_nice_huge() { .no_stdout(); } +#[uutests::wasi_ignore(NoSubprocessSpawning)] #[test] #[cfg(unix)] fn test_nice_huge_negative() { @@ -168,6 +183,15 @@ fn test_nice_huge_negative() { //.stderr_contains("Permission denied"); Depending on platform? } +// Because WASI lacks subprocess support, `nice` fails with exit code 126. +// This tests that Wasmtime exits cleanly via `proc_exit` rather than trapping. +#[test] +#[cfg(all(unix, wasi_runner))] +fn test_exec_failure_remaps_exit_code_under_wasi() { + new_ucmd!().arg("true").fails_with_code(125); +} + +#[uutests::wasi_ignore(NoSubprocessSpawning)] #[test] #[cfg(unix)] fn test_sign_middle() {