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