Skip to content

Add libstd support for Trusty targets #136842

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

Merged
merged 9 commits into from
Mar 13, 2025
1 change: 1 addition & 0 deletions library/std/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ fn main() {
|| target_os == "fuchsia"
|| (target_vendor == "fortanix" && target_env == "sgx")
|| target_os == "hermit"
|| target_os == "trusty"
|| target_os == "l4re"
|| target_os == "redox"
|| target_os == "haiku"
Expand Down
3 changes: 2 additions & 1 deletion library/std/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
target_os = "emscripten",
target_os = "wasi",
target_env = "sgx",
target_os = "xous"
target_os = "xous",
target_os = "trusty",
))
))]
mod tests;
Expand Down
3 changes: 2 additions & 1 deletion library/std/src/net/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
not(any(
target_os = "emscripten",
all(target_os = "wasi", target_env = "p1"),
target_os = "xous"
target_os = "xous",
target_os = "trusty",
))
))]
mod tests;
Expand Down
3 changes: 2 additions & 1 deletion library/std/src/net/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
target_os = "emscripten",
all(target_os = "wasi", target_env = "p1"),
target_env = "sgx",
target_os = "xous"
target_os = "xous",
target_os = "trusty",
))
))]
mod tests;
Expand Down
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
28 changes: 24 additions & 4 deletions library/std/src/os/fd/owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@
#![deny(unsafe_op_in_unsafe_fn)]

use super::raw::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
#[cfg(not(target_os = "trusty"))]
use crate::fs;
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};

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

Expand Down Expand Up @@ -87,7 +95,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 +118,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 +288,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 +297,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 +307,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 +318,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 +327,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 +337,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 +348,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 +357,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 +367,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 +378,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 +387,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 +397,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 @@ -5,6 +5,9 @@
#[cfg(target_os = "hermit")]
use hermit_abi as libc;

#[cfg(not(target_os = "trusty"))]
use crate::fs;
use crate::io;
#[cfg(target_os = "hermit")]
use crate::os::hermit::io::OwnedFd;
#[cfg(not(target_os = "hermit"))]
Expand All @@ -15,8 +18,8 @@ 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};

/// 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/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;
3 changes: 2 additions & 1 deletion library/std/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@
target_os = "emscripten",
target_os = "wasi",
target_env = "sgx",
target_os = "xous"
target_os = "xous",
target_os = "trusty",
))
))]
mod tests;
Expand Down
1 change: 1 addition & 0 deletions library/std/src/sys/alloc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ cfg_if::cfg_if! {
target_family = "unix",
target_os = "wasi",
target_os = "teeos",
target_os = "trusty",
))] {
mod unix;
} else if #[cfg(target_os = "windows")] {
Expand Down
3 changes: 3 additions & 0 deletions library/std/src/sys/pal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ cfg_if::cfg_if! {
} else if #[cfg(target_os = "hermit")] {
mod hermit;
pub use self::hermit::*;
} else if #[cfg(target_os = "trusty")] {
mod trusty;
pub use self::trusty::*;
} else if #[cfg(all(target_os = "wasi", target_env = "p2"))] {
mod wasip2;
pub use self::wasip2::*;
Expand Down
21 changes: 21 additions & 0 deletions library/std/src/sys/pal/trusty/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//! System bindings for the Trusty OS.

#[path = "../unsupported/args.rs"]
pub mod args;
#[path = "../unsupported/common.rs"]
#[deny(unsafe_op_in_unsafe_fn)]
mod common;
#[path = "../unsupported/env.rs"]
pub mod env;
#[path = "../unsupported/os.rs"]
pub mod os;
#[path = "../unsupported/pipe.rs"]
pub mod pipe;
#[path = "../unsupported/process.rs"]
pub mod process;
#[path = "../unsupported/thread.rs"]
pub mod thread;
#[path = "../unsupported/time.rs"]
pub mod time;

pub use common::*;
3 changes: 3 additions & 0 deletions library/std/src/sys/random/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ cfg_if::cfg_if! {
} else if #[cfg(target_os = "teeos")] {
mod teeos;
pub use teeos::fill_bytes;
} else if #[cfg(target_os = "trusty")] {
mod trusty;
pub use trusty::fill_bytes;
} else if #[cfg(target_os = "uefi")] {
mod uefi;
pub use uefi::fill_bytes;
Expand Down
7 changes: 7 additions & 0 deletions library/std/src/sys/random/trusty.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
extern "C" {
fn trusty_rng_secure_rand(randomBuffer: *mut core::ffi::c_void, randomBufferLen: libc::size_t);
}

pub fn fill_bytes(bytes: &mut [u8]) {
unsafe { trusty_rng_secure_rand(bytes.as_mut_ptr().cast(), bytes.len()) }
}
3 changes: 3 additions & 0 deletions library/std/src/sys/stdio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ cfg_if::cfg_if! {
} else if #[cfg(target_os = "teeos")] {
mod teeos;
pub use teeos::*;
} else if #[cfg(target_os = "trusty")] {
mod trusty;
pub use trusty::*;
} else if #[cfg(target_os = "uefi")] {
mod uefi;
pub use uefi::*;
Expand Down
Loading
Loading