Skip to content

Commit

Permalink
Rollup merge of rust-lang#56978 - jethrogb:jb/sgx-os-mod, r=joshtriplett
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Centril committed Dec 24, 2018
2 parents f1051b5 + dcb5db8 commit 05f3b3c
Show file tree
Hide file tree
Showing 14 changed files with 360 additions and 77 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "fortanix-sgx-abi"
version = "0.3.1"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"compiler_builtins 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
Expand Down Expand Up @@ -2892,7 +2892,7 @@ dependencies = [
"compiler_builtins 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"core 0.0.0",
"dlmalloc 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"fortanix-sgx-abi 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"fortanix-sgx-abi 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.45 (registry+https://github.com/rust-lang/crates.io-index)",
"panic_abort 0.0.0",
"panic_unwind 0.0.0",
Expand Down Expand Up @@ -3449,7 +3449,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2fad85553e09a6f881f739c29f0b00b0f01357c743266d478b68951ce23285f3"
"checksum foreign-types 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1"
"checksum foreign-types-shared 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b"
"checksum fortanix-sgx-abi 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "26105e20b4c3f7a319db1376b54ac9a46e5761e949405553375095d05a0cee4d"
"checksum fortanix-sgx-abi 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "3f8cbee5e872cf7db61a999a041f9bc4706ca7bf7df4cb914f53fabb1c1bc550"
"checksum fs2 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "9564fc758e15025b46aa6643b1b77d047d1a56a1aea6e01002ac0c7026876213"
"checksum fs_extra 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5f2a4a2034423744d2cc7ca2068453168dcdb82c438419e639a26bd87839c674"
"checksum fst 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d94485a00b1827b861dd9d1a2cc9764f9044d4c535514c0760a5a2012ef3399f"
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ rustc_tsan = { path = "../librustc_tsan" }
dlmalloc = { version = "0.1", features = ['rustc-dep-of-std'] }

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

[build-dependencies]
cc = "1.0"
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@
#![feature(alloc_layout_extra)]
#![feature(maybe_uninit)]
#![cfg_attr(target_env = "sgx", feature(global_asm, range_contains, slice_index_methods,
decl_macro, coerce_unsized))]
decl_macro, coerce_unsized, sgx_platform))]

#![default_lib_allocator]

Expand Down
67 changes: 67 additions & 0 deletions src/libstd/os/fortanix_sgx/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Functionality specific to the `x86_64-fortanix-unknown-sgx` target.
//!
//! This includes functions to deal with memory isolation, usercalls, and the
//! SGX instruction set.

#![deny(missing_docs, missing_debug_implementations)]
#![unstable(feature = "sgx_platform", issue = "56975")]

/// Low-level interfaces to usercalls. See the [ABI documentation] for more
/// information.
///
/// [ABI documentation]: https://docs.rs/fortanix-sgx-abi/
pub mod usercalls {
pub use sys::abi::usercalls::*;

/// Primitives for allocating memory in userspace as well as copying data
/// to and from user memory.
pub mod alloc {
pub use sys::abi::usercalls::alloc;
}

/// Lowest-level interfaces to usercalls and usercall ABI type definitions.
pub mod raw {
use sys::abi::usercalls::raw::invoke_with_usercalls;
pub use sys::abi::usercalls::raw::do_usercall;
pub use sys::abi::usercalls::raw::{accept_stream, alloc, async_queues, bind_stream, close,
connect_stream, exit, flush, free, insecure_time,
launch_thread, read, read_alloc, send, wait, write};

macro_rules! define_usercallnrs {
($(fn $f:ident($($n:ident: $t:ty),*) $(-> $r:ty)*; )*) => {
/// Usercall numbers as per the ABI.
#[repr(C)]
#[unstable(feature = "sgx_platform", issue = "56975")]
#[derive(Copy, Clone, Hash, PartialEq, Eq, Debug)]
#[allow(missing_docs)]
pub enum UsercallNrs {
$($f,)*
}
};
}
invoke_with_usercalls!(define_usercallnrs);

// fortanix-sgx-abi re-exports
pub use sys::abi::usercalls::raw::{ByteBuffer, FifoDescriptor, Return, Usercall};
pub use sys::abi::usercalls::raw::Error;
pub use sys::abi::usercalls::raw::{EV_RETURNQ_NOT_EMPTY, EV_UNPARK, EV_USERCALLQ_NOT_FULL,
FD_STDERR, FD_STDIN, FD_STDOUT, RESULT_SUCCESS,
USERCALL_USER_DEFINED, WAIT_INDEFINITE, WAIT_NO};
pub use sys::abi::usercalls::raw::{Fd, Result, Tcs};
}
}

