about summary refs log tree commit diff
path: root/src/libstd/sys/sgx/abi/panic.rs
blob: 5ace7fb3368b66be9ad718b709f09afc8835ae8d (plain)
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
use io::{self, Write};
use slice::from_raw_parts_mut;

extern "C" {
    fn take_debug_panic_buf_ptr() -> *mut u8;
    static DEBUG: u8;
}

pub(crate) struct SgxPanicOutput(Option<&'static mut [u8]>);

impl SgxPanicOutput {
    pub(crate) fn new() -> Option<Self> {
        if unsafe { DEBUG == 0 } {
            None
        } else {
            Some(SgxPanicOutput(None))
        }
    }

    fn init(&mut self) -> &mut &'static mut [u8] {
        self.0.get_or_insert_with(|| unsafe {
            let ptr = take_debug_panic_buf_ptr();
            if ptr.is_null() {
                &mut []
            } else {
                from_raw_parts_mut(ptr, 1024)
            }
        })
    }
}

impl Write for SgxPanicOutput {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        self.init().write(buf)
    }

    fn flush(&mut self) -> io::Result<()> {
        self.init().flush()
    }
}

#[no_mangle]
pub extern "C" fn panic_msg(msg: &str) -> ! {
    let _ = SgxPanicOutput::new().map(|mut out| out.write(msg.as_bytes()));
    unsafe { panic_exit(); }
}

extern "C" { pub fn panic_exit() -> !; }