Skip to content

Commit 2069305

Browse files
committed
Apply rustc-0023-Add-Trusty-OS-support-to-Rust-std.patch
1 parent 3f33b30 commit 2069305

File tree

9 files changed

+164
-0
lines changed

9 files changed

+164
-0
lines changed

library/std/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ fn main() {
3737
|| target_os == "fuchsia"
3838
|| (target_vendor == "fortanix" && target_env == "sgx")
3939
|| target_os == "hermit"
40+
|| target_os == ("trusty")
4041
|| target_os == "l4re"
4142
|| target_os == "redox"
4243
|| target_os == "haiku"

library/std/src/sys/alloc/mod.rs

Lines changed: 1 addition & 0 deletions
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")] {

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

Lines changed: 3 additions & 0 deletions
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::*;

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//! System bindings for the Trusty OS.
2+
3+
#[path = "../unsupported/args.rs"]
4+
pub mod args;
5+
#[path = "../unsupported/env.rs"]
6+
pub mod env;
7+
#[path = "../unsupported/fs.rs"]
8+
pub mod fs;
9+
#[path = "../unsupported/io.rs"]
10+
pub mod io;
11+
#[path = "../unsupported/net.rs"]
12+
pub mod net;
13+
#[path = "../unsupported/os.rs"]
14+
pub mod os;
15+
#[path = "../unsupported/pipe.rs"]
16+
pub mod pipe;
17+
#[path = "../unsupported/process.rs"]
18+
pub mod process;
19+
pub mod stdio;
20+
#[path = "../unsupported/time.rs"]
21+
pub mod time;
22+
#[path = "../unsupported/thread.rs"]
23+
pub mod thread;
24+
#[path = "../unsupported/common.rs"]
25+
#[deny(unsafe_op_in_unsafe_fn)]
26+
mod common;
27+
28+
pub use common::*;
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
use crate::io;
2+
3+
pub struct Stdin;
4+
pub struct Stdout;
5+
pub struct Stderr;
6+
7+
impl Stdin {
8+
pub const fn new() -> Stdin {
9+
Stdin
10+
}
11+
}
12+
13+
impl io::Read for Stdin {
14+
fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> {
15+
Ok(0)
16+
}
17+
}
18+
19+
impl Stdout {
20+
pub const fn new() -> Stdout {
21+
Stdout
22+
}
23+
}
24+
25+
impl io::Write for Stdout {
26+
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
27+
_write(libc::STDOUT_FILENO, buf)
28+
}
29+
30+
fn flush(&mut self) -> io::Result<()> {
31+
Ok(())
32+
}
33+
}
34+
35+
impl Stderr {
36+
pub const fn new() -> Stderr {
37+
Stderr
38+
}
39+
}
40+
41+
impl io::Write for Stderr {
42+
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
43+
_write(libc::STDERR_FILENO, buf)
44+
}
45+
46+
fn flush(&mut self) -> io::Result<()> {
47+
Ok(())
48+
}
49+
}
50+
51+
pub const STDIN_BUF_SIZE: usize = 0;
52+
53+
pub fn is_ebadf(_err: &io::Error) -> bool {
54+
true
55+
}
56+
57+
pub fn panic_output() -> Option<impl io::Write> {
58+
Some(Stderr)
59+
}
60+
61+
fn _write(fd: i32, message: &[u8]) -> io::Result<usize> {
62+
let mut iov =
63+
libc::iovec { iov_base: message.as_ptr() as *mut _, iov_len: message.len() };
64+
loop {
65+
// SAFETY: syscall, safe arguments.
66+
let ret = unsafe { libc::writev(fd, &iov, 1) };
67+
if ret < 0 {
68+
return Err(io::Error::last_os_error());
69+
}
70+
let ret = ret as usize;
71+
if ret > iov.iov_len {
72+
return Err(io::Error::last_os_error());
73+
}
74+
if ret == iov.iov_len {
75+
return Ok(message.len());
76+
}
77+
// SAFETY: ret has been checked to be less than the length of
78+
// the buffer
79+
iov.iov_base = unsafe { iov.iov_base.add(ret) };
80+
iov.iov_len -= ret;
81+
}
82+
}

library/std/src/sys/random/mod.rs

Lines changed: 3 additions & 0 deletions
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;

library/std/src/sys/random/trusty.rs

Lines changed: 7 additions & 0 deletions
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+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use crate::ptr;
2+
3+
pub type Key = usize;
4+
type Dtor = unsafe extern "C" fn(*mut u8);
5+
6+
static mut STORAGE: crate::vec::Vec<(*mut u8, Option<Dtor>)> = Vec::new();
7+
8+
#[inline]
9+
pub fn create(dtor: Option<Dtor>) -> Key {
10+
unsafe {
11+
#[allow(static_mut_refs)]
12+
let key = STORAGE.len();
13+
#[allow(static_mut_refs)]
14+
STORAGE.push((ptr::null_mut(), dtor));
15+
key
16+
}
17+
}
18+
19+
#[inline]
20+
pub unsafe fn set(key: Key, value: *mut u8) {
21+
unsafe { STORAGE[key].0 = value };
22+
}
23+
24+
#[inline]
25+
pub unsafe fn get(key: Key) -> *mut u8 {
26+
unsafe { STORAGE[key].0 }
27+
}
28+
29+
#[inline]
30+
pub fn destroy(_key: Key) {}

library/std/src/sys/thread_local/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,15 @@ pub(crate) mod key {
170170
pub(crate) use xous::destroy_tls;
171171
pub(super) use xous::{Key, get, set};
172172
use xous::{create, destroy};
173+
} else if #[cfg(target_os = "trusty")] {
174+
#[allow(unused_unsafe)]
175+
mod racy;
176+
#[cfg(test)]
177+
mod tests;
178+
mod trusty;
179+
pub(super) use racy::LazyKey;
180+
pub(super) use trusty::{Key, get, set};
181+
use trusty::{create, destroy};
173182
}
174183
}
175184
}

0 commit comments

Comments
 (0)