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

Move fd into std::sys #139092

Merged
merged 2 commits into from
Apr 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
19 changes: 19 additions & 0 deletions library/std/src/sys/fd/mod.rs
Original file line number Diff line number Diff line change
@@ -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::*;
}
}
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) })
}
}
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/fs/hermit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
1 change: 1 addition & 0 deletions library/std/src/sys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion library/std/src/sys/pal/hermit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
1 change: 0 additions & 1 deletion library/std/src/sys/pal/sgx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/pal/unix/linux/pidfd.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down
1 change: 0 additions & 1 deletion library/std/src/sys/pal/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 12 additions & 7 deletions library/std/src/sys/pal/unix/weak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) })
}
}
}

Expand All @@ -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")))]
Expand All @@ -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
Expand All @@ -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 }
}
}
)
Expand All @@ -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 }
}
)
}
1 change: 0 additions & 1 deletion library/std/src/sys/pal/wasi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

pub mod args;
pub mod env;
pub mod fd;
#[allow(unused)]
#[path = "../wasm/atomics/futex.rs"]
pub mod futex;
Expand Down
5 changes: 1 addition & 4 deletions library/std/src/sys/pal/wasip2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
2 changes: 1 addition & 1 deletion library/std/src/sys/stdio/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/bootstrap/mk/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
Loading