about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <476013+matthiaskrgr@users.noreply.github.com>2025-04-05 10:18:04 +0200
committerGitHub <noreply@github.com>2025-04-05 10:18:04 +0200
commita64ccf4a46c80a975e197cb4610125838ca24cbf (patch)
treeb783dacf2f65227b4ab746a1817c6c77ead7fdd8
parent56ffb43629bf58996c367073a0fa19e7d422df19 (diff)
parent3ab22fabf1a21077556c708633ceaefbb678c178 (diff)
downloadrust-a64ccf4a46c80a975e197cb4610125838ca24cbf.tar.gz
rust-a64ccf4a46c80a975e197cb4610125838ca24cbf.zip
Rollup merge of #139092 - thaliaarchi:move-fd-pal, r=joboet
Move `fd` into `std::sys`

Move platform definitions of `fd` into `std::sys`, as part of https://github.com/rust-lang/rust/issues/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`

try-job: x86_64-gnu-aux
-rw-r--r--library/std/src/sys/fd/hermit.rs (renamed from library/std/src/sys/pal/hermit/fd.rs)2
-rw-r--r--library/std/src/sys/fd/mod.rs19
-rw-r--r--library/std/src/sys/fd/sgx.rs (renamed from library/std/src/sys/pal/sgx/fd.rs)2
-rw-r--r--library/std/src/sys/fd/unix.rs (renamed from library/std/src/sys/pal/unix/fd.rs)18
-rw-r--r--library/std/src/sys/fd/unix/tests.rs (renamed from library/std/src/sys/pal/unix/fd/tests.rs)3
-rw-r--r--library/std/src/sys/fd/wasi.rs (renamed from library/std/src/sys/pal/wasi/fd.rs)5
-rw-r--r--library/std/src/sys/fs/hermit.rs2
-rw-r--r--library/std/src/sys/mod.rs1
-rw-r--r--library/std/src/sys/pal/hermit/mod.rs1
-rw-r--r--library/std/src/sys/pal/sgx/mod.rs1
-rw-r--r--library/std/src/sys/pal/unix/linux/pidfd.rs2
-rw-r--r--library/std/src/sys/pal/unix/mod.rs1
-rw-r--r--library/std/src/sys/pal/unix/weak.rs19
-rw-r--r--library/std/src/sys/pal/wasi/mod.rs1
-rw-r--r--library/std/src/sys/pal/wasip2/mod.rs5
-rw-r--r--library/std/src/sys/stdio/wasi.rs2
-rw-r--r--src/bootstrap/mk/Makefile.in4
17 files changed, 55 insertions, 33 deletions
diff --git a/library/std/src/sys/pal/hermit/fd.rs b/library/std/src/sys/fd/hermit.rs
index edd984d920a..7e8ba065f1b 100644
--- a/library/std/src/sys/pal/hermit/fd.rs
+++ b/library/std/src/sys/fd/hermit.rs
@@ -1,8 +1,8 @@
 #![unstable(reason = "not public", issue = "none", feature = "fd")]
 
-use super::hermit_abi;
 use crate::cmp;
 use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut, Read, SeekFrom};
+use crate::os::hermit::hermit_abi;
 use crate::os::hermit::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
 use crate::sys::{cvt, unsupported};
 use crate::sys_common::{AsInner, FromInner, IntoInner};
diff --git a/library/std/src/sys/fd/mod.rs b/library/std/src/sys/fd/mod.rs
new file mode 100644
index 00000000000..e0f5eab6951
--- /dev/null
+++ b/library/std/src/sys/fd/mod.rs
@@ -0,0 +1,19 @@
+//! Platform-dependent file descriptor abstraction.
+
+#![forbid(unsafe_op_in_unsafe_fn)]
+
+cfg_if::cfg_if! {
+    if #[cfg(target_family = "unix")] {
+        mod unix;
+        pub use unix::*;
+    } else if #[cfg(target_os = "hermit")] {
+        mod hermit;
+        pub use hermit::*;
+    } else if #[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] {
+        mod sgx;
+        pub use sgx::*;
+    } else if #[cfg(target_os = "wasi")] {
+        mod wasi;
+        pub use wasi::*;
+    }
+}
diff --git a/library/std/src/sys/pal/sgx/fd.rs b/library/std/src/sys/fd/sgx.rs
index 399f6a16489..1ef768db64c 100644
--- a/library/std/src/sys/pal/sgx/fd.rs
+++ b/library/std/src/sys/fd/sgx.rs
@@ -1,8 +1,8 @@
 use fortanix_sgx_abi::Fd;
 
