Skip to content

Commit 87ca2db

Browse files
committed
Apply rustc-0023-Add-Trusty-OS-support-to-Rust-std.patch
1 parent 2b285cd commit 87ca2db

9 files changed

Lines changed: 164 additions & 0 deletions

File tree

‎library/std/build.rs‎

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

‎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
pubuseself::hermit::*;
40+
} else if #[cfg(target_os = "trusty")]{
41+
mod trusty;
42+
pubuseself::trusty::*;
4043
} else if #[cfg(all(target_os = "wasi", target_env = "p2"))]{
4144
mod wasip2;
4245
pubuseself::wasip2::*;
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+
pubmod args;
5+
#[path = "../unsupported/env.rs"]
6+
pubmod env;
7+
#[path = "../unsupported/fs.rs"]
8+
pubmod fs;
9+
#[path = "../unsupported/io.rs"]
10+
pubmod io;
11+
#[path = "../unsupported/net.rs"]
12+
pubmod net;
13+
#[path = "../unsupported/os.rs"]
14+
pubmod os;
15+
#[path = "../unsupported/pipe.rs"]
16+
pubmod pipe;
17+
#[path = "../unsupported/process.rs"]
18+
pubmod process;
19+
pubmod stdio;
20+
#[path = "../unsupported/time.rs"]
21+
pubmod time;
22+
#[path = "../unsupported/thread.rs"]
23+
pubmod thread;
24+
#[path = "../unsupported/common.rs"]
25+
#[deny(unsafe_op_in_unsafe_fn)]
26+
mod common;
27+
28+
pubuse common::*;
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
usecrate::io;
2+
3+
pubstructStdin;
4+
pubstructStdout;
5+
pubstructStderr;
6+
7+
implStdin{
8+
pubconstfnnew() -> Stdin{
9+
Stdin
10+
}
11+
}
12+
13+
impl io::ReadforStdin{
14+
fnread(&mutself,_buf:&mut[u8]) -> io::Result<usize>{
15+
Ok(0)
16+
}
17+
}
18+
19+
implStdout{
20+
pubconstfnnew() -> Stdout{
21+
Stdout
22+
}
23+
}
24+
25+
impl io::WriteforStdout{
26+
fnwrite(&mutself,buf:&[u8]) -> io::Result<usize>{
27+
_write(libc::STDOUT_FILENO, buf)
28+
}
29+
30+
fnflush(&mutself) -> io::Result<()>{
31+
Ok(())
32+
}
33+
}
34+
35+
implStderr{
36+
pubconstfnnew() -> Stderr{
37+
Stderr
38+
}
39+
}
40+
41+
impl io::WriteforStderr{
42+
fnwrite(&mutself,buf:&[u8]) -> io::Result<usize>{
43+
_write(libc::STDERR_FILENO, buf)
44+
}
45+
46+
fnflush(&mutself) -> io::Result<()>{
47+
Ok(())
48+
}
49+
}
50+
51+
pubconstSTDIN_BUF_SIZE:usize = 0;
52+
53+
pubfnis_ebadf(_err:&io::Error) -> bool{
54+
true
55+
}
56+
57+
pubfnpanic_output() -> Option<impl io::Write>{
58+
Some(Stderr)
59+
}
60+
61+
fn_write(fd:i32,message:&[u8]) -> io::Result<usize>{
62+
letmut 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+
returnErr(io::Error::last_os_error());
69+
}
70+
let ret = ret asusize;
71+
if ret > iov.iov_len{
72+
returnErr(io::Error::last_os_error());
73+
}
74+
if ret == iov.iov_len{
75+
returnOk(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
pubuse teeos::fill_bytes;
63+
} else if #[cfg(target_os = "trusty")]{
64+
mod trusty;
65+
pubuse trusty::fill_bytes;
6366
} else if #[cfg(target_os = "uefi")]{
6467
mod uefi;
6568
pubuse uefi::fill_bytes;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
extern"C"{
2+
fntrusty_rng_secure_rand(randomBuffer:*mut core::ffi::c_void,randomBufferLen: libc::size_t);
3+
}
4+
5+
pubfnfill_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+
usecrate::ptr;
2+
3+
pubtypeKey = usize;
4+
typeDtor = unsafeextern"C"fn(*mutu8);
5+
6+
staticmutSTORAGE:crate::vec::Vec<(*mutu8,Option<Dtor>)> = Vec::new();
7+
8+
#[inline]
9+
pubfncreate(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+
pubunsafefnset(key:Key,value:*mutu8){
21+
unsafe{STORAGE[key].0 = value };
22+
}
23+
24+
#[inline]
25+
pubunsafefnget(key:Key) -> *mutu8{
26+
unsafe{STORAGE[key].0}
27+
}
28+
29+
#[inline]
30+
pubfndestroy(_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)