Skip to content

Commit b15da48

Browse files
committed
Auto merge of rust-lang#139092 - thaliaarchi:move-fd-pal, r=joboet
Move `fd` into `std::sys` Move platform definitions of `fd` into `std::sys`, as part of rust-lang#117276. Unlike other modules directly under `std::sys`, this is only available on some platforms and I have not provided a fallback abstraction for unsupported platforms. That is similar to how `std::os::fd` is gated to only supported platforms. Also, fix the `unsafe_op_in_unsafe_fn` lint, which was allowed for the Unix fd impl. Since macro expansions from `std::sys::pal::unix::weak` trigger this lint, fix it there too. cc `@joboet,` `@ChrisDenton`
2 parents 85f518e + 41d6fbf commit b15da48

File tree

16 files changed

+53
-31
lines changed

16 files changed

+53
-31
lines changed

library/std/src/sys/pal/hermit/fd.rs library/std/src/sys/fd/hermit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#![unstable(reason = "not public", issue = "none", feature = "fd")]
22

3-
use super::hermit_abi;
43
use crate::cmp;
54
use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut, Read, SeekFrom};
5+
use crate::os::hermit::hermit_abi;
66
use crate::os::hermit::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
77
use crate::sys::{cvt, unsupported};
88
use crate::sys_common::{AsInner, FromInner, IntoInner};

library/std/src/sys/fd/mod.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//! Platform-dependent file descriptor abstraction.
2+
3+
#![forbid(unsafe_op_in_unsafe_fn)]
4+
5+
cfg_if::cfg_if! {
6+
if #[cfg(target_family = "unix")] {
7+
mod unix;
8+
pub use unix::*;
9+
} else if #[cfg(target_os = "hermit")] {
10+
mod hermit;
11+
pub use hermit::*;
12+
} else if #[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] {
13+
mod sgx;
14+
pub use sgx::*;
15+
} else if #[cfg(target_os = "wasi")] {
16+
mod wasi;
17+
pub use wasi::*;
18+
}
19+
}

library/std/src/sys/pal/sgx/fd.rs library/std/src/sys/fd/sgx.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use fortanix_sgx_abi::Fd;
22

3-
use super::abi::usercalls;
43
use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut};
54
use crate::mem::ManuallyDrop;
5+
use crate::sys::pal::abi::usercalls;
66
use crate::sys::{AsInner, FromInner, IntoInner};
77

88
#[derive(Debug)]

library/std/src/sys/pal/unix/fd.rs library/std/src/sys/fd/unix.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ use crate::cmp;
2222
use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut, Read};
2323
use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
2424
use crate::sys::cvt;
25+
#[cfg(all(target_os = "android", target_pointer_width = "64"))]
26+
use crate::sys::pal::weak::syscall;
27+
#[cfg(any(all(target_os = "android", target_pointer_width = "32"), target_vendor = "apple"))]
28+
use crate::sys::pal::weak::weak;
2529
use crate::sys_common::{AsInner, FromInner, IntoInner};
2630

