forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmem.rs
125 lines (109 loc) · 3.6 KB
/
mem.rs
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Do not remove inline: will result in relocation failure
#[inline(always)]
pub(crate) unsafe fn rel_ptr<T>(offset: u64) -> *const T {
(image_base() + offset) as *const T
}
// Do not remove inline: will result in relocation failure
#[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;
}
/// Returns the base memory address of the heap
pub(crate) fn heap_base() -> *const u8 {
unsafe { rel_ptr_mut(HEAP_BASE) }
}
/// Returns the size of the heap
pub(crate) fn heap_size() -> usize {
unsafe { HEAP_SIZE }
}
// Do not remove inline: will result in relocation failure
// For the same reason we use inline ASM here instead of an extern static to
// locate the base
/// Returns address at which current enclave is loaded.
#[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
}
/// Returns `true` if the specified memory range is in the enclave.
///
/// For safety, this function also checks whether the range given overflows,
/// returning `false` if so.
#[unstable(feature = "sgx_platform", issue = "56975")]
pub fn is_enclave_range(p: *const u8, len: usize) -> bool {
let start = p as usize;
// Subtract one from `len` when calculating `end` in case `p + len` is
// exactly at the end of addressable memory (`p + len` would overflow, but
// the range is still valid).
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) // unsafe ok: link-time constant
}
/// Returns `true` if the specified memory range is in userspace.
///
/// For safety, this function also checks whether the range given overflows,
/// returning `false` if so.
#[unstable(feature = "sgx_platform", issue = "56975")]
pub fn is_user_range(p: *const u8, len: usize) -> bool {
let start = p as usize;
// Subtract one from `len` when calculating `end` in case `p + len` is
// exactly at the end of addressable memory (`p + len` would overflow, but
// the range is still valid).
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) // unsafe ok: link-time constant
}
#[repr(C, packed)]
#[derive(Default)]
struct TcslsTcsListItem {
tcs_offset: u64,
next_offset: u64,
}
extern "C" {
fn next_tcsls() -> *const u8;
fn static_tcs_offset() -> u64;
fn clist_next_offset() -> u64;
}
/// Returns the location of all TCSes available at compile time in the enclave
#[unstable(feature = "sgx_platform", issue = "56975")]
pub fn static_tcses() -> Vec<*const u8> {
unsafe {
let mut tcsls = next_tcsls();
let mut tcses = Vec::new();
loop {
let tcs_addr = rel_ptr(*rel_ptr::<u64>(tcsls as u64 + static_tcs_offset()));
tcsls = *(rel_ptr::<*const u8>(tcsls as u64 + clist_next_offset()));
if tcses.first() != Some(&tcs_addr) {
tcses.push(tcs_addr);
} else {
break;
}
}
tcses
}
}