Skip to content

Commit e4f478d

Browse files
authored
Merge pull request #7 from jethrogb/jb/sgx-target
Add SGX target
2 parents 37c571c + 373989e commit e4f478d

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
1414
#![cfg_attr(feature = "allocator-api", feature(allocator_api))]
1515
#![cfg_attr(target_arch = "wasm32", feature(link_llvm_intrinsics))]
16+
#![cfg_attr(target_env = "sgx", feature(asm))]
1617
#![cfg_attr(not(feature = "allocator-api"), allow(dead_code))]
1718
#![no_std]
1819
#![deny(missing_docs)]
@@ -51,6 +52,10 @@ mod sys;
5152
#[path = "linux.rs"]
5253
mod sys;
5354

55+
#[cfg(target_env = "sgx")]
56+
#[path = "sgx.rs"]
57+
mod sys;
58+
5459
impl Dlmalloc {
5560
/// Creates a new instance of an allocator, same as `DLMALLOC_INIT`.
5661
pub fn new() -> Dlmalloc {

src/sgx.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use core::ptr;
2+
use core::sync::atomic::{AtomicBool, ATOMIC_BOOL_INIT, Ordering};
3+
4+
// Do not remove inline: will result in relocation failure
5+
#[inline(always)]
6+
unsafe fn rel_ptr_mut<T>(offset: u64) -> *mut T {
7+
(image_base()+offset) as *mut T
8+
}
9+
10+
// Do not remove inline: will result in relocation failure
11+
// For the same reason we use inline ASM here instead of an extern static to
12+
// locate the base
13+
#[inline(always)]
14+
fn image_base() -> u64 {
15+
let base;
16+
unsafe{asm!("lea IMAGE_BASE(%rip),$0":"=r"(base))};
17+
base
18+
}
19+
20+
pub unsafe fn alloc(_size: usize) -> (*mut u8, usize, u32) {
21+
extern {
22+
static HEAP_BASE: u64;
23+
static HEAP_SIZE: usize;
24+
}
25+
static INIT: AtomicBool = ATOMIC_BOOL_INIT;
26+
// No ordering requirement since this function is protected by the global lock.
27+
if !INIT.swap(true, Ordering::Relaxed) {
28+
(rel_ptr_mut(HEAP_BASE), HEAP_SIZE, 0)
29+
} else {
30+
(ptr::null_mut(), 0, 0)
31+
}
32+
}
33+
34+
pub unsafe fn remap(_ptr: *mut u8, _oldsize: usize, _newsize: usize, _can_move: bool)
35+
-> *mut u8
36+
{
37+
ptr::null_mut()
38+
}
39+
40+
pub unsafe fn free_part(_ptr: *mut u8, _oldsize: usize, _newsize: usize) -> bool {
41+
false
42+
}
43+
44+
pub unsafe fn free(_ptr: *mut u8, _size: usize) -> bool {
45+
false
46+
}
47+
48+
pub fn can_release_part(_flags: u32) -> bool {
49+
false
50+
}
51+
52+
#[cfg(feature = "global")]
53+
pub fn acquire_global_lock() {
54+
compile_error!("The `global` feature is not implemented for the SGX platform")
55+
}
56+
57+
#[cfg(feature = "global")]
58+
pub fn release_global_lock() {
59+
compile_error!("The `global` feature is not implemented for the SGX platform")
60+
}
61+
62+
pub fn allocates_zeros() -> bool {
63+
false
64+
}
65+
66+
pub fn page_size() -> usize {
67+
0x1000
68+
}

0 commit comments

Comments
 (0)