Skip to content

Commit 05f3b3c

Browse files
authored
Rollup merge of rust-lang#56978 - jethrogb:jb/sgx-os-mod, r=joshtriplett
Add `std::os::fortanix_sgx` module This PR adds the `std::os::sgx` module to expose platform-specific APIs behind the `sgx_platform` feature gate. Depends on rust-lang#56972 to be able to meaningfully build `std::os` documentation for non-standard targets. Tracking issue: rust-lang#56975
2 parents f1051b5 + dcb5db8 commit 05f3b3c

File tree

14 files changed

+360
-77
lines changed

14 files changed

+360
-77
lines changed

Cargo.lock

+3-3
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
819819

820820
[[package]]
821821
name = "fortanix-sgx-abi"
822-
version = "0.3.1"
822+
version = "0.3.2"
823823
source = "registry+https://github.com/rust-lang/crates.io-index"
824824
dependencies = [
825825
"compiler_builtins 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -2892,7 +2892,7 @@ dependencies = [
28922892
"compiler_builtins 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
28932893
"core 0.0.0",
28942894
"dlmalloc 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
2895-
"fortanix-sgx-abi 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
2895+
"fortanix-sgx-abi 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
28962896
"libc 0.2.45 (registry+https://github.com/rust-lang/crates.io-index)",
28972897
"panic_abort 0.0.0",
28982898
"panic_unwind 0.0.0",
@@ -3449,7 +3449,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
34493449
"checksum fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2fad85553e09a6f881f739c29f0b00b0f01357c743266d478b68951ce23285f3"
34503450
"checksum foreign-types 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1"
34513451
"checksum foreign-types-shared 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b"
3452-
"checksum fortanix-sgx-abi 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "26105e20b4c3f7a319db1376b54ac9a46e5761e949405553375095d05a0cee4d"
3452+
"checksum fortanix-sgx-abi 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "3f8cbee5e872cf7db61a999a041f9bc4706ca7bf7df4cb914f53fabb1c1bc550"
34533453
"checksum fs2 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "9564fc758e15025b46aa6643b1b77d047d1a56a1aea6e01002ac0c7026876213"
34543454
"checksum fs_extra 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5f2a4a2034423744d2cc7ca2068453168dcdb82c438419e639a26bd87839c674"
34553455
"checksum fst 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d94485a00b1827b861dd9d1a2cc9764f9044d4c535514c0760a5a2012ef3399f"

src/libstd/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ rustc_tsan = { path = "../librustc_tsan" }
4040
dlmalloc = { version = "0.1", features = ['rustc-dep-of-std'] }
4141

4242
[target.x86_64-fortanix-unknown-sgx.dependencies]
43-
fortanix-sgx-abi = { version = "0.3.1", features = ['rustc-dep-of-std'] }
43+
fortanix-sgx-abi = { version = "0.3.2", features = ['rustc-dep-of-std'] }
4444

4545
[build-dependencies]
4646
cc = "1.0"

src/libstd/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@
314314
#![feature(alloc_layout_extra)]
315315
#![feature(maybe_uninit)]
316316
#![cfg_attr(target_env = "sgx", feature(global_asm, range_contains, slice_index_methods,
317-
decl_macro, coerce_unsized))]
317+
decl_macro, coerce_unsized, sgx_platform))]
318318

319319
#![default_lib_allocator]
320320

