Skip to content

Commit 802547b

Browse files
authored
Rollup merge of rust-lang#115280 - RalfJung:panic-cleanup-triple-backtrace, r=Amanieu
avoid triple-backtrace due to panic-during-cleanup Supersedes rust-lang#115020 Cc rust-lang#114954 r? ``@Amanieu``
2 parents 3ddf80c + 42779ed commit 802547b

File tree

4 files changed

+77
-18
lines changed

4 files changed

+77
-18
lines changed

alloc/src/alloc.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -408,9 +408,10 @@ pub mod __alloc_error_handler {
408408
if unsafe { __rust_alloc_error_handler_should_panic != 0 } {
409409
panic!("memory allocation of {size} bytes failed")
410410
} else {
411-
core::panicking::panic_nounwind_fmt(format_args!(
412-
"memory allocation of {size} bytes failed"
413-
))
411+
core::panicking::panic_nounwind_fmt(
412+
format_args!("memory allocation of {size} bytes failed"),
413+
/* force_no_backtrace */ false,
414+
)
414415
}
415416
}
416417
}

core/src/panic/panic_info.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub struct PanicInfo<'a> {
2828
message: Option<&'a fmt::Arguments<'a>>,
2929
location: &'a Location<'a>,
3030
can_unwind: bool,
31+
force_no_backtrace: bool,
3132
}
3233

3334
impl<'a> PanicInfo<'a> {
@@ -42,9 +43,10 @@ impl<'a> PanicInfo<'a> {
4243
message: Option<&'a fmt::Arguments<'a>>,
4344
location: &'a Location<'a>,
4445
can_unwind: bool,
46+
force_no_backtrace: bool,
4547
) -> Self {
4648
struct NoPayload;
47-
PanicInfo { location, message, payload: &NoPayload, can_unwind }
49+
PanicInfo { location, message, payload: &NoPayload, can_unwind, force_no_backtrace }
4850
}
4951

5052
#[unstable(
@@ -141,6 +143,17 @@ impl<'a> PanicInfo<'a> {
141143
pub fn can_unwind(&self) -> bool {
142144
self.can_unwind
143145
}
146+
147+
#[unstable(
148+
feature = "panic_internals",
149+
reason = "internal details of the implementation of the `panic!` and related macros",
150+
issue = "none"
151+
)]
152+
#[doc(hidden)]
153+
#[inline]
154+
pub fn force_no_backtrace(&self) -> bool {
155+
self.force_no_backtrace
156+
}
144157
}
145158

146159
#[stable(feature = "panic_hook_display", since = "1.26.0")]

