diff --git a/CHANGELOG.md b/CHANGELOG.md index 1308dbad45b..d1be9801613 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## **[Unreleased]** +- [#1213](https://github.com/wasmerio/wasmer/pull/1213) Fixed WASI `fdstat` to detect `isatty` properly. - [#1192](https://github.com/wasmerio/wasmer/pull/1192) Use `ExceptionCode` for error representation. - [#1191](https://github.com/wasmerio/wasmer/pull/1191) Fix singlepass miscompilation on `Operator::CallIndirect`. - [#1180](https://github.com/wasmerio/wasmer/pull/1180) Fix compilation for target `x86_64-unknown-linux-musl`. diff --git a/lib/wasi-tests/tests/wasitests/isatty.rs b/lib/wasi-tests/tests/wasitests/isatty.rs new file mode 100644 index 00000000000..55c97a60659 --- /dev/null +++ b/lib/wasi-tests/tests/wasitests/isatty.rs @@ -0,0 +1,15 @@ +// !!! THIS IS A GENERATED FILE !!! +// ANY MANUAL EDITS MAY BE OVERWRITTEN AT ANY TIME +// Files autogenerated with cargo build (build/wasitests.rs). + +#[test] +fn test_isatty() { + assert_wasi_output!( + "../../wasitests/isatty.wasm", + "isatty", + vec![], + vec![], + vec![], + "../../wasitests/isatty.out" + ); +} diff --git a/lib/wasi-tests/tests/wasitests/mod.rs b/lib/wasi-tests/tests/wasitests/mod.rs index d226dc0ee0d..59092573ab2 100644 --- a/lib/wasi-tests/tests/wasitests/mod.rs +++ b/lib/wasi-tests/tests/wasitests/mod.rs @@ -18,6 +18,7 @@ mod file_metadata; mod fs_sandbox_test; mod fseek; mod hello; +mod isatty; mod mapdir; mod path_link; mod path_rename; diff --git a/lib/wasi-tests/wasitests/isatty.out b/lib/wasi-tests/wasitests/isatty.out new file mode 100644 index 00000000000..497b5781889 --- /dev/null +++ b/lib/wasi-tests/wasitests/isatty.out @@ -0,0 +1,3 @@ +stdin: 1 +stdout: 1 +stderr: 1 diff --git a/lib/wasi-tests/wasitests/isatty.rs b/lib/wasi-tests/wasitests/isatty.rs new file mode 100644 index 00000000000..57d343568bf --- /dev/null +++ b/lib/wasi-tests/wasitests/isatty.rs @@ -0,0 +1,20 @@ +// We don't have access to libc, so we just use isatty +// as an external function +// use libc::isatty; + +extern "C" { + pub fn isatty(fd: i32) -> i32; +} + +fn main() { + #[cfg(target = "wasi")] { + println!("stdin: {}", unsafe { isatty(0) }); + println!("stdout: {}", unsafe { isatty(1) }); + println!("stderr: {}", unsafe { isatty(2) }); + } + #[cfg(not(target = "wasi"))] { + println!("stdin: 1"); + println!("stdout: 1"); + println!("stderr: 1"); + } +} diff --git a/lib/wasi-tests/wasitests/isatty.wasm b/lib/wasi-tests/wasitests/isatty.wasm new file mode 100755 index 00000000000..5a414754f9f Binary files /dev/null and b/lib/wasi-tests/wasitests/isatty.wasm differ diff --git a/lib/wasi/src/state/mod.rs b/lib/wasi/src/state/mod.rs index ceb72fdc837..f7d022aa0d8 100644 --- a/lib/wasi/src/state/mod.rs +++ b/lib/wasi/src/state/mod.rs @@ -1045,12 +1045,28 @@ impl WasiFs { pub fn fdstat(&self, fd: __wasi_fd_t) -> Result<__wasi_fdstat_t, __wasi_errno_t> { match fd { - __WASI_STDOUT_FILENO => { + __WASI_STDIN_FILENO => { return Ok(__wasi_fdstat_t { fs_filetype: __WASI_FILETYPE_CHARACTER_DEVICE, fs_flags: 0, - fs_rights_base: ALL_RIGHTS, - fs_rights_inheriting: ALL_RIGHTS, + fs_rights_base: STDIN_DEFAULT_RIGHTS, + fs_rights_inheriting: 0, + }) + } + __WASI_STDOUT_FILENO => { + return Ok(__wasi_fdstat_t { + fs_filetype: __WASI_FILETYPE_CHARACTER_DEVICE, + fs_flags: __WASI_FDFLAG_APPEND, + fs_rights_base: STDOUT_DEFAULT_RIGHTS, + fs_rights_inheriting: 0, + }) + } + __WASI_STDERR_FILENO => { + return Ok(__wasi_fdstat_t { + fs_filetype: __WASI_FILETYPE_CHARACTER_DEVICE, + fs_flags: __WASI_FDFLAG_APPEND, + fs_rights_base: STDERR_DEFAULT_RIGHTS, + fs_rights_inheriting: 0, }) } _ => (), diff --git a/lib/wasi/src/syscalls/mod.rs b/lib/wasi/src/syscalls/mod.rs index 6c60863ecc3..ea253968cc1 100644 --- a/lib/wasi/src/syscalls/mod.rs +++ b/lib/wasi/src/syscalls/mod.rs @@ -1221,7 +1221,14 @@ pub fn fd_write( iovs_len: u32, nwritten: WasmPtr, ) -> __wasi_errno_t { - debug!("wasi::fd_write: fd={}", fd); + // If we are writing to stdout or stderr + // we skip debug to not pollute the stdout/err + // and do debugging happily after :) + if fd != __WASI_STDOUT_FILENO && fd != __WASI_STDERR_FILENO { + debug!("wasi::fd_write: fd={}", fd); + } else { + trace!("wasi::fd_write: fd={}", fd); + } let (memory, state) = get_memory_and_wasi_state(ctx, 0); let iovs_arr_cell = wasi_try!(iovs.deref(memory, 0, iovs_len)); let nwritten_cell = wasi_try!(nwritten.deref(memory));