Skip to content

Commit 9c5d8e9

Browse files
committed
adjust Miri interaction with panic runtime
1 parent 6208743 commit 9c5d8e9

File tree

3 files changed

+47
-16
lines changed

3 files changed

+47
-16
lines changed

src/libcore/intrinsics.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1892,10 +1892,12 @@ extern "rust-intrinsic" {
18921892
pub fn ptr_offset_from<T>(ptr: *const T, base: *const T) -> isize;
18931893

18941894
/// Internal hook used by Miri to implement unwinding.
1895-
/// Compiles to a NOP during non-Miri codegen.
1895+
/// ICEs when encountered during non-Miri codegen.
18961896
///
1897-
/// Perma-unstable: do not use
1898-
pub fn miri_start_panic(data: *mut (dyn crate::any::Any + crate::marker::Send)) -> ();
1897+
/// The `payload` ptr here will be exactly the one `do_catch` gets passed by `try`.
1898+
///
1899+
/// Perma-unstable: do not use.
1900+
pub fn miri_start_panic(payload: *mut u8) -> !;
18991901
}
19001902

19011903
// Some functions are defined here because they accidentally got made

src/libpanic_unwind/lib.rs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,32 +31,48 @@
3131
#![panic_runtime]
3232
#![feature(panic_runtime)]
3333

34+
// `real_imp` is unused with Miri, so silence warnings.
35+
#![cfg_attr(miri, allow(dead_code))]
36+
3437
use alloc::boxed::Box;
3538
use core::any::Any;
3639
use core::panic::BoxMeUp;
3740

3841
cfg_if::cfg_if! {
3942
if #[cfg(target_os = "emscripten")] {
4043
#[path = "emcc.rs"]
41-
mod imp;
44+
mod real_imp;
4245
} else if #[cfg(target_arch = "wasm32")] {
4346
#[path = "dummy.rs"]
44-
mod imp;
47+
mod real_imp;
4548
} else if #[cfg(target_os = "hermit")] {
4649
#[path = "hermit.rs"]
47-
mod imp;
50+
mod real_imp;
4851
} else if #[cfg(all(target_env = "msvc", target_arch = "aarch64"))] {
4952
#[path = "dummy.rs"]
50-
mod imp;
53+
mod real_imp;
5154
} else if #[cfg(target_env = "msvc")] {
5255
#[path = "seh.rs"]
53-
mod imp;
56+
mod real_imp;
5457
} else {
5558
// Rust runtime's startup objects depend on these symbols, so make them public.
5659
#[cfg(all(target_os="windows", target_arch = "x86", target_env="gnu"))]
57-
pub use imp::eh_frame_registry::*;
60+
pub use real_imp::eh_frame_registry::*;
5861
#[path = "gcc.rs"]
62+
mod real_imp;
63+
}
64+
}
65+
66+
cfg_if::cfg_if! {
67+
if #[cfg(miri)] {
68+
// Use the Miri runtime.
69+
// We still need to also load the normal runtime above, as rustc expects certain lang
70+
// items from there to be defined.
71+
#[path = "miri.rs"]
5972
mod imp;
73+
} else {
74+
// Use the real runtime.
75+
use real_imp as imp;
6076
}
6177
}
6278

@@ -81,12 +97,5 @@ pub unsafe extern "C" fn __rust_start_panic(payload: usize) -> u32 {
8197
let payload = payload as *mut &mut dyn BoxMeUp;
8298
let payload = (*payload).take_box();
8399

84-
// Miri panic support: cfg'd out of normal builds just to be sure.
85-
// When going through normal codegen, `miri_start_panic` is a NOP, so the
86-
// Miri-enabled sysroot still supports normal unwinding. But when executed in
87-
// Miri, this line initiates unwinding.
88-
#[cfg(miri)]
89-
core::intrinsics::miri_start_panic(payload);
90-
91100
imp::panic(Box::from_raw(payload))
92101
}

src/libpanic_unwind/miri.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//! Unwinding panics for Miri.
2+
use core::any::Any;
3+
use alloc::boxed::Box;
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+
pub unsafe fn panic(payload: Box<dyn Any + Send>) -> u32 {
10+
// The payload we pass to `miri_start_panic` will be exactly the argument we get
11+
// in `cleanup` below. So we just box it up once, to get something pointer-sized.
12+
let payload_box: Payload = Box::new(payload);
13+
core::intrinsics::miri_start_panic(Box::into_raw(payload_box) as *mut u8)
14+
}
15+
16+
pub unsafe fn cleanup(payload_box: *mut u8) -> Box<dyn Any + Send> {
17+
// Recover the underlying `Box`.
18+
let payload_box: Payload = Box::from_raw(payload_box as *mut _);
19+
*payload_box
20+
}

0 commit comments

Comments
 (0)