src/libstd/os/fortanix_sgx/mod.rs

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
//! Functionality specific to the `x86_64-fortanix-unknown-sgx` target.
12+
//!
13+
//! This includes functions to deal with memory isolation, usercalls, and the
14+
//! SGX instruction set.
15+
16+
#![deny(missing_docs, missing_debug_implementations)]
17+
#![unstable(feature = "sgx_platform", issue = "56975")]
18+
19+
/// Low-level interfaces to usercalls. See the [ABI documentation] for more
20+
/// information.
21+
///
22+
/// [ABI documentation]: https://docs.rs/fortanix-sgx-abi/
23+
pub mod usercalls {
24+
pub use sys::abi::usercalls::*;
25+
26+
/// Primitives for allocating memory in userspace as well as copying data
27+
/// to and from user memory.
28+
pub mod alloc {
29+
pub use sys::abi::usercalls::alloc;
30+
}
31+
32+
/// Lowest-level interfaces to usercalls and usercall ABI type definitions.
33+
pub mod raw {
34+
use sys::abi::usercalls::raw::invoke_with_usercalls;
35+
pub use sys::abi::usercalls::raw::do_usercall;
36+
pub use sys::abi::usercalls::raw::{accept_stream, alloc, async_queues, bind_stream, close,
37+
connect_stream, exit, flush, free, insecure_time,
38+
launch_thread, read, read_alloc, send, wait, write};
39+
40+
macro_rules! define_usercallnrs {
41+
($(fn $f:ident($($n:ident: $t:ty),*) $(-> $r:ty)*; )*) => {
42+
/// Usercall numbers as per the ABI.
43+
#[repr(C)]
44+
#[unstable(feature = "sgx_platform", issue = "56975")]
45+
#[derive(Copy, Clone, Hash, PartialEq, Eq, Debug)]
46+
#[allow(missing_docs)]
47+
pub enum UsercallNrs {
48+
$($f,)*
49+
}
50+
};
51+
}
52+
invoke_with_usercalls!(define_usercallnrs);
53+
54+
// fortanix-sgx-abi re-exports
55+
pub use sys::abi::usercalls::raw::{ByteBuffer, FifoDescriptor, Return, Usercall};
56+
pub use sys::abi::usercalls::raw::Error;
57+
pub use sys::abi::usercalls::raw::{EV_RETURNQ_NOT_EMPTY, EV_UNPARK, EV_USERCALLQ_NOT_FULL,
58+
FD_STDERR, FD_STDIN, FD_STDOUT, RESULT_SUCCESS,
59+
USERCALL_USER_DEFINED, WAIT_INDEFINITE, WAIT_NO};
60+
pub use sys::abi::usercalls::raw::{Fd, Result, Tcs};
61+
}
62+
}
63+
64+
/// Functions for querying mapping information for pointers.
65+
pub mod mem {
66+
pub use sys::abi::mem::*;
67+
}

src/libstd/os/mod.rs

+17-17
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,10 @@ cfg_if! {
2929

3030
#[doc(cfg(target_os = "linux"))]
3131
pub mod linux;
32-
3332
} else {
3433

35-
// If we're not documenting libstd then we just expose everything as we
36-
// otherwise would.
37-
38-
#[cfg(target_os = "android")] pub mod android;
39-
#[cfg(target_os = "bitrig")] pub mod bitrig;
40-
#[cfg(target_os = "dragonfly")] pub mod dragonfly;
41-
#[cfg(target_os = "freebsd")] pub mod freebsd;
42-
#[cfg(target_os = "haiku")] pub mod haiku;
43-
#[cfg(target_os = "ios")] pub mod ios;
44-
#[cfg(target_os = "macos")] pub mod macos;
45-
#[cfg(target_os = "netbsd")] pub mod netbsd;
46-
#[cfg(target_os = "openbsd")] pub mod openbsd;
47-
#[cfg(target_os = "solaris")] pub mod solaris;
48-
#[cfg(target_os = "emscripten")] pub mod emscripten;
49-
#[cfg(target_os = "fuchsia")] pub mod fuchsia;
50-
#[cfg(target_os = "hermit")] pub mod hermit;
34+
// If we're not documenting libstd then we just expose the main modules
35+
// as we otherwise would.
5136

5237
#[cfg(any(target_os = "redox", unix))]
5338
#[stable(feature = "rust1", since = "1.0.0")]
@@ -63,4 +48,19 @@ cfg_if! {
6348
}
6449
}
6550