-use super::abi::usercalls;
 use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut};
 use crate::mem::ManuallyDrop;
+use crate::sys::pal::abi::usercalls;
 use crate::sys::{AsInner, FromInner, IntoInner};
 
 #[derive(Debug)]
diff --git a/library/std/src/sys/pal/unix/fd.rs b/library/std/src/sys/fd/unix.rs
index 2ec8d01c13f..2042ea2c73d 100644
--- a/library/std/src/sys/pal/unix/fd.rs
+++ b/library/std/src/sys/fd/unix.rs
@@ -22,6 +22,10 @@ use crate::cmp;
 use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut, Read};
 use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
 use crate::sys::cvt;
+#[cfg(all(target_os = "android", target_pointer_width = "64"))]
+use crate::sys::pal::weak::syscall;
+#[cfg(any(all(target_os = "android", target_pointer_width = "32"), target_vendor = "apple"))]
+use crate::sys::pal::weak::weak;
 use crate::sys_common::{AsInner, FromInner, IntoInner};
 
 #[derive(Debug)]
@@ -232,7 +236,7 @@ impl FileDesc {
     // implementation if `preadv` is not available.
     #[cfg(all(target_os = "android", target_pointer_width = "64"))]
     pub fn read_vectored_at(&self, bufs: &mut [IoSliceMut<'_>], offset: u64) -> io::Result<usize> {
-        super::weak::syscall!(
+        syscall!(
             fn preadv(
                 fd: libc::c_int,
                 iovec: *const libc::iovec,
@@ -257,7 +261,7 @@ impl FileDesc {
     // and its metadata from LLVM IR.
     #[no_sanitize(cfi)]
     pub fn read_vectored_at(&self, bufs: &mut [IoSliceMut<'_>], offset: u64) -> io::Result<usize> {
-        super::weak::weak!(
+        weak!(
             fn preadv64(
                 fd: libc::c_int,
                 iovec: *const libc::iovec,
@@ -293,7 +297,7 @@ impl FileDesc {
     // use "weak" linking.
     #[cfg(target_vendor = "apple")]
     pub fn read_vectored_at(&self, bufs: &mut [IoSliceMut<'_>], offset: u64) -> io::Result<usize> {
-        super::weak::weak!(
+        weak!(
             fn preadv(
                 fd: libc::c_int,
                 iovec: *const libc::iovec,
@@ -442,7 +446,7 @@ impl FileDesc {
     // implementation if `pwritev` is not available.
     #[cfg(all(target_os = "android", target_pointer_width = "64"))]
     pub fn write_vectored_at(&self, bufs: &[IoSlice<'_>], offset: u64) -> io::Result<usize> {
-        super::weak::syscall!(
+        syscall!(
             fn pwritev(
                 fd: libc::c_int,
                 iovec: *const libc::iovec,
@@ -464,7 +468,7 @@ impl FileDesc {
 
     #[cfg(all(target_os = "android", target_pointer_width = "32"))]
     pub fn write_vectored_at(&self, bufs: &[IoSlice<'_>], offset: u64) -> io::Result<usize> {
-        super::weak::weak!(
+        weak!(
             fn pwritev64(
                 fd: libc::c_int,
                 iovec: *const libc::iovec,
@@ -500,7 +504,7 @@ impl FileDesc {
     // use "weak" linking.
     #[cfg(target_vendor = "apple")]
     pub fn write_vectored_at(&self, bufs: &[IoSlice<'_>], offset: u64) -> io::Result<usize> {
-        super::weak::weak!(
+        weak!(
             fn pwritev(
                 fd: libc::c_int,
                 iovec: *const libc::iovec,
@@ -669,6 +673,6 @@ impl IntoRawFd for FileDesc {
 
 impl FromRawFd for FileDesc {
     unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
-        Self(FromRawFd::from_raw_fd(raw_fd))
+        Self(unsafe { FromRawFd::from_raw_fd(raw_fd) })
     }
 }
diff --git a/library/std/src/sys/pal/unix/fd/tests.rs b/library/std/src/sys/fd/unix/tests.rs
index c5301ce6557..fcd66c71707 100644
--- a/library/std/src/sys/pal/unix/fd/tests.rs
+++ b/library/std/src/sys/fd/unix/tests.rs
@@ -1,6 +1,7 @@
 use core::mem::ManuallyDrop;
 
-use super::{FileDesc, IoSlice};
+use super::FileDesc;
+use crate::io::IoSlice;
 use crate::os::unix::io::FromRawFd;
 
 #[test]
diff --git a/library/std/src/sys/pal/wasi/fd.rs b/library/std/src/sys/fd/wasi.rs
index 4b3dd1ce49e..80a5143ff0b 100644
--- a/library/std/src/sys/pal/wasi/fd.rs
+++ b/library/std/src/sys/fd/wasi.rs
@@ -1,11 +1,10 @@
-#![forbid(unsafe_op_in_unsafe_fn)]
-#![allow(dead_code)]
+#![expect(dead_code)]
 
-use super::err2io;
 use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut, SeekFrom};
 use crate::mem;
 use crate::net::Shutdown;
 use crate::os::wasi::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
+use crate::sys::pal::err2io;
 use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
 
 #[derive(Debug)]
diff --git a/library/std/src/sys/fs/hermit.rs b/library/std/src/sys/fs/hermit.rs
index f83a2f90ed2..99690abe8ed 100644
--- a/library/std/src/sys/fs/hermit.rs
+++ b/library/std/src/sys/fs/hermit.rs
@@ -9,8 +9,8 @@ use crate::os::hermit::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, Raw
 use crate::path::{Path, PathBuf};
 use crate::sync::Arc;
 use crate::sys::common::small_c_string::run_path_with_cstr;
+use crate::sys::fd::FileDesc;
 pub use crate::sys::fs::common::{copy, exists};
-use crate::sys::pal::fd::FileDesc;
 use crate::sys::time::SystemTime;
 use crate::sys::{cvt, unsupported};
 use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
diff --git a/library/std/src/sys/mod.rs b/library/std/src/sys/mod.rs
index f0cfb9b2773..f8f220fafd1 100644
--- a/library/std/src/sys/mod.rs
+++ b/library/std/src/sys/mod.rs
@@ -12,6 +12,7 @@ pub mod anonymous_pipe;
 pub mod backtrace;
 pub mod cmath;
 pub mod exit_guard;
+pub mod fd;
 pub mod fs;
 pub mod io;
 pub mod net;
diff --git a/library/std/src/sys/pal/hermit/mod.rs b/library/std/src/sys/pal/hermit/mod.rs
index 67eab96fa40..26211bcb152 100644
--- a/library/std/src/sys/pal/hermit/mod.rs
+++ b/library/std/src/sys/pal/hermit/mod.rs
@@ -20,7 +20,6 @@ use crate::os::raw::c_char;
 
 pub mod args;
 pub mod env;
-pub mod fd;
 pub mod futex;
 pub mod os;
 #[path = "../unsupported/pipe.rs"]
diff --git a/library/std/src/sys/pal/sgx/mod.rs b/library/std/src/sys/pal/sgx/mod.rs
index fe43cfd2caf..52684e18ac2 100644
--- a/library/std/src/sys/pal/sgx/mod.rs
+++ b/library/std/src/sys/pal/sgx/mod.rs
@@ -11,7 +11,6 @@ use crate::sync::atomic::{AtomicBool, Ordering};
 pub mod abi;
 pub mod args;
 pub mod env;
-pub mod fd;
 mod libunwind_integration;
 pub mod os;
 #[path = "../unsupported/pipe.rs"]
diff --git a/library/std/src/sys/pal/unix/linux/pidfd.rs b/library/std/src/sys/pal/unix/linux/pidfd.rs
index 78744430f3b..2d949ec9e91 100644
--- a/library/std/src/sys/pal/unix/linux/pidfd.rs
+++ b/library/std/src/sys/pal/unix/linux/pidfd.rs
@@ -1,7 +1,7 @@
 use crate::io;
 use crate::os::fd::{AsRawFd, FromRawFd, RawFd};
 use crate::sys::cvt;
-use crate::sys::pal::unix::fd::FileDesc;
+use crate::sys::fd::FileDesc;
 use crate::sys::process::ExitStatus;
 use crate::sys_common::{AsInner, FromInner, IntoInner};
 
diff --git a/library/std/src/sys/pal/unix/mod.rs b/library/std/src/sys/pal/unix/mod.rs
index 413fda1d8d8..d7106c33974 100644
--- a/library/std/src/sys/pal/unix/mod.rs
+++ b/library/std/src/sys/pal/unix/mod.rs
@@ -8,7 +8,6 @@ pub mod weak;
 
 pub mod args;
 pub mod env;
-pub mod fd;
 #[cfg(target_os = "fuchsia")]
 pub mod fuchsia;
 pub mod futex;
diff --git a/library/std/src/sys/pal/unix/weak.rs b/library/std/src/sys/pal/unix/weak.rs
index e7f4e005cc4..e4c814fba8c 100644
--- a/library/std/src/sys/pal/unix/weak.rs
+++ b/library/std/src/sys/pal/unix/weak.rs
@@ -20,6 +20,7 @@
 // each instance of `weak!` and `syscall!`. Rather than trying to unify all of
 // that, we'll just allow that some unix targets don't use this module at all.
 #![allow(dead_code, unused_macros)]
+#![forbid(unsafe_op_in_unsafe_fn)]
 
 use crate::ffi::CStr;
 use crate::marker::PhantomData;
@@ -131,11 +132,15 @@ impl<F> DlsymWeak<F> {
     unsafe fn initialize(&self) -> Option<F> {
         assert_eq!(size_of::<F>(), size_of::<*mut libc::c_void>());
 
-        let val = fetch(self.name);
+        let val = unsafe { fetch(self.name) };
         // This synchronizes with the acquire fence in `get`.
         self.func.store(val, Ordering::Release);
 
-        if val.is_null() { None } else { Some(mem::transmute_copy::<*mut libc::c_void, F>(&val)) }
+        if val.is_null() {
+            None
+        } else {
+            Some(unsafe { mem::transmute_copy::<*mut libc::c_void, F>(&val) })
+        }
     }
 }
 
@@ -144,7 +149,7 @@ unsafe fn fetch(name: &str) -> *mut libc::c_void {
         Ok(cstr) => cstr,
         Err(..) => return ptr::null_mut(),
     };
-    libc::dlsym(libc::RTLD_DEFAULT, name.as_ptr())
+    unsafe { libc::dlsym(libc::RTLD_DEFAULT, name.as_ptr()) }
 }
 
 #[cfg(not(any(target_os = "linux", target_os = "android")))]
@@ -157,7 +162,7 @@ pub(crate) macro syscall {
             weak!(fn $name($($param: $t),*) -> $ret;);
 
             if let Some(fun) = $name.get() {
-                fun($($param),*)
+                unsafe { fun($($param),*) }
             } else {
                 super::os::set_errno(libc::ENOSYS);
                 -1
@@ -177,9 +182,9 @@ pub(crate) macro syscall {
             // Use a weak symbol from libc when possible, allowing `LD_PRELOAD`
             // interposition, but if it's not found just use a raw syscall.
             if let Some(fun) = $name.get() {
-                fun($($param),*)
+                unsafe { fun($($param),*) }
             } else {
-                libc::syscall(libc::${concat(SYS_, $name)}, $($param),*) as $ret
+                unsafe { libc::syscall(libc::${concat(SYS_, $name)}, $($param),*) as $ret }
             }
         }
     )
@@ -189,7 +194,7 @@ pub(crate) macro syscall {
 pub(crate) macro raw_syscall {
     (fn $name:ident($($param:ident : $t:ty),* $(,)?) -> $ret:ty;) => (
         unsafe fn $name($($param: $t),*) -> $ret {
-            libc::syscall(libc::${concat(SYS_, $name)}, $($param),*) as $ret
+            unsafe { libc::syscall(libc::${concat(SYS_, $name)}, $($param),*) as $ret }
         }
     )
 }
diff --git a/library/std/src/sys/pal/wasi/mod.rs b/library/std/src/sys/pal/wasi/mod.rs
index cdd613f76b6..80853e7b5a2 100644
--- a/library/std/src/sys/pal/wasi/mod.rs
+++ b/library/std/src/sys/pal/wasi/mod.rs
@@ -15,7 +15,6 @@
 
 pub mod args;
 pub mod env;
-pub mod fd;
 #[allow(unused)]
 #[path = "../wasm/atomics/futex.rs"]
 pub mod futex;
diff --git a/library/std/src/sys/pal/wasip2/mod.rs b/library/std/src/sys/pal/wasip2/mod.rs
index 6ac28f1bf4f..504b947d09e 100644
--- a/library/std/src/sys/pal/wasip2/mod.rs
+++ b/library/std/src/sys/pal/wasip2/mod.rs
@@ -10,8 +10,6 @@
 pub mod args;
 #[path = "../wasi/env.rs"]
 pub mod env;
-#[path = "../wasi/fd.rs"]
-pub mod fd;
 #[allow(unused)]
 #[path = "../wasm/atomics/futex.rs"]
 pub mod futex;
@@ -39,7 +37,6 @@ mod helpers;
 // import conflict rules. If we glob export `helpers` and `common` together,
 // then the compiler complains about conflicts.
 
-use helpers::err2io;
-pub use helpers::{abort_internal, decode_error_kind, is_interrupted};
+pub(crate) use helpers::{abort_internal, decode_error_kind, err2io, is_interrupted};
 
 mod cabi_realloc;
diff --git a/library/std/src/sys/stdio/wasi.rs b/library/std/src/sys/stdio/wasi.rs
index 8105b0cfa2f..b70efd026f9 100644
--- a/library/std/src/sys/stdio/wasi.rs
+++ b/library/std/src/sys/stdio/wasi.rs
@@ -4,7 +4,7 @@ use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut};
 use crate::mem::ManuallyDrop;
 use crate::os::raw;
 use crate::os::wasi::io::{AsRawFd, FromRawFd};
-use crate::sys::pal::fd::WasiFd;
+use crate::sys::fd::WasiFd;
 
 pub struct Stdin;
 pub struct Stdout;
diff --git a/src/bootstrap/mk/Makefile.in b/src/bootstrap/mk/Makefile.in
index 605f54d1d7f..08a288170fa 100644
--- a/src/bootstrap/mk/Makefile.in
+++ b/src/bootstrap/mk/Makefile.in
@@ -73,12 +73,12 @@ check-aux:
 		$(BOOTSTRAP) miri --stage 2 library/std \
 		$(BOOTSTRAP_ARGS) \
 		--no-doc -- \
-		--skip fs:: --skip net:: --skip process:: --skip sys::pal::
+		--skip fs:: --skip net:: --skip process:: --skip sys::fd:: --skip sys::pal::
 	$(Q)MIRIFLAGS="-Zmiri-disable-isolation" \
 		$(BOOTSTRAP) miri --stage 2 library/std \
 		$(BOOTSTRAP_ARGS) \
 		--doc -- \
-		--skip fs:: --skip net:: --skip process:: --skip sys::pal::
+		--skip fs:: --skip net:: --skip process:: --skip sys::fd:: --skip sys::pal::
 	# Also test some very target-specific modules on other targets
 	# (making sure to cover an i686 target as well).
 	$(Q)MIRIFLAGS="-Zmiri-disable-isolation" BOOTSTRAP_SKIP_TARGET_SANITY=1 \