Skip to content

Commit 39c03fb

Browse files
committed
Auto merge of #112026 - saethlin:misaligned-addrof, r=pnkfelix
Don't check for misaligned raw pointer derefs inside Rvalue::AddressOf From #112026 (comment): rustc 1.70 (stable next week) added a Mir pass to add pointer alignment checks in debug mode. Adding these checks caused some crates to break, but that was expected, since they contain broken code (#111487) for tracking that. However, the checks added are slightly more aggressive than they should have been. Specifically, they also check the place in an `addr_of!` expression. Whether lack of alignment there is or isn't UB is unclear. This PR modifies the pass to not affect those cases. I spot checked the crater regressions and the ones I saw were not the case that this PR is modifying. It still seems good to not land anything overaggressive though
2 parents f59d577 + 783b1ce commit 39c03fb

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

compiler/rustc_mir_transform/src/check_alignment.rs

+8
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ struct PointerFinder<'tcx, 'a> {
7575
}
7676

7777
impl<'tcx, 'a> Visitor<'tcx> for PointerFinder<'tcx, 'a> {
78+
fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
79+
if let Rvalue::AddressOf(..) = rvalue {
80+
// Ignore dereferences inside of an AddressOf
81+
return;
82+
}
83+
self.super_rvalue(rvalue, location);
84+
}
85+
7886
fn visit_place(&mut self, place: &Place<'tcx>, context: PlaceContext, _location: Location) {
7987
if let PlaceContext::NonUse(_) = context {
8088
return;

tests/ui/mir/addrof_alignment.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// run-pass
2+
// ignore-wasm32-bare: No panic messages
3+
// compile-flags: -C debug-assertions
4+
5+
struct Misalignment {
6+
a: u32,
7+
}
8+
9+
fn main() {
10+
let items: [Misalignment; 2] = [Misalignment { a: 0 }, Misalignment { a: 1 }];
11+
unsafe {
12+
let ptr: *const Misalignment = items.as_ptr().cast::<u8>().add(1).cast::<Misalignment>();
13+
let _ptr = core::ptr::addr_of!((*ptr).a);
14+
}
15+
}

0 commit comments

Comments
 (0)