1 //! Implementation of various bits and pieces of the `panic!` macro and
2 //! associated runtime pieces.
4 //! Specifically, this module contains the implementation of:
7 //! * Executing a panic up to doing the actual implementation
8 //! * Shims around "try"
10 use core::panic::{BoxMeUp, Location, PanicInfo};
14 use crate::intrinsics;
15 use crate::mem::{self, ManuallyDrop, MaybeUninit};
17 use crate::sync::atomic::{AtomicBool, Ordering};
18 use crate::sys::stdio::panic_output;
19 use crate::sys_common::backtrace::{self, RustBacktrace};
20 use crate::sys_common::rwlock::RWLock;
21 use crate::sys_common::{thread_info, util};
25 use crate::io::set_panic;
26 // make sure to use the stderr output configured
27 // by libtest in the real copy of std
29 use realstd::io::set_panic;
31 // This must be kept in sync with the implementations in libpanic_unwind.
33 // This is *not* checked in anyway; the compiler does not allow us to use a
34 // type/macro/anything from panic_unwind, since we're then linking in the
35 // panic_unwind runtime even during -Cpanic=abort.
37 // Essentially this must be the type of `imp::Payload` in libpanic_unwind.
39 if #[cfg(not(feature = "panic_unwind"))] {
41 } else if #[cfg(target_os = "emscripten")] {
42 type Payload = *mut u8;
43 } else if #[cfg(target_arch = "wasm32")] {
44 type Payload = *mut u8;
45 } else if #[cfg(target_os = "hermit")] {
46 type Payload = *mut u8;
47 } else if #[cfg(all(target_env = "msvc", target_arch = "aarch64"))] {
48 type Payload = *mut u8;
49 } else if #[cfg(target_env = "msvc")] {
50 type Payload = [u64; 2];
52 type Payload = *mut u8;
56 // Binary interface to the panic runtime that the standard library depends on.
58 // The standard library is tagged with `#![needs_panic_runtime]` (introduced in
59 // RFC 1513) to indicate that it requires some other crate tagged with
60 // `#![panic_runtime]` to exist somewhere. Each panic runtime is intended to
61 // implement these symbols (with the same signatures) so we can get matched up
64 // One day this may look a little less ad-hoc with the compiler helping out to
65 // hook up these functions, but it is not this day!
66 #[allow(improper_ctypes)]
68 /// The payload ptr here is actually the same as the payload ptr for the try
69 /// intrinsic (i.e., is really `*mut [u64; 2]` or `*mut *mut u8`).
70 fn __rust_panic_cleanup(payload: *mut u8) -> *mut (dyn Any + Send + 'static);
72 /// `payload` is actually a `*mut &mut dyn BoxMeUp` but that would cause FFI warnings.
73 /// It cannot be `Box<dyn BoxMeUp>` because the other end of this call does not depend
74 /// on liballoc, and thus cannot use `Box`.
76 fn __rust_start_panic(payload: usize) -> u32;
79 /// This function is called by the panic runtime if FFI code catches a Rust
80 /// panic but doesn't rethrow it. We don't support this case since it messes
81 /// with our panic count.
83 #[rustc_std_internal_symbol]
84 extern "C" fn __rust_drop_panic() -> ! {
85 rtabort!("Rust panics must be rethrown");
88 #[derive(Copy, Clone)]
91 Custom(*mut (dyn Fn(&PanicInfo<'_>) + 'static + Sync + Send)),
94 static HOOK_LOCK: RWLock = RWLock::new();
95 static mut HOOK: Hook = Hook::Default;
97 /// Registers a custom panic hook, replacing any that was previously registered.
99 /// The panic hook is invoked when a thread panics, but before the panic runtime
100 /// is invoked. As such, the hook will run with both the aborting and unwinding
101 /// runtimes. The default hook prints a message to standard error and generates
102 /// a backtrace if requested, but this behavior can be customized with the
103 /// `set_hook` and [`take_hook`] functions.
105 /// [`take_hook`]: ./fn.take_hook.html
107 /// The hook is provided with a `PanicInfo` struct which contains information
108 /// about the origin of the panic, including the payload passed to `panic!` and
109 /// the source code location from which the panic originated.
111 /// The panic hook is a global resource.
115 /// Panics if called from a panicking thread.
119 /// The following will print "Custom panic hook":
124 /// panic::set_hook(Box::new(|_| {
125 /// println!("Custom panic hook");
128 /// panic!("Normal panic");
130 #[stable(feature = "panic_hooks", since = "1.10.0")]
131 pub fn set_hook(hook: Box<dyn Fn(&PanicInfo<'_>) + 'static + Sync + Send>) {
132 if thread::panicking() {
133 panic!("cannot modify the panic hook from a panicking thread");
139 HOOK = Hook::Custom(Box::into_raw(hook));
140 HOOK_LOCK.write_unlock();
142 if let Hook::Custom(ptr) = old_hook {
143 #[allow(unused_must_use)]
151 /// Unregisters the current panic hook, returning it.
153 /// *See also the function [`set_hook`].*
155 /// [`set_hook`]: ./fn.set_hook.html
157 /// If no custom hook is registered, the default hook will be returned.
161 /// Panics if called from a panicking thread.
165 /// The following will print "Normal panic":
170 /// panic::set_hook(Box::new(|_| {
171 /// println!("Custom panic hook");
174 /// let _ = panic::take_hook();
176 /// panic!("Normal panic");
178 #[stable(feature = "panic_hooks", since = "1.10.0")]
179 pub fn take_hook() -> Box<dyn Fn(&PanicInfo<'_>) + 'static + Sync + Send> {
180 if thread::panicking() {
181 panic!("cannot modify the panic hook from a panicking thread");
187 HOOK = Hook::Default;
188 HOOK_LOCK.write_unlock();
191 Hook::Default => Box::new(default_hook),
192 Hook::Custom(ptr) => Box::from_raw(ptr),
197 fn default_hook(info: &PanicInfo<'_>) {
198 // If this is a double panic, make sure that we print a backtrace
199 // for this panic. Otherwise only print it if logging is enabled.
200 let backtrace_env = if update_panic_count(0) >= 2 {
201 RustBacktrace::Print(backtrace_rs::PrintFmt::Full)
203 backtrace::rust_backtrace_env()
206 // The current implementation always returns `Some`.
207 let location = info.location().unwrap();
209 let msg = match info.payload().downcast_ref::<&'static str>() {
211 None => match info.payload().downcast_ref::<String>() {
216 let thread = thread_info::current_thread();
217 let name = thread.as_ref().and_then(|t| t.name()).unwrap_or("<unnamed>");
219 let write = |err: &mut dyn crate::io::Write| {
220 let _ = writeln!(err, "thread '{}' panicked at '{}', {}", name, msg, location);
222 static FIRST_PANIC: AtomicBool = AtomicBool::new(true);
224 match backtrace_env {
225 RustBacktrace::Print(format) => drop(backtrace::print(err, format)),
226 RustBacktrace::Disabled => {}
227 RustBacktrace::RuntimeDisabled => {
228 if FIRST_PANIC.swap(false, Ordering::SeqCst) {
231 "note: run with `RUST_BACKTRACE=1` \
232 environment variable to display a backtrace"
239 if let Some(mut local) = set_panic(None) {
240 // NB. In `cfg(test)` this uses the forwarding impl
241 // for `Box<dyn (::realstd::io::Write) + Send>`.
243 set_panic(Some(local));
244 } else if let Some(mut out) = panic_output() {
251 #[unstable(feature = "update_panic_count", issue = "none")]
252 pub fn update_panic_count(amt: isize) -> usize {
253 use crate::cell::Cell;
254 thread_local! { static PANIC_COUNT: Cell<usize> = Cell::new(0) }
256 PANIC_COUNT.with(|c| {
257 let next = (c.get() as isize + amt) as usize;
264 pub use realstd::rt::update_panic_count;
266 /// Invoke a closure, capturing the cause of an unwinding panic if one occurs.
267 pub unsafe fn r#try<R, F: FnOnce() -> R>(f: F) -> Result<R, Box<dyn Any + Send>> {
273 // We do some sketchy operations with ownership here for the sake of
274 // performance. We can only pass pointers down to `do_call` (can't pass
275 // objects by value), so we do all the ownership tracking here manually
278 // We go through a transition where:
280 // * First, we set the data to be the closure that we're going to call.
281 // * When we make the function call, the `do_call` function below, we take
282 // ownership of the function pointer. At this point the `Data` union is
283 // entirely uninitialized.
284 // * If the closure successfully returns, we write the return value into the
285 // data's return slot. Note that `ptr::write` is used as it's overwriting
286 // uninitialized data.
287 // * Finally, when we come back out of the `try` intrinsic we're
288 // in one of two states:
290 // 1. The closure didn't panic, in which case the return value was
291 // filled in. We move it out of `data` and return it.
292 // 2. The closure panicked, in which case the return value wasn't
293 // filled in. In this case the entire `data` union is invalid, so
294 // there is no need to drop anything.
296 // Once we stack all that together we should have the "most efficient'
297 // method of calling a catch panic whilst juggling ownership.
298 let mut data = Data { f: ManuallyDrop::new(f) };
300 let mut payload: MaybeUninit<Payload> = MaybeUninit::uninit();
302 let data_ptr = &mut data as *mut _ as *mut u8;
303 let payload_ptr = payload.as_mut_ptr() as *mut _;
304 return if intrinsics::r#try(do_call::<F, R>, data_ptr, payload_ptr) == 0 {
305 Ok(ManuallyDrop::into_inner(data.r))
307 Err(cleanup(payload.assume_init()))
310 // We consider unwinding to be rare, so mark this function as cold. However,
311 // do not mark it no-inline -- that decision is best to leave to the
312 // optimizer (in most cases this function is not inlined even as a normal,
313 // non-cold function, though, as of the writing of this comment).
315 unsafe fn cleanup(mut payload: Payload) -> Box<dyn Any + Send + 'static> {
316 let obj = Box::from_raw(__rust_panic_cleanup(&mut payload as *mut _ as *mut u8));
317 update_panic_count(-1);
321 fn do_call<F: FnOnce() -> R, R>(data: *mut u8) {
323 let data = data as *mut Data<F, R>;
324 let data = &mut (*data);
325 let f = ManuallyDrop::take(&mut data.f);
326 data.r = ManuallyDrop::new(f());
331 /// Determines whether the current thread is unwinding because of panic.
332 pub fn panicking() -> bool {
333 update_panic_count(0) != 0
336 /// The entry point for panicking with a formatted message.
338 /// This is designed to reduce the amount of code required at the call
339 /// site as much as possible (so that `panic!()` has as low an impact
340 /// on (e.g.) the inlining of other functions as possible), by moving
341 /// the actual formatting into this shared place.
342 #[unstable(feature = "libstd_sys_internals", reason = "used by the panic! macro", issue = "none")]
344 // If panic_immediate_abort, inline the abort call,
345 // otherwise avoid inlining because of it is cold path.
346 #[cfg_attr(not(feature = "panic_immediate_abort"), track_caller)]
347 #[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
348 #[cfg_attr(feature = "panic_immediate_abort", inline)]
349 pub fn begin_panic_fmt(msg: &fmt::Arguments<'_>) -> ! {
350 if cfg!(feature = "panic_immediate_abort") {
351 unsafe { intrinsics::abort() }
354 let info = PanicInfo::internal_constructor(Some(msg), Location::caller());
355 begin_panic_handler(&info)
358 /// Entry point of panics from the libcore crate (`panic_impl` lang item).
359 #[cfg_attr(not(test), panic_handler)]
361 pub fn begin_panic_handler(info: &PanicInfo<'_>) -> ! {
362 struct PanicPayload<'a> {
363 inner: &'a fmt::Arguments<'a>,
364 string: Option<String>,
367 impl<'a> PanicPayload<'a> {
368 fn new(inner: &'a fmt::Arguments<'a>) -> PanicPayload<'a> {
369 PanicPayload { inner, string: None }
372 fn fill(&mut self) -> &mut String {
373 use crate::fmt::Write;
375 let inner = self.inner;
376 // Lazily, the first time this gets called, run the actual string formatting.
377 self.string.get_or_insert_with(|| {
378 let mut s = String::new();
379 drop(s.write_fmt(*inner));
385 unsafe impl<'a> BoxMeUp for PanicPayload<'a> {
386 fn take_box(&mut self) -> *mut (dyn Any + Send) {
387 // We do two allocations here, unfortunately. But (a) they're required with the current
388 // scheme, and (b) we don't handle panic + OOM properly anyway (see comment in
389 // begin_panic below).
390 let contents = mem::take(self.fill());
391 Box::into_raw(Box::new(contents))
394 fn get(&mut self) -> &(dyn Any + Send) {
399 let loc = info.location().unwrap(); // The current implementation always returns Some
400 let msg = info.message().unwrap(); // The current implementation always returns Some
401 rust_panic_with_hook(&mut PanicPayload::new(msg), info.message(), loc);
404 /// This is the entry point of panicking for the non-format-string variants of
405 /// panic!() and assert!(). In particular, this is the only entry point that supports
406 /// arbitrary payloads, not just format strings.
407 #[unstable(feature = "libstd_sys_internals", reason = "used by the panic! macro", issue = "none")]
408 #[cfg_attr(not(test), lang = "begin_panic")]
409 // lang item for CTFE panic support
410 // never inline unless panic_immediate_abort to avoid code
411 // bloat at the call sites as much as possible
412 #[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
415 pub fn begin_panic<M: Any + Send>(msg: M) -> ! {
416 if cfg!(feature = "panic_immediate_abort") {
417 unsafe { intrinsics::abort() }
420 rust_panic_with_hook(&mut PanicPayload::new(msg), None, Location::caller());
422 struct PanicPayload<A> {
426 impl<A: Send + 'static> PanicPayload<A> {
427 fn new(inner: A) -> PanicPayload<A> {
428 PanicPayload { inner: Some(inner) }
432 unsafe impl<A: Send + 'static> BoxMeUp for PanicPayload<A> {
433 fn take_box(&mut self) -> *mut (dyn Any + Send) {
434 // Note that this should be the only allocation performed in this code path. Currently
435 // this means that panic!() on OOM will invoke this code path, but then again we're not
436 // really ready for panic on OOM anyway. If we do start doing this, then we should
437 // propagate this allocation to be performed in the parent of this thread instead of the
438 // thread that's panicking.
439 let data = match self.inner.take() {
440 Some(a) => Box::new(a) as Box<dyn Any + Send>,
441 None => process::abort(),
446 fn get(&mut self) -> &(dyn Any + Send) {
449 None => process::abort(),
455 /// Central point for dispatching panics.
457 /// Executes the primary logic for a panic, including checking for recursive
458 /// panics, panic hooks, and finally dispatching to the panic runtime to either
460 fn rust_panic_with_hook(
461 payload: &mut dyn BoxMeUp,
462 message: Option<&fmt::Arguments<'_>>,
463 location: &Location<'_>,
465 let panics = update_panic_count(1);
467 // If this is the third nested call (e.g., panics == 2, this is 0-indexed),
468 // the panic hook probably triggered the last panic, otherwise the
469 // double-panic check would have aborted the process. In this case abort the
470 // process real quickly as we don't want to try calling it again as it'll
471 // probably just panic again.
473 util::dumb_print(format_args!(
474 "thread panicked while processing \
477 unsafe { intrinsics::abort() }
481 let mut info = PanicInfo::internal_constructor(message, location);
484 // Some platforms (like wasm) know that printing to stderr won't ever actually
485 // print anything, and if that's the case we can skip the default
486 // hook. Since string formatting happens lazily when calling `payload`
487 // methods, this means we avoid formatting the string at all!
488 // (The panic runtime might still call `payload.take_box()` though and trigger
490 Hook::Default if panic_output().is_none() => {}
492 info.set_payload(payload.get());
495 Hook::Custom(ptr) => {
496 info.set_payload(payload.get());
500 HOOK_LOCK.read_unlock();
504 // If a thread panics while it's already unwinding then we
505 // have limited options. Currently our preference is to
506 // just abort. In the future we may consider resuming
507 // unwinding or otherwise exiting the thread cleanly.
508 util::dumb_print(format_args!(
509 "thread panicked while panicking. \
512 unsafe { intrinsics::abort() }
518 /// This is the entry point for `resume_unwind`.
519 /// It just forwards the payload to the panic runtime.
520 pub fn rust_panic_without_hook(payload: Box<dyn Any + Send>) -> ! {
521 update_panic_count(1);
523 struct RewrapBox(Box<dyn Any + Send>);
525 unsafe impl BoxMeUp for RewrapBox {
526 fn take_box(&mut self) -> *mut (dyn Any + Send) {
527 Box::into_raw(mem::replace(&mut self.0, Box::new(())))
530 fn get(&mut self) -> &(dyn Any + Send) {
535 rust_panic(&mut RewrapBox(payload))
538 /// An unmangled function (through `rustc_std_internal_symbol`) on which to slap
541 #[cfg_attr(not(test), rustc_std_internal_symbol)]
542 fn rust_panic(mut msg: &mut dyn BoxMeUp) -> ! {
544 let obj = &mut msg as *mut &mut dyn BoxMeUp;
545 __rust_start_panic(obj as usize)
547 rtabort!("failed to initiate panic, error {}", code)