51+
#[cfg(target_os = "android")] pub mod android;
52+
#[cfg(target_os = "bitrig")] pub mod bitrig;
53+
#[cfg(target_os = "dragonfly")] pub mod dragonfly;
54+
#[cfg(target_os = "freebsd")] pub mod freebsd;
55+
#[cfg(target_os = "haiku")] pub mod haiku;
56+
#[cfg(target_os = "ios")] pub mod ios;
57+
#[cfg(target_os = "macos")] pub mod macos;
58+
#[cfg(target_os = "netbsd")] pub mod netbsd;
59+
#[cfg(target_os = "openbsd")] pub mod openbsd;
60+
#[cfg(target_os = "solaris")] pub mod solaris;
61+
#[cfg(target_os = "emscripten")] pub mod emscripten;
62+
#[cfg(target_os = "fuchsia")] pub mod fuchsia;
63+
#[cfg(target_os = "hermit")] pub mod hermit;
64+
#[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] pub mod fortanix_sgx;
65+
6666
pub mod raw;

src/libstd/sys/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ cfg_if! {
6565
if #[cfg(any(unix, target_os = "redox"))] {
6666
// On unix we'll document what's already available
6767
pub use self::ext as unix_ext;
68-
} else if #[cfg(any(target_os = "cloudabi", target_arch = "wasm32"))] {
68+
} else if #[cfg(any(target_os = "cloudabi", target_arch = "wasm32", target_env = "sgx"))] {
6969
// On CloudABI and wasm right now the module below doesn't compile
7070
// (missing things in `libc` which is empty) so just omit everything
7171
// with an empty module
@@ -86,7 +86,7 @@ cfg_if! {
8686
// On windows we'll just be documenting what's already available
8787
#[allow(missing_docs)]
8888
pub use self::ext as windows_ext;
89-
} else if #[cfg(any(target_os = "cloudabi", target_arch = "wasm32"))] {
89+
} else if #[cfg(any(target_os = "cloudabi", target_arch = "wasm32", target_env = "sgx"))] {
9090
// On CloudABI and wasm right now the shim below doesn't compile, so
9191
// just omit it
9292
#[unstable(issue = "0", feature = "std_internals")]

src/libstd/sys/sgx/abi/mem.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010

1111
// Do not remove inline: will result in relocation failure
1212
#[inline(always)]
13-
pub unsafe fn rel_ptr<T>(offset: u64) -> *const T {
13+
pub(crate) unsafe fn rel_ptr<T>(offset: u64) -> *const T {
1414
(image_base() + offset) as *const T
1515
}
1616

1717
// Do not remove inline: will result in relocation failure
1818
#[inline(always)]
19-
pub unsafe fn rel_ptr_mut<T>(offset: u64) -> *mut T {
19+
pub(crate) unsafe fn rel_ptr_mut<T>(offset: u64) -> *mut T {
2020
(image_base() + offset) as *mut T
2121
}
2222

@@ -34,6 +34,17 @@ fn image_base() -> u64 {
3434
base
3535
}
3636

37+
/// Returns `true` if the specified memory range is in the enclave.
38+
#[unstable(feature = "sgx_platform", issue = "56975")]
39+
pub fn is_enclave_range(p: *const u8, len: usize) -> bool {
40+
let start=p as u64;
41+
let end=start + (len as u64);
42+
start >= image_base() &&
43+
end <= image_base() + (unsafe { ENCLAVE_SIZE } as u64) // unsafe ok: link-time constant
44+
}
45+
46+
/// Returns `true` if the specified memory range is in userspace.
47+
#[unstable(feature = "sgx_platform", issue = "56975")]
3748
pub fn is_user_range(p: *const u8, len: usize) -> bool {
3849
let start=p as u64;
3950
let end=start + (len as u64);

src/libstd/sys/sgx/abi/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ use io::Write;
1313

1414
// runtime features
1515
mod reloc;
16-
mod mem;
1716
pub(super) mod panic;
1817

1918
// library features
19+
pub mod mem;
2020
pub mod thread;
2121
pub mod tls;
2222
#[macro_use]

0 commit comments

Comments
 (0)