Skip to content

Commit 007b229

Browse files
authored
Rollup merge of rust-lang#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.
2 parents b296531 + a1e7fd4 commit 007b229

File tree

19 files changed

+174
-10
lines changed

19 files changed

+174
-10
lines changed

std/build.rs

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ fn main() {
4242
|| target_os == "fuchsia"
4343
|| (target_vendor == "fortanix" && target_env == "sgx")
4444
|| target_os == "hermit"
45+
|| target_os == "trusty"
4546
|| target_os == "l4re"
4647
|| target_os == "redox"
4748
|| target_os == "haiku"

std/src/fs.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
target_os = "emscripten",
1515
target_os = "wasi",
1616
target_env = "sgx",
17-
target_os = "xous"
17+
target_os = "xous",
18+
target_os = "trusty",
1819
))
1920
))]
2021
mod tests;

std/src/net/tcp.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
not(any(
66
target_os = "emscripten",
77
all(target_os = "wasi", target_env = "p1"),
8-
target_os = "xous"
8+
target_os = "xous",
9+
target_os = "trusty",
910
))
1011
))]
1112
mod tests;

std/src/net/udp.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
target_os = "emscripten",
55
all(target_os = "wasi", target_env = "p1"),
66
target_env = "sgx",
7-
target_os = "xous"
7+
target_os = "xous",
8+
target_os = "trusty",
89
))
910
))]
1011
mod tests;

std/src/os/fd/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ mod raw;
1313
mod owned;
1414

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

1819
#[cfg(test)]

std/src/os/fd/owned.rs

+24-4
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,20 @@
44
#![deny(unsafe_op_in_unsafe_fn)]
55

66
use super::raw::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
7+
#[cfg(not(target_os = "trusty"))]
8+
use crate::fs;
79
use crate::marker::PhantomData;
810
use crate::mem::ManuallyDrop;
9-
#[cfg(not(any(target_arch = "wasm32", target_env = "sgx", target_os = "hermit")))]
11+
#[cfg(not(any(
12+
target_arch = "wasm32",
13+
target_env = "sgx",
14+
target_os = "hermit",
15+
target_os = "trusty"
16+
)))]
1017
use crate::sys::cvt;
18+
#[cfg(not(target_os = "trusty"))]
1119
use crate::sys_common::{AsInner, FromInner, IntoInner};
12-
use crate::{fmt, fs, io};
20+
use crate::{fmt, io};
1321

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

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

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

