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

I/O safety. #87329

Merged
merged 28 commits into from
Aug 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
d154185
I/O safety.
sunfishcode Jul 1, 2021
3a38511
Update library/std/src/os/unix/io/fd.rs
sunfishcode Jul 21, 2021
a23ca7c
Update library/std/src/os/windows/io/handle.rs
sunfishcode Jul 21, 2021
1c6bf04
Update library/std/src/os/windows/io/socket.rs
sunfishcode Jul 28, 2021
1f8a450
Add a test to ensure that RawFd is the size we assume it is.
sunfishcode Jul 27, 2021
6b4dbdb
Be more precise about `mmap` and undefined behavior.
sunfishcode Jul 27, 2021
31f7bf8
Add a comment about `OptionFileHandle`.
sunfishcode Jul 27, 2021
926344a
Add a comment about how `OwnedHandle` should not be used with registr…
sunfishcode Jul 28, 2021
45b5de3
Delete a spurious empty comment line.
sunfishcode Jul 28, 2021
0cb69de
Rename `OwnedFd`'s private field to match it's debug output.
sunfishcode Jul 28, 2021
6486f89
Add Owned*, Borrowed*, and As* to the preludes.
sunfishcode Jul 28, 2021
6d72117
Add Safety comments to the `As*` for `Owned*` implementations.
sunfishcode Jul 28, 2021
1b35f74
Reword the description of dup2/dup3.
sunfishcode Jul 28, 2021
68964a7
Fix copypasta of "Unix" within the WASI directory.
sunfishcode Jul 28, 2021
ab08639
Add comments about impls for File, TcpStream, ChildStdin, etc.
sunfishcode Jul 28, 2021
907f00b
Add more comments about the `INVALID_HANDLE_VALUE` situation.
sunfishcode Jul 28, 2021
71dab73
Synchronize minor differences between Unix and WASI implementations.
sunfishcode Jul 28, 2021
1dbd6d6
Factor out Unix and WASI fd code into a common module.
sunfishcode Jul 28, 2021
18a9f46
Don't encourage migration until io_safety is stablized.
sunfishcode Jul 28, 2021
1ae1eee
Rename OptionFileHandle to HandleOrInvalid and make it just wrap an O…
sunfishcode Jul 29, 2021
cada5fb
Update PidFd for the new I/O safety APIs.
sunfishcode Aug 3, 2021
a7d9ab5
Fix an unused import warning.
sunfishcode Aug 4, 2021
9b99f8c
Remove the `#![feature(io_safety)]` from lib.rs.
sunfishcode Aug 6, 2021
6f87288
Use the correct `into_*` on Windows to avoid dropping a stdio handle.
sunfishcode Aug 13, 2021
187ee5c
Add I/O safety trait impls for process::Stdio and process::Child.
sunfishcode Aug 17, 2021
0377a63
Fix syntax for non-doc comments, and use `crate::` instead of `std::`.
sunfishcode Aug 19, 2021
e555003
Factor out a common `RawFd`/`AsRawFd`/etc for Unix and WASI.
sunfishcode Aug 19, 2021
b4dfa19
Fix doc test failures on Windows.
sunfishcode Aug 19, 2021
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
6 changes: 6 additions & 0 deletions library/std/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,12 @@ impl File {
}
}

// In addition to the `impl`s here, `File` also has `impl`s for
// `AsFd`/`From<OwnedFd>`/`Into<OwnedFd>` and
// `AsRawFd`/`IntoRawFd`/`FromRawFd`, on Unix and WASI, and
// `AsHandle`/`From<OwnedHandle>`/`Into<OwnedHandle>` and
// `AsRawHandle`/`IntoRawHandle`/`FromRawHandle` on Windows.

