Skip to content

Commit 00741b8

Browse files
authored
Rollup merge of #80260 - RalfJung:less-untyped-panics, r=m-ou-se
slightly more typed interface to panic implementation The panic payload is currently being passed around as a `usize`. However, it actually is a pointer, and the involved types are available on all ends of this API, so I propose we use the proper pointer type to avoid some casts. Avoiding int-to-ptr casts also makes this code work with `miri -Zmiri-track-raw-pointers`.
2 parents 1caa5b0 + 1600f7d commit 00741b8

File tree

3 files changed

+11
-10
lines changed

3 files changed

+11
-10
lines changed

library/panic_abort/src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
#![feature(core_intrinsics)]
1515
#![feature(nll)]
1616
#![feature(panic_runtime)]
17+
#![feature(std_internals)]
1718
#![feature(staged_api)]
1819
#![feature(rustc_attrs)]
1920
#![feature(asm)]
2021

2122
use core::any::Any;
23+
use core::panic::BoxMeUp;
2224

2325
#[rustc_std_internal_symbol]
2426
#[allow(improper_ctypes_definitions)]
@@ -28,7 +30,7 @@ pub unsafe extern "C" fn __rust_panic_cleanup(_: *mut u8) -> *mut (dyn Any + Sen
2830

2931
// "Leak" the payload and shim to the relevant abort on the platform in question.
3032
#[rustc_std_internal_symbol]
31-
pub unsafe extern "C" fn __rust_start_panic(_payload: usize) -> u32 {
33+
pub unsafe extern "C" fn __rust_start_panic(_payload: *mut &mut dyn BoxMeUp) -> u32 {
3234
abort();
3335

3436
cfg_if::cfg_if! {

library/panic_unwind/src/lib.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,8 @@ pub unsafe extern "C" fn __rust_panic_cleanup(payload: *mut u8) -> *mut (dyn Any
104104
// implementation.
105105
#[rustc_std_internal_symbol]
106106
#[unwind(allowed)]
107-
pub unsafe extern "C" fn __rust_start_panic(payload: usize) -> u32 {
108-
let payload = payload as *mut &mut dyn BoxMeUp;
109-
let payload = (*payload).take_box();
107+
pub unsafe extern "C" fn __rust_start_panic(payload: *mut &mut dyn BoxMeUp) -> u32 {
108+
let payload = Box::from_raw((*payload).take_box());
110109

111-
imp::panic(Box::from_raw(payload))
110+
imp::panic(payload)
112111
}

library/std/src/panicking.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ use realstd::io::set_output_capture;
4444
extern "C" {
4545
fn __rust_panic_cleanup(payload: *mut u8) -> *mut (dyn Any + Send + 'static);
4646

47-
/// `payload` is actually a `*mut &mut dyn BoxMeUp` but that would cause FFI warnings.
48-
/// It cannot be `Box<dyn BoxMeUp>` because the other end of this call does not depend
49-
/// on liballoc, and thus cannot use `Box`.
47+
/// `payload` is passed through another layer of raw pointers as `&mut dyn Trait` is not
48+
/// FFI-safe. `BoxMeUp` lazily performs allocation only when needed (this avoids allocations
49+
/// when using the "abort" panic runtime).
5050
#[unwind(allowed)]
51-
fn __rust_start_panic(payload: usize) -> u32;
51+
fn __rust_start_panic(payload: *mut &mut dyn BoxMeUp) -> u32;
5252
}
5353

5454
/// This function is called by the panic runtime if FFI code catches a Rust
@@ -637,7 +637,7 @@ pub fn rust_panic_without_hook(payload: Box<dyn Any + Send>) -> ! {
637637
fn rust_panic(mut msg: &mut dyn BoxMeUp) -> ! {
638638
let code = unsafe {
639639
let obj = &mut msg as *mut &mut dyn BoxMeUp;
640-
__rust_start_panic(obj as usize)
640+
__rust_start_panic(obj)
641641
};
642642
rtabort!("failed to initiate panic, error {}", code)
643643
}

0 commit comments

Comments
 (0)