282290
#[stable(feature = "io_safety", since = "1.63.0")]
291+
#[cfg(not(target_os = "trusty"))]
283292
impl AsFd for fs::File {
284293
#[inline]
285294
fn as_fd(&self) -> BorrowedFd<'_> {
@@ -288,6 +297,7 @@ impl AsFd for fs::File {
288297
}
289298

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

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

309320
#[stable(feature = "io_safety", since = "1.63.0")]
321+
#[cfg(not(target_os = "trusty"))]
310322
impl AsFd for crate::net::TcpStream {
311323
#[inline]
312324
fn as_fd(&self) -> BorrowedFd<'_> {
@@ -315,6 +327,7 @@ impl AsFd for crate::net::TcpStream {
315327
}
316328

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

326339
#[stable(feature = "io_safety", since = "1.63.0")]
340+
#[cfg(not(target_os = "trusty"))]
327341
impl From<OwnedFd> for crate::net::TcpStream {
328342
#[inline]
329343
fn from(owned_fd: OwnedFd) -> Self {
@@ -334,6 +348,7 @@ impl From<OwnedFd> for crate::net::TcpStream {
334348
}
335349

336350
#[stable(feature = "io_safety", since = "1.63.0")]
351+
#[cfg(not(target_os = "trusty"))]
337352
impl AsFd for crate::net::TcpListener {
338353
#[inline]
339354
fn as_fd(&self) -> BorrowedFd<'_> {
@@ -342,6 +357,7 @@ impl AsFd for crate::net::TcpListener {
342357
}
343358

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

353369
#[stable(feature = "io_safety", since = "1.63.0")]
370+
#[cfg(not(target_os = "trusty"))]
354371
impl From<OwnedFd> for crate::net::TcpListener {
355372
#[inline]
356373
fn from(owned_fd: OwnedFd) -> Self {
@@ -361,6 +378,7 @@ impl From<OwnedFd> for crate::net::TcpListener {
361378
}
362379

363380
#[stable(feature = "io_safety", since = "1.63.0")]
381+
#[cfg(not(target_os = "trusty"))]
364382
impl AsFd for crate::net::UdpSocket {
365383
#[inline]
366384
fn as_fd(&self) -> BorrowedFd<'_> {
@@ -369,6 +387,7 @@ impl AsFd for crate::net::UdpSocket {
369387
}
370388

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

380399
#[stable(feature = "io_safety", since = "1.63.0")]
400+
#[cfg(not(target_os = "trusty"))]
381401
impl From<OwnedFd> for crate::net::UdpSocket {
382402
#[inline]
383403
fn from(owned_fd: OwnedFd) -> Self {

std/src/os/fd/raw.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
#[cfg(target_os = "hermit")]
66
use hermit_abi as libc;
77

8+
#[cfg(not(target_os = "trusty"))]
9+
use crate::fs;
10+
use crate::io;
811
#[cfg(target_os = "hermit")]
912
use crate::os::hermit::io::OwnedFd;
1013
#[cfg(not(target_os = "hermit"))]
@@ -15,8 +18,8 @@ use crate::os::unix::io::AsFd;
1518
use crate::os::unix::io::OwnedFd;
1619
#[cfg(target_os = "wasi")]
1720
use crate::os::wasi::io::OwnedFd;
21+
#[cfg(not(target_os = "trusty"))]
1822
use crate::sys_common::{AsInner, IntoInner};
19-
use crate::{fs, io};
2023

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

163166
#[stable(feature = "rust1", since = "1.0.0")]
167+
#[cfg(not(target_os = "trusty"))]
164168
impl AsRawFd for fs::File {
165169
#[inline]
166170
fn as_raw_fd(&self) -> RawFd {
167171
self.as_inner().as_raw_fd()
168172
}
169173
}
170174
#[stable(feature = "from_raw_os", since = "1.1.0")]
175+
#[cfg(not(target_os = "trusty"))]
171176
impl FromRawFd for fs::File {
172177
#[inline]
173178
unsafe fn from_raw_fd(fd: RawFd) -> fs::File {
174179
unsafe { fs::File::from(OwnedFd::from_raw_fd(fd)) }
175180
}
176181
}
177182
#[stable(feature = "into_raw_os", since = "1.4.0")]
183+
#[cfg(not(target_os = "trusty"))]
178184
impl IntoRawFd for fs::File {
179185
#[inline]
180186
fn into_raw_fd(self) -> RawFd {
@@ -183,6 +189,7 @@ impl IntoRawFd for fs::File {
183189
}
184190

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

209216
#[stable(feature = "asraw_stdio_locks", since = "1.35.0")]
217+
#[cfg(not(target_os = "trusty"))]
210218
impl<'a> AsRawFd for io::StdinLock<'a> {
211219
#[inline]
212220
fn as_raw_fd(&self) -> RawFd {

std/src/os/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ pub mod rtems;
169169
pub mod solaris;
170170
#[cfg(target_os = "solid_asp3")]
171171
pub mod solid;
172+
#[cfg(target_os = "trusty")]
173+
pub mod trusty;
172174
#[cfg(target_os = "uefi")]
173175
pub mod uefi;
174176
#[cfg(target_os = "vita")]
@@ -178,7 +180,7 @@ pub mod vxworks;
178180
#[cfg(target_os = "xous")]
179181
pub mod xous;
180182

181-
#[cfg(any(unix, target_os = "hermit", target_os = "wasi", doc))]
183+
#[cfg(any(unix, target_os = "hermit", target_os = "trusty", target_os = "wasi", doc))]
182184
pub mod fd;
183185

184186
#[cfg(any(target_os = "linux", target_os = "android", doc))]

std/src/os/trusty/io/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#![stable(feature = "os_fd", since = "1.66.0")]
2+
3+
#[stable(feature = "os_fd", since = "1.66.0")]
4+
pub use crate::os::fd::*;

std/src/os/trusty/mod.rs

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

std/src/process.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@
154154
target_os = "emscripten",
155155
target_os = "wasi",
156156
target_env = "sgx",
157-
target_os = "xous"
157+
target_os = "xous",
158+
target_os = "trusty",
158159
))
159160
))]
160161
mod tests;

std/src/sys/alloc/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ cfg_if::cfg_if! {
7272
target_family = "unix",
7373
target_os = "wasi",
7474
target_os = "teeos",
75+
target_os = "trusty",
7576
))] {
7677
mod unix;
7778
} else if #[cfg(target_os = "windows")] {

std/src/sys/pal/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ cfg_if::cfg_if! {
3737
} else if #[cfg(target_os = "hermit")] {
3838
mod hermit;
3939
pub use self::hermit::*;
40+
} else if #[cfg(target_os = "trusty")] {
41+
mod trusty;
42+
pub use self::trusty::*;
4043
} else if #[cfg(all(target_os = "wasi", target_env = "p2"))] {
4144
mod wasip2;
4245
pub use self::wasip2::*;

std/src/sys/pal/trusty/mod.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//! System bindings for the Trusty OS.
2+
3+
#[path = "../unsupported/args.rs"]
4+
pub mod args;
5+
#[path = "../unsupported/common.rs"]
6+
#[deny(unsafe_op_in_unsafe_fn)]
7+
mod common;
8+
#[path = "../unsupported/env.rs"]
9+
pub mod env;
10+
#[path = "../unsupported/os.rs"]
11+
pub mod os;
12+
#[path = "../unsupported/pipe.rs"]
13+
pub mod pipe;
14+
#[path = "../unsupported/process.rs"]
15+
pub mod process;
16+
#[path = "../unsupported/thread.rs"]
17+
pub mod thread;
18+
#[path = "../unsupported/time.rs"]
19+
pub mod time;
20+
21+
pub use common::*;

std/src/sys/random/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ cfg_if::cfg_if! {
6060
} else if #[cfg(target_os = "teeos")] {
6161
mod teeos;
6262
pub use teeos::fill_bytes;
63+
} else if #[cfg(target_os = "trusty")] {
64+
mod trusty;
65+
pub use trusty::fill_bytes;
6366
} else if #[cfg(target_os = "uefi")] {
6467
mod uefi;
6568
pub use uefi::fill_bytes;

std/src/sys/random/trusty.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
extern "C" {
2+
fn trusty_rng_secure_rand(randomBuffer: *mut core::ffi::c_void, randomBufferLen: libc::size_t);
3+
}
4+
5+
pub fn fill_bytes(bytes: &mut [u8]) {
6+
unsafe { trusty_rng_secure_rand(bytes.as_mut_ptr().cast(), bytes.len()) }
7+
}

std/src/sys/stdio/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ cfg_if::cfg_if! {
1919
} else if #[cfg(target_os = "teeos")] {
2020
mod teeos;
2121
pub use teeos::*;
22+
} else if #[cfg(target_os = "trusty")] {
23+
mod trusty;
24+
pub use trusty::*;
2225
} else if #[cfg(target_os = "uefi")] {
2326
mod uefi;
2427
pub use uefi::*;

0 commit comments

Comments
 (0)