std/panicking.rs
1//! Implementation of various bits and pieces of the `panic!` macro and
2//! associated runtime pieces.
3//!
4//! Specifically, this module contains the implementation of:
5//!
6//! * Panic hooks
7//! * Executing a panic up to doing the actual implementation
8//! * Shims around "try"
9
10#![deny(unsafe_op_in_unsafe_fn)]
11
12use core::panic::{Location, PanicPayload};
13
14// make sure to use the stderr output configured
15// by libtest in the real copy of std
16#[cfg(test)]
17use realstd::io::try_set_output_capture;
18
19use crate::any::Any;
20#[cfg(not(test))]
21use crate::io::try_set_output_capture;
22use crate::mem::{self, ManuallyDrop};
23use crate::panic::{BacktraceStyle, PanicHookInfo};
24use crate::sync::atomic::{AtomicBool, Ordering};
25use crate::sync::{PoisonError, RwLock};
26use crate::sys::backtrace;
27use crate::sys::stdio::panic_output;
28use crate::{fmt, intrinsics, process, thread};
29
30// This forces codegen of the function called by panic!() inside the std crate, rather than in
31// downstream crates. Primarily this is useful for rustc's codegen tests, which rely on noticing
32// complete removal of panic from generated IR. Since begin_panic is inline(never), it's only
33// codegen'd once per crate-graph so this pushes that to std rather than our codegen test crates.
34//
35// (See https://github.com/rust-lang/rust/pull/123244 for more info on why).
36//
37// If this is causing problems we can also modify those codegen tests to use a crate type like
38// cdylib which doesn't export "Rust" symbols to downstream linkage units.
39#[unstable(feature = "libstd_sys_internals", reason = "used by the panic! macro", issue = "none")]
40#[doc(hidden)]
41#[allow(dead_code)]
42#[used(compiler)]
43pub static EMPTY_PANIC: fn(&'static str) -> ! =
44 begin_panic::<&'static str> as fn(&'static str) -> !;
45
46// Binary interface to the panic runtime that the standard library depends on.
47//
48// The standard library is tagged with `#![needs_panic_runtime]` (introduced in
49// RFC 1513) to indicate that it requires some other crate tagged with
50// `#![panic_runtime]` to exist somewhere. Each panic runtime is intended to
51// implement these symbols (with the same signatures) so we can get matched up
52// to them.
53//
54// One day this may look a little less ad-hoc with the compiler helping out to
55// hook up these functions, but it is not this day!
56#[allow(improper_ctypes)]
57unsafe extern "C" {
58 fn __rust_panic_cleanup(payload: *mut u8) -> *mut (dyn Any + Send + 'static);
59}
60
61unsafe extern "Rust" {
62 /// `PanicPayload` lazily performs allocation only when needed (this avoids
63 /// allocations when using the "abort" panic runtime).
64 fn __rust_start_panic(payload: &mut dyn PanicPayload) -> u32;
65}
66
67/// This function is called by the panic runtime if FFI code catches a Rust
68/// panic but doesn't rethrow it. We don't support this case since it messes
69/// with our panic count.
70#[cfg(not(test))]
71#[rustc_std_internal_symbol]
72extern "C" fn __rust_drop_panic() -> ! {
73 rtabort!("Rust panics must be rethrown");
74}
75
76/// This function is called by the panic runtime if it catches an exception
77/// object which does not correspond to a Rust panic.
78#[cfg(not(test))]
79#[rustc_std_internal_symbol]
80extern "C" fn __rust_foreign_exception() -> ! {
81 rtabort!("Rust cannot catch foreign exceptions");
82}
83
84#[derive(Default)]
85enum Hook {
86 #[default]
87 Default,
88 Custom(Box<dyn Fn(&PanicHookInfo<'_>) + 'static + Sync + Send>),
89}
90
91impl Hook {
92 #[inline]
93 fn into_box(self) -> Box<dyn Fn(&PanicHookInfo<'_>) + 'static + Sync + Send> {
94 match self {
95 Hook::Default => Box::new(default_hook),
96 Hook::Custom(hook) => hook,
97 }
98 }
99}
100
101static HOOK: RwLock<Hook> = RwLock::new(Hook::Default);
102
103/// Registers a custom panic hook, replacing the previously registered hook.
104///
105/// The panic hook is invoked when a thread panics, but before the panic runtime
106/// is invoked. As such, the hook will run with both the aborting and unwinding
107/// runtimes.
108///
109/// The default hook, which is registered at startup, prints a message to standard error and
110/// generates a backtrace if requested. This behavior can be customized using the `set_hook` function.
111/// The current hook can be retrieved while reinstating the default hook with the [`take_hook`]
112/// function.
113///
114/// [`take_hook`]: ./fn.take_hook.html
115///
116/// The hook is provided with a `PanicHookInfo` struct which contains information
117/// about the origin of the panic, including the payload passed to `panic!` and
118/// the source code location from which the panic originated.
119///
120/// The panic hook is a global resource.
121///
122/// # Panics
123///
124/// Panics if called from a panicking thread.
125///
126/// # Examples
127///
128/// The following will print "Custom panic hook":
129///
130/// ```should_panic
131/// use std::panic;
132///
133/// panic::set_hook(Box::new(|_| {
134/// println!("Custom panic hook");
135/// }));
136///
137/// panic!("Normal panic");
138/// ```
139#[stable(feature = "panic_hooks", since = "1.10.0")]
140pub fn set_hook(hook: Box<dyn Fn(&PanicHookInfo<'_>) + 'static + Sync + Send>) {
141 if thread::panicking() {
142 panic!("cannot modify the panic hook from a panicking thread");
143 }
144
145 let new = Hook::Custom(hook);
146 let mut hook = HOOK.write().unwrap_or_else(PoisonError::into_inner);
147 let old = mem::replace(&mut *hook, new);
148 drop(hook);
149 // Only drop the old hook after releasing the lock to avoid deadlocking
150 // if its destructor panics.
151 drop(old);
152}
153
154/// Unregisters the current panic hook and returns it, registering the default hook
155/// in its place.
156///
157/// *See also the function [`set_hook`].*
158///
159/// [`set_hook`]: ./fn.set_hook.html
160///
161/// If the default hook is registered it will be returned, but remain registered.
162///
163/// # Panics
164///
165/// Panics if called from a panicking thread.
166///
167/// # Examples
168///
169/// The following will print "Normal panic":
170///
171/// ```should_panic
172/// use std::panic;
173///
174/// panic::set_hook(Box::new(|_| {
175/// println!("Custom panic hook");
176/// }));
177///
178/// let _ = panic::take_hook();
179///
180/// panic!("Normal panic");
181/// ```
182#[must_use]
183#[stable(feature = "panic_hooks", since = "1.10.0")]
184pub fn take_hook() -> Box<dyn Fn(&PanicHookInfo<'_>) + 'static + Sync + Send> {
185 if thread::panicking() {
186 panic!("cannot modify the panic hook from a panicking thread");
187 }
188
189 let mut hook = HOOK.write().unwrap_or_else(PoisonError::into_inner);
190 let old_hook = mem::take(&mut *hook);
191 drop(hook);
192
193 old_hook.into_box()
194}
195
196/// Atomic combination of [`take_hook`] and [`set_hook`]. Use this to replace the panic handler with
197/// a new panic handler that does something and then executes the old handler.
198///
199/// [`take_hook`]: ./fn.take_hook.html
200/// [`set_hook`]: ./fn.set_hook.html
201///
202/// # Panics
203///
204/// Panics if called from a panicking thread.
205///
206/// # Examples
207///
208/// The following will print the custom message, and then the normal output of panic.
209///
210/// ```should_panic
211/// #![feature(panic_update_hook)]
212/// use std::panic;
213///
214/// // Equivalent to
215/// // let prev = panic::take_hook();
216/// // panic::set_hook(move |info| {
217/// // println!("...");
218/// // prev(info);
219/// // );
220/// panic::update_hook(move |prev, info| {
221/// println!("Print custom message and execute panic handler as usual");
222/// prev(info);
223/// });
224///
225/// panic!("Custom and then normal");
226/// ```
227#[unstable(feature = "panic_update_hook", issue = "92649")]
228pub fn update_hook<F>(hook_fn: F)
229where
230 F: Fn(&(dyn Fn(&PanicHookInfo<'_>) + Send + Sync + 'static), &PanicHookInfo<'_>)
231 + Sync
232 + Send
233 + 'static,
234{
235 if thread::panicking() {
236 panic!("cannot modify the panic hook from a panicking thread");
237 }
238
239 let mut hook = HOOK.write().unwrap_or_else(PoisonError::into_inner);
240 let prev = mem::take(&mut *hook).into_box();
241 *hook = Hook::Custom(Box::new(move |info| hook_fn(&prev, info)));
242}
243
244/// The default panic handler.
245#[optimize(size)]
246fn default_hook(info: &PanicHookInfo<'_>) {
247 // If this is a double panic, make sure that we print a backtrace
248 // for this panic. Otherwise only print it if logging is enabled.
249 let backtrace = if info.force_no_backtrace() {
250 None
251 } else if panic_count::get_count() >= 2 {
252 BacktraceStyle::full()
253 } else {
254 crate::panic::get_backtrace_style()
255 };
256
257 // The current implementation always returns `Some`.
258 let location = info.location().unwrap();
259
260 let msg = payload_as_str(info.payload());
261
262 let write = #[optimize(size)]
263 |err: &mut dyn crate::io::Write| {
264 // Use a lock to prevent mixed output in multithreading context.
265 // Some platforms also require it when printing a backtrace, like `SymFromAddr` on Windows.
266 let mut lock = backtrace::lock();
267
268 thread::with_current_name(|name| {
269 let name = name.unwrap_or("<unnamed>");
270
271 // Try to write the panic message to a buffer first to prevent other concurrent outputs
272 // interleaving with it.
273 let mut buffer = [0u8; 512];
274 let mut cursor = crate::io::Cursor::new(&mut buffer[..]);
275
276 let write_msg = |dst: &mut dyn crate::io::Write| {
277 // We add a newline to ensure the panic message appears at the start of a line.
278 writeln!(dst, "\nthread '{name}' panicked at {location}:\n{msg}")
279 };
280
281 if write_msg(&mut cursor).is_ok() {
282 let pos = cursor.position() as usize;
283 let _ = err.write_all(&buffer[0..pos]);
284 } else {
285 // The message did not fit into the buffer, write it directly instead.
286 let _ = write_msg(err);
287 };
288 });
289
290 static FIRST_PANIC: AtomicBool = AtomicBool::new(true);
291
292 match backtrace {
293 // SAFETY: we took out a lock just a second ago.
294 Some(BacktraceStyle::Short) => {
295 drop(lock.print(err, crate::backtrace_rs::PrintFmt::Short))
296 }
297 Some(BacktraceStyle::Full) => {
298 drop(lock.print(err, crate::backtrace_rs::PrintFmt::Full))
299 }
300 Some(BacktraceStyle::Off) => {
301 if FIRST_PANIC.swap(false, Ordering::Relaxed) {
302 let _ = writeln!(
303 err,
304 "note: run with `RUST_BACKTRACE=1` environment variable to display a \
305 backtrace"
306 );
307 if cfg!(miri) {
308 let _ = writeln!(
309 err,
310 "note: in Miri, you may have to set `MIRIFLAGS=-Zmiri-env-forward=RUST_BACKTRACE` \
311 for the environment variable to have an effect"
312 );
313 }
314 }
315 }
316 // If backtraces aren't supported or are forced-off, do nothing.
317 None => {}
318 }
319 };
320
321 if let Ok(Some(local)) = try_set_output_capture(None) {
322 write(&mut *local.lock().unwrap_or_else(|e| e.into_inner()));
323 try_set_output_capture(Some(local)).ok();
324 } else if let Some(mut out) = panic_output() {
325 write(&mut out);
326 }
327}
328
329#[cfg(not(test))]
330#[doc(hidden)]
331#[cfg(feature = "panic_immediate_abort")]
332#[unstable(feature = "update_panic_count", issue = "none")]
333pub mod panic_count {
334 /// A reason for forcing an immediate abort on panic.
335 #[derive(Debug)]
336 pub enum MustAbort {
337 AlwaysAbort,
338 PanicInHook,
339 }
340
341 #[inline]
342 pub fn increase(run_panic_hook: bool) -> Option<MustAbort> {
343 None
344 }
345
346 #[inline]
347 pub fn finished_panic_hook() {}
348
349 #[inline]
350 pub fn decrease() {}
351
352 #[inline]
353 pub fn set_always_abort() {}
354
355 // Disregards ALWAYS_ABORT_FLAG
356 #[inline]
357 #[must_use]
358 pub fn get_count() -> usize {
359 0
360 }
361
362 #[must_use]
363 #[inline]
364 pub fn count_is_zero() -> bool {
365 true
366 }
367}
368
369#[cfg(not(test))]
370#[doc(hidden)]
371#[cfg(not(feature = "panic_immediate_abort"))]
372#[unstable(feature = "update_panic_count", issue = "none")]
373pub mod panic_count {
374 use crate::cell::Cell;
375 use crate::sync::atomic::{AtomicUsize, Ordering};
376
377 const ALWAYS_ABORT_FLAG: usize = 1 << (usize::BITS - 1);
378
379 /// A reason for forcing an immediate abort on panic.
380 #[derive(Debug)]
381 pub enum MustAbort {
382 AlwaysAbort,
383 PanicInHook,
384 }
385
386 // Panic count for the current thread and whether a panic hook is currently
387 // being executed..
388 thread_local! {
389 static LOCAL_PANIC_COUNT: Cell<(usize, bool)> = const { Cell::new((0, false)) }
390 }
391
392 // Sum of panic counts from all threads. The purpose of this is to have
393 // a fast path in `count_is_zero` (which is used by `panicking`). In any particular
394 // thread, if that thread currently views `GLOBAL_PANIC_COUNT` as being zero,
395 // then `LOCAL_PANIC_COUNT` in that thread is zero. This invariant holds before
396 // and after increase and decrease, but not necessarily during their execution.
397 //
398 // Additionally, the top bit of GLOBAL_PANIC_COUNT (GLOBAL_ALWAYS_ABORT_FLAG)
399 // records whether panic::always_abort() has been called. This can only be
400 // set, never cleared.
401 // panic::always_abort() is usually called to prevent memory allocations done by
402 // the panic handling in the child created by `libc::fork`.
403 // Memory allocations performed in a child created with `libc::fork` are undefined
404 // behavior in most operating systems.
405 // Accessing LOCAL_PANIC_COUNT in a child created by `libc::fork` would lead to a memory
406 // allocation. Only GLOBAL_PANIC_COUNT can be accessed in this situation. This is
407 // sufficient because a child process will always have exactly one thread only.
408 // See also #85261 for details.
409 //
410 // This could be viewed as a struct containing a single bit and an n-1-bit
411 // value, but if we wrote it like that it would be more than a single word,
412 // and even a newtype around usize would be clumsy because we need atomics.
413 // But we use such a tuple for the return type of increase().
414 //
415 // Stealing a bit is fine because it just amounts to assuming that each
416 // panicking thread consumes at least 2 bytes of address space.
417 static GLOBAL_PANIC_COUNT: AtomicUsize = AtomicUsize::new(0);
418
419 // Increases the global and local panic count, and returns whether an
420 // immediate abort is required.
421 //
422 // This also updates thread-local state to keep track of whether a panic
423 // hook is currently executing.
424 pub fn increase(run_panic_hook: bool) -> Option<MustAbort> {
425 let global_count = GLOBAL_PANIC_COUNT.fetch_add(1, Ordering::Relaxed);
426 if global_count & ALWAYS_ABORT_FLAG != 0 {
427 // Do *not* access thread-local state, we might be after a `fork`.
428 return Some(MustAbort::AlwaysAbort);
429 }
430
431 LOCAL_PANIC_COUNT.with(|c| {
432 let (count, in_panic_hook) = c.get();
433 if in_panic_hook {
434 return Some(MustAbort::PanicInHook);
435 }
436 c.set((count + 1, run_panic_hook));
437 None
438 })
439 }
440
441 pub fn finished_panic_hook() {
442 LOCAL_PANIC_COUNT.with(|c| {
443 let (count, _) = c.get();
444 c.set((count, false));
445 });
446 }
447
448 pub fn decrease() {
449 GLOBAL_PANIC_COUNT.fetch_sub(1, Ordering::Relaxed);
450 LOCAL_PANIC_COUNT.with(|c| {
451 let (count, _) = c.get();
452 c.set((count - 1, false));
453 });
454 }
455
456 pub fn set_always_abort() {
457 GLOBAL_PANIC_COUNT.fetch_or(ALWAYS_ABORT_FLAG, Ordering::Relaxed);
458 }
459
460 // Disregards ALWAYS_ABORT_FLAG
461 #[must_use]
462 pub fn get_count() -> usize {
463 LOCAL_PANIC_COUNT.with(|c| c.get().0)
464 }
465
466 // Disregards ALWAYS_ABORT_FLAG
467 #[must_use]
468 #[inline]
469 pub fn count_is_zero() -> bool {
470 if GLOBAL_PANIC_COUNT.load(Ordering::Relaxed) & !ALWAYS_ABORT_FLAG == 0 {
471 // Fast path: if `GLOBAL_PANIC_COUNT` is zero, all threads
472 // (including the current one) will have `LOCAL_PANIC_COUNT`
473 // equal to zero, so TLS access can be avoided.
474 //
475 // In terms of performance, a relaxed atomic load is similar to a normal
476 // aligned memory read (e.g., a mov instruction in x86), but with some
477 // compiler optimization restrictions. On the other hand, a TLS access
478 // might require calling a non-inlinable function (such as `__tls_get_addr`
479 // when using the GD TLS model).
480 true
481 } else {
482 is_zero_slow_path()
483 }
484 }
485
486 // Slow path is in a separate function to reduce the amount of code
487 // inlined from `count_is_zero`.
488 #[inline(never)]
489 #[cold]
490 fn is_zero_slow_path() -> bool {
491 LOCAL_PANIC_COUNT.with(|c| c.get().0 == 0)
492 }
493}
494
495#[cfg(test)]
496pub use realstd::rt::panic_count;
497
498/// Invoke a closure, capturing the cause of an unwinding panic if one occurs.
499#[cfg(feature = "panic_immediate_abort")]
500pub unsafe fn r#try<R, F: FnOnce() -> R>(f: F) -> Result<R, Box<dyn Any + Send>> {
501 Ok(f())
502}
503
504/// Invoke a closure, capturing the cause of an unwinding panic if one occurs.
505#[cfg(not(feature = "panic_immediate_abort"))]
506pub unsafe fn r#try<R, F: FnOnce() -> R>(f: F) -> Result<R, Box<dyn Any + Send>> {
507 union Data<F, R> {
508 f: ManuallyDrop<F>,
509 r: ManuallyDrop<R>,
510 p: ManuallyDrop<Box<dyn Any + Send>>,
511 }
512
513 // We do some sketchy operations with ownership here for the sake of
514 // performance. We can only pass pointers down to `do_call` (can't pass
515 // objects by value), so we do all the ownership tracking here manually
516 // using a union.
517 //
518 // We go through a transition where:
519 //
520 // * First, we set the data field `f` to be the argumentless closure that we're going to call.
521 // * When we make the function call, the `do_call` function below, we take
522 // ownership of the function pointer. At this point the `data` union is
523 // entirely uninitialized.
524 // * If the closure successfully returns, we write the return value into the
525 // data's return slot (field `r`).
526 // * If the closure panics (`do_catch` below), we write the panic payload into field `p`.
527 // * Finally, when we come back out of the `try` intrinsic we're
528 // in one of two states:
529 //
530 // 1. The closure didn't panic, in which case the return value was
531 // filled in. We move it out of `data.r` and return it.
532 // 2. The closure panicked, in which case the panic payload was
533 // filled in. We move it out of `data.p` and return it.
534 //
535 // Once we stack all that together we should have the "most efficient'
536 // method of calling a catch panic whilst juggling ownership.
537 let mut data = Data { f: ManuallyDrop::new(f) };
538
539 let data_ptr = (&raw mut data) as *mut u8;
540 // SAFETY:
541 //
542 // Access to the union's fields: this is `std` and we know that the `r#try`
543 // intrinsic fills in the `r` or `p` union field based on its return value.
544 //
545 // The call to `intrinsics::catch_unwind` is made safe by:
546 // - `do_call`, the first argument, can be called with the initial `data_ptr`.
547 // - `do_catch`, the second argument, can be called with the `data_ptr` as well.
548 // See their safety preconditions for more information
549 unsafe {
550 return if intrinsics::catch_unwind(do_call::<F, R>, data_ptr, do_catch::<F, R>) == 0 {
551 Ok(ManuallyDrop::into_inner(data.r))
552 } else {
553 Err(ManuallyDrop::into_inner(data.p))
554 };
555 }
556
557 // We consider unwinding to be rare, so mark this function as cold. However,
558 // do not mark it no-inline -- that decision is best to leave to the
559 // optimizer (in most cases this function is not inlined even as a normal,
560 // non-cold function, though, as of the writing of this comment).
561 #[cold]
562 #[optimize(size)]
563 unsafe fn cleanup(payload: *mut u8) -> Box<dyn Any + Send + 'static> {
564 // SAFETY: The whole unsafe block hinges on a correct implementation of
565 // the panic handler `__rust_panic_cleanup`. As such we can only
566 // assume it returns the correct thing for `Box::from_raw` to work
567 // without undefined behavior.
568 let obj = unsafe { Box::from_raw(__rust_panic_cleanup(payload)) };
569 panic_count::decrease();
570 obj
571 }
572
573 // SAFETY:
574 // data must be non-NUL, correctly aligned, and a pointer to a `Data<F, R>`
575 // Its must contains a valid `f` (type: F) value that can be use to fill
576 // `data.r`.
577 //
578 // This function cannot be marked as `unsafe` because `intrinsics::catch_unwind`
579 // expects normal function pointers.
580 #[inline]
581 fn do_call<F: FnOnce() -> R, R>(data: *mut u8) {
582 // SAFETY: this is the responsibility of the caller, see above.
583 unsafe {
584 let data = data as *mut Data<F, R>;
585 let data = &mut (*data);
586 let f = ManuallyDrop::take(&mut data.f);
587 data.r = ManuallyDrop::new(f());
588 }
589 }
590
591 // We *do* want this part of the catch to be inlined: this allows the
592 // compiler to properly track accesses to the Data union and optimize it
593 // away most of the time.
594 //
595 // SAFETY:
596 // data must be non-NUL, correctly aligned, and a pointer to a `Data<F, R>`
597 // Since this uses `cleanup` it also hinges on a correct implementation of
598 // `__rustc_panic_cleanup`.
599 //
600 // This function cannot be marked as `unsafe` because `intrinsics::catch_unwind`
601 // expects normal function pointers.
602 #[inline]
603 #[rustc_nounwind] // `intrinsic::r#try` requires catch fn to be nounwind
604 fn do_catch<F: FnOnce() -> R, R>(data: *mut u8, payload: *mut u8) {
605 // SAFETY: this is the responsibility of the caller, see above.
606 //
607 // When `__rustc_panic_cleaner` is correctly implemented we can rely
608 // on `obj` being the correct thing to pass to `data.p` (after wrapping
609 // in `ManuallyDrop`).
610 unsafe {
611 let data = data as *mut Data<F, R>;
612 let data = &mut (*data);
613 let obj = cleanup(payload);
614 data.p = ManuallyDrop::new(obj);
615 }
616 }
617}
618
619/// Determines whether the current thread is unwinding because of panic.
620#[inline]
621pub fn panicking() -> bool {
622 !panic_count::count_is_zero()
623}
624
625/// Entry point of panics from the core crate (`panic_impl` lang item).
626#[cfg(not(any(test, doctest)))]
627#[panic_handler]
628pub fn begin_panic_handler(info: &core::panic::PanicInfo<'_>) -> ! {
629 struct FormatStringPayload<'a> {
630 inner: &'a core::panic::PanicMessage<'a>,
631 string: Option<String>,
632 }
633
634 impl FormatStringPayload<'_> {
635 fn fill(&mut self) -> &mut String {
636 let inner = self.inner;
637 // Lazily, the first time this gets called, run the actual string formatting.
638 self.string.get_or_insert_with(|| {
639 let mut s = String::new();
640 let mut fmt = fmt::Formatter::new(&mut s, fmt::FormattingOptions::new());
641 let _err = fmt::Display::fmt(&inner, &mut fmt);
642 s
643 })
644 }
645 }
646
647 unsafe impl PanicPayload for FormatStringPayload<'_> {
648 fn take_box(&mut self) -> *mut (dyn Any + Send) {
649 // We do two allocations here, unfortunately. But (a) they're required with the current
650 // scheme, and (b) we don't handle panic + OOM properly anyway (see comment in
651 // begin_panic below).
652 let contents = mem::take(self.fill());
653 Box::into_raw(Box::new(contents))
654 }
655
656 fn get(&mut self) -> &(dyn Any + Send) {
657 self.fill()
658 }
659 }
660
661 impl fmt::Display for FormatStringPayload<'_> {
662 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
663 if let Some(s) = &self.string {
664 f.write_str(s)
665 } else {
666 fmt::Display::fmt(&self.inner, f)
667 }
668 }
669 }
670
671 struct StaticStrPayload(&'static str);
672
673 unsafe impl PanicPayload for StaticStrPayload {
674 fn take_box(&mut self) -> *mut (dyn Any + Send) {
675 Box::into_raw(Box::new(self.0))
676 }
677
678 fn get(&mut self) -> &(dyn Any + Send) {
679 &self.0
680 }
681
682 fn as_str(&mut self) -> Option<&str> {
683 Some(self.0)
684 }
685 }
686
687 impl fmt::Display for StaticStrPayload {
688 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
689 f.write_str(self.0)
690 }
691 }
692
693 let loc = info.location().unwrap(); // The current implementation always returns Some
694 let msg = info.message();
695 crate::sys::backtrace::__rust_end_short_backtrace(move || {
696 if let Some(s) = msg.as_str() {
697 rust_panic_with_hook(
698 &mut StaticStrPayload(s),
699 loc,
700 info.can_unwind(),
701 info.force_no_backtrace(),
702 );
703 } else {
704 rust_panic_with_hook(
705 &mut FormatStringPayload { inner: &msg, string: None },
706 loc,
707 info.can_unwind(),
708 info.force_no_backtrace(),
709 );
710 }
711 })
712}
713
714/// This is the entry point of panicking for the non-format-string variants of
715/// panic!() and assert!(). In particular, this is the only entry point that supports
716/// arbitrary payloads, not just format strings.
717#[unstable(feature = "libstd_sys_internals", reason = "used by the panic! macro", issue = "none")]
718#[cfg_attr(not(any(test, doctest)), lang = "begin_panic")]
719// lang item for CTFE panic support
720// never inline unless panic_immediate_abort to avoid code
721// bloat at the call sites as much as possible
722#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold, optimize(size))]
723#[cfg_attr(feature = "panic_immediate_abort", inline)]
724#[track_caller]
725#[rustc_do_not_const_check] // hooked by const-eval
726pub const fn begin_panic<M: Any + Send>(msg: M) -> ! {
727 if cfg!(feature = "panic_immediate_abort") {
728 intrinsics::abort()
729 }
730
731 struct Payload<A> {
732 inner: Option<A>,
733 }
734
735 unsafe impl<A: Send + 'static> PanicPayload for Payload<A> {
736 fn take_box(&mut self) -> *mut (dyn Any + Send) {
737 // Note that this should be the only allocation performed in this code path. Currently
738 // this means that panic!() on OOM will invoke this code path, but then again we're not
739 // really ready for panic on OOM anyway. If we do start doing this, then we should
740 // propagate this allocation to be performed in the parent of this thread instead of the
741 // thread that's panicking.
742 let data = match self.inner.take() {
743 Some(a) => Box::new(a) as Box<dyn Any + Send>,
744 None => process::abort(),
745 };
746 Box::into_raw(data)
747 }
748
749 fn get(&mut self) -> &(dyn Any + Send) {
750 match self.inner {
751 Some(ref a) => a,
752 None => process::abort(),
753 }
754 }
755 }
756
757 impl<A: 'static> fmt::Display for Payload<A> {
758 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
759 match &self.inner {
760 Some(a) => f.write_str(payload_as_str(a)),
761 None => process::abort(),
762 }
763 }
764 }
765
766 let loc = Location::caller();
767 crate::sys::backtrace::__rust_end_short_backtrace(move || {
768 rust_panic_with_hook(
769 &mut Payload { inner: Some(msg) },
770 loc,
771 /* can_unwind */ true,
772 /* force_no_backtrace */ false,
773 )
774 })
775}
776
777fn payload_as_str(payload: &dyn Any) -> &str {
778 if let Some(&s) = payload.downcast_ref::<&'static str>() {
779 s
780 } else if let Some(s) = payload.downcast_ref::<String>() {
781 s.as_str()
782 } else {
783 "Box<dyn Any>"
784 }
785}
786
787/// Central point for dispatching panics.
788///
789/// Executes the primary logic for a panic, including checking for recursive
790/// panics, panic hooks, and finally dispatching to the panic runtime to either
791/// abort or unwind.
792#[optimize(size)]
793fn rust_panic_with_hook(
794 payload: &mut dyn PanicPayload,
795 location: &Location<'_>,
796 can_unwind: bool,
797 force_no_backtrace: bool,
798) -> ! {
799 let must_abort = panic_count::increase(true);
800
801 // Check if we need to abort immediately.
802 if let Some(must_abort) = must_abort {
803 match must_abort {
804 panic_count::MustAbort::PanicInHook => {
805 // Don't try to format the message in this case, perhaps that is causing the
806 // recursive panics. However if the message is just a string, no user-defined
807 // code is involved in printing it, so that is risk-free.
808 let message: &str = payload.as_str().unwrap_or_default();
809 rtprintpanic!(
810 "panicked at {location}:\n{message}\nthread panicked while processing panic. aborting.\n"
811 );
812 }
813 panic_count::MustAbort::AlwaysAbort => {
814 // Unfortunately, this does not print a backtrace, because creating
815 // a `Backtrace` will allocate, which we must avoid here.
816 rtprintpanic!("aborting due to panic at {location}:\n{payload}\n");
817 }
818 }
819 crate::sys::abort_internal();
820 }
821
822 match *HOOK.read().unwrap_or_else(PoisonError::into_inner) {
823 // Some platforms (like wasm) know that printing to stderr won't ever actually
824 // print anything, and if that's the case we can skip the default
825 // hook. Since string formatting happens lazily when calling `payload`
826 // methods, this means we avoid formatting the string at all!
827 // (The panic runtime might still call `payload.take_box()` though and trigger
828 // formatting.)
829 Hook::Default if panic_output().is_none() => {}
830 Hook::Default => {
831 default_hook(&PanicHookInfo::new(
832 location,
833 payload.get(),
834 can_unwind,
835 force_no_backtrace,
836 ));
837 }
838 Hook::Custom(ref hook) => {
839 hook(&PanicHookInfo::new(location, payload.get(), can_unwind, force_no_backtrace));
840 }
841 }
842
843 // Indicate that we have finished executing the panic hook. After this point
844 // it is fine if there is a panic while executing destructors, as long as it
845 // it contained within a `catch_unwind`.
846 panic_count::finished_panic_hook();
847
848 if !can_unwind {
849 // If a thread panics while running destructors or tries to unwind
850 // through a nounwind function (e.g. extern "C") then we cannot continue
851 // unwinding and have to abort immediately.
852 rtprintpanic!("thread caused non-unwinding panic. aborting.\n");
853 crate::sys::abort_internal();
854 }
855
856 rust_panic(payload)
857}
858
859/// This is the entry point for `resume_unwind`.
860/// It just forwards the payload to the panic runtime.
861#[cfg_attr(feature = "panic_immediate_abort", inline)]
862pub fn rust_panic_without_hook(payload: Box<dyn Any + Send>) -> ! {
863 panic_count::increase(false);
864
865 struct RewrapBox(Box<dyn Any + Send>);
866
867 unsafe impl PanicPayload for RewrapBox {
868 fn take_box(&mut self) -> *mut (dyn Any + Send) {
869 Box::into_raw(mem::replace(&mut self.0, Box::new(())))
870 }
871
872 fn get(&mut self) -> &(dyn Any + Send) {
873 &*self.0
874 }
875 }
876
877 impl fmt::Display for RewrapBox {
878 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
879 f.write_str(payload_as_str(&self.0))
880 }
881 }
882
883 rust_panic(&mut RewrapBox(payload))
884}
885
886/// An unmangled function (through `rustc_std_internal_symbol`) on which to slap
887/// yer breakpoints.
888#[inline(never)]
889#[cfg_attr(not(test), rustc_std_internal_symbol)]
890#[cfg(not(feature = "panic_immediate_abort"))]
891fn rust_panic(msg: &mut dyn PanicPayload) -> ! {
892 let code = unsafe { __rust_start_panic(msg) };
893 rtabort!("failed to initiate panic, error {code}")
894}
895
896#[cfg_attr(not(test), rustc_std_internal_symbol)]
897#[cfg(feature = "panic_immediate_abort")]
898fn rust_panic(_: &mut dyn PanicPayload) -> ! {
899 unsafe {
900 crate::intrinsics::abort();
901 }
902}