bytecodealliance / preview2-prototyping

Polyfill adapter for preview1-using wasm modules to call preview2 functions.
Other
77 stars 20 forks source link

stdin/out/err issues #146

Closed yamt closed 1 year ago

yamt commented 1 year ago

when running an adapted preview1 commands with host cli,

  1. fd_fdstat_get says stdin etc are of __WASI_FILETYPE_UNKNOWN while i expected them being __WASI_FILETYPE_CHARACTER_DEVICE.
  2. fcntl(F_SETFL) to set O_NONBLOCK on them fails.
yamt commented 1 year ago

a small demo of the first problem. https://github.com/yamt/garbage/tree/master/wasm/stat

spacetanuki% ./test.sh
===== host cli
__wasi_fd_fdstat_get returned 0
fs_filetype 0
fs_rights_base 2
===== toywasm
__wasi_fd_fdstat_get returned 0
fs_filetype 2
fs_rights_base 0
===== wasmtime
__wasi_fd_fdstat_get returned 0
fs_filetype 2
fs_rights_base 8e001db
===== iwasm
__wasi_fd_fdstat_get returned 0
fs_filetype 2
fs_rights_base 820004a
spacetanuki% 
yamt commented 1 year ago

the first problem confuses this isatty: https://github.com/yamt/toywasm/blob/4a8f13cc0e315a0a2df91570cc70b6ea95fbac36/libwasi/wasi.c#L3296

the second problem was observed here: https://github.com/yamt/toywasm/blob/4a8f13cc0e315a0a2df91570cc70b6ea95fbac36/libwasi/wasi.c#L3309

sunfishcode commented 1 year ago

when running an adapted preview1 commands with host cli,

1. `fd_fdstat_get` says stdin etc are of `__WASI_FILETYPE_UNKNOWN` while i expected them being `__WASI_FILETYPE_CHARACTER_DEVICE`.

We're currently lacking a way to implement isatty in preview2; once we add that, we can make the preview1 adapter call it and have fd_fdstat_get set the type to __WASI_FILETYPE_CHARACTER_DEVICE,

2. fcntl(F_SETFL) to set O_NONBLOCK on them on them fails.

It isn't implemented yet. It should be failing with ENOTSUP for now.

yamt commented 1 year ago

when running an adapted preview1 commands with host cli,

1. `fd_fdstat_get` says stdin etc are of `__WASI_FILETYPE_UNKNOWN` while i expected them being `__WASI_FILETYPE_CHARACTER_DEVICE`.

We're currently lacking a way to implement isatty in preview2; once we add that, we can make the preview1 adapter call it and have fd_fdstat_get set the type to __WASI_FILETYPE_CHARACTER_DEVICE,

ok. it makes sense.

2. fcntl(F_SETFL) to set O_NONBLOCK on them on them fails.

It isn't implemented yet. It should be failing with ENOTSUP for now.

it's actually EINVAL. (28) https://github.com/bytecodealliance/preview2-prototyping/issues/141#issuecomment-1510780527

pchickey commented 1 year ago

Is this still an issue? I added support for setting O_NONBLOCK through the adapter in https://github.com/bytecodealliance/preview2-prototyping/pull/149/, though it doesn't actually do anything on the host side, I am going to work on that soon

pchickey commented 1 year ago

I can have the adapter make stdio a character device instead of unknown, as long as that seems like its always the correct choice for the stdio streams, @sunfishcode agree?

sunfishcode commented 1 year ago

@pchickey Use cases want isatty to tell them whether they're talking to a terminal, which is useful to support, so we should make it reflect whether they're connected to a host terminal. I think we should add a function to WASI to provide that information.

pchickey commented 1 year ago

Ok. I will send a PR that changes it to character device and we will hook that to stream isatty when its available, and have it be unknown if not.