Skip to content

Commit 9d52978

Browse files
committed
Change TerminatorKind::Abort to call the panic handler instead of
aborting immediately. The panic handler is called with a special flag which forces it to abort after calling the panic hook.
1 parent e0a4e56 commit 9d52978

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

core/src/panicking.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,31 @@ fn panic_bounds_check(index: usize, len: usize) -> ! {
7777
panic!("index out of bounds: the len is {} but the index is {}", len, index)
7878
}
7979

80+
#[cfg(not(bootstrap))]
81+
#[cold]
82+
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
83+
#[track_caller]
84+
#[lang = "panic_no_unwind"] // needed by codegen for panic in nounwind function
85+
fn panic_no_unwind() -> ! {
86+
if cfg!(feature = "panic_immediate_abort") {
87+
super::intrinsics::abort()
88+
}
89+
90+
// NOTE This function never crosses the FFI boundary; it's a Rust-to-Rust call
91+
// that gets resolved to the `#[panic_handler]` function.
92+
extern "Rust" {
93+
#[lang = "panic_impl"]
94+
fn panic_impl(pi: &PanicInfo<'_>) -> !;
95+
}
96+
97+
// PanicInfo with the `can_unwind` flag set to false forces an abort.
98+
let fmt = format_args!("panic in a function that cannot unwind");
99+
let pi = PanicInfo::internal_constructor(Some(&fmt), Location::caller(), false);
100+
101+
// SAFETY: `panic_impl` is defined in safe Rust code and thus is safe to call.
102+
unsafe { panic_impl(&pi) }
103+
}
104+
80105
/// The entry point for panicking with a formatted message.
81106
///
82107
/// This is designed to reduce the amount of code required at the call

0 commit comments

Comments
 (0)