]> git.lizzy.rs Git - rust.git/blob - library/panic_unwind/src/miri.rs
Merge commit '57b3c4b90f4346b3990c1be387c3b3ca7b78412c' into clippyup
[rust.git] / library / panic_unwind / src / miri.rs
1 //! Unwinding panics for Miri.
2 use alloc::boxed::Box;
3 use core::any::Any;
4
5 // The type of the payload that the Miri engine propagates through unwinding for us.
6 // Must be pointer-sized.
7 type Payload = Box<Box<dyn Any + Send>>;
8
9 extern "Rust" {
10     /// Miri-provided extern function to begin unwinding.
11     fn miri_start_panic(payload: *mut u8) -> !;
12 }
13
14 pub unsafe fn panic(payload: Box<dyn Any + Send>) -> u32 {
15     // The payload we pass to `miri_start_panic` will be exactly the argument we get
16     // in `cleanup` below. So we just box it up once, to get something pointer-sized.
17     let payload_box: Payload = Box::new(payload);
18     miri_start_panic(Box::into_raw(payload_box) as *mut u8)
19 }
20
21 pub unsafe fn cleanup(payload_box: *mut u8) -> Box<dyn Any + Send> {
22     // Recover the underlying `Box`.
23     let payload_box: Payload = Box::from_raw(payload_box as *mut _);
24     *payload_box
25 }