Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SGX target #7

Merged
merged 1 commit into from
Nov 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#![cfg_attr(feature = "allocator-api", feature(allocator_api))]
#![cfg_attr(target_arch = "wasm32", feature(link_llvm_intrinsics))]
#![cfg_attr(target_env = "sgx", feature(asm))]
#![cfg_attr(not(feature = "allocator-api"), allow(dead_code))]
#![no_std]
#![deny(missing_docs)]
Expand Down Expand Up @@ -51,6 +52,10 @@ mod sys;
#[path = "linux.rs"]
mod sys;

#[cfg(target_env = "sgx")]
#[path = "sgx.rs"]
mod sys;

impl Dlmalloc {
/// Creates a new instance of an allocator, same as `DLMALLOC_INIT`.
pub fn new() -> Dlmalloc {
Expand Down
68 changes: 68 additions & 0 deletions src/sgx.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use core::ptr;
use core::sync::atomic::{AtomicBool, ATOMIC_BOOL_INIT, Ordering};

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

// Do not remove inline: will result in relocation failure
// For the same reason we use inline ASM here instead of an extern static to
// locate the base
#[inline(always)]
fn image_base() -> u64 {
let base;
unsafe{asm!("lea IMAGE_BASE(%rip),$0":"=r"(base))};
base
}

pub unsafe fn alloc(_size: usize) -> (*mut u8, usize, u32) {
extern {
static HEAP_BASE: u64;
static HEAP_SIZE: usize;
}
static INIT: AtomicBool = ATOMIC_BOOL_INIT;
// No ordering requirement since this function is protected by the global lock.
if !INIT.swap(true, Ordering::Relaxed) {
(rel_ptr_mut(HEAP_BASE), HEAP_SIZE, 0)
} else {
(ptr::null_mut(), 0, 0)
}
}

pub unsafe fn remap(_ptr: *mut u8, _oldsize: usize, _newsize: usize, _can_move: bool)
-> *mut u8
{
ptr::null_mut()
}

pub unsafe fn free_part(_ptr: *mut u8, _oldsize: usize, _newsize: usize) -> bool {
false
}

pub unsafe fn free(_ptr: *mut u8, _size: usize) -> bool {
false
}

pub fn can_release_part(_flags: u32) -> bool {
false
}

#[cfg(feature = "global")]
pub fn acquire_global_lock() {
compile_error!("The `global` feature is not implemented for the SGX platform")
}

#[cfg(feature = "global")]
pub fn release_global_lock() {
compile_error!("The `global` feature is not implemented for the SGX platform")
}

pub fn allocates_zeros() -> bool {
false
}

pub fn page_size() -> usize {
0x1000
}