std/sys/pal/sgx/abi/
mem.rs1use core::arch::asm;
2
3#[inline(always)]
5pub(crate) unsafe fn rel_ptr<T>(offset: u64) -> *const T {
6 (image_base() + offset) as *const T
7}
8
9#[inline(always)]
11pub(crate) unsafe fn rel_ptr_mut<T>(offset: u64) -> *mut T {
12 (image_base() + offset) as *mut T
13}
14
15unsafe extern "C" {
16 static ENCLAVE_SIZE: usize;
17 static HEAP_BASE: u64;
18 static HEAP_SIZE: usize;
19}
20
21pub(crate) fn heap_base() -> *const u8 {
23 unsafe { rel_ptr_mut(HEAP_BASE) }
24}
25
26pub(crate) fn heap_size() -> usize {
28 unsafe { HEAP_SIZE }
29}
30
31#[inline(always)]
36#[unstable(feature = "sgx_platform", issue = "56975")]
37pub fn image_base() -> u64 {
38 let base: u64;
39 unsafe {
40 asm!(
41 "lea IMAGE_BASE(%rip), {}",
42 lateout(reg) base,
43 options(att_syntax, nostack, preserves_flags, nomem, pure),
44 )
45 };
46 base
47}
48
49#[unstable(feature = "sgx_platform", issue = "56975")]
54pub fn is_enclave_range(p: *const u8, len: usize) -> bool {
55 let start = p as usize;
56
57 let end = if len == 0 {
61 start
62 } else if let Some(end) = start.checked_add(len - 1) {
63 end
64 } else {
65 return false;
66 };
67
68 let base = image_base() as usize;
69 start >= base && end <= base + (unsafe { ENCLAVE_SIZE } - 1) }
71
72#[unstable(feature = "sgx_platform", issue = "56975")]
77pub fn is_user_range(p: *const u8, len: usize) -> bool {
78 let start = p as usize;
79
80 let end = if len == 0 {
84 start
85 } else if let Some(end) = start.checked_add(len - 1) {
86 end
87 } else {
88 return false;
89 };
90
91 let base = image_base() as usize;
92 end < base || start > base + (unsafe { ENCLAVE_SIZE } - 1) }