Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Prev Previous commit
Next Next commit
Apply rustc-0054-Add-std-os-fd-support-for-Trusty.patch
  • Loading branch information
randomPoison committed Mar 10, 2025
commit 7f6ee12526700e037ef34912b2b0c628028d382c
1 change: 1 addition & 0 deletions library/std/src/os/fd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ mod raw;
mod owned;

// Implementations for `AsRawFd` etc. for network types.
#[cfg(not(target_os = "trusty"))]
mod net;

#[cfg(test)]
Expand Down
23 changes: 19 additions & 4 deletions library/std/src/os/fd/owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
use super::raw::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
use crate::marker::PhantomData;
use crate::mem::ManuallyDrop;
#[cfg(not(any(target_arch = "wasm32", target_env = "sgx", target_os = "hermit")))]
#[cfg(not(any(target_arch = "wasm32", target_env = "sgx", target_os = "hermit", target_os = "trusty")))]
use crate::sys::cvt;
#[cfg(not(target_os = "trusty"))]
use crate::sys_common::{AsInner, FromInner, IntoInner};
use crate::{fmt, fs, io};
use crate::{fmt, io};
#[cfg(not(target_os = "trusty"))]
use crate::fs;

type ValidRawFd = core::num::niche_types::NotAllOnes<RawFd>;

