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