Launch a non-unwinding panic for misaligned pointer deref

This commit is contained in:
Ben Kimock 2023-06-13 18:25:15 -04:00
parent 99b334696f
commit 7a2490eba3
2 changed files with 7 additions and 8 deletions

View File

@ -9,7 +9,6 @@ use rustc_middle::mir::{
}; };
use rustc_middle::ty::{Ty, TyCtxt, TypeAndMut}; use rustc_middle::ty::{Ty, TyCtxt, TypeAndMut};
use rustc_session::Session; use rustc_session::Session;
use rustc_target::spec::PanicStrategy;
pub struct CheckAlignment; pub struct CheckAlignment;
@ -237,11 +236,10 @@ fn insert_alignment_check<'tcx>(
required: Operand::Copy(alignment), required: Operand::Copy(alignment),
found: Operand::Copy(addr), found: Operand::Copy(addr),
}), }),
unwind: if tcx.sess.panic_strategy() == PanicStrategy::Unwind { // The panic symbol that this calls is #[rustc_nounwind]. We never want to insert an
UnwindAction::Terminate // unwind into unsafe code, because unwinding could make a failing UB check turn into
} else { // much worse UB when we start unwinding.
UnwindAction::Unreachable unwind: UnwindAction::Unreachable,
},
}, },
}); });
} }

View File

@ -166,14 +166,15 @@ fn panic_bounds_check(index: usize, len: usize) -> ! {
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))] #[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
#[track_caller] #[track_caller]
#[lang = "panic_misaligned_pointer_dereference"] // needed by codegen for panic on misaligned pointer deref #[lang = "panic_misaligned_pointer_dereference"] // needed by codegen for panic on misaligned pointer deref
#[rustc_nounwind] // `CheckAlignment` MIR pass requires this function to never unwind
fn panic_misaligned_pointer_dereference(required: usize, found: usize) -> ! { fn panic_misaligned_pointer_dereference(required: usize, found: usize) -> ! {
if cfg!(feature = "panic_immediate_abort") { if cfg!(feature = "panic_immediate_abort") {
super::intrinsics::abort() super::intrinsics::abort()
} }
panic!( panic_nounwind_fmt(format_args!(
"misaligned pointer dereference: address must be a multiple of {required:#x} but is {found:#x}" "misaligned pointer dereference: address must be a multiple of {required:#x} but is {found:#x}"
) ))
} }
/// Panic because we cannot unwind out of a function. /// Panic because we cannot unwind out of a function.