Rollup merge of #136842 - randomPoison:trusty-libstd-v3, r=ChrisDenton

Add libstd support for Trusty targets

This PR adds support for `alloc` and `std` for the Trusty targets based on the internal patches used in Android. The original patches can be seen [here](https://android.googlesource.com/toolchain/android_rust/+/refs/heads/main/patches/development/rustc-0023-Add-Trusty-OS-support-to-Rust-std.patch) and [here](https://android.googlesource.com/toolchain/android_rust/+/refs/heads/main/patches/development/rustc-0054-Add-std-os-fd-support-for-Trusty.patch). Please let me know if there's any additional context I need to add.
This commit is contained in:
Matthias Krüger 2025-03-13 10:58:17 +01:00 committed by GitHub
commit a488cf8a70
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
21 changed files with 181 additions and 15 deletions

View file

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

View file

@ -14,7 +14,8 @@
target_os = "emscripten",
target_os = "wasi",
target_env = "sgx",
target_os = "xous"
target_os = "xous",
target_os = "trusty",
))
))]
mod tests;

View file

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

View file

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

View file

@ -13,6 +13,7 @@ mod raw;
mod owned;
// Implementations for `AsRawFd` etc. for network types.
#[cfg(not(target_os = "trusty"))]
mod net;
#[cfg(test)]

View file

@ -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>;
@ -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
@ -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)
@ -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<'_> {
@ -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]
@ -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.
@ -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<'_> {
@ -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]
@ -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 {
@ -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<'_> {
@ -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]
@ -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 {
@ -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<'_> {
@ -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]
@ -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 {

View file

@ -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"))]
@ -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")]
@ -161,6 +164,7 @@ 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 {
@ -168,6 +172,7 @@ impl AsRawFd for fs::File {
}
}
#[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 {
@ -175,6 +180,7 @@ impl FromRawFd for fs::File {
}
}
#[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 {
@ -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 {
@ -207,6 +214,7 @@ impl AsRawFd for io::Stderr {
}
#[stable(feature = "asraw_stdio_locks", since = "1.35.0")]
#[cfg(not(target_os = "trusty"))]
impl<'a> AsRawFd for io::StdinLock<'a> {
#[inline]
fn as_raw_fd(&self) -> RawFd {

View file

@ -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")]
@ -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))]
pub mod fd;
#[cfg(any(target_os = "linux", target_os = "android", doc))]

View file

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

View file

@ -0,0 +1,3 @@
#![stable(feature = "rust1", since = "1.0.0")]
pub mod io;

View file

