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