1 // Do not remove inline: will result in relocation failure
3 pub(crate) unsafe fn rel_ptr<T>(offset: u64) -> *const T {
4 (image_base() + offset) as *const T
7 // Do not remove inline: will result in relocation failure
9 pub(crate) unsafe fn rel_ptr_mut<T>(offset: u64) -> *mut T {
10 (image_base() + offset) as *mut T
14 static ENCLAVE_SIZE: usize;
15 static HEAP_BASE: u64;
16 static HEAP_SIZE: usize;
19 /// Returns the base memory address of the heap
20 pub(crate) fn heap_base() -> *const u8 {
21 unsafe { rel_ptr_mut(HEAP_BASE) }
24 /// Returns the size of the heap
25 pub(crate) fn heap_size() -> usize {
29 // Do not remove inline: will result in relocation failure
30 // For the same reason we use inline ASM here instead of an extern static to
32 /// Returns address at which current enclave is loaded.
34 #[unstable(feature = "sgx_platform", issue = "56975")]
35 pub fn image_base() -> u64 {
39 "lea {}, qword ptr [rip + IMAGE_BASE]",
41 options(nostack, preserves_flags, nomem, pure),
47 /// Returns `true` if the specified memory range is in the enclave.
49 /// For safety, this function also checks whether the range given overflows,
50 /// returning `false` if so.
51 #[unstable(feature = "sgx_platform", issue = "56975")]
52 pub fn is_enclave_range(p: *const u8, len: usize) -> bool {
53 let start = p as usize;
55 // Subtract one from `len` when calculating `end` in case `p + len` is
56 // exactly at the end of addressable memory (`p + len` would overflow, but
57 // the range is still valid).
58 let end = if len == 0 {
60 } else if let Some(end) = start.checked_add(len - 1) {
66 let base = image_base() as usize;
67 start >= base && end <= base + (unsafe { ENCLAVE_SIZE } - 1) // unsafe ok: link-time constant
70 /// Returns `true` if the specified memory range is in userspace.
72 /// For safety, this function also checks whether the range given overflows,
73 /// returning `false` if so.
74 #[unstable(feature = "sgx_platform", issue = "56975")]
75 pub fn is_user_range(p: *const u8, len: usize) -> bool {
76 let start = p as usize;
78 // Subtract one from `len` when calculating `end` in case `p + len` is
79 // exactly at the end of addressable memory (`p + len` would overflow, but
80 // the range is still valid).
81 let end = if len == 0 {
83 } else if let Some(end) = start.checked_add(len - 1) {
89 let base = image_base() as usize;
90 end < base || start > base + (unsafe { ENCLAVE_SIZE } - 1) // unsafe ok: link-time constant