2020-03-14 05:23:39 -05:00
|
|
|
//! Unwinding panics for Miri.
|
|
|
|
use alloc::boxed::Box;
|
2020-03-14 06:10:00 -05:00
|
|
|
use core::any::Any;
|
2020-03-14 05:23:39 -05:00
|
|
|
|
|
|
|
// The type of the payload that the Miri engine propagates through unwinding for us.
|
|
|
|
// Must be pointer-sized.
|
|
|
|
type Payload = Box<Box<dyn Any + Send>>;
|
|
|
|
|
2020-07-23 06:07:21 -05:00
|
|
|
extern "Rust" {
|
|
|
|
/// Miri-provided extern function to begin unwinding.
|
|
|
|
fn miri_start_panic(payload: *mut u8) -> !;
|
|
|
|
}
|
|
|
|
|
2020-03-14 05:23:39 -05:00
|
|
|
pub unsafe fn panic(payload: Box<dyn Any + Send>) -> u32 {
|
|
|
|
// The payload we pass to `miri_start_panic` will be exactly the argument we get
|
|
|
|
// in `cleanup` below. So we just box it up once, to get something pointer-sized.
|
|
|
|
let payload_box: Payload = Box::new(payload);
|
2020-07-23 06:07:21 -05:00
|
|
|
miri_start_panic(Box::into_raw(payload_box) as *mut u8)
|
2020-03-14 05:23:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub unsafe fn cleanup(payload_box: *mut u8) -> Box<dyn Any + Send> {
|
|
|
|
// Recover the underlying `Box`.
|
|
|
|
let payload_box: Payload = Box::from_raw(payload_box as *mut _);
|
|
|
|
*payload_box
|
|
|
|
}
|