impl AsInner<fs_imp::File> for File {
fn as_inner(&self) -> &fs_imp::File {
&self.inner
Expand Down
12 changes: 12 additions & 0 deletions library/std/src/net/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,12 @@ impl TcpStream {
}
}

// In addition to the `impl`s here, `TcpStream` also has `impl`s for
// `AsFd`/`From<OwnedFd>`/`Into<OwnedFd>` and
// `AsRawFd`/`IntoRawFd`/`FromRawFd`, on Unix and WASI, and
// `AsSocket`/`From<OwnedSocket>`/`Into<OwnedSocket>` and
// `AsRawSocket`/`IntoRawSocket`/`FromRawSocket` on Windows.

#[stable(feature = "rust1", since = "1.0.0")]
impl Read for TcpStream {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
Expand Down Expand Up @@ -908,6 +914,12 @@ impl TcpListener {
}
}

// In addition to the `impl`s here, `TcpListener` also has `impl`s for
// `AsFd`/`From<OwnedFd>`/`Into<OwnedFd>` and
// `AsRawFd`/`IntoRawFd`/`FromRawFd`, on Unix and WASI, and
// `AsSocket`/`From<OwnedSocket>`/`Into<OwnedSocket>` and
// `AsRawSocket`/`IntoRawSocket`/`FromRawSocket` on Windows.

#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> Iterator for Incoming<'a> {
type Item = io::Result<TcpStream>;
Expand Down
6 changes: 6 additions & 0 deletions library/std/src/net/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,12 @@ impl UdpSocket {
}
}

// In addition to the `impl`s here, `UdpSocket` also has `impl`s for
// `AsFd`/`From<OwnedFd>`/`Into<OwnedFd>` and
// `AsRawFd`/`IntoRawFd`/`FromRawFd`, on Unix and WASI, and
// `AsSocket`/`From<OwnedSocket>`/`Into<OwnedSocket>` and
// `AsRawSocket`/`IntoRawSocket`/`FromRawSocket` on Windows.

impl AsInner<net_imp::UdpSocket> for UdpSocket {
fn as_inner(&self) -> &net_imp::UdpSocket {
&self.0
Expand Down
3 changes: 1 addition & 2 deletions library/std/src/net/udp/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::io::ErrorKind;
use crate::net::test::{next_test_ip4, next_test_ip6};
use crate::net::*;
use crate::sync::mpsc::channel;
use crate::sys_common::AsInner;
use crate::thread;
use crate::time::{Duration, Instant};

Expand Down Expand Up @@ -173,7 +172,7 @@ fn debug() {
let socket_addr = next_test_ip4();

let udpsock = t!(UdpSocket::bind(&socket_addr));
let udpsock_inner = udpsock.0.socket().as_inner();
let udpsock_inner = udpsock.0.socket().as_raw();
let compare = format!("UdpSocket {{ addr: {:?}, {}: {:?} }}", socket_addr, name, udpsock_inner);
assert_eq!(format!("{:?}", udpsock), compare);
}
Expand Down
13 changes: 13 additions & 0 deletions library/std/src/os/fd/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//! Owned and borrowed Unix-like file descriptors.

#![unstable(feature = "io_safety", issue = "87074")]
#![deny(unsafe_op_in_unsafe_fn)]

// `RawFd`, `AsRawFd`, etc.
pub mod raw;

// `OwnedFd`, `AsFd`, etc.
pub mod owned;

// Implementations for `AsRawFd` etc. for network types.
mod net;
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
use crate::os::fd::owned::OwnedFd;
use crate::os::fd::raw::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
use crate::sys_common::{self, AsInner, FromInner, IntoInner};
use crate::{net, sys};

Expand All @@ -8,7 +9,7 @@ macro_rules! impl_as_raw_fd {
impl AsRawFd for net::$t {
#[inline]
fn as_raw_fd(&self) -> RawFd {
*self.as_inner().socket().as_inner()
self.as_inner().socket().as_raw_fd()
}
}
)*};
Expand All @@ -21,8 +22,10 @@ macro_rules! impl_from_raw_fd {
impl FromRawFd for net::$t {
#[inline]
unsafe fn from_raw_fd(fd: RawFd) -> net::$t {
let socket = sys::net::Socket::from_inner(fd);
net::$t::from_inner(sys_common::net::$t::from_inner(socket))
unsafe {
let socket = sys::net::Socket::from_inner(FromInner::from_inner(OwnedFd::from_raw_fd(fd)));
net::$t::from_inner(sys_common::net::$t::from_inner(socket))
}
}
}
)*};
Expand All @@ -35,7 +38,7 @@ macro_rules! impl_into_raw_fd {
impl IntoRawFd for net::$t {
#[inline]
fn into_raw_fd(self) -> RawFd {
self.into_inner().into_socket().into_inner()
self.into_inner().into_socket().into_inner().into_inner().into_raw_fd()
}
}
)*};
Expand Down
Loading