Skip to main content

std/sys/pal/sgx/abi/
mem.rs

1use core::arch::asm;
2
3// Do not remove inline: will result in relocation failure
4#[inline(always)]
5pub(crate) unsafe fn rel_ptr<T>(offset: u64) -> *const T {
6    (image_base() + offset) as *const T
7}
8
9// Do not remove inline: will result in relocation failure
10#[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
21/// Returns the base memory address of the heap
22pub(crate) fn heap_base() -> *const u8 {
23    unsafe { rel_ptr_mut(HEAP_BASE) }
24}
25
26/// Returns the size of the heap
27pub(crate) fn heap_size() -> usize {
28    unsafe { HEAP_SIZE }
29}
30
31// Do not remove inline: will result in relocation failure
32// For the same reason we use inline ASM here instead of an extern static to
33// locate the base
34/// Returns address at which current enclave is loaded.
35#[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/// Returns `true` if the specified memory range is in the enclave.
50///
51/// For safety, this function also checks whether the range given overflows,
52/// returning `false` if so.
53#[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    // Subtract one from `len` when calculating `end` in case `p + len` is
58    // exactly at the end of addressable memory (`p + len` would overflow, but
59    // the range is still valid).
60    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) // unsafe ok: link-time constant
70}
71
72/// Returns `true` if the specified memory range is in userspace.
73///
74/// For safety, this function also checks whether the range given overflows,
75/// returning `false` if so.
76#[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    // Subtract one from `len` when calculating `end` in case `p + len` is
81    // exactly at the end of addressable memory (`p + len` would overflow, but
82    // the range is still valid).
83    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) // unsafe ok: link-time constant
93}