Skip to content

Commit 0c2c243

Browse files
committed
Auto merge of rust-lang#112599 - saethlin:cleaner-panics, r=thomcc
Launch a non-unwinding panic for misaligned pointer deref This panic already never unwinds, but that's only because it always hits the unwind guard that's created by our `UnwindAction::Terminate`. Hitting the unwind guard generates a huge double-panic backtrace. Now we generate a normal-looking panic message when this check is hit. r? `@thomcc`
2 parents ed7281e + 7a2490e commit 0c2c243

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

compiler/rustc_mir_transform/src/check_alignment.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use rustc_middle::mir::{
99
};
1010
use rustc_middle::ty::{Ty, TyCtxt, TypeAndMut};
1111
use rustc_session::Session;
12-
use rustc_target::spec::PanicStrategy;
1312

1413
pub struct CheckAlignment;
1514

@@ -241,11 +240,10 @@ fn insert_alignment_check<'tcx>(
241240
required: Operand::Copy(alignment),
242241
found: Operand::Copy(addr),
243242
}),
244-
unwind: if tcx.sess.panic_strategy() == PanicStrategy::Unwind {
245-
UnwindAction::Terminate
246-
} else {
247-
UnwindAction::Unreachable
248-
},
243+
// The panic symbol that this calls is #[rustc_nounwind]. We never want to insert an
244+
// unwind into unsafe code, because unwinding could make a failing UB check turn into
245+
// much worse UB when we start unwinding.
246+
unwind: UnwindAction::Unreachable,
249247
},
250248
});
251249
}

library/core/src/panicking.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,15 @@ fn panic_bounds_check(index: usize, len: usize) -> ! {
166166
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
167167
#[track_caller]
168168
#[lang = "panic_misaligned_pointer_dereference"] // needed by codegen for panic on misaligned pointer deref
169+
#[rustc_nounwind] // `CheckAlignment` MIR pass requires this function to never unwind
169170
fn panic_misaligned_pointer_dereference(required: usize, found: usize) -> ! {
170171
if cfg!(feature = "panic_immediate_abort") {
171172
super::intrinsics::abort()
172173
}
173174

174-
panic!(
175+
panic_nounwind_fmt(format_args!(
175176
"misaligned pointer dereference: address must be a multiple of {required:#x} but is {found:#x}"
176-
)
177+
))
177178
}
178179

179180
/// Panic because we cannot unwind out of a function.

0 commit comments

Comments
 (0)