Skip to content

Commit 750d750

Browse files
committed
Auto merge of #98112 - saethlin:mir-alignment-checks, r=oli-obk
Insert alignment checks for pointer dereferences when debug assertions are enabled Closes rust-lang/rust#54915 - [x] Jake tells me this sounds like a place to use `MirPatch`, but I can't figure out how to insert a new basic block with a new terminator in the middle of an existing basic block, using `MirPatch`. (if nobody else backs up this point I'm checking this as "not actually a good idea" because the code looks pretty clean to me after rearranging it a bit) - [x] Using `CastKind::PointerExposeAddress` is definitely wrong, we don't want to expose. Calling a function to get the pointer address seems quite excessive. ~I'll see if I can add a new `CastKind`.~ `CastKind::Transmute` to the rescue! - [x] Implement a more helpful panic message like slice bounds checking. r? `@oli-obk`
2 parents e286b4c + c186ebd commit 750d750

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

core/src/panicking.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,20 @@ fn panic_bounds_check(index: usize, len: usize) -> ! {
162162
panic!("index out of bounds: the len is {len} but the index is {index}")
163163
}
164164

165+
#[cold]
166+
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
167+
#[track_caller]
168+
#[cfg_attr(not(bootstrap), lang = "panic_misaligned_pointer_dereference")] // needed by codegen for panic on misaligned pointer deref
169+
fn panic_misaligned_pointer_dereference(required: usize, found: usize) -> ! {
170+
if cfg!(feature = "panic_immediate_abort") {
171+
super::intrinsics::abort()
172+
}
173+
174+
panic!(
175+
"misaligned pointer dereference: address must be a multiple of {required:#x} but is {found:#x}"
176+
)
177+
}
178+
165179
/// Panic because we cannot unwind out of a function.
166180
///
167181
/// This function is called directly by the codegen backend, and must not have

0 commit comments

Comments
 (0)