Expand Down Expand Up @@ -87,7 +90,7 @@ impl OwnedFd {
impl BorrowedFd<'_> {
/// Creates a new `OwnedFd` instance that shares the same underlying file
/// description as the existing `BorrowedFd` instance.
#[cfg(not(any(target_arch = "wasm32", target_os = "hermit")))]
#[cfg(not(any(target_arch = "wasm32", target_os = "hermit", target_os = "trusty")))]
#[stable(feature = "io_safety", since = "1.63.0")]
pub fn try_clone_to_owned(&self) -> crate::io::Result<OwnedFd> {
// We want to atomically duplicate this file descriptor and set the
Expand All @@ -110,7 +113,7 @@ impl BorrowedFd<'_> {

/// Creates a new `OwnedFd` instance that shares the same underlying file
/// description as the existing `BorrowedFd` instance.
#[cfg(any(target_arch = "wasm32", target_os = "hermit"))]
#[cfg(any(target_arch = "wasm32", target_os = "hermit", target_os = "trusty"))]
#[stable(feature = "io_safety", since = "1.63.0")]
pub fn try_clone_to_owned(&self) -> crate::io::Result<OwnedFd> {
Err(crate::io::Error::UNSUPPORTED_PLATFORM)
Expand Down Expand Up @@ -280,6 +283,7 @@ impl AsFd for OwnedFd {
}

#[stable(feature = "io_safety", since = "1.63.0")]
#[cfg(not(target_os = "trusty"))]
impl AsFd for fs::File {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
Expand All @@ -288,6 +292,7 @@ impl AsFd for fs::File {
}

#[stable(feature = "io_safety", since = "1.63.0")]
#[cfg(not(target_os = "trusty"))]
impl From<fs::File> for OwnedFd {
/// Takes ownership of a [`File`](fs::File)'s underlying file descriptor.
#[inline]
Expand All @@ -297,6 +302,7 @@ impl From<fs::File> for OwnedFd {
}

#[stable(feature = "io_safety", since = "1.63.0")]
#[cfg(not(target_os = "trusty"))]
impl From<OwnedFd> for fs::File {
/// Returns a [`File`](fs::File) that takes ownership of the given
/// file descriptor.
Expand All @@ -307,6 +313,7 @@ impl From<OwnedFd> for fs::File {
}

#[stable(feature = "io_safety", since = "1.63.0")]
#[cfg(not(target_os = "trusty"))]
impl AsFd for crate::net::TcpStream {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
Expand All @@ -315,6 +322,7 @@ impl AsFd for crate::net::TcpStream {
}

#[stable(feature = "io_safety", since = "1.63.0")]
#[cfg(not(target_os = "trusty"))]
impl From<crate::net::TcpStream> for OwnedFd {
/// Takes ownership of a [`TcpStream`](crate::net::TcpStream)'s socket file descriptor.
#[inline]
Expand All @@ -324,6 +332,7 @@ impl From<crate::net::TcpStream> for OwnedFd {
}

#[stable(feature = "io_safety", since = "1.63.0")]
#[cfg(not(target_os = "trusty"))]
impl From<OwnedFd> for crate::net::TcpStream {
#[inline]
fn from(owned_fd: OwnedFd) -> Self {
Expand All @@ -334,6 +343,7 @@ impl From<OwnedFd> for crate::net::TcpStream {
}

#[stable(feature = "io_safety", since = "1.63.0")]
#[cfg(not(target_os = "trusty"))]
impl AsFd for crate::net::TcpListener {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
Expand All @@ -342,6 +352,7 @@ impl AsFd for crate::net::TcpListener {
}

#[stable(feature = "io_safety", since = "1.63.0")]
#[cfg(not(target_os = "trusty"))]
impl From<crate::net::TcpListener> for OwnedFd {
/// Takes ownership of a [`TcpListener`](crate::net::TcpListener)'s socket file descriptor.
#[inline]
Expand All @@ -351,6 +362,7 @@ impl From<crate::net::TcpListener> for OwnedFd {
}

#[stable(feature = "io_safety", since = "1.63.0")]
#[cfg(not(target_os = "trusty"))]
impl From<OwnedFd> for crate::net::TcpListener {
#[inline]
fn from(owned_fd: OwnedFd) -> Self {
Expand All @@ -361,6 +373,7 @@ impl From<OwnedFd> for crate::net::TcpListener {
}

#[stable(feature = "io_safety", since = "1.63.0")]
#[cfg(not(target_os = "trusty"))]
impl AsFd for crate::net::UdpSocket {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
Expand All @@ -369,6 +382,7 @@ impl AsFd for crate::net::UdpSocket {
}

#[stable(feature = "io_safety", since = "1.63.0")]
#[cfg(not(target_os = "trusty"))]
impl From<crate::net::UdpSocket> for OwnedFd {
/// Takes ownership of a [`UdpSocket`](crate::net::UdpSocket)'s file descriptor.
#[inline]
Expand All @@ -378,6 +392,7 @@ impl From<crate::net::UdpSocket> for OwnedFd {
}

#[stable(feature = "io_safety", since = "1.63.0")]
#[cfg(not(target_os = "trusty"))]
impl From<OwnedFd> for crate::net::UdpSocket {
#[inline]
fn from(owned_fd: OwnedFd) -> Self {
Expand Down
10 changes: 9 additions & 1 deletion library/std/src/os/fd/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ use crate::os::unix::io::AsFd;
use crate::os::unix::io::OwnedFd;
#[cfg(target_os = "wasi")]
use crate::os::wasi::io::OwnedFd;
#[cfg(not(target_os = "trusty"))]
use crate::sys_common::{AsInner, IntoInner};
use crate::{fs, io};
#[cfg(not(target_os = "trusty"))]
use crate::fs;
use crate::io;

/// Raw file descriptors.
#[stable(feature = "rust1", since = "1.0.0")]
Expand Down Expand Up @@ -161,20 +164,23 @@ impl FromRawFd for RawFd {
}

#[stable(feature = "rust1", since = "1.0.0")]
#[cfg(not(target_os = "trusty"))]
impl AsRawFd for fs::File {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.as_inner().as_raw_fd()
}
}
#[stable(feature = "from_raw_os", since = "1.1.0")]
#[cfg(not(target_os = "trusty"))]
impl FromRawFd for fs::File {
#[inline]
unsafe fn from_raw_fd(fd: RawFd) -> fs::File {
unsafe { fs::File::from(OwnedFd::from_raw_fd(fd)) }
}
}
#[stable(feature = "into_raw_os", since = "1.4.0")]
#[cfg(not(target_os = "trusty"))]
impl IntoRawFd for fs::File {
#[inline]
fn into_raw_fd(self) -> RawFd {
Expand All @@ -183,6 +189,7 @@ impl IntoRawFd for fs::File {
}

#[stable(feature = "asraw_stdio", since = "1.21.0")]
#[cfg(not(target_os = "trusty"))]
impl AsRawFd for io::Stdin {
#[inline]
fn as_raw_fd(&self) -> RawFd {
Expand All @@ -207,6 +214,7 @@ impl AsRawFd for io::Stderr {
}

#[stable(feature = "asraw_stdio_locks", since = "1.35.0")]
#[cfg(not(target_os = "trusty"))]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a bunch of cfg seems like a tacit admission that this implementation is not very useful, and depends on certain implementations never being even referenced.

In general, we have avoided adding cfg to the implementations of std. I do not think we should get in the habit here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a better approach to follow for disabling unsupported parts of std? Would it be better to instead stub out platform APIs with unimplemented!? That would involve fewer cfgs, though I'm not sure how to handle tests with that approach.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is really no satisfactory answer, to be clear, which is why we have shifted towards being increasingly reluctant to allow targets to add only partial support for std.

impl<'a> AsRawFd for io::StdinLock<'a> {
#[inline]
fn as_raw_fd(&self) -> RawFd {
Expand Down
4 changes: 3 additions & 1 deletion library/std/src/os/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ pub mod rtems;
pub mod solaris;
#[cfg(target_os = "solid_asp3")]
pub mod solid;
#[cfg(target_os = "trusty")]
pub mod trusty;
#[cfg(target_os = "uefi")]
pub mod uefi;
#[cfg(target_os = "vita")]
Expand All @@ -178,7 +180,7 @@ pub mod vxworks;
#[cfg(target_os = "xous")]
pub mod xous;

#[cfg(any(unix, target_os = "hermit", target_os = "wasi", doc))]
#[cfg(any(unix, target_os = "hermit", target_os = "trusty", target_os = "wasi", doc))]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole module is about providing safe abstractions for file descriptors that maintain IO safety. But if I understand the documentation correctly, there is no way to open or close file descriptors on Trusty, meaning the whole concept of IO safety is unnecessary. Thus I don't think that this module should exist on Trusty.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do make heavy use of OwnedFd in Trusty. While userspace applications can't directly open e.g. a file, we do have a concept of handles that are used for things like IPC. We'd also like to have access to the os::fd module because Trusty shares code with Android, and it'd be difficult if we had two different fd types. Sharing the same fd infrastructure in libstd simplifies things for us a lot.

pub mod fd;

#[cfg(any(target_os = "linux", target_os = "android", doc))]
Expand Down
4 changes: 4 additions & 0 deletions library/std/src/os/trusty/io/fd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//! Owned and borrowed file descriptors.
#![stable(feature = "os_fd", since = "1.66.0")]

pub use crate::os::fd::owned::*;
4 changes: 4 additions & 0 deletions library/std/src/os/trusty/io/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#![stable(feature = "os_fd", since = "1.66.0")]

#[stable(feature = "os_fd", since = "1.66.0")]
pub use crate::os::fd::*;
3 changes: 3 additions & 0 deletions library/std/src/os/trusty/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#![stable(feature = "rust1", since = "1.0.0")]

pub mod io;