core/src/panicking.rs

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@ pub const fn panic_fmt(fmt: fmt::Arguments<'_>) -> ! {
6161
fn panic_impl(pi: &PanicInfo<'_>) -> !;
6262
}
6363

64-
let pi = PanicInfo::internal_constructor(Some(&fmt), Location::caller(), true);
64+
let pi = PanicInfo::internal_constructor(
65+
Some(&fmt),
66+
Location::caller(),
67+
/* can_unwind */ true,
68+
/* force_no_backtrace */ false,
69+
);
6570

6671
// SAFETY: `panic_impl` is defined in safe Rust code and thus is safe to call.
6772
unsafe { panic_impl(&pi) }
@@ -77,7 +82,7 @@ pub const fn panic_fmt(fmt: fmt::Arguments<'_>) -> ! {
7782
// and unwinds anyway, we will hit the "unwinding out of nounwind function" guard,
7883
// which causes a "panic in a function that cannot unwind".
7984
#[rustc_nounwind]
80-
pub fn panic_nounwind_fmt(fmt: fmt::Arguments<'_>) -> ! {
85+
pub fn panic_nounwind_fmt(fmt: fmt::Arguments<'_>, force_no_backtrace: bool) -> ! {
8186
if cfg!(feature = "panic_immediate_abort") {
8287
super::intrinsics::abort()
8388
}
@@ -90,7 +95,12 @@ pub fn panic_nounwind_fmt(fmt: fmt::Arguments<'_>) -> ! {
9095
}
9196

9297
// PanicInfo with the `can_unwind` flag set to false forces an abort.
93-
let pi = PanicInfo::internal_constructor(Some(&fmt), Location::caller(), false);
98+
let pi = PanicInfo::internal_constructor(
99+
Some(&fmt),
100+
Location::caller(),
101+
/* can_unwind */ false,
102+
force_no_backtrace,
103+
);
94104

95105
// SAFETY: `panic_impl` is defined in safe Rust code and thus is safe to call.
96106
unsafe { panic_impl(&pi) }
@@ -123,7 +133,15 @@ pub const fn panic(expr: &'static str) -> ! {
123133
#[lang = "panic_nounwind"] // needed by codegen for non-unwinding panics
124134
#[rustc_nounwind]
125135
pub fn panic_nounwind(expr: &'static str) -> ! {
126-
panic_nounwind_fmt(fmt::Arguments::new_const(&[expr]));
136+
panic_nounwind_fmt(fmt::Arguments::new_const(&[expr]), /* force_no_backtrace */ false);
137+
}
138+
139+
/// Like `panic_nounwind`, but also inhibits showing a backtrace.
140+
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
141+
#[cfg_attr(feature = "panic_immediate_abort", inline)]
142+
#[rustc_nounwind]
143+
pub fn panic_nounwind_nobacktrace(expr: &'static str) -> ! {
144+
panic_nounwind_fmt(fmt::Arguments::new_const(&[expr]), /* force_no_backtrace */ true);
127145
}
128146

129147
#[inline]
@@ -172,9 +190,12 @@ fn panic_misaligned_pointer_dereference(required: usize, found: usize) -> ! {
172190
super::intrinsics::abort()
173191
}
174192

175-
panic_nounwind_fmt(format_args!(
176-
"misaligned pointer dereference: address must be a multiple of {required:#x} but is {found:#x}"
177-
))
193+
panic_nounwind_fmt(
194+
format_args!(
195+
"misaligned pointer dereference: address must be a multiple of {required:#x} but is {found:#x}"
196+
),
197+
/* force_no_backtrace */ false,
198+
)
178199
}
179200

180201
/// Panic because we cannot unwind out of a function.
@@ -205,7 +226,7 @@ fn panic_cannot_unwind() -> ! {
205226
#[rustc_nounwind]
206227
fn panic_in_cleanup() -> ! {
207228
// Keep the text in sync with `UnwindTerminateReason::as_str` in `rustc_middle`.
208-
panic_nounwind("panic in a destructor during cleanup")
229+
panic_nounwind_nobacktrace("panic in a destructor during cleanup")
209230
}
210231

211232
/// This function is used instead of panic_fmt in const eval.

std/src/panicking.rs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,9 @@ fn default_hook(info: &PanicInfo<'_>) {
246246
pub fn panic_hook_with_disk_dump(info: &PanicInfo<'_>, path: Option<&crate::path::Path>) {
247247
// If this is a double panic, make sure that we print a backtrace
248248
// for this panic. Otherwise only print it if logging is enabled.
249-
let backtrace = if panic_count::get_count() >= 2 {
249+
let backtrace = if info.force_no_backtrace() {
250+
None
251+
} else if panic_count::get_count() >= 2 {
250252
BacktraceStyle::full()
251253
} else {
252254
crate::panic::get_backtrace_style()
@@ -294,7 +296,7 @@ pub fn panic_hook_with_disk_dump(info: &PanicInfo<'_>, path: Option<&crate::path
294296
}
295297
}
296298
}
297-
// If backtraces aren't supported, do nothing.
299+
// If backtraces aren't supported or are forced-off, do nothing.
298300
None => {}
299301
}
300302
};
@@ -615,14 +617,23 @@ pub fn begin_panic_handler(info: &PanicInfo<'_>) -> ! {
615617
let loc = info.location().unwrap(); // The current implementation always returns Some
616618
let msg = info.message().unwrap(); // The current implementation always returns Some
617619
crate::sys_common::backtrace::__rust_end_short_backtrace(move || {
620+
// FIXME: can we just pass `info` along rather than taking it apart here, only to have
621+
// `rust_panic_with_hook` construct a new `PanicInfo`?
618622
if let Some(msg) = msg.as_str() {
619-
rust_panic_with_hook(&mut StrPanicPayload(msg), info.message(), loc, info.can_unwind());
623+
rust_panic_with_hook(
624+
&mut StrPanicPayload(msg),
625+
info.message(),
626+
loc,
627+
info.can_unwind(),
628+
info.force_no_backtrace(),
629+
);
620630
} else {
621631
rust_panic_with_hook(
622632
&mut PanicPayload::new(msg),
623633
info.message(),
624634
loc,
625635
info.can_unwind(),
636+
info.force_no_backtrace(),
626637
);
627638
}
628639
})
@@ -647,7 +658,13 @@ pub const fn begin_panic<M: Any + Send>(msg: M) -> ! {
647658

648659
let loc = Location::caller();
649660
return crate::sys_common::backtrace::__rust_end_short_backtrace(move || {
650-
rust_panic_with_hook(&mut PanicPayload::new(msg), None, loc, true)
661+
rust_panic_with_hook(
662+
&mut PanicPayload::new(msg),
663+
None,
664+
loc,
665+
/* can_unwind */ true,
666+
/* force_no_backtrace */ false,
667+
)
651668
});
652669

653670
struct PanicPayload<A> {
@@ -693,6 +710,7 @@ fn rust_panic_with_hook(
693710
message: Option<&fmt::Arguments<'_>>,
694711
location: &Location<'_>,
695712
can_unwind: bool,
713+
force_no_backtrace: bool,
696714
) -> ! {
697715
let must_abort = panic_count::increase(true);
698716

@@ -707,14 +725,20 @@ fn rust_panic_with_hook(
707725
panic_count::MustAbort::AlwaysAbort => {
708726
// Unfortunately, this does not print a backtrace, because creating
709727
// a `Backtrace` will allocate, which we must to avoid here.
710-
let panicinfo = PanicInfo::internal_constructor(message, location, can_unwind);
728+
let panicinfo = PanicInfo::internal_constructor(
729+
message,
730+
location,
731+
can_unwind,
732+
force_no_backtrace,
733+
);
711734
rtprintpanic!("{panicinfo}\npanicked after panic::always_abort(), aborting.\n");
712735
}
713736
}
714737
crate::sys::abort_internal();
715738
}
716739

717-
let mut info = PanicInfo::internal_constructor(message, location, can_unwind);
740+
let mut info =
741+
PanicInfo::internal_constructor(message, location, can_unwind, force_no_backtrace);
718742
let hook = HOOK.read().unwrap_or_else(PoisonError::into_inner);
719743
match *hook {
720744
// Some platforms (like wasm) know that printing to stderr won't ever actually

0 commit comments

Comments
 (0)