Skip to content

Commit dcd0c58

Browse files
committed
Add Trusty OS as tier 3 target
1 parent f361413 commit dcd0c58

File tree

15 files changed

+282
-1
lines changed

15 files changed

+282
-1
lines changed

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -115,5 +115,7 @@ rustc-std-workspace-core = { path = 'library/rustc-std-workspace-core' }
115115
rustc-std-workspace-alloc = { path = 'library/rustc-std-workspace-alloc' }
116116
rustc-std-workspace-std = { path = 'library/rustc-std-workspace-std' }
117117

118+
libc = { git = "https://github.com/randomPoison/libc.git", rev = "7ccd2753aa1e26301c73bdb6f9e2df8085ac39e6" }
119+
118120
[patch."https://github.com/rust-lang/rust-clippy"]
119121
clippy_lints = { path = "src/tools/clippy/clippy_lints" }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Trusty OS target for AArch64.
2+
3+
use super::{PanicStrategy, RelroLevel, Target, TargetOptions};
4+
5+
pub fn target() -> Target {
6+
Target {
7+
llvm_target: "aarch64-unknown-linux-musl".into(),
8+
pointer_width: 64,
9+
data_layout: "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128".into(),
10+
arch: "aarch64".into(),
11+
options: TargetOptions {
12+
features: "+neon,+fp-armv8".into(),
13+
executables: true,
14+
max_atomic_width: Some(128),
15+
panic_strategy: PanicStrategy::Abort,
16+
os: "trusty".into(),
17+
position_independent_executables: true,
18+
crt_static_default: true,
19+
crt_static_respected: true,
20+
dynamic_linking: false,
21+
env: "musl".into(),
22+
relro_level: RelroLevel::Full,
23+
mcount: "\u{1}_mcount".into(),
24+
..Default::default()
25+
},
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use crate::spec::{Target, TargetOptions};
2+
3+
use super::{crt_objects::LinkSelfContainedDefault, PanicStrategy, RelroLevel};
4+
5+
pub fn target() -> Target {
6+
Target {
7+
// It's important we use "gnueabi" and not "musleabi" here. LLVM uses it
8+
// to determine the calling convention and float ABI, and it doesn't
9+
// support the "musleabi" value.
10+
llvm_target: "armv7-unknown-linux-gnueabi".into(),
11+
pointer_width: 32,
12+
data_layout: "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64".into(),
13+
arch: "arm".into(),
14+
options: TargetOptions {
15+
abi: "eabi".into(),
16+
features: "+v7,+thumb2,+soft-float,-neon".into(),
17+
max_atomic_width: Some(64),
18+
mcount: "\u{1}mcount".into(),
19+
os: "trusty".into(),
20+
env: "musl".into(),
21+
link_self_contained: LinkSelfContainedDefault::Musl,
22+
dynamic_linking: false,
23+
executables: true,
24+
relro_level: RelroLevel::Full,
25+
panic_strategy: PanicStrategy::Abort,
26+
position_independent_executables: true,
27+
28+
..Default::default()
29+
},
30+
}
31+
}

compiler/rustc_target/src/spec/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1190,6 +1190,9 @@ supported_targets! {
11901190
("aarch64-unknown-hermit", aarch64_unknown_hermit),
11911191
("x86_64-unknown-hermit", x86_64_unknown_hermit),
11921192

1193+
("armv7-unknown-trusty", armv7_unknown_trusty),
1194+
("aarch64-unknown-trusty", aarch64_unknown_trusty),
1195+
11931196
("riscv32i-unknown-none-elf", riscv32i_unknown_none_elf),
11941197
("riscv32im-unknown-none-elf", riscv32im_unknown_none_elf),
11951198
("riscv32imc-unknown-none-elf", riscv32imc_unknown_none_elf),

library/core/src/ffi/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ mod c_char_definition {
144144
)
145145
),
146146
all(target_os = "fuchsia", target_arch = "aarch64"),
147+
all(
148+
target_os = "trusty",
149+
any(target_arch = "aarch64", target_arch = "arm")
150+
),
147151
target_os = "horizon"
148152
))] {
149153
pub type c_char = u8;

library/std/build.rs

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ fn main() {
2121
|| target.contains("fuchsia")
2222
|| (target.contains("sgx") && target.contains("fortanix"))
2323
|| target.contains("hermit")
24+
|| target.contains("trusty")
2425
|| target.contains("l4re")
2526
|| target.contains("redox")
2627
|| target.contains("haiku")

library/std/src/sys/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(target_os = "wasi")] {
4144
mod wasi;
4245
pub use self::wasi::*;

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

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//! System bindings for the Trusty OS.
2+
3+
#[path = "../unix/alloc.rs"]
4+
pub mod alloc;
5+
#[path = "../unsupported/args.rs"]
6+
pub mod args;
7+
#[path = "../unix/cmath.rs"]
8+
pub mod cmath;
9+
#[path = "../unsupported/common.rs"]
10+
#[deny(unsafe_op_in_unsafe_fn)]
11+
mod common;
12+
#[path = "../unsupported/env.rs"]
13+
pub mod env;
14+
#[path = "../unsupported/fs.rs"]
15+
pub mod fs;
16+
#[path = "../unsupported/io.rs"]
17+
pub mod io;
18+
#[path = "../unsupported/locks/mod.rs"]
19+
pub mod locks;
20+
#[path = "../unsupported/net.rs"]
21+
pub mod net;
22+
#[path = "../unsupported/os.rs"]
23+
pub mod os;
24+
#[path = "../unix/os_str.rs"]
25+
pub mod os_str;
26+
#[path = "../unix/path.rs"]
27+
pub mod path;
28+
#[path = "../unsupported/pipe.rs"]
29+
pub mod pipe;
30+
#[path = "../unsupported/process.rs"]
31+
pub mod process;
32+
pub mod stdio;
33+
#[path = "../unsupported/thread.rs"]
34+
pub mod thread;
35+
#[cfg(target_thread_local)]
36+
#[path = "../unsupported/thread_local_dtor.rs"]
37+
pub mod thread_local_dtor;
38+
pub mod thread_local_key;
39+
#[path = "../unsupported/time.rs"]
40+
pub mod time;
41+
42+
pub use common::*;

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

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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 = libc::iovec { iov_base: message.as_ptr() as *mut _, iov_len: message.len() };
63+
loop {
64+
// SAFETY: syscall, safe arguments.
65+
let ret = unsafe { libc::writev(fd, &iov, 1) };
66+
if ret < 0 {
67+
return Err(io::Error::last_os_error());
68+
}
69+
let ret = ret as usize;
70+
if ret > iov.iov_len {
71+
return Err(io::Error::last_os_error());
72+
}
73+
if ret == iov.iov_len {
74+
return Ok(message.len());
75+
}
76+
// SAFETY: ret has been checked to be less than the length of
77+
// the buffer
78+
iov.iov_base = unsafe { iov.iov_base.add(ret) };
79+
iov.iov_len -= ret;
80+
}
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 unsafe fn create(dtor: Option<Dtor>) -> Key {
10+
let key = STORAGE.len();
11+
STORAGE.push((ptr::null_mut(), dtor));
12+
key
13+
}
14+
15+
#[inline]
16+
pub unsafe fn set(key: Key, value: *mut u8) {
17+
STORAGE[key].0 = value;
18+
}
19+
20+
#[inline]
21+
pub unsafe fn get(key: Key) -> *mut u8 {
22+
STORAGE[key].0
23+
}
24+
25+
#[inline]
26+
pub unsafe fn destroy(_key: Key) {}
27+
28+
#[inline]
29+
pub fn requires_synchronized_create() -> bool {
30+
false
31+
}

library/std/src/sys_common/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ cfg_if::cfg_if! {
4545
cfg_if::cfg_if! {
4646
if #[cfg(any(target_os = "l4re",
4747
target_os = "hermit",
48+
target_os = "trusty",
4849
feature = "restricted-std",
4950
all(target_family = "wasm", not(target_os = "emscripten")),
5051
all(target_vendor = "fortanix", target_env = "sgx")))] {

src/bootstrap/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,8 @@ const EXTRA_CHECK_CFGS: &[(Option<Mode>, &'static str, Option<&[&'static str]>)]
213213
(Some(Mode::Std), "backtrace_in_libstd", None),
214214
/* Extra values not defined in the built-in targets yet, but used in std */
215215
(Some(Mode::Std), "target_env", Some(&["libnx"])),
216-
// (Some(Mode::Std), "target_os", Some(&[])),
216+
// #[cfg(bootstrap)] trusty
217+
(Some(Mode::Std), "target_os", Some(&["trusty"])),
217218
(Some(Mode::Std), "target_arch", Some(&["asmjs", "spirv", "nvptx", "xtensa"])),
218219
/* Extra names used by dependencies */
219220
// FIXME: Used by serde_json, but we should not be triggering on external dependencies.

src/doc/rustc/src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
- [armv7-unknown-linux-uclibceabihf](platform-support/armv7-unknown-linux-uclibceabihf.md)
2828
- [\*-android and \*-androideabi](platform-support/android.md)
2929
- [\*-unknown-fuchsia](platform-support/fuchsia.md)
30+
- [\*-unknown-trusty](platform-support/trusty.md)
3031
- [\*-kmc-solid_\*](platform-support/kmc-solid.md)
3132
- [m68k-unknown-linux-gnu](platform-support/m68k-unknown-linux-gnu.md)
3233
- [mips64-openwrt-linux-musl](platform-support/mips64-openwrt-linux-musl.md)

src/doc/rustc/src/platform-support.md

+2
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ target | std | host | notes
223223
`aarch64-unknown-netbsd` | ✓ | ✓ |
224224
[`aarch64-unknown-openbsd`](platform-support/openbsd.md) | ✓ | ✓ | ARM64 OpenBSD
225225
`aarch64-unknown-redox` | ? | | ARM64 Redox OS
226+
[`aarch64-unknown-trusty`](platform-support/trusty.md) | ? | |
226227
`aarch64-uwp-windows-msvc` | ? | |
227228
`aarch64-wrs-vxworks` | ? | |
228229
`aarch64_be-unknown-linux-gnu_ilp32` | ✓ | ✓ | ARM64 Linux (big-endian, ILP32 ABI)
@@ -242,6 +243,7 @@ target | std | host | notes
242243
[`armv7-unknown-linux-uclibceabihf`](platform-support/armv7-unknown-linux-uclibceabihf.md) | ✓ | ? | ARMv7 Linux with uClibc, hardfloat
243244
`armv7-unknown-freebsd` | ✓ | ✓ | ARMv7 FreeBSD
244245
`armv7-unknown-netbsd-eabihf` | ✓ | ✓ |
246+
[`armv7-unknown-trusty`](platform-support/trusty.md) | ? | |
245247
`armv7-wrs-vxworks-eabihf` | ? | |
246248
[`armv7a-kmc-solid_asp3-eabi`](platform-support/kmc-solid.md) | ✓ | | ARM SOLID with TOPPERS/ASP3
247249
[`armv7a-kmc-solid_asp3-eabihf`](platform-support/kmc-solid.md) | ✓ | | ARM SOLID with TOPPERS/ASP3, hardfloat
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# `aarch64-unknown-trusty` and `armv7-unknown-trusty`
2+
3+
**Tier: 3**
4+
5+
[Trusty] is a secure Operating System that provides a Trusted Execution
6+
Environment (TEE) for Android.
7+
8+
## Target maintainers
9+
10+
- David LeGare, `[email protected]`, https://github.com/randomPoison
11+
12+
## Requirements
13+
14+
This target is cross-compiled. It has no special requirements for the host.
15+
16+
It fully supports alloc with the default allocator, and partially supports std.
17+
Notably, most I/O functionality is not supported, e.g. filesystem support and
18+
networking support are not present and any APIs that rely on them will panic at
19+
runtime.
20+
21+
Trusty uses the ELF file format.
22+
23+
## Building the target
24+
25+
The targets can be built by enabling them for a `rustc` build, for example:
26+
27+
```toml
28+
[build]
29+
build-stage = 1
30+
target = ["aarch64-unknown-trusty", "armv7-unknown-trusty"]
31+
```
32+
33+
## Building Rust programs
34+
35+
There is currently no supported way to build a Trusty app with Cargo. You can
36+
follow the [Trusty build instructions] to build the Trusty kernel along with any
37+
Rust apps that are setup in the project.
38+
39+
## Testing
40+
41+
See the [Trusty build instructions] for information on how to build Rust code
42+
within the main Trusty project. The main project also includes infrastructure
43+
for testing Rust applications within a QEMU emulator.
44+
45+
## Cross-compilation toolchains and C code
46+
47+
See the [Trusty build instructions] for information on how C code is built
48+
within Trusty.
49+
50+
[Trusty]: https://source.android.com/docs/security/features/trusty
51+
[Trusty build instructions]: https://source.android.com/docs/security/features/trusty/download-and-build

0 commit comments

Comments
 (0)