Skip to content

Commit eb37bbc

Browse files
committed
Document that BorrowedFd may be used to do a dup.
1 parent a4cec97 commit eb37bbc

File tree

4 files changed

+29
-13
lines changed

4 files changed

+29
-13
lines changed

library/std/src/os/unix/io/mod.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,30 @@
2626
//! that they don't outlive the resource they point to. These are safe to
2727
//! use. `BorrowedFd` values may be used in APIs which provide safe access to
2828
//! any system call except for:
29+
//!
2930
//! - `close`, because that would end the dynamic lifetime of the resource
3031
//! without ending the lifetime of the file descriptor.
32+
//!
3133
//! - `dup2`/`dup3`, in the second argument, because this argument is
3234
//! closed and assigned a new resource, which may break the assumptions
3335
//! other code using that file descriptor.
34-
//! This list doesn't include `mmap`, since `mmap` does do a proper borrow of
35-
//! its file descriptor argument. That said, `mmap` is unsafe for other
36-
//! reasons: it operates on raw pointers, and it can have undefined behavior if
37-
//! the underlying storage is mutated. Mutations may come from other processes,
38-
//! or from the same process if the API provides `BorrowedFd` access, since as
39-
//! mentioned earlier, `BorrowedFd` values may be used in APIs which provide
40-
//! safe access to any system call. Consequently, code using `mmap` and
41-
//! presenting a safe API must take full responsibility for ensuring that safe
42-
//! Rust code cannot evoke undefined behavior through it.
36+
//!
37+
//! `BorrowedFd` values may be used in APIs which provide safe access to `dup`
38+
//! system calls, so types implementing `AsFd` or `From<OwnedFd>` should not
39+
//! assume they always have exclusive access to the underlying file
40+
//! description.
41+
//!
42+
//! `BorrowedFd` values may also be used with `mmap`, since `mmap` uses the
43+
//! provided file descriptor in a manner similar to `dup` and does not require
44+
//! the `BorrowedFd` passed to it to live for the lifetime of the resulting
45+
//! mapping. That said, `mmap` is unsafe for other reasons: it operates on raw
46+
//! pointers, and it can have undefined behavior if the underlying storage is
47+
//! mutated. Mutations may come from other processes, or from the same process
48+
//! if the API provides `BorrowedFd` access, since as mentioned earlier,
49+
//! `BorrowedFd` values may be used in APIs which provide safe access to any
50+
//! system call. Consequently, code using `mmap` and presenting a safe API must
51+
//! take full responsibility for ensuring that safe Rust code cannot evoke
52+
//! undefined behavior through it.
4353
//!
4454
//! Like boxes, `OwnedFd` values conceptually own the resource they point to,
4555
//! and free (close) it when they are dropped.

library/std/src/os/windows/io/handle.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,15 @@ impl OwnedHandle {
189189
access: c::DWORD,
190190
inherit: bool,
191191
options: c::DWORD,
192-
) -> io::Result<Self> {
192+
) -> io::Result<OwnedHandle> {
193193
let handle = self.as_raw_handle();
194194

195195
// `Stdin`, `Stdout`, and `Stderr` can all hold null handles, such as
196196
// in a process with a detached console. `DuplicateHandle` would fail
197197
// if we passed it a null handle, but we can treat null as a valid
198198
// handle which doesn't do any I/O, and allow it to be duplicated.
199199
if handle.is_null() {
200-
return unsafe { Ok(Self::from_raw_handle(handle)) };
200+
return unsafe { Ok(OwnedHandle::from_raw_handle(handle)) };
201201
}
202202

203203
let mut ret = ptr::null_mut();
@@ -213,7 +213,7 @@ impl OwnedHandle {
213213
options,
214214
)
215215
})?;
216-
unsafe { Ok(Self::from_raw_handle(ret)) }
216+
unsafe { Ok(OwnedHandle::from_raw_handle(ret)) }
217217
}
218218
}
219219

library/std/src/os/windows/io/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@
3636
//! dynamic lifetime of the resource without ending the lifetime of the
3737
//! handle or socket.
3838
//!
39+
//! `BorrowedHandle` and `BorrowedSocket` values may be used in APIs which
40+
//! provide safe access to `DuplicateHandle` and `WSADuplicateSocketW` and
41+
//! related functions, so types implementing `AsHandle`, `AsSocket`,
42+
//! `From<OwnedHandle>`, or `From<OwnedSocket>` should not assume they always
43+
//! have exclusive access to the underlying object.
44+
//!
3945
//! Like boxes, `OwnedHandle` and `OwnedSocket` values conceptually own the
4046
//! resource they point to, and free (close) it when they are dropped.
4147
//!

library/std/src/sys/windows/handle.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ impl Handle {
218218
inherit: bool,
219219
options: c::DWORD,
220220
) -> io::Result<Self> {
221-
Ok(Self(self.0.duplicate(access, inherit, options)?))
221+
Ok(Self(self.0.as_handle().duplicate(access, inherit, options)?))
222222
}
223223

224224
/// Performs a synchronous read.

0 commit comments

Comments
 (0)