/// Functions for querying mapping information for pointers.
pub mod mem {
pub use sys::abi::mem::*;
}
34 changes: 17 additions & 17 deletions src/libstd/os/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,10 @@ cfg_if! {

#[doc(cfg(target_os = "linux"))]
pub mod linux;

} else {

// If we're not documenting libstd then we just expose everything as we
// otherwise would.

#[cfg(target_os = "android")] pub mod android;
#[cfg(target_os = "bitrig")] pub mod bitrig;
#[cfg(target_os = "dragonfly")] pub mod dragonfly;
#[cfg(target_os = "freebsd")] pub mod freebsd;
#[cfg(target_os = "haiku")] pub mod haiku;
#[cfg(target_os = "ios")] pub mod ios;
#[cfg(target_os = "macos")] pub mod macos;
#[cfg(target_os = "netbsd")] pub mod netbsd;
#[cfg(target_os = "openbsd")] pub mod openbsd;
#[cfg(target_os = "solaris")] pub mod solaris;
#[cfg(target_os = "emscripten")] pub mod emscripten;
#[cfg(target_os = "fuchsia")] pub mod fuchsia;
#[cfg(target_os = "hermit")] pub mod hermit;
// If we're not documenting libstd then we just expose the main modules
// as we otherwise would.

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

#[cfg(target_os = "android")] pub mod android;
#[cfg(target_os = "bitrig")] pub mod bitrig;
#[cfg(target_os = "dragonfly")] pub mod dragonfly;
#[cfg(target_os = "freebsd")] pub mod freebsd;
#[cfg(target_os = "haiku")] pub mod haiku;
#[cfg(target_os = "ios")] pub mod ios;
#[cfg(target_os = "macos")] pub mod macos;
#[cfg(target_os = "netbsd")] pub mod netbsd;
#[cfg(target_os = "openbsd")] pub mod openbsd;
#[cfg(target_os = "solaris")] pub mod solaris;
#[cfg(target_os = "emscripten")] pub mod emscripten;
#[cfg(target_os = "fuchsia")] pub mod fuchsia;
#[cfg(target_os = "hermit")] pub mod hermit;
#[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] pub mod fortanix_sgx;

pub mod raw;
4 changes: 2 additions & 2 deletions src/libstd/sys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ cfg_if! {
if #[cfg(any(unix, target_os = "redox"))] {
// On unix we'll document what's already available
pub use self::ext as unix_ext;
} else if #[cfg(any(target_os = "cloudabi", target_arch = "wasm32"))] {
} else if #[cfg(any(target_os = "cloudabi", target_arch = "wasm32", target_env = "sgx"))] {
// On CloudABI and wasm right now the module below doesn't compile
// (missing things in `libc` which is empty) so just omit everything
// with an empty module
Expand All @@ -86,7 +86,7 @@ cfg_if! {
// On windows we'll just be documenting what's already available
#[allow(missing_docs)]
pub use self::ext as windows_ext;
} else if #[cfg(any(target_os = "cloudabi", target_arch = "wasm32"))] {
} else if #[cfg(any(target_os = "cloudabi", target_arch = "wasm32", target_env = "sgx"))] {
// On CloudABI and wasm right now the shim below doesn't compile, so
// just omit it
#[unstable(issue = "0", feature = "std_internals")]
Expand Down
15 changes: 13 additions & 2 deletions src/libstd/sys/sgx/abi/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@

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

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

Expand All @@ -34,6 +34,17 @@ fn image_base() -> u64 {
base
}

/// Returns `true` if the specified memory range is in the enclave.
#[unstable(feature = "sgx_platform", issue = "56975")]
pub fn is_enclave_range(p: *const u8, len: usize) -> bool {
let start=p as u64;
let end=start + (len as u64);
start >= image_base() &&
end <= image_base() + (unsafe { ENCLAVE_SIZE } as u64) // unsafe ok: link-time constant
}

/// Returns `true` if the specified memory range is in userspace.
#[unstable(feature = "sgx_platform", issue = "56975")]
pub fn is_user_range(p: *const u8, len: usize) -> bool {
let start=p as u64;
let end=start + (len as u64);
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/sys/sgx/abi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ use io::Write;

// runtime features
mod reloc;
mod mem;
pub(super) mod panic;

// library features
pub mod mem;
pub mod thread;
pub mod tls;
#[macro_use]
Expand Down
Loading

0 comments on commit 05f3b3c

Please sign in to comment.