2731
#[derive(Debug)]
@@ -232,7 +236,7 @@ impl FileDesc {
232236
// implementation if `preadv` is not available.
233237
#[cfg(all(target_os = "android", target_pointer_width = "64"))]
234238
pub fn read_vectored_at(&self, bufs: &mut [IoSliceMut<'_>], offset: u64) -> io::Result<usize> {
235-
super::weak::syscall!(
239+
syscall!(
236240
fn preadv(
237241
fd: libc::c_int,
238242
iovec: *const libc::iovec,
@@ -257,7 +261,7 @@ impl FileDesc {
257261
// and its metadata from LLVM IR.
258262
#[no_sanitize(cfi)]
259263
pub fn read_vectored_at(&self, bufs: &mut [IoSliceMut<'_>], offset: u64) -> io::Result<usize> {
260-
super::weak::weak!(
264+
weak!(
261265
fn preadv64(
262266
fd: libc::c_int,
263267
iovec: *const libc::iovec,
@@ -293,7 +297,7 @@ impl FileDesc {
293297
// use "weak" linking.
294298
#[cfg(target_vendor = "apple")]
295299
pub fn read_vectored_at(&self, bufs: &mut [IoSliceMut<'_>], offset: u64) -> io::Result<usize> {
296-
super::weak::weak!(
300+
weak!(
297301
fn preadv(
298302
fd: libc::c_int,
299303
iovec: *const libc::iovec,
@@ -442,7 +446,7 @@ impl FileDesc {
442446
// implementation if `pwritev` is not available.
443447
#[cfg(all(target_os = "android", target_pointer_width = "64"))]
444448
pub fn write_vectored_at(&self, bufs: &[IoSlice<'_>], offset: u64) -> io::Result<usize> {
445-
super::weak::syscall!(
449+
syscall!(
446450
fn pwritev(
447451
fd: libc::c_int,
448452
iovec: *const libc::iovec,
@@ -464,7 +468,7 @@ impl FileDesc {
464468

465469
#[cfg(all(target_os = "android", target_pointer_width = "32"))]
466470
pub fn write_vectored_at(&self, bufs: &[IoSlice<'_>], offset: u64) -> io::Result<usize> {
467-
super::weak::weak!(
471+
weak!(
468472
fn pwritev64(
469473
fd: libc::c_int,
470474
iovec: *const libc::iovec,
@@ -500,7 +504,7 @@ impl FileDesc {
500504
// use "weak" linking.
501505
#[cfg(target_vendor = "apple")]
502506
pub fn write_vectored_at(&self, bufs: &[IoSlice<'_>], offset: u64) -> io::Result<usize> {
503-
super::weak::weak!(
507+
weak!(
504508
fn pwritev(
505509
fd: libc::c_int,
506510
iovec: *const libc::iovec,
@@ -669,6 +673,6 @@ impl IntoRawFd for FileDesc {
669673

670674
impl FromRawFd for FileDesc {
671675
unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
672-
Self(FromRawFd::from_raw_fd(raw_fd))
676+
Self(unsafe { FromRawFd::from_raw_fd(raw_fd) })
673677
}
674678
}

library/std/src/sys/pal/unix/fd/tests.rs library/std/src/sys/fd/unix/tests.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use core::mem::ManuallyDrop;
22

3-
use super::{FileDesc, IoSlice};
3+
use super::FileDesc;
4+
use crate::io::IoSlice;
45
use crate::os::unix::io::FromRawFd;
56

67
#[test]

library/std/src/sys/pal/wasi/fd.rs library/std/src/sys/fd/wasi.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
#![forbid(unsafe_op_in_unsafe_fn)]
2-
#![allow(dead_code)]
1+
#![expect(dead_code)]
32

4-
use super::err2io;
53
use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut, SeekFrom};
64
use crate::mem;
75
use crate::net::Shutdown;
86
use crate::os::wasi::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
7+
use crate::sys::pal::err2io;
98
use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
109

1110
#[derive(Debug)]

library/std/src/sys/fs/hermit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use crate::os::hermit::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, Raw
99
use crate::path::{Path, PathBuf};
1010
use crate::sync::Arc;
1111
use crate::sys::common::small_c_string::run_path_with_cstr;
12+
use crate::sys::fd::FileDesc;
1213
pub use crate::sys::fs::common::{copy, exists};
13-
use crate::sys::pal::fd::FileDesc;
1414
use crate::sys::time::SystemTime;
1515
use crate::sys::{cvt, unsupported};
1616
use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};

library/std/src/sys/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub mod anonymous_pipe;
1212
pub mod backtrace;
1313
pub mod cmath;
1414
pub mod exit_guard;
15+
pub mod fd;
1516
pub mod fs;
1617
pub mod io;
1718
pub mod net;

library/std/src/sys/pal/hermit/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use crate::os::raw::c_char;
2020

2121
pub mod args;
2222
pub mod env;
23-
pub mod fd;
2423
pub mod futex;
2524
pub mod os;
2625
#[path = "../unsupported/pipe.rs"]

library/std/src/sys/pal/sgx/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use crate::sync::atomic::{AtomicBool, Ordering};
1111
pub mod abi;
1212
pub mod args;
1313
pub mod env;
14-
pub mod fd;
1514
mod libunwind_integration;
1615
pub mod os;
1716
#[path = "../unsupported/pipe.rs"]

library/std/src/sys/pal/unix/linux/pidfd.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::io;
22
use crate::os::fd::{AsRawFd, FromRawFd, RawFd};
33
use crate::sys::cvt;
4-
use crate::sys::pal::unix::fd::FileDesc;
4+
use crate::sys::fd::FileDesc;
55
use crate::sys::process::ExitStatus;
66
use crate::sys_common::{AsInner, FromInner, IntoInner};
77

library/std/src/sys/pal/unix/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ pub mod weak;
88

99
pub mod args;
1010
pub mod env;
11-
pub mod fd;
1211
#[cfg(target_os = "fuchsia")]
1312
pub mod fuchsia;
1413
pub mod futex;

library/std/src/sys/pal/unix/weak.rs

+12-7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
// each instance of `weak!` and `syscall!`. Rather than trying to unify all of
2121
// that, we'll just allow that some unix targets don't use this module at all.
2222
#![allow(dead_code, unused_macros)]
23+
#![forbid(unsafe_op_in_unsafe_fn)]
2324

2425
use crate::ffi::CStr;
2526
use crate::marker::PhantomData;
@@ -131,11 +132,15 @@ impl<F> DlsymWeak<F> {
131132
unsafe fn initialize(&self) -> Option<F> {
132133
assert_eq!(size_of::<F>(), size_of::<*mut libc::c_void>());
133134

134-
let val = fetch(self.name);
135+
let val = unsafe { fetch(self.name) };
135136
// This synchronizes with the acquire fence in `get`.
136137
self.func.store(val, Ordering::Release);
137138

138-
if val.is_null() { None } else { Some(mem::transmute_copy::<*mut libc::c_void, F>(&val)) }
139+
if val.is_null() {
140+
None
141+
} else {
142+
Some(unsafe { mem::transmute_copy::<*mut libc::c_void, F>(&val) })
143+
}
139144
}
140145
}
141146

@@ -144,7 +149,7 @@ unsafe fn fetch(name: &str) -> *mut libc::c_void {
144149
Ok(cstr) => cstr,
145150
Err(..) => return ptr::null_mut(),
146151
};
147-
libc::dlsym(libc::RTLD_DEFAULT, name.as_ptr())
152+
unsafe { libc::dlsym(libc::RTLD_DEFAULT, name.as_ptr()) }
148153
}
149154

150155
#[cfg(not(any(target_os = "linux", target_os = "android")))]
@@ -157,7 +162,7 @@ pub(crate) macro syscall {
157162
weak!(fn $name($($param: $t),*) -> $ret;);
158163

159164
if let Some(fun) = $name.get() {
160-
fun($($param),*)
165+
unsafe { fun($($param),*) }
161166
} else {
162167
super::os::set_errno(libc::ENOSYS);
163168
-1
@@ -177,9 +182,9 @@ pub(crate) macro syscall {
177182
// Use a weak symbol from libc when possible, allowing `LD_PRELOAD`
178183
// interposition, but if it's not found just use a raw syscall.
179184
if let Some(fun) = $name.get() {
180-
fun($($param),*)
185+
unsafe { fun($($param),*) }
181186
} else {
182-
libc::syscall(libc::${concat(SYS_, $name)}, $($param),*) as $ret
187+
unsafe { libc::syscall(libc::${concat(SYS_, $name)}, $($param),*) as $ret }
183188
}
184189
}
185190
)
@@ -189,7 +194,7 @@ pub(crate) macro syscall {
189194
pub(crate) macro raw_syscall {
190195
(fn $name:ident($($param:ident : $t:ty),* $(,)?) -> $ret:ty;) => (
191196
unsafe fn $name($($param: $t),*) -> $ret {
192-
libc::syscall(libc::${concat(SYS_, $name)}, $($param),*) as $ret
197+
unsafe { libc::syscall(libc::${concat(SYS_, $name)}, $($param),*) as $ret }
193198
}
194199
)
195200
}

library/std/src/sys/pal/wasi/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
1616
pub mod args;
1717
pub mod env;
18-
pub mod fd;
1918
#[allow(unused)]
2019
#[path = "../wasm/atomics/futex.rs"]
2120
pub mod futex;

library/std/src/sys/pal/wasip2/mod.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
pub mod args;
1111
#[path = "../wasi/env.rs"]
1212
pub mod env;
13-
#[path = "../wasi/fd.rs"]
14-
pub mod fd;
1513
#[allow(unused)]
1614
#[path = "../wasm/atomics/futex.rs"]
1715
pub mod futex;
@@ -39,7 +37,6 @@ mod helpers;
3937
// import conflict rules. If we glob export `helpers` and `common` together,
4038
// then the compiler complains about conflicts.
4139

42-
use helpers::err2io;
43-
pub use helpers::{abort_internal, decode_error_kind, is_interrupted};
40+
pub(crate) use helpers::{abort_internal, decode_error_kind, err2io, is_interrupted};
4441

4542
mod cabi_realloc;

library/std/src/sys/stdio/wasi.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut};
44
use crate::mem::ManuallyDrop;
55
use crate::os::raw;
66
use crate::os::wasi::io::{AsRawFd, FromRawFd};
7-
use crate::sys::pal::fd::WasiFd;
7+
use crate::sys::fd::WasiFd;
88

99
pub struct Stdin;
1010
pub struct Stdout;

0 commit comments

Comments
 (0)