Skip to content

Commit 1e44e2d

Browse files
author
Jethro Beekman
committed
SGX target: implement user memory management
1 parent 39f9751 commit 1e44e2d

File tree

5 files changed

+502
-6
lines changed

5 files changed

+502
-6
lines changed

src/libstd/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,8 @@
312312
#![feature(non_exhaustive)]
313313
#![feature(alloc_layout_extra)]
314314
#![feature(maybe_uninit)]
315-
#![cfg_attr(target_env = "sgx", feature(global_asm, range_contains))]
315+
#![cfg_attr(target_env = "sgx", feature(global_asm, range_contains, slice_index_methods,
316+
decl_macro, coerce_unsized))]
316317

317318
#![default_lib_allocator]
318319

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@
1111
// Do not remove inline: will result in relocation failure
1212
#[inline(always)]
1313
pub unsafe fn rel_ptr<T>(offset: u64) -> *const T {
14-
(image_base()+offset) as *const T
14+
(image_base() + offset) as *const T
1515
}
1616

1717
// Do not remove inline: will result in relocation failure
1818
#[inline(always)]
1919
pub unsafe fn rel_ptr_mut<T>(offset: u64) -> *mut T {
20-
(image_base()+offset) as *mut T
20+
(image_base() + offset) as *mut T
21+
}
22+
23+
extern {
24+
static ENCLAVE_SIZE: usize;
2125
}
2226

2327
// Do not remove inline: will result in relocation failure
@@ -26,6 +30,20 @@ pub unsafe fn rel_ptr_mut<T>(offset: u64) -> *mut T {
2630
#[inline(always)]
2731
fn image_base() -> u64 {
2832
let base;
29-
unsafe{asm!("lea IMAGE_BASE(%rip),$0":"=r"(base))};
33+
unsafe { asm!("lea IMAGE_BASE(%rip),$0":"=r"(base)) };
3034
base
3135
}
36+
37+
pub fn is_enclave_range(p: *const u8, len: usize) -> bool {
38+
let start=p as u64;
39+
let end=start + (len as u64);
40+
start >= image_base() &&
41+
end <= image_base() + (unsafe { ENCLAVE_SIZE } as u64) // unsafe ok: link-time constant
42+
}
43+
44+
pub fn is_user_range(p: *const u8, len: usize) -> bool {
45+
let start=p as u64;
46+
let end=start + (len as u64);
47+
end <= image_base() ||
48+
start >= image_base() + (unsafe { ENCLAVE_SIZE } as u64) // unsafe ok: link-time constant
49+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,5 @@ pub(super) fn exit_with_code(code: isize) -> ! {
9696
let _ = write!(out, "Exited with status code {}", code);
9797
}
9898
}
99-
unsafe { usercalls::raw::exit(code != 0) };
99+
usercalls::exit(code != 0);
100100
}

0 commit comments

Comments
 (0)