@ -154,7 +154,8 @@
target_os = "emscripten",
target_os = "wasi",
target_env = "sgx",
target_os = "xous"
target_os = "xous",
target_os = "trusty",
))
))]
mod tests;

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -0,0 +1,81 @@
use crate::io;
pub struct Stdin;
pub struct Stdout;
pub struct Stderr;
impl Stdin {
pub const fn new() -> Stdin {
Stdin
}
}
impl io::Read for Stdin {
fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> {
Ok(0)
}
}
impl Stdout {
pub const fn new() -> Stdout {
Stdout
}
}
impl io::Write for Stdout {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
_write(libc::STDOUT_FILENO, buf)
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
impl Stderr {
pub const fn new() -> Stderr {
Stderr
}
}
impl io::Write for Stderr {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
_write(libc::STDERR_FILENO, buf)
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
pub const STDIN_BUF_SIZE: usize = 0;
pub fn is_ebadf(_err: &io::Error) -> bool {
true
}
pub fn panic_output() -> Option<impl io::Write> {
Some(Stderr)
}
fn _write(fd: i32, message: &[u8]) -> io::Result<usize> {
let mut iov = libc::iovec { iov_base: message.as_ptr() as *mut _, iov_len: message.len() };
loop {
// SAFETY: syscall, safe arguments.
let ret = unsafe { libc::writev(fd, &iov, 1) };
if ret < 0 {
return Err(io::Error::last_os_error());
}
let ret = ret as usize;
if ret > iov.iov_len {
return Err(io::Error::last_os_error());
}
if ret == iov.iov_len {
return Ok(message.len());
}
// SAFETY: ret has been checked to be less than the length of
// the buffer
iov.iov_base = unsafe { iov.iov_base.add(ret) };
iov.iov_len -= ret;
}
}

View file

@ -28,6 +28,7 @@ cfg_if::cfg_if! {
all(target_family = "wasm", not(target_feature = "atomics")),
target_os = "uefi",
target_os = "zkvm",
target_os = "trusty",
))] {
mod statik;
pub use statik::{EagerStorage, LazyStorage, thread_local_inner};
@ -91,6 +92,7 @@ pub(crate) mod guard {
)),
target_os = "uefi",
target_os = "zkvm",
target_os = "trusty",
))] {
pub(crate) fn enable() {
// FIXME: Right now there is no concept of "thread exit" on

View file

@ -265,7 +265,7 @@ target | std | host | notes
[`aarch64-unknown-openbsd`](platform-support/openbsd.md) | ✓ | ✓ | ARM64 OpenBSD
[`aarch64-unknown-redox`](platform-support/redox.md) | ✓ | | ARM64 Redox OS
[`aarch64-unknown-teeos`](platform-support/aarch64-unknown-teeos.md) | ? | | ARM64 TEEOS |
[`aarch64-unknown-trusty`](platform-support/trusty.md) | ? | |
[`aarch64-unknown-trusty`](platform-support/trusty.md) | | |
[`aarch64-uwp-windows-msvc`](platform-support/uwp-windows-msvc.md) | ✓ | |
[`aarch64-wrs-vxworks`](platform-support/vxworks.md) | ✓ | | ARM64 VxWorks OS
`aarch64_be-unknown-linux-gnu` | ✓ | ✓ | ARM64 Linux (big-endian)
@ -290,7 +290,7 @@ target | std | host | notes
[`armv7-unknown-linux-uclibceabi`](platform-support/armv7-unknown-linux-uclibceabi.md) | ✓ | ✓ | Armv7-A Linux with uClibc, softfloat
[`armv7-unknown-linux-uclibceabihf`](platform-support/armv7-unknown-linux-uclibceabihf.md) | ✓ | ? | Armv7-A Linux with uClibc, hardfloat
[`armv7-unknown-netbsd-eabihf`](platform-support/netbsd.md) | ✓ | ✓ | Armv7-A NetBSD w/hard-float
[`armv7-unknown-trusty`](platform-support/trusty.md) | ? | |
[`armv7-unknown-trusty`](platform-support/trusty.md) | | |
[`armv7-wrs-vxworks-eabihf`](platform-support/vxworks.md) | ✓ | | Armv7-A for VxWorks
[`armv7a-kmc-solid_asp3-eabi`](platform-support/kmc-solid.md) | ✓ | | ARM SOLID with TOPPERS/ASP3
[`armv7a-kmc-solid_asp3-eabihf`](platform-support/kmc-solid.md) | ✓ | | ARM SOLID with TOPPERS/ASP3, hardfloat
@ -419,7 +419,7 @@ target | std | host | notes
`x86_64-unknown-l4re-uclibc` | ? | |
[`x86_64-unknown-linux-none`](platform-support/x86_64-unknown-linux-none.md) | * | | 64-bit Linux with no libc
[`x86_64-unknown-openbsd`](platform-support/openbsd.md) | ✓ | ✓ | 64-bit OpenBSD
[`x86_64-unknown-trusty`](platform-support/trusty.md) | ? | |
[`x86_64-unknown-trusty`](platform-support/trusty.md) | | |
`x86_64-uwp-windows-gnu` | ✓ | |
[`x86_64-uwp-windows-msvc`](platform-support/uwp-windows-msvc.md) | ✓ | |
[`x86_64-win7-windows-gnu`](platform-support/win7-windows-gnu.md) | ✓ | | 64-bit Windows 7 support

View file

@ -16,8 +16,10 @@ Environment (TEE) for Android.
These targets are cross-compiled. They have no special requirements for the host.
Support for the standard library is work-in-progress. It is expected that
they will support alloc with the default allocator, and partially support std.
Trusty targets have partial support for the standard library: `alloc` is fully
supported and `std` has limited support that excludes things like filesystem
access, network I/O, and spawning processes/threads. File descriptors are
supported for the purpose of IPC.
Trusty uses the ELF file format.