]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/sgx/abi/mem.rs
Merge remote-tracking branch 'upstream/master'
[rust.git] / src / libstd / sys / sgx / abi / mem.rs
1 // Do not remove inline: will result in relocation failure
2 #[inline(always)]
3 pub(crate) unsafe fn rel_ptr<T>(offset: u64) -> *const T {
4     (image_base() + offset) as *const T
5 }
6
7 // Do not remove inline: will result in relocation failure
8 #[inline(always)]
9 pub(crate) unsafe fn rel_ptr_mut<T>(offset: u64) -> *mut T {
10     (image_base() + offset) as *mut T
11 }
12
13 extern {
14     static ENCLAVE_SIZE: usize;
15 }
16
17 // Do not remove inline: will result in relocation failure
18 // For the same reason we use inline ASM here instead of an extern static to
19 // locate the base
20 #[inline(always)]
21 fn image_base() -> u64 {
22     let base;
23     unsafe { asm!("lea IMAGE_BASE(%rip),$0":"=r"(base)) };
24     base
25 }
26
27 /// Returns `true` if the specified memory range is in the enclave.
28 #[unstable(feature = "sgx_platform", issue = "56975")]
29 pub fn is_enclave_range(p: *const u8, len: usize) -> bool {
30     let start=p as u64;
31     let end=start + (len as u64);
32     start >= image_base() &&
33         end <= image_base() + (unsafe { ENCLAVE_SIZE } as u64) // unsafe ok: link-time constant
34 }
35
36 /// Returns `true` if the specified memory range is in userspace.
37 #[unstable(feature = "sgx_platform", issue = "56975")]
38 pub fn is_user_range(p: *const u8, len: usize) -> bool {
39     let start=p as u64;
40     let end=start + (len as u64);
41     end <= image_base() ||
42         start >= image_base() + (unsafe { ENCLAVE_SIZE } as u64) // unsafe ok: link-time constant
43 }