Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AsFd implementations for stdio lock types on WASI. #101768

Merged
merged 3 commits into from
Oct 5, 2022
Merged
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
27 changes: 27 additions & 0 deletions library/std/src/sys/wasi/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ impl AsFd for Stdin {
}
}

#[stable(feature = "io_safety", since = "1.63.0")]
impl<'a> AsFd for io::StdinLock<'a> {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
// SAFETY: user code should not close stdin out from under the standard library
unsafe { BorrowedFd::borrow_raw(0) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why exactly is this correct? A safety comment here would be great to have for someone stumbling upon this without having prior knowledge of the API and WASI spec

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, but note that the implementations of AsFd for UNIX don't have such comments either. Nonetheless, added as a suggestion.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a little tricky. WASI itself doesn't make such a guarantees (Unix doesn't either, for that matter). It's efffectively Rust's std that makes this guarantee/requirement, and for its part, its own API doesn't provide a way to close stdin/stdout/stderr without using unsafe. @joshtriplett's added comments look ok for now, though eventually we should add public documentation about this.

joshtriplett marked this conversation as resolved.
Show resolved Hide resolved
}
}

impl io::Read for Stdin {
fn read(&mut self, data: &mut [u8]) -> io::Result<usize> {
self.read_vectored(&mut [IoSliceMut::new(data)])
Expand Down Expand Up @@ -65,6 +74,15 @@ impl AsFd for Stdout {
}
}

#[stable(feature = "io_safety", since = "1.63.0")]
impl<'a> AsFd for io::StdoutLock<'a> {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
// SAFETY: user code should not close stdout out from under the standard library
unsafe { BorrowedFd::borrow_raw(1) }
joshtriplett marked this conversation as resolved.
Show resolved Hide resolved
}
}

impl io::Write for Stdout {
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
self.write_vectored(&[IoSlice::new(data)])
Expand Down Expand Up @@ -103,6 +121,15 @@ impl AsFd for Stderr {
}
}

#[stable(feature = "io_safety", since = "1.63.0")]
impl<'a> AsFd for io::StderrLock<'a> {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
// SAFETY: user code should not close stderr out from under the standard library
unsafe { BorrowedFd::borrow_raw(2) }
joshtriplett marked this conversation as resolved.
Show resolved Hide resolved
}
}

impl io::Write for Stderr {
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
self.write_vectored(&[IoSlice::new(data)])
Expand Down