]> git.proxmox.com Git - rustc.git/blob - src/libstd/sys/sgx/abi/mem.rs
New upstream version 1.44.1+dfsg1
[rustc.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 "C" {
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 /// Returns address at which current enclave is loaded.
21 #[inline(always)]
22 #[unstable(feature = "sgx_platform", issue = "56975")]
23 pub fn image_base() -> u64 {
24 let base;
25 unsafe { llvm_asm!("lea IMAGE_BASE(%rip),$0":"=r"(base)) };
26 base
27 }
28
29 /// Returns `true` if the specified memory range is in the enclave.
30 ///
31 /// `p + len` must not overflow.
32 #[unstable(feature = "sgx_platform", issue = "56975")]
33 pub fn is_enclave_range(p: *const u8, len: usize) -> bool {
34 let start = p as u64;
35 let end = start + (len as u64);
36 start >= image_base() && end <= image_base() + (unsafe { ENCLAVE_SIZE } as u64) // unsafe ok: link-time constant
37 }
38
39 /// Returns `true` if the specified memory range is in userspace.
40 ///
41 /// `p + len` must not overflow.
42 #[unstable(feature = "sgx_platform", issue = "56975")]
43 pub fn is_user_range(p: *const u8, len: usize) -> bool {
44 let start = p as u64;
45 let end = start + (len as u64);
46 end <= image_base() || start >= image_base() + (unsafe { ENCLAVE_SIZE } as u64) // unsafe ok: link-time constant
47 }