File: sgx-image-base.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 893,176 kB
  • sloc: xml: 158,127; python: 35,830; javascript: 19,497; cpp: 19,002; sh: 17,245; ansic: 13,127; asm: 4,376; makefile: 1,051; lisp: 29; perl: 29; ruby: 19; sql: 11
file content (56 lines) | stat: -rw-r--r-- 1,275 bytes parent folder | download | duplicates (15)
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
#![cfg(all(target_env = "sgx", target_vendor = "fortanix"))]
#![feature(sgx_platform)]

#[cfg(feature = "std")]
#[test]
fn sgx_image_base_with_std() {
    use backtrace::trace;

    let image_base = std::os::fortanix_sgx::mem::image_base();

    let mut frame_ips = Vec::new();
    trace(|frame| {
        frame_ips.push(frame.ip());
        true
    });

    assert!(frame_ips.len() > 0);
    for ip in frame_ips {
        let ip: u64 = ip as _;
        assert!(ip < image_base);
    }
}

#[cfg(not(feature = "std"))]
#[test]
fn sgx_image_base_no_std() {
    use backtrace::trace_unsynchronized;

    fn guess_image_base() -> u64 {
        let mut top_frame_ip = None;
        unsafe {
            trace_unsynchronized(|frame| {
                top_frame_ip = Some(frame.ip());
                false
            });
        }
        top_frame_ip.unwrap() as u64 & 0xFFFFFF000000
    }

    let image_base = guess_image_base();
    backtrace::set_image_base(image_base as _);

    let mut frame_ips = Vec::new();
    unsafe {
        trace_unsynchronized(|frame| {
            frame_ips.push(frame.ip());
            true
        });
    }

    assert!(frame_ips.len() > 0);
    for ip in frame_ips {
        let ip: u64 = ip as _;
        assert!(ip < image_base);
    }
}