From 87ca2dbb0054256a675e18ddb7098406db4e42ed Mon Sep 17 00:00:00 2001 From: Nicole LeGare Date: Tue, 4 Feb 2025 21:59:22 +0000 Subject: [PATCH 1/9] Apply rustc-0023-Add-Trusty-OS-support-to-Rust-std.patch --- library/std/build.rs | 1 + library/std/src/sys/alloc/mod.rs | 1 + library/std/src/sys/pal/mod.rs | 3 + library/std/src/sys/pal/trusty/mod.rs | 28 +++++++ library/std/src/sys/pal/trusty/stdio.rs | 82 +++++++++++++++++++ library/std/src/sys/random/mod.rs | 3 + library/std/src/sys/random/trusty.rs | 7 ++ .../std/src/sys/thread_local/key/trusty.rs | 30 +++++++ library/std/src/sys/thread_local/mod.rs | 9 ++ 9 files changed, 164 insertions(+) create mode 100644 library/std/src/sys/pal/trusty/mod.rs create mode 100644 library/std/src/sys/pal/trusty/stdio.rs create mode 100644 library/std/src/sys/random/trusty.rs create mode 100644 library/std/src/sys/thread_local/key/trusty.rs diff --git a/library/std/build.rs b/library/std/build.rs index cedfd7406a1aa..20373aab689b2 100644 --- a/library/std/build.rs +++ b/library/std/build.rs @@ -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" diff --git a/library/std/src/sys/alloc/mod.rs b/library/std/src/sys/alloc/mod.rs index 2c0b533a5703f..8489e17c971d9 100644 --- a/library/std/src/sys/alloc/mod.rs +++ b/library/std/src/sys/alloc/mod.rs @@ -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")] { diff --git a/library/std/src/sys/pal/mod.rs b/library/std/src/sys/pal/mod.rs index 9be018c8a5312..fbefc62ac88eb 100644 --- a/library/std/src/sys/pal/mod.rs +++ b/library/std/src/sys/pal/mod.rs @@ -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::*; diff --git a/library/std/src/sys/pal/trusty/mod.rs b/library/std/src/sys/pal/trusty/mod.rs new file mode 100644 index 0000000000000..41005205c832d --- /dev/null +++ b/library/std/src/sys/pal/trusty/mod.rs @@ -0,0 +1,28 @@ +//! System bindings for the Trusty OS. + +#[path = "../unsupported/args.rs"] +pub mod args; +#[path = "../unsupported/env.rs"] +pub mod env; +#[path = "../unsupported/fs.rs"] +pub mod fs; +#[path = "../unsupported/io.rs"] +pub mod io; +#[path = "../unsupported/net.rs"] +pub mod net; +#[path = "../unsupported/os.rs"] +pub mod os; +#[path = "../unsupported/pipe.rs"] +pub mod pipe; +#[path = "../unsupported/process.rs"] +pub mod process; +pub mod stdio; +#[path = "../unsupported/time.rs"] +pub mod time; +#[path = "../unsupported/thread.rs"] +pub mod thread; +#[path = "../unsupported/common.rs"] +#[deny(unsafe_op_in_unsafe_fn)] +mod common; + +pub use common::*; diff --git a/library/std/src/sys/pal/trusty/stdio.rs b/library/std/src/sys/pal/trusty/stdio.rs new file mode 100644 index 0000000000000..3f7c9f76e71dd --- /dev/null +++ b/library/std/src/sys/pal/trusty/stdio.rs @@ -0,0 +1,82 @@ +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 { + Ok(0) + } +} + +impl Stdout { + pub const fn new() -> Stdout { + Stdout + } +} + +impl io::Write for Stdout { + fn write(&mut self, buf: &[u8]) -> io::Result { + _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 { + _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 { + Some(Stderr) +} + +fn _write(fd: i32, message: &[u8]) -> io::Result { + 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; + } +} diff --git a/library/std/src/sys/random/mod.rs b/library/std/src/sys/random/mod.rs index f42351deb92c0..870039602bcf0 100644 --- a/library/std/src/sys/random/mod.rs +++ b/library/std/src/sys/random/mod.rs @@ -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; diff --git a/library/std/src/sys/random/trusty.rs b/library/std/src/sys/random/trusty.rs new file mode 100644 index 0000000000000..da6ca3eea2426 --- /dev/null +++ b/library/std/src/sys/random/trusty.rs @@ -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()) } +} diff --git a/library/std/src/sys/thread_local/key/trusty.rs b/library/std/src/sys/thread_local/key/trusty.rs new file mode 100644 index 0000000000000..894091d2d812c --- /dev/null +++ b/library/std/src/sys/thread_local/key/trusty.rs @@ -0,0 +1,30 @@ +use crate::ptr; + +pub type Key = usize; +type Dtor = unsafe extern "C" fn(*mut u8); + +static mut STORAGE: crate::vec::Vec<(*mut u8, Option)> = Vec::new(); + +#[inline] +pub fn create(dtor: Option) -> Key { + unsafe { + #[allow(static_mut_refs)] + let key = STORAGE.len(); + #[allow(static_mut_refs)] + STORAGE.push((ptr::null_mut(), dtor)); + key + } +} + +#[inline] +pub unsafe fn set(key: Key, value: *mut u8) { + unsafe { STORAGE[key].0 = value }; +} + +#[inline] +pub unsafe fn get(key: Key) -> *mut u8 { + unsafe { STORAGE[key].0 } +} + +#[inline] +pub fn destroy(_key: Key) {} diff --git a/library/std/src/sys/thread_local/mod.rs b/library/std/src/sys/thread_local/mod.rs index f0a13323ec93f..827f464e86042 100644 --- a/library/std/src/sys/thread_local/mod.rs +++ b/library/std/src/sys/thread_local/mod.rs @@ -170,6 +170,15 @@ pub(crate) mod key { pub(crate) use xous::destroy_tls; pub(super) use xous::{Key, get, set}; use xous::{create, destroy}; + } else if #[cfg(target_os = "trusty")] { + #[allow(unused_unsafe)] + mod racy; + #[cfg(test)] + mod tests; + mod trusty; + pub(super) use racy::LazyKey; + pub(super) use trusty::{Key, get, set}; + use trusty::{create, destroy}; } } } From 7f6ee12526700e037ef34912b2b0c628028d382c Mon Sep 17 00:00:00 2001 From: Nicole LeGare Date: Tue, 4 Feb 2025 22:00:20 +0000 Subject: [PATCH 2/9] Apply rustc-0054-Add-std-os-fd-support-for-Trusty.patch --- library/std/src/os/fd/mod.rs | 1 + library/std/src/os/fd/owned.rs | 23 +++++++++++++++++++---- library/std/src/os/fd/raw.rs | 10 +++++++++- library/std/src/os/mod.rs | 4 +++- library/std/src/os/trusty/io/fd.rs | 4 ++++ library/std/src/os/trusty/io/mod.rs | 4 ++++ library/std/src/os/trusty/mod.rs | 3 +++ 7 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 library/std/src/os/trusty/io/fd.rs create mode 100644 library/std/src/os/trusty/io/mod.rs create mode 100644 library/std/src/os/trusty/mod.rs diff --git a/library/std/src/os/fd/mod.rs b/library/std/src/os/fd/mod.rs index 35de4860fe249..95cf4932e6e2c 100644 --- a/library/std/src/os/fd/mod.rs +++ b/library/std/src/os/fd/mod.rs @@ -13,6 +13,7 @@ mod raw; mod owned; // Implementations for `AsRawFd` etc. for network types. +#[cfg(not(target_os = "trusty"))] mod net; #[cfg(test)] diff --git a/library/std/src/os/fd/owned.rs b/library/std/src/os/fd/owned.rs index 5cec11ecccf1c..9743da96197fa 100644 --- a/library/std/src/os/fd/owned.rs +++ b/library/std/src/os/fd/owned.rs @@ -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; @@ -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 { // We want to atomically duplicate this file descriptor and set the @@ -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 { Err(crate::io::Error::UNSUPPORTED_PLATFORM) @@ -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<'_> { @@ -288,6 +292,7 @@ impl AsFd for fs::File { } #[stable(feature = "io_safety", since = "1.63.0")] +#[cfg(not(target_os = "trusty"))] impl From for OwnedFd { /// Takes ownership of a [`File`](fs::File)'s underlying file descriptor. #[inline] @@ -297,6 +302,7 @@ impl From for OwnedFd { } #[stable(feature = "io_safety", since = "1.63.0")] +#[cfg(not(target_os = "trusty"))] impl From for fs::File { /// Returns a [`File`](fs::File) that takes ownership of the given /// file descriptor. @@ -307,6 +313,7 @@ impl From 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 +322,7 @@ impl AsFd for crate::net::TcpStream { } #[stable(feature = "io_safety", since = "1.63.0")] +#[cfg(not(target_os = "trusty"))] impl From for OwnedFd { /// Takes ownership of a [`TcpStream`](crate::net::TcpStream)'s socket file descriptor. #[inline] @@ -324,6 +332,7 @@ impl From for OwnedFd { } #[stable(feature = "io_safety", since = "1.63.0")] +#[cfg(not(target_os = "trusty"))] impl From for crate::net::TcpStream { #[inline] fn from(owned_fd: OwnedFd) -> Self { @@ -334,6 +343,7 @@ impl From 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 +352,7 @@ impl AsFd for crate::net::TcpListener { } #[stable(feature = "io_safety", since = "1.63.0")] +#[cfg(not(target_os = "trusty"))] impl From for OwnedFd { /// Takes ownership of a [`TcpListener`](crate::net::TcpListener)'s socket file descriptor. #[inline] @@ -351,6 +362,7 @@ impl From for OwnedFd { } #[stable(feature = "io_safety", since = "1.63.0")] +#[cfg(not(target_os = "trusty"))] impl From for crate::net::TcpListener { #[inline] fn from(owned_fd: OwnedFd) -> Self { @@ -361,6 +373,7 @@ impl From 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 +382,7 @@ impl AsFd for crate::net::UdpSocket { } #[stable(feature = "io_safety", since = "1.63.0")] +#[cfg(not(target_os = "trusty"))] impl From for OwnedFd { /// Takes ownership of a [`UdpSocket`](crate::net::UdpSocket)'s file descriptor. #[inline] @@ -378,6 +392,7 @@ impl From for OwnedFd { } #[stable(feature = "io_safety", since = "1.63.0")] +#[cfg(not(target_os = "trusty"))] impl From for crate::net::UdpSocket { #[inline] fn from(owned_fd: OwnedFd) -> Self { diff --git a/library/std/src/os/fd/raw.rs b/library/std/src/os/fd/raw.rs index 03dff94350dad..8cbed7d9686c5 100644 --- a/library/std/src/os/fd/raw.rs +++ b/library/std/src/os/fd/raw.rs @@ -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")] @@ -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 { diff --git a/library/std/src/os/mod.rs b/library/std/src/os/mod.rs index e28a1c3e6d5f4..58cbecd30e538 100644 --- a/library/std/src/os/mod.rs +++ b/library/std/src/os/mod.rs @@ -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))] diff --git a/library/std/src/os/trusty/io/fd.rs b/library/std/src/os/trusty/io/fd.rs new file mode 100644 index 0000000000000..0f0b5a8b33456 --- /dev/null +++ b/library/std/src/os/trusty/io/fd.rs @@ -0,0 +1,4 @@ +//! Owned and borrowed file descriptors. +#![stable(feature = "os_fd", since = "1.66.0")] + +pub use crate::os::fd::owned::*; diff --git a/library/std/src/os/trusty/io/mod.rs b/library/std/src/os/trusty/io/mod.rs new file mode 100644 index 0000000000000..4cfd448305b65 --- /dev/null +++ b/library/std/src/os/trusty/io/mod.rs @@ -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::*; diff --git a/library/std/src/os/trusty/mod.rs b/library/std/src/os/trusty/mod.rs new file mode 100644 index 0000000000000..cc67c92d7ff47 --- /dev/null +++ b/library/std/src/os/trusty/mod.rs @@ -0,0 +1,3 @@ +#![stable(feature = "rust1", since = "1.0.0")] + +pub mod io; From d633d8e074512fde1b1e7507ae758c8a7f96639b Mon Sep 17 00:00:00 2001 From: Nicole LeGare Date: Tue, 4 Feb 2025 22:18:53 +0000 Subject: [PATCH 3/9] Format after patches have been applied --- library/std/src/os/fd/owned.rs | 11 ++++++++--- library/std/src/os/fd/raw.rs | 6 +++--- library/std/src/sys/pal/trusty/mod.rs | 10 +++++----- library/std/src/sys/pal/trusty/stdio.rs | 3 +-- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/library/std/src/os/fd/owned.rs b/library/std/src/os/fd/owned.rs index 9743da96197fa..701cf82335757 100644 --- a/library/std/src/os/fd/owned.rs +++ b/library/std/src/os/fd/owned.rs @@ -4,15 +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", target_os = "trusty")))] +#[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, io}; -#[cfg(not(target_os = "trusty"))] -use crate::fs; type ValidRawFd = core::num::niche_types::NotAllOnes; diff --git a/library/std/src/os/fd/raw.rs b/library/std/src/os/fd/raw.rs index 8cbed7d9686c5..083ac6e3fe6b1 100644 --- a/library/std/src/os/fd/raw.rs +++ b/library/std/src/os/fd/raw.rs @@ -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"))] @@ -17,9 +20,6 @@ use crate::os::unix::io::OwnedFd; use crate::os::wasi::io::OwnedFd; #[cfg(not(target_os = "trusty"))] use crate::sys_common::{AsInner, IntoInner}; -#[cfg(not(target_os = "trusty"))] -use crate::fs; -use crate::io; /// Raw file descriptors. #[stable(feature = "rust1", since = "1.0.0")] diff --git a/library/std/src/sys/pal/trusty/mod.rs b/library/std/src/sys/pal/trusty/mod.rs index 41005205c832d..2b2774119e152 100644 --- a/library/std/src/sys/pal/trusty/mod.rs +++ b/library/std/src/sys/pal/trusty/mod.rs @@ -2,6 +2,9 @@ #[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/fs.rs"] @@ -17,12 +20,9 @@ pub mod pipe; #[path = "../unsupported/process.rs"] pub mod process; pub mod stdio; -#[path = "../unsupported/time.rs"] -pub mod time; #[path = "../unsupported/thread.rs"] pub mod thread; -#[path = "../unsupported/common.rs"] -#[deny(unsafe_op_in_unsafe_fn)] -mod common; +#[path = "../unsupported/time.rs"] +pub mod time; pub use common::*; diff --git a/library/std/src/sys/pal/trusty/stdio.rs b/library/std/src/sys/pal/trusty/stdio.rs index 3f7c9f76e71dd..d393e95394d1a 100644 --- a/library/std/src/sys/pal/trusty/stdio.rs +++ b/library/std/src/sys/pal/trusty/stdio.rs @@ -59,8 +59,7 @@ pub fn panic_output() -> Option { } fn _write(fd: i32, message: &[u8]) -> io::Result { - let mut iov = - libc::iovec { iov_base: message.as_ptr() as *mut _, iov_len: message.len() }; + 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) }; From 22fea97c9d799a6246620d557b55c1c8094e3fc9 Mon Sep 17 00:00:00 2001 From: Nicole LeGare Date: Tue, 4 Feb 2025 23:02:00 +0000 Subject: [PATCH 4/9] Disable unsupported tests Unclear why this needs to be done manually and is not done by the existing Trusty patches. --- library/std/src/fs.rs | 3 ++- library/std/src/net/tcp.rs | 3 ++- library/std/src/net/udp.rs | 3 ++- library/std/src/process.rs | 3 ++- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/library/std/src/fs.rs b/library/std/src/fs.rs index 46b5860123fc1..f9a360585e852 100644 --- a/library/std/src/fs.rs +++ b/library/std/src/fs.rs @@ -14,7 +14,8 @@ target_os = "emscripten", target_os = "wasi", target_env = "sgx", - target_os = "xous" + target_os = "xous", + target_os = "trusty", )) ))] mod tests; diff --git a/library/std/src/net/tcp.rs b/library/std/src/net/tcp.rs index 9b68f872955c0..6a95142640726 100644 --- a/library/std/src/net/tcp.rs +++ b/library/std/src/net/tcp.rs @@ -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; diff --git a/library/std/src/net/udp.rs b/library/std/src/net/udp.rs index 3eb798ad34aaa..a97b3299774bb 100644 --- a/library/std/src/net/udp.rs +++ b/library/std/src/net/udp.rs @@ -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; diff --git a/library/std/src/process.rs b/library/std/src/process.rs index bdd4844b6511a..37762c65f6556 100644 --- a/library/std/src/process.rs +++ b/library/std/src/process.rs @@ -154,7 +154,8 @@ target_os = "emscripten", target_os = "wasi", target_env = "sgx", - target_os = "xous" + target_os = "xous", + target_os = "trusty", )) ))] mod tests; From 5b941136f1dbceca775b8770014783dc98998e45 Mon Sep 17 00:00:00 2001 From: Nicole LeGare Date: Mon, 10 Feb 2025 16:23:25 -0800 Subject: [PATCH 5/9] Update Trusty platform docs --- src/doc/rustc/src/platform-support.md | 6 +++--- src/doc/rustc/src/platform-support/trusty.md | 6 ++++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/doc/rustc/src/platform-support.md b/src/doc/rustc/src/platform-support.md index f78ab151b9c24..da2f4f68672ad 100644 --- a/src/doc/rustc/src/platform-support.md +++ b/src/doc/rustc/src/platform-support.md @@ -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 @@ -418,7 +418,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 diff --git a/src/doc/rustc/src/platform-support/trusty.md b/src/doc/rustc/src/platform-support/trusty.md index 6b37543b600a7..73fcbbdddca36 100644 --- a/src/doc/rustc/src/platform-support/trusty.md +++ b/src/doc/rustc/src/platform-support/trusty.md @@ -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. From 0b1a7ab3393e59f59a0d53e66cccb195dadc378e Mon Sep 17 00:00:00 2001 From: Nicole LeGare Date: Wed, 19 Feb 2025 11:55:28 -0800 Subject: [PATCH 6/9] Remove custom TLS implementation for Trusty targets --- .../std/src/sys/thread_local/key/trusty.rs | 30 ------------------- library/std/src/sys/thread_local/mod.rs | 11 ++----- 2 files changed, 2 insertions(+), 39 deletions(-) delete mode 100644 library/std/src/sys/thread_local/key/trusty.rs diff --git a/library/std/src/sys/thread_local/key/trusty.rs b/library/std/src/sys/thread_local/key/trusty.rs deleted file mode 100644 index 894091d2d812c..0000000000000 --- a/library/std/src/sys/thread_local/key/trusty.rs +++ /dev/null @@ -1,30 +0,0 @@ -use crate::ptr; - -pub type Key = usize; -type Dtor = unsafe extern "C" fn(*mut u8); - -static mut STORAGE: crate::vec::Vec<(*mut u8, Option)> = Vec::new(); - -#[inline] -pub fn create(dtor: Option) -> Key { - unsafe { - #[allow(static_mut_refs)] - let key = STORAGE.len(); - #[allow(static_mut_refs)] - STORAGE.push((ptr::null_mut(), dtor)); - key - } -} - -#[inline] -pub unsafe fn set(key: Key, value: *mut u8) { - unsafe { STORAGE[key].0 = value }; -} - -#[inline] -pub unsafe fn get(key: Key) -> *mut u8 { - unsafe { STORAGE[key].0 } -} - -#[inline] -pub fn destroy(_key: Key) {} diff --git a/library/std/src/sys/thread_local/mod.rs b/library/std/src/sys/thread_local/mod.rs index 827f464e86042..1ff13154b7b3c 100644 --- a/library/std/src/sys/thread_local/mod.rs +++ b/library/std/src/sys/thread_local/mod.rs @@ -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 @@ -170,15 +172,6 @@ pub(crate) mod key { pub(crate) use xous::destroy_tls; pub(super) use xous::{Key, get, set}; use xous::{create, destroy}; - } else if #[cfg(target_os = "trusty")] { - #[allow(unused_unsafe)] - mod racy; - #[cfg(test)] - mod tests; - mod trusty; - pub(super) use racy::LazyKey; - pub(super) use trusty::{Key, get, set}; - use trusty::{create, destroy}; } } } From f5dd3d13fc4685b2846f130f5e5b633c50cefc55 Mon Sep 17 00:00:00 2001 From: Nicole L Date: Mon, 10 Mar 2025 12:54:59 -0700 Subject: [PATCH 7/9] Update Trusty support to account for recent libstd reorganization --- library/std/src/sys/pal/trusty/mod.rs | 7 ------- library/std/src/sys/stdio/mod.rs | 3 +++ .../std/src/sys/{pal/trusty/stdio.rs => stdio/trusty.rs} | 0 3 files changed, 3 insertions(+), 7 deletions(-) rename library/std/src/sys/{pal/trusty/stdio.rs => stdio/trusty.rs} (100%) diff --git a/library/std/src/sys/pal/trusty/mod.rs b/library/std/src/sys/pal/trusty/mod.rs index 2b2774119e152..7034b643d8e8e 100644 --- a/library/std/src/sys/pal/trusty/mod.rs +++ b/library/std/src/sys/pal/trusty/mod.rs @@ -7,19 +7,12 @@ pub mod args; mod common; #[path = "../unsupported/env.rs"] pub mod env; -#[path = "../unsupported/fs.rs"] -pub mod fs; -#[path = "../unsupported/io.rs"] -pub mod io; -#[path = "../unsupported/net.rs"] -pub mod net; #[path = "../unsupported/os.rs"] pub mod os; #[path = "../unsupported/pipe.rs"] pub mod pipe; #[path = "../unsupported/process.rs"] pub mod process; -pub mod stdio; #[path = "../unsupported/thread.rs"] pub mod thread; #[path = "../unsupported/time.rs"] diff --git a/library/std/src/sys/stdio/mod.rs b/library/std/src/sys/stdio/mod.rs index 2a9167bfe966c..336d4c8527db3 100644 --- a/library/std/src/sys/stdio/mod.rs +++ b/library/std/src/sys/stdio/mod.rs @@ -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::*; diff --git a/library/std/src/sys/pal/trusty/stdio.rs b/library/std/src/sys/stdio/trusty.rs similarity index 100% rename from library/std/src/sys/pal/trusty/stdio.rs rename to library/std/src/sys/stdio/trusty.rs From 2b3b0bd50b8e62d837253c6787de0c763ed17bce Mon Sep 17 00:00:00 2001 From: Nicole L Date: Mon, 10 Mar 2025 14:19:27 -0700 Subject: [PATCH 8/9] Remove unused file --- library/std/src/os/trusty/io/fd.rs | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 library/std/src/os/trusty/io/fd.rs diff --git a/library/std/src/os/trusty/io/fd.rs b/library/std/src/os/trusty/io/fd.rs deleted file mode 100644 index 0f0b5a8b33456..0000000000000 --- a/library/std/src/os/trusty/io/fd.rs +++ /dev/null @@ -1,4 +0,0 @@ -//! Owned and borrowed file descriptors. -#![stable(feature = "os_fd", since = "1.66.0")] - -pub use crate::os::fd::owned::*; From d3c55cd52b40ce2088122933bf3527670a42bd8a Mon Sep 17 00:00:00 2001 From: Nicole L Date: Tue, 11 Mar 2025 11:16:10 -0700 Subject: [PATCH 9/9] Remove unnecessary parens Co-authored-by: bjorn3 <17426603+bjorn3@users.noreply.github.com> --- library/std/build.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/build.rs b/library/std/build.rs index 20373aab689b2..a0cfbc4685ee5 100644 --- a/library/std/build.rs +++ b/library/std/build.rs @@ -42,7 +42,7 @@ fn main() { || target_os == "fuchsia" || (target_vendor == "fortanix" && target_env == "sgx") || target_os == "hermit" - || target_os == ("trusty") + || target_os == "trusty" || target_os == "l4re" || target_os == "redox" || target_os == "haiku"