1
Fork 0

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
This commit is contained in:
Matthias Krüger 2025-04-05 10:18:04 +02:00 committed by GitHub
commit a64ccf4a46
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 55 additions and 33 deletions

View file

@ -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};

View file

@ -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::*;
}
}

View file

@ -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)]

View file

@ -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) })
}
}

View file

@ -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]

View file

@ -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)]

View file

@ -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};

View file

@ -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;

View file

@ -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"]

View file

@ -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"]

View file

@ -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};

View file

@ -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;

View file

@ -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 }
}
)
}

View file

@ -15,7 +15,6 @@
pub mod args;
pub mod env;
pub mod fd;
#[allow(unused)]
#[path = "../wasm/atomics/futex.rs"]
pub mod futex;

View file

@ -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;

View file

@ -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;

View file

@ -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 \