Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .github/workflows/wasi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,16 @@ 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
# scratch directories resolve.
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:
Expand Down Expand Up @@ -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:: \
Expand Down
6 changes: 6 additions & 0 deletions src/bin/coreutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/common/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/uucore/src/lib/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};
Expand Down
21 changes: 20 additions & 1 deletion src/uucore/src/lib/mods/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Comment thread
eduardomourar marked this conversation as resolved.
Resolved
///
/// `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 }
Comment thread
eduardomourar marked this conversation as resolved.
Resolved
}

/// Print `<util>: <prefix><msg>` to stderr, used by [`crate::show!`] and friends.
///
/// stderr is unbuffered, so the line is formatted first and written at once.
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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);
}
}
26 changes: 25 additions & 1 deletion tests/by-util/test_nice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))]
Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -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() {
Expand Down Expand Up @@ -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() {
Expand All @@ -148,6 +161,7 @@ fn test_trailing_empty_adjustment() {
);
}

#[uutests::wasi_ignore(NoSubprocessSpawning)]
#[test]
#[cfg(unix)]
fn test_nice_huge() {
Expand All @@ -161,13 +175,23 @@ fn test_nice_huge() {
.no_stdout();
}

#[uutests::wasi_ignore(NoSubprocessSpawning)]
#[test]
#[cfg(unix)]
fn test_nice_huge_negative() {
new_ucmd!().args(&["-n", "-9999999999", "true"]).succeeds();
//.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() {
Expand Down
Loading