]> git.lizzy.rs Git - rust.git/blob - src/libstd/panicking.rs
b02cedd5da5bf321d088329658fcd53afc695a53
[rust.git] / src / libstd / 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 use core::panic::{BoxMeUp, Location, PanicInfo};
11
12 use crate::any::Any;
13 use crate::fmt;
14 use crate::intrinsics;
15 use crate::mem::{self, ManuallyDrop, MaybeUninit};
16 use crate::process;
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};
22 use crate::thread;
23
24 #[cfg(not(test))]
25 use crate::io::set_panic;
26 // make sure to use the stderr output configured
27 // by libtest in the real copy of std
28 #[cfg(test)]
29 use realstd::io::set_panic;
30
31 // This must be kept in sync with the implementations in libpanic_unwind.
32 //
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.
36 //
37 // Essentially this must be the type of `imp::Payload` in libpanic_unwind.
38 cfg_if::cfg_if! {
39     if #[cfg(not(feature = "panic_unwind"))] {
40         type Payload = ();
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];
51     } else {
52         type Payload = *mut u8;
53     }
54 }
55
56 // Binary interface to the panic runtime that the standard library depends on.
57 //
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
62 // to them.
63 //
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)]
67 extern "C" {
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);
71
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`.
75     #[unwind(allowed)]
76     fn __rust_start_panic(payload: usize) -> u32;
77 }
78
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.
82 #[cfg(not(test))]
83 #[rustc_std_internal_symbol]
84 extern "C" fn __rust_drop_panic() -> ! {
85     rtabort!("Rust panics must be rethrown");
86 }
87
88 #[derive(Copy, Clone)]
89 enum Hook {
90     Default,
91     Custom(*mut (dyn Fn(&PanicInfo<'_>) + 'static + Sync + Send)),
92 }
93
94 static HOOK_LOCK: RWLock = RWLock::new();
95 static mut HOOK: Hook = Hook::Default;
96
97 /// Registers a custom panic hook, replacing any that was previously registered.
98 ///
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.
104 ///
105 /// [`take_hook`]: ./fn.take_hook.html
106 ///
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.
110 ///
111 /// The panic hook is a global resource.
112 ///
113 /// # Panics
114 ///
115 /// Panics if called from a panicking thread.
116 ///
117 /// # Examples
118 ///
119 /// The following will print "Custom panic hook":
120 ///
121 /// ```should_panic
122 /// use std::panic;
123 ///
124 /// panic::set_hook(Box::new(|_| {
125 ///     println!("Custom panic hook");
126 /// }));
127 ///
128 /// panic!("Normal panic");
129 /// ```
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");
134     }
135
136     unsafe {
137         HOOK_LOCK.write();
138         let old_hook = HOOK;
139         HOOK = Hook::Custom(Box::into_raw(hook));
140         HOOK_LOCK.write_unlock();
141
142         if let Hook::Custom(ptr) = old_hook {
143             #[allow(unused_must_use)]
144             {
145                 Box::from_raw(ptr);
146             }
147         }
148     }
149 }
150
151 /// Unregisters the current panic hook, returning it.
152 ///
153 /// *See also the function [`set_hook`].*
154 ///
155 /// [`set_hook`]: ./fn.set_hook.html
156 ///
157 /// If no custom hook is registered, the default hook will be returned.
158 ///
159 /// # Panics
160 ///
161 /// Panics if called from a panicking thread.
162 ///
163 /// # Examples
164 ///
165 /// The following will print "Normal panic":
166 ///
167 /// ```should_panic
168 /// use std::panic;
169 ///
170 /// panic::set_hook(Box::new(|_| {
171 ///     println!("Custom panic hook");
172 /// }));
173 ///
174 /// let _ = panic::take_hook();
175 ///
176 /// panic!("Normal panic");
177 /// ```
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");
182     }
183
184     unsafe {
185         HOOK_LOCK.write();
186         let hook = HOOK;
187         HOOK = Hook::Default;
188         HOOK_LOCK.write_unlock();
189
190         match hook {
191             Hook::Default => Box::new(default_hook),
192             Hook::Custom(ptr) => Box::from_raw(ptr),
193         }
194     }
195 }
196
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)
202     } else {
203         backtrace::rust_backtrace_env()
204     };
205
206     // The current implementation always returns `Some`.
207     let location = info.location().unwrap();
208
209     let msg = match info.payload().downcast_ref::<&'static str>() {
210         Some(s) => *s,
211         None => match info.payload().downcast_ref::<String>() {
212             Some(s) => &s[..],
213             None => "Box<Any>",
214         },
215     };
216     let thread = thread_info::current_thread();
217     let name = thread.as_ref().and_then(|t| t.name()).unwrap_or("<unnamed>");
218
219     let write = |err: &mut dyn crate::io::Write| {
220         let _ = writeln!(err, "thread '{}' panicked at '{}', {}", name, msg, location);
221
222         static FIRST_PANIC: AtomicBool = AtomicBool::new(true);
223
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) {
229                     let _ = writeln!(
230                         err,
231                         "note: run with `RUST_BACKTRACE=1` \
232                                            environment variable to display a backtrace"
233                     );
234                 }
235             }
236         }
237     };
238
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>`.
242         write(&mut local);
243         set_panic(Some(local));
244     } else if let Some(mut out) = panic_output() {
245         write(&mut out);
246     }
247 }
248
249 #[cfg(not(test))]
250 #[doc(hidden)]
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) }
255
256     PANIC_COUNT.with(|c| {
257         let next = (c.get() as isize + amt) as usize;
258         c.set(next);
259         next
260     })
261 }
262
263 #[cfg(test)]
264 pub use realstd::rt::update_panic_count;
265
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>> {
268     union Data<F, R> {
269         f: ManuallyDrop<F>,
270         r: ManuallyDrop<R>,
271     }
272
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
276     // using a union.
277     //
278     // We go through a transition where:
279     //
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:
289     //
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.
295     //
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) };
299
300     let mut payload: MaybeUninit<Payload> = MaybeUninit::uninit();
301
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))
306     } else {
307         Err(cleanup(payload.assume_init()))
308     };
309
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).
314     #[cold]
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);
318         obj
319     }
320
321     fn do_call<F: FnOnce() -> R, R>(data: *mut u8) {
322         unsafe {
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());
327         }
328     }
329 }
330
331 /// Determines whether the current thread is unwinding because of panic.
332 pub fn panicking() -> bool {
333     update_panic_count(0) != 0
334 }
335
336 /// The entry point for panicking with a formatted message.
337 ///
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")]
343 #[cold]
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() }
352     }
353
354     let info = PanicInfo::internal_constructor(Some(msg), Location::caller());
355     begin_panic_handler(&info)
356 }
357
358 /// Entry point of panics from the libcore crate (`panic_impl` lang item).
359 #[cfg_attr(not(test), panic_handler)]
360 #[unwind(allowed)]
361 pub fn begin_panic_handler(info: &PanicInfo<'_>) -> ! {
362     struct PanicPayload<'a> {
363         inner: &'a fmt::Arguments<'a>,
364         string: Option<String>,
365     }
366
367     impl<'a> PanicPayload<'a> {
368         fn new(inner: &'a fmt::Arguments<'a>) -> PanicPayload<'a> {
369             PanicPayload { inner, string: None }
370         }
371
372         fn fill(&mut self) -> &mut String {
373             use crate::fmt::Write;
374
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));
380                 s
381             })
382         }
383     }
384
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))
392         }
393
394         fn get(&mut self) -> &(dyn Any + Send) {
395             self.fill()
396         }
397     }
398
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);
402 }
403
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))]
413 #[cold]
414 #[track_caller]
415 pub fn begin_panic<M: Any + Send>(msg: M) -> ! {
416     if cfg!(feature = "panic_immediate_abort") {
417         unsafe { intrinsics::abort() }
418     }
419
420     rust_panic_with_hook(&mut PanicPayload::new(msg), None, Location::caller());
421
422     struct PanicPayload<A> {
423         inner: Option<A>,
424     }
425
426     impl<A: Send + 'static> PanicPayload<A> {
427         fn new(inner: A) -> PanicPayload<A> {
428             PanicPayload { inner: Some(inner) }
429         }
430     }
431
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(),
442             };
443             Box::into_raw(data)
444         }
445
446         fn get(&mut self) -> &(dyn Any + Send) {
447             match self.inner {
448                 Some(ref a) => a,
449                 None => process::abort(),
450             }
451         }
452     }
453 }
454
455 /// Central point for dispatching panics.
456 ///
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
459 /// abort or unwind.
460 fn rust_panic_with_hook(
461     payload: &mut dyn BoxMeUp,
462     message: Option<&fmt::Arguments<'_>>,
463     location: &Location<'_>,
464 ) -> ! {
465     let panics = update_panic_count(1);
466
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.
472     if panics > 2 {
473         util::dumb_print(format_args!(
474             "thread panicked while processing \
475                                        panic. aborting.\n"
476         ));
477         unsafe { intrinsics::abort() }
478     }
479
480     unsafe {
481         let mut info = PanicInfo::internal_constructor(message, location);
482         HOOK_LOCK.read();
483         match HOOK {
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
489             // formatting.)
490             Hook::Default if panic_output().is_none() => {}
491             Hook::Default => {
492                 info.set_payload(payload.get());
493                 default_hook(&info);
494             }
495             Hook::Custom(ptr) => {
496                 info.set_payload(payload.get());
497                 (*ptr)(&info);
498             }
499         };
500         HOOK_LOCK.read_unlock();
501     }
502
503     if panics > 1 {
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. \
510                                        aborting.\n"
511         ));
512         unsafe { intrinsics::abort() }
513     }
514
515     rust_panic(payload)
516 }
517
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);
522
523     struct RewrapBox(Box<dyn Any + Send>);
524
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(())))
528         }
529
530         fn get(&mut self) -> &(dyn Any + Send) {
531             &*self.0
532         }
533     }
534
535     rust_panic(&mut RewrapBox(payload))
536 }
537
538 /// An unmangled function (through `rustc_std_internal_symbol`) on which to slap
539 /// yer breakpoints.
540 #[inline(never)]
541 #[cfg_attr(not(test), rustc_std_internal_symbol)]
542 fn rust_panic(mut msg: &mut dyn BoxMeUp) -> ! {
543     let code = unsafe {
544         let obj = &mut msg as *mut &mut dyn BoxMeUp;
545         __rust_start_panic(obj as usize)
546     };
547     rtabort!("failed to initiate panic, error {}", code)
548 }