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