]> git.lizzy.rs Git - rust.git/blob - library/std/src/panicking.rs
Rollup merge of #91641 - dtolnay:cchar-if, r=Mark-Simulacrum
[rust.git] / library / std / src / 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
12 use core::panic::{BoxMeUp, Location, PanicInfo};
13
14 use crate::any::Any;
15 use crate::fmt;
16 use crate::intrinsics;
17 use crate::mem::{self, ManuallyDrop};
18 use crate::process;
19 use crate::sync::atomic::{AtomicBool, Ordering};
20 use crate::sys::stdio::panic_output;
21 use crate::sys_common::backtrace::{self, RustBacktrace};
22 use crate::sys_common::rwlock::StaticRWLock;
23 use crate::sys_common::thread_info;
24 use crate::thread;
25
26 #[cfg(not(test))]
27 use crate::io::set_output_capture;
28 // make sure to use the stderr output configured
29 // by libtest in the real copy of std
30 #[cfg(test)]
31 use realstd::io::set_output_capture;
32
33 // Binary interface to the panic runtime that the standard library depends on.
34 //
35 // The standard library is tagged with `#![needs_panic_runtime]` (introduced in
36 // RFC 1513) to indicate that it requires some other crate tagged with
37 // `#![panic_runtime]` to exist somewhere. Each panic runtime is intended to
38 // implement these symbols (with the same signatures) so we can get matched up
39 // to them.
40 //
41 // One day this may look a little less ad-hoc with the compiler helping out to
42 // hook up these functions, but it is not this day!
43 #[allow(improper_ctypes)]
44 extern "C" {
45     fn __rust_panic_cleanup(payload: *mut u8) -> *mut (dyn Any + Send + 'static);
46 }
47
48 #[allow(improper_ctypes)]
49 extern "C-unwind" {
50     /// `payload` is passed through another layer of raw pointers as `&mut dyn Trait` is not
51     /// FFI-safe. `BoxMeUp` lazily performs allocation only when needed (this avoids allocations
52     /// when using the "abort" panic runtime).
53     fn __rust_start_panic(payload: *mut &mut dyn BoxMeUp) -> u32;
54 }
55
56 /// This function is called by the panic runtime if FFI code catches a Rust
57 /// panic but doesn't rethrow it. We don't support this case since it messes
58 /// with our panic count.
59 #[cfg(not(test))]
60 #[rustc_std_internal_symbol]
61 extern "C" fn __rust_drop_panic() -> ! {
62     rtabort!("Rust panics must be rethrown");
63 }
64
65 /// This function is called by the panic runtime if it catches an exception
66 /// object which does not correspond to a Rust panic.
67 #[cfg(not(test))]
68 #[rustc_std_internal_symbol]
69 extern "C" fn __rust_foreign_exception() -> ! {
70     rtabort!("Rust cannot catch foreign exceptions");
71 }
72
73 #[derive(Copy, Clone)]
74 enum Hook {
75     Default,
76     Custom(*mut (dyn Fn(&PanicInfo<'_>) + 'static + Sync + Send)),
77 }
78
79 impl Hook {
80     fn custom(f: impl Fn(&PanicInfo<'_>) + 'static + Sync + Send) -> Self {
81         Self::Custom(Box::into_raw(Box::new(f)))
82     }
83 }
84
85 static HOOK_LOCK: StaticRWLock = StaticRWLock::new();
86 static mut HOOK: Hook = Hook::Default;
87
88 /// Registers a custom panic hook, replacing any that was previously registered.
89 ///
90 /// The panic hook is invoked when a thread panics, but before the panic runtime
91 /// is invoked. As such, the hook will run with both the aborting and unwinding
92 /// runtimes. The default hook prints a message to standard error and generates
93 /// a backtrace if requested, but this behavior can be customized with the
94 /// `set_hook` and [`take_hook`] functions.
95 ///
96 /// [`take_hook`]: ./fn.take_hook.html
97 ///
98 /// The hook is provided with a `PanicInfo` struct which contains information
99 /// about the origin of the panic, including the payload passed to `panic!` and
100 /// the source code location from which the panic originated.
101 ///
102 /// The panic hook is a global resource.
103 ///
104 /// # Panics
105 ///
106 /// Panics if called from a panicking thread.
107 ///
108 /// # Examples
109 ///
110 /// The following will print "Custom panic hook":
111 ///
112 /// ```should_panic
113 /// use std::panic;
114 ///
115 /// panic::set_hook(Box::new(|_| {
116 ///     println!("Custom panic hook");
117 /// }));
118 ///
119 /// panic!("Normal panic");
120 /// ```
121 #[stable(feature = "panic_hooks", since = "1.10.0")]
122 pub fn set_hook(hook: Box<dyn Fn(&PanicInfo<'_>) + 'static + Sync + Send>) {
123     if thread::panicking() {
124         panic!("cannot modify the panic hook from a panicking thread");
125     }
126
127     // SAFETY:
128     //
129     // - `HOOK` can only be modified while holding write access to `HOOK_LOCK`.
130     // - The argument of `Box::from_raw` is always a valid pointer that was created using
131     // `Box::into_raw`.
132     unsafe {
133         let guard = HOOK_LOCK.write();
134         let old_hook = HOOK;
135         HOOK = Hook::Custom(Box::into_raw(hook));
136         drop(guard);
137
138         if let Hook::Custom(ptr) = old_hook {
139             #[allow(unused_must_use)]
140             {
141                 Box::from_raw(ptr);
142             }
143         }
144     }
145 }
146
147 /// Unregisters the current panic hook, returning it.
148 ///
149 /// *See also the function [`set_hook`].*
150 ///
151 /// [`set_hook`]: ./fn.set_hook.html
152 ///
153 /// If no custom hook is registered, the default hook will be returned.
154 ///
155 /// # Panics
156 ///
157 /// Panics if called from a panicking thread.
158 ///
159 /// # Examples
160 ///
161 /// The following will print "Normal panic":
162 ///
163 /// ```should_panic
164 /// use std::panic;
165 ///
166 /// panic::set_hook(Box::new(|_| {
167 ///     println!("Custom panic hook");
168 /// }));
169 ///
170 /// let _ = panic::take_hook();
171 ///
172 /// panic!("Normal panic");
173 /// ```
174 #[must_use]
175 #[stable(feature = "panic_hooks", since = "1.10.0")]
176 pub fn take_hook() -> Box<dyn Fn(&PanicInfo<'_>) + 'static + Sync + Send> {
177     if thread::panicking() {
178         panic!("cannot modify the panic hook from a panicking thread");
179     }
180
181     // SAFETY:
182     //
183     // - `HOOK` can only be modified while holding write access to `HOOK_LOCK`.
184     // - The argument of `Box::from_raw` is always a valid pointer that was created using
185     // `Box::into_raw`.
186     unsafe {
187         let guard = HOOK_LOCK.write();
188         let hook = HOOK;
189         HOOK = Hook::Default;
190         drop(guard);
191
192         match hook {
193             Hook::Default => Box::new(default_hook),
194             Hook::Custom(ptr) => Box::from_raw(ptr),
195         }
196     }
197 }
198
199 /// Atomic combination of [`take_hook`] and [`set_hook`]. Use this to replace the panic handler with
200 /// a new panic handler that does something and then executes the old handler.
201 ///
202 /// [`take_hook`]: ./fn.take_hook.html
203 /// [`set_hook`]: ./fn.set_hook.html
204 ///
205 /// # Panics
206 ///
207 /// Panics if called from a panicking thread.
208 ///
209 /// # Examples
210 ///
211 /// The following will print the custom message, and then the normal output of panic.
212 ///
213 /// ```should_panic
214 /// #![feature(panic_update_hook)]
215 /// use std::panic;
216 ///
217 /// // Equivalent to
218 /// // let prev = panic::take_hook();
219 /// // panic::set_hook(move |info| {
220 /// //     println!("...");
221 /// //     prev(info);
222 /// // );
223 /// panic::update_hook(move |prev, info| {
224 ///     println!("Print custom message and execute panic handler as usual");
225 ///     prev(info);
226 /// });
227 ///
228 /// panic!("Custom and then normal");
229 /// ```
230 #[unstable(feature = "panic_update_hook", issue = "92649")]
231 pub fn update_hook<F>(hook_fn: F)
232 where
233     F: Fn(&(dyn Fn(&PanicInfo<'_>) + Send + Sync + 'static), &PanicInfo<'_>)
234         + Sync
235         + Send
236         + 'static,
237 {
238     if thread::panicking() {
239         panic!("cannot modify the panic hook from a panicking thread");
240     }
241
242     // SAFETY:
243     //
244     // - `HOOK` can only be modified while holding write access to `HOOK_LOCK`.
245     // - The argument of `Box::from_raw` is always a valid pointer that was created using
246     // `Box::into_raw`.
247     unsafe {
248         let guard = HOOK_LOCK.write();
249         let old_hook = HOOK;
250         HOOK = Hook::Default;
251
252         let prev = match old_hook {
253             Hook::Default => Box::new(default_hook),
254             Hook::Custom(ptr) => Box::from_raw(ptr),
255         };
256
257         HOOK = Hook::custom(move |info| hook_fn(&prev, info));
258         drop(guard);
259     }
260 }
261
262 fn default_hook(info: &PanicInfo<'_>) {
263     // If this is a double panic, make sure that we print a backtrace
264     // for this panic. Otherwise only print it if logging is enabled.
265     let backtrace_env = if panic_count::get_count() >= 2 {
266         backtrace::rust_backtrace_print_full()
267     } else {
268         backtrace::rust_backtrace_env()
269     };
270
271     // The current implementation always returns `Some`.
272     let location = info.location().unwrap();
273
274     let msg = match info.payload().downcast_ref::<&'static str>() {
275         Some(s) => *s,
276         None => match info.payload().downcast_ref::<String>() {
277             Some(s) => &s[..],
278             None => "Box<dyn Any>",
279         },
280     };
281     let thread = thread_info::current_thread();
282     let name = thread.as_ref().and_then(|t| t.name()).unwrap_or("<unnamed>");
283
284     let write = |err: &mut dyn crate::io::Write| {
285         let _ = writeln!(err, "thread '{}' panicked at '{}', {}", name, msg, location);
286
287         static FIRST_PANIC: AtomicBool = AtomicBool::new(true);
288
289         match backtrace_env {
290             RustBacktrace::Print(format) => drop(backtrace::print(err, format)),
291             RustBacktrace::Disabled => {}
292             RustBacktrace::RuntimeDisabled => {
293                 if FIRST_PANIC.swap(false, Ordering::SeqCst) {
294                     let _ = writeln!(
295                         err,
296                         "note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace"
297                     );
298                 }
299             }
300         }
301     };
302
303     if let Some(local) = set_output_capture(None) {
304         write(&mut *local.lock().unwrap_or_else(|e| e.into_inner()));
305         set_output_capture(Some(local));
306     } else if let Some(mut out) = panic_output() {
307         write(&mut out);
308     }
309 }
310
311 #[cfg(not(test))]
312 #[doc(hidden)]
313 #[unstable(feature = "update_panic_count", issue = "none")]
314 pub mod panic_count {
315     use crate::cell::Cell;
316     use crate::sync::atomic::{AtomicUsize, Ordering};
317
318     pub const ALWAYS_ABORT_FLAG: usize = 1 << (usize::BITS - 1);
319
320     // Panic count for the current thread.
321     thread_local! { static LOCAL_PANIC_COUNT: Cell<usize> = Cell::new(0) }
322
323     // Sum of panic counts from all threads. The purpose of this is to have
324     // a fast path in `is_zero` (which is used by `panicking`). In any particular
325     // thread, if that thread currently views `GLOBAL_PANIC_COUNT` as being zero,
326     // then `LOCAL_PANIC_COUNT` in that thread is zero. This invariant holds before
327     // and after increase and decrease, but not necessarily during their execution.
328     //
329     // Additionally, the top bit of GLOBAL_PANIC_COUNT (GLOBAL_ALWAYS_ABORT_FLAG)
330     // records whether panic::always_abort() has been called.  This can only be
331     // set, never cleared.
332     //
333     // This could be viewed as a struct containing a single bit and an n-1-bit
334     // value, but if we wrote it like that it would be more than a single word,
335     // and even a newtype around usize would be clumsy because we need atomics.
336     // But we use such a tuple for the return type of increase().
337     //
338     // Stealing a bit is fine because it just amounts to assuming that each
339     // panicking thread consumes at least 2 bytes of address space.
340     static GLOBAL_PANIC_COUNT: AtomicUsize = AtomicUsize::new(0);
341
342     pub fn increase() -> (bool, usize) {
343         (
344             GLOBAL_PANIC_COUNT.fetch_add(1, Ordering::Relaxed) & ALWAYS_ABORT_FLAG != 0,
345             LOCAL_PANIC_COUNT.with(|c| {
346                 let next = c.get() + 1;
347                 c.set(next);
348                 next
349             }),
350         )
351     }
352
353     pub fn decrease() {
354         GLOBAL_PANIC_COUNT.fetch_sub(1, Ordering::Relaxed);
355         LOCAL_PANIC_COUNT.with(|c| {
356             let next = c.get() - 1;
357             c.set(next);
358             next
359         });
360     }
361
362     pub fn set_always_abort() {
363         GLOBAL_PANIC_COUNT.fetch_or(ALWAYS_ABORT_FLAG, Ordering::Relaxed);
364     }
365
366     // Disregards ALWAYS_ABORT_FLAG
367     #[must_use]
368     pub fn get_count() -> usize {
369         LOCAL_PANIC_COUNT.with(|c| c.get())
370     }
371
372     // Disregards ALWAYS_ABORT_FLAG
373     #[must_use]
374     #[inline]
375     pub fn count_is_zero() -> bool {
376         if GLOBAL_PANIC_COUNT.load(Ordering::Relaxed) & !ALWAYS_ABORT_FLAG == 0 {
377             // Fast path: if `GLOBAL_PANIC_COUNT` is zero, all threads
378             // (including the current one) will have `LOCAL_PANIC_COUNT`
379             // equal to zero, so TLS access can be avoided.
380             //
381             // In terms of performance, a relaxed atomic load is similar to a normal
382             // aligned memory read (e.g., a mov instruction in x86), but with some
383             // compiler optimization restrictions. On the other hand, a TLS access
384             // might require calling a non-inlinable function (such as `__tls_get_addr`
385             // when using the GD TLS model).
386             true
387         } else {
388             is_zero_slow_path()
389         }
390     }
391
392     // Slow path is in a separate function to reduce the amount of code
393     // inlined from `is_zero`.
394     #[inline(never)]
395     #[cold]
396     fn is_zero_slow_path() -> bool {
397         LOCAL_PANIC_COUNT.with(|c| c.get() == 0)
398     }
399 }
400
401 #[cfg(test)]
402 pub use realstd::rt::panic_count;
403
404 /// Invoke a closure, capturing the cause of an unwinding panic if one occurs.
405 pub unsafe fn r#try<R, F: FnOnce() -> R>(f: F) -> Result<R, Box<dyn Any + Send>> {
406     union Data<F, R> {
407         f: ManuallyDrop<F>,
408         r: ManuallyDrop<R>,
409         p: ManuallyDrop<Box<dyn Any + Send>>,
410     }
411
412     // We do some sketchy operations with ownership here for the sake of
413     // performance. We can only pass pointers down to `do_call` (can't pass
414     // objects by value), so we do all the ownership tracking here manually
415     // using a union.
416     //
417     // We go through a transition where:
418     //
419     // * First, we set the data field `f` to be the argumentless closure that we're going to call.
420     // * When we make the function call, the `do_call` function below, we take
421     //   ownership of the function pointer. At this point the `data` union is
422     //   entirely uninitialized.
423     // * If the closure successfully returns, we write the return value into the
424     //   data's return slot (field `r`).
425     // * If the closure panics (`do_catch` below), we write the panic payload into field `p`.
426     // * Finally, when we come back out of the `try` intrinsic we're
427     //   in one of two states:
428     //
429     //      1. The closure didn't panic, in which case the return value was
430     //         filled in. We move it out of `data.r` and return it.
431     //      2. The closure panicked, in which case the panic payload was
432     //         filled in. We move it out of `data.p` and return it.
433     //
434     // Once we stack all that together we should have the "most efficient'
435     // method of calling a catch panic whilst juggling ownership.
436     let mut data = Data { f: ManuallyDrop::new(f) };
437
438     let data_ptr = &mut data as *mut _ as *mut u8;
439     // SAFETY:
440     //
441     // Access to the union's fields: this is `std` and we know that the `r#try`
442     // intrinsic fills in the `r` or `p` union field based on its return value.
443     //
444     // The call to `intrinsics::r#try` is made safe by:
445     // - `do_call`, the first argument, can be called with the initial `data_ptr`.
446     // - `do_catch`, the second argument, can be called with the `data_ptr` as well.
447     // See their safety preconditions for more information
448     unsafe {
449         return if intrinsics::r#try(do_call::<F, R>, data_ptr, do_catch::<F, R>) == 0 {
450             Ok(ManuallyDrop::into_inner(data.r))
451         } else {
452             Err(ManuallyDrop::into_inner(data.p))
453         };
454     }
455
456     // We consider unwinding to be rare, so mark this function as cold. However,
457     // do not mark it no-inline -- that decision is best to leave to the
458     // optimizer (in most cases this function is not inlined even as a normal,
459     // non-cold function, though, as of the writing of this comment).
460     #[cold]
461     unsafe fn cleanup(payload: *mut u8) -> Box<dyn Any + Send + 'static> {
462         // SAFETY: The whole unsafe block hinges on a correct implementation of
463         // the panic handler `__rust_panic_cleanup`. As such we can only
464         // assume it returns the correct thing for `Box::from_raw` to work
465         // without undefined behavior.
466         let obj = unsafe { Box::from_raw(__rust_panic_cleanup(payload)) };
467         panic_count::decrease();
468         obj
469     }
470
471     // SAFETY:
472     // data must be non-NUL, correctly aligned, and a pointer to a `Data<F, R>`
473     // Its must contains a valid `f` (type: F) value that can be use to fill
474     // `data.r`.
475     //
476     // This function cannot be marked as `unsafe` because `intrinsics::r#try`
477     // expects normal function pointers.
478     #[inline]
479     fn do_call<F: FnOnce() -> R, R>(data: *mut u8) {
480         // SAFETY: this is the responsibility of the caller, see above.
481         unsafe {
482             let data = data as *mut Data<F, R>;
483             let data = &mut (*data);
484             let f = ManuallyDrop::take(&mut data.f);
485             data.r = ManuallyDrop::new(f());
486         }
487     }
488
489     // We *do* want this part of the catch to be inlined: this allows the
490     // compiler to properly track accesses to the Data union and optimize it
491     // away most of the time.
492     //
493     // SAFETY:
494     // data must be non-NUL, correctly aligned, and a pointer to a `Data<F, R>`
495     // Since this uses `cleanup` it also hinges on a correct implementation of
496     // `__rustc_panic_cleanup`.
497     //
498     // This function cannot be marked as `unsafe` because `intrinsics::r#try`
499     // expects normal function pointers.
500     #[inline]
501     fn do_catch<F: FnOnce() -> R, R>(data: *mut u8, payload: *mut u8) {
502         // SAFETY: this is the responsibility of the caller, see above.
503         //
504         // When `__rustc_panic_cleaner` is correctly implemented we can rely
505         // on `obj` being the correct thing to pass to `data.p` (after wrapping
506         // in `ManuallyDrop`).
507         unsafe {
508             let data = data as *mut Data<F, R>;
509             let data = &mut (*data);
510             let obj = cleanup(payload);
511             data.p = ManuallyDrop::new(obj);
512         }
513     }
514 }
515
516 /// Determines whether the current thread is unwinding because of panic.
517 #[inline]
518 pub fn panicking() -> bool {
519     !panic_count::count_is_zero()
520 }
521
522 /// Entry point of panics from the libcore crate (`panic_impl` lang item).
523 #[cfg(not(test))]
524 #[panic_handler]
525 pub fn begin_panic_handler(info: &PanicInfo<'_>) -> ! {
526     struct PanicPayload<'a> {
527         inner: &'a fmt::Arguments<'a>,
528         string: Option<String>,
529     }
530
531     impl<'a> PanicPayload<'a> {
532         fn new(inner: &'a fmt::Arguments<'a>) -> PanicPayload<'a> {
533             PanicPayload { inner, string: None }
534         }
535
536         fn fill(&mut self) -> &mut String {
537             use crate::fmt::Write;
538
539             let inner = self.inner;
540             // Lazily, the first time this gets called, run the actual string formatting.
541             self.string.get_or_insert_with(|| {
542                 let mut s = String::new();
543                 drop(s.write_fmt(*inner));
544                 s
545             })
546         }
547     }
548
549     unsafe impl<'a> BoxMeUp for PanicPayload<'a> {
550         fn take_box(&mut self) -> *mut (dyn Any + Send) {
551             // We do two allocations here, unfortunately. But (a) they're required with the current
552             // scheme, and (b) we don't handle panic + OOM properly anyway (see comment in
553             // begin_panic below).
554             let contents = mem::take(self.fill());
555             Box::into_raw(Box::new(contents))
556         }
557
558         fn get(&mut self) -> &(dyn Any + Send) {
559             self.fill()
560         }
561     }
562
563     struct StrPanicPayload(&'static str);
564
565     unsafe impl BoxMeUp for StrPanicPayload {
566         fn take_box(&mut self) -> *mut (dyn Any + Send) {
567             Box::into_raw(Box::new(self.0))
568         }
569
570         fn get(&mut self) -> &(dyn Any + Send) {
571             &self.0
572         }
573     }
574
575     let loc = info.location().unwrap(); // The current implementation always returns Some
576     let msg = info.message().unwrap(); // The current implementation always returns Some
577     crate::sys_common::backtrace::__rust_end_short_backtrace(move || {
578         if let Some(msg) = msg.as_str() {
579             rust_panic_with_hook(&mut StrPanicPayload(msg), info.message(), loc, info.can_unwind());
580         } else {
581             rust_panic_with_hook(
582                 &mut PanicPayload::new(msg),
583                 info.message(),
584                 loc,
585                 info.can_unwind(),
586             );
587         }
588     })
589 }
590
591 /// This is the entry point of panicking for the non-format-string variants of
592 /// panic!() and assert!(). In particular, this is the only entry point that supports
593 /// arbitrary payloads, not just format strings.
594 #[unstable(feature = "libstd_sys_internals", reason = "used by the panic! macro", issue = "none")]
595 #[cfg_attr(not(test), lang = "begin_panic")]
596 // lang item for CTFE panic support
597 // never inline unless panic_immediate_abort to avoid code
598 // bloat at the call sites as much as possible
599 #[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
600 #[cold]
601 #[track_caller]
602 #[rustc_do_not_const_check] // hooked by const-eval
603 pub const fn begin_panic<M: Any + Send>(msg: M) -> ! {
604     if cfg!(feature = "panic_immediate_abort") {
605         intrinsics::abort()
606     }
607
608     let loc = Location::caller();
609     return crate::sys_common::backtrace::__rust_end_short_backtrace(move || {
610         rust_panic_with_hook(&mut PanicPayload::new(msg), None, loc, true)
611     });
612
613     struct PanicPayload<A> {
614         inner: Option<A>,
615     }
616
617     impl<A: Send + 'static> PanicPayload<A> {
618         fn new(inner: A) -> PanicPayload<A> {
619             PanicPayload { inner: Some(inner) }
620         }
621     }
622
623     unsafe impl<A: Send + 'static> BoxMeUp for PanicPayload<A> {
624         fn take_box(&mut self) -> *mut (dyn Any + Send) {
625             // Note that this should be the only allocation performed in this code path. Currently
626             // this means that panic!() on OOM will invoke this code path, but then again we're not
627             // really ready for panic on OOM anyway. If we do start doing this, then we should
628             // propagate this allocation to be performed in the parent of this thread instead of the
629             // thread that's panicking.
630             let data = match self.inner.take() {
631                 Some(a) => Box::new(a) as Box<dyn Any + Send>,
632                 None => process::abort(),
633             };
634             Box::into_raw(data)
635         }
636
637         fn get(&mut self) -> &(dyn Any + Send) {
638             match self.inner {
639                 Some(ref a) => a,
640                 None => process::abort(),
641             }
642         }
643     }
644 }
645
646 /// Central point for dispatching panics.
647 ///
648 /// Executes the primary logic for a panic, including checking for recursive
649 /// panics, panic hooks, and finally dispatching to the panic runtime to either
650 /// abort or unwind.
651 fn rust_panic_with_hook(
652     payload: &mut dyn BoxMeUp,
653     message: Option<&fmt::Arguments<'_>>,
654     location: &Location<'_>,
655     can_unwind: bool,
656 ) -> ! {
657     let (must_abort, panics) = panic_count::increase();
658
659     // If this is the third nested call (e.g., panics == 2, this is 0-indexed),
660     // the panic hook probably triggered the last panic, otherwise the
661     // double-panic check would have aborted the process. In this case abort the
662     // process real quickly as we don't want to try calling it again as it'll
663     // probably just panic again.
664     if must_abort || panics > 2 {
665         if panics > 2 {
666             // Don't try to print the message in this case
667             // - perhaps that is causing the recursive panics.
668             rtprintpanic!("thread panicked while processing panic. aborting.\n");
669         } else {
670             // Unfortunately, this does not print a backtrace, because creating
671             // a `Backtrace` will allocate, which we must to avoid here.
672             let panicinfo = PanicInfo::internal_constructor(message, location, can_unwind);
673             rtprintpanic!("{}\npanicked after panic::always_abort(), aborting.\n", panicinfo);
674         }
675         crate::sys::abort_internal();
676     }
677
678     unsafe {
679         let mut info = PanicInfo::internal_constructor(message, location, can_unwind);
680         let _guard = HOOK_LOCK.read();
681         match HOOK {
682             // Some platforms (like wasm) know that printing to stderr won't ever actually
683             // print anything, and if that's the case we can skip the default
684             // hook. Since string formatting happens lazily when calling `payload`
685             // methods, this means we avoid formatting the string at all!
686             // (The panic runtime might still call `payload.take_box()` though and trigger
687             // formatting.)
688             Hook::Default if panic_output().is_none() => {}
689             Hook::Default => {
690                 info.set_payload(payload.get());
691                 default_hook(&info);
692             }
693             Hook::Custom(ptr) => {
694                 info.set_payload(payload.get());
695                 (*ptr)(&info);
696             }
697         };
698     }
699
700     if panics > 1 || !can_unwind {
701         // If a thread panics while it's already unwinding then we
702         // have limited options. Currently our preference is to
703         // just abort. In the future we may consider resuming
704         // unwinding or otherwise exiting the thread cleanly.
705         rtprintpanic!("thread panicked while panicking. aborting.\n");
706         crate::sys::abort_internal();
707     }
708
709     rust_panic(payload)
710 }
711
712 /// This is the entry point for `resume_unwind`.
713 /// It just forwards the payload to the panic runtime.
714 pub fn rust_panic_without_hook(payload: Box<dyn Any + Send>) -> ! {
715     panic_count::increase();
716
717     struct RewrapBox(Box<dyn Any + Send>);
718
719     unsafe impl BoxMeUp for RewrapBox {
720         fn take_box(&mut self) -> *mut (dyn Any + Send) {
721             Box::into_raw(mem::replace(&mut self.0, Box::new(())))
722         }
723
724         fn get(&mut self) -> &(dyn Any + Send) {
725             &*self.0
726         }
727     }
728
729     rust_panic(&mut RewrapBox(payload))
730 }
731
732 /// An unmangled function (through `rustc_std_internal_symbol`) on which to slap
733 /// yer breakpoints.
734 #[inline(never)]
735 #[cfg_attr(not(test), rustc_std_internal_symbol)]
736 fn rust_panic(mut msg: &mut dyn BoxMeUp) -> ! {
737     let code = unsafe {
738         let obj = &mut msg as *mut &mut dyn BoxMeUp;
739         __rust_start_panic(obj)
740     };
741     rtabort!("failed to initiate panic, error {}", code)
742 }