Auto merge of #112026 - saethlin:misaligned-addrof, r=pnkfelix
Don't check for misaligned raw pointer derefs inside Rvalue::AddressOf From https://github.com/rust-lang/rust/pull/112026#issuecomment-1565686697: 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 (https://github.com/rust-lang/rust/issues/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
This commit is contained in:
commit
39c03fb652
2 changed files with 23 additions and 0 deletions
|
@ -75,6 +75,14 @@ struct PointerFinder<'tcx, 'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'tcx, 'a> Visitor<'tcx> for PointerFinder<'tcx, 'a> {
|
impl<'tcx, 'a> Visitor<'tcx> for PointerFinder<'tcx, 'a> {
|
||||||
|
fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
|
||||||
|
if let Rvalue::AddressOf(..) = rvalue {
|
||||||
|
// Ignore dereferences inside of an AddressOf
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
self.super_rvalue(rvalue, location);
|
||||||
|
}
|
||||||
|
|
||||||
fn visit_place(&mut self, place: &Place<'tcx>, context: PlaceContext, _location: Location) {
|
fn visit_place(&mut self, place: &Place<'tcx>, context: PlaceContext, _location: Location) {
|
||||||
if let PlaceContext::NonUse(_) = context {
|
if let PlaceContext::NonUse(_) = context {
|
||||||
return;
|
return;
|
||||||
|
|
15
tests/ui/mir/addrof_alignment.rs
Normal file
15
tests/ui/mir/addrof_alignment.rs
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
// run-pass
|
||||||
|
// ignore-wasm32-bare: No panic messages
|
||||||
|
// compile-flags: -C debug-assertions
|
||||||
|
|
||||||
|
struct Misalignment {
|
||||||
|
a: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let items: [Misalignment; 2] = [Misalignment { a: 0 }, Misalignment { a: 1 }];
|
||||||
|
unsafe {
|
||||||
|
let ptr: *const Misalignment = items.as_ptr().cast::<u8>().add(1).cast::<Misalignment>();
|
||||||
|
let _ptr = core::ptr::addr_of!((*ptr).a);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue