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
//
// Copyright 2023, Colias Group, LLC
//
// SPDX-License-Identifier: BSD-2-Clause
//

use alloc::boxed::Box;
use core::mem;

use unwinding::abi::*;

use super::{drop_panic, foreign_exception, RUST_EXCEPTION_CLASS};
use crate::Payload;

#[repr(C)]
struct ExceptionWithPayload {
    exception: UnwindException,
    payload: Payload,
}

pub(crate) fn panic_cleanup(exception: *mut u8) -> Payload {
    let exception = exception as *mut UnwindException;
    unsafe {
        if (*exception).exception_class != RUST_EXCEPTION_CLASS {
            _Unwind_DeleteException(exception);
            foreign_exception()
        } else {
            let exception = Box::from_raw(exception as *mut ExceptionWithPayload);
            exception.payload
        }
    }
}

pub(crate) fn start_panic(payload: Payload) -> i32 {
    extern "C" fn exception_cleanup(
        _unwind_code: UnwindReasonCode,
        exception: *mut UnwindException,
    ) {
        unsafe {
            let _: Box<ExceptionWithPayload> =
                Box::from_raw(exception as *mut ExceptionWithPayload);
        }
        drop_panic()
    }

    let mut exception = unsafe { mem::zeroed::<UnwindException>() };
    exception.exception_class = RUST_EXCEPTION_CLASS;
    exception.exception_cleanup = Some(exception_cleanup);
    let exception = Box::into_raw(Box::new(ExceptionWithPayload { exception, payload }))
        as *mut UnwindException;
    unsafe { _Unwind_RaiseException(&mut *exception).0 }
}