]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/sgx/abi/mem.rs
508f2ff4d4fa5e4b9cb95308555111550cee95b1
[rust.git] / src / libstd / sys / sgx / abi / mem.rs
1 // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // Do not remove inline: will result in relocation failure
12 #[inline(always)]
13 pub unsafe fn rel_ptr<T>(offset: u64) -> *const T {
14     (image_base() + offset) as *const T
15 }
16
17 // Do not remove inline: will result in relocation failure
18 #[inline(always)]
19 pub unsafe fn rel_ptr_mut<T>(offset: u64) -> *mut T {
20     (image_base() + offset) as *mut T
21 }
22
23 extern {
24     static ENCLAVE_SIZE: usize;
25 }
26
27 // Do not remove inline: will result in relocation failure
28 // For the same reason we use inline ASM here instead of an extern static to
29 // locate the base
30 #[inline(always)]
31 fn image_base() -> u64 {
32     let base;
33     unsafe { asm!("lea IMAGE_BASE(%rip),$0":"=r"(base)) };
34     base
35 }
36
37 pub fn is_enclave_range(p: *const u8, len: usize) -> bool {
38     let start=p as u64;
39     let end=start + (len as u64);
40     start >= image_base() &&
41         end <= image_base() + (unsafe { ENCLAVE_SIZE } as u64) // unsafe ok: link-time constant
42 }
43
44 pub fn is_user_range(p: *const u8, len: usize) -> bool {
45     let start=p as u64;
46     let end=start + (len as u64);
47     end <= image_base() ||
48         start >= image_base() + (unsafe { ENCLAVE_SIZE } as u64) // unsafe ok: link-time constant
49 }