Skip to content

Commit

Permalink
Merge #1213
Browse files Browse the repository at this point in the history
1213: Fixed WASI isatty r=syrusakbary a=syrusakbary

<!-- 
Prior to submitting a PR, review the CONTRIBUTING.md document for recommendations on how to test:
https://github.com/wasmerio/wasmer/blob/master/CONTRIBUTING.md#pull-requests

-->

# Description

Current WASI implementation returns a wrong response when libc `isatty` is used for `stdin`, `stdout` or `stderr`.

This PR fixes it.

<!-- 
Provide details regarding the change including motivation,
links to related issues, and the context of the PR.
-->

# Review

- [x] Add a short description of the the change to the CHANGELOG.md file


Co-authored-by: Syrus <[email protected]>
Co-authored-by: Syrus Akbary <[email protected]>
  • Loading branch information
bors[bot] and syrusakbary authored Feb 13, 2020
2 parents 16245c6 + 7a45bd8 commit 1d3ae6a
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
15 changes: 15 additions & 0 deletions lib/wasi-tests/tests/wasitests/isatty.rs
Original file line number Diff line number Diff line change
@@ -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"
);
}
1 change: 1 addition & 0 deletions lib/wasi-tests/tests/wasitests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions lib/wasi-tests/wasitests/isatty.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
stdin: 1
stdout: 1
stderr: 1
20 changes: 20 additions & 0 deletions lib/wasi-tests/wasitests/isatty.rs
Original file line number Diff line number Diff line change
@@ -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");
}
}
Binary file added lib/wasi-tests/wasitests/isatty.wasm
Binary file not shown.
22 changes: 19 additions & 3 deletions lib/wasi/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}
_ => (),
Expand Down
9 changes: 8 additions & 1 deletion lib/wasi/src/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1221,7 +1221,14 @@ pub fn fd_write(
iovs_len: u32,
nwritten: WasmPtr<u32>,
) -> __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));
Expand Down

0 comments on commit 1d3ae6a

Please sign in to comment.