Skip to content

Commit e7c6db7

Browse files
committed
fix handling of alignment for dyn-sized places
1 parent 124fb14 commit e7c6db7

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

compiler/rustc_const_eval/src/interpret/place.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,8 @@ where
328328
};
329329

330330
let mplace = MemPlace { ptr: ptr.to_pointer(self)?, meta };
331-
// When deref'ing a pointer, the *static* alignment given by the type is what matters.
331+
// `ref_to_mplace` is called on raw pointers even if they don't actually get dereferenced;
332+
// we hence can't call `size_and_align_of` since that asserts more validity than we want.
332333
let align = layout.align.abi;
333334
Ok(MPlaceTy { mplace, layout, align })
334335
}
@@ -379,11 +380,12 @@ where
379380

380381
/// Check if this mplace is dereferenceable and sufficiently aligned.
381382
pub fn check_mplace(&self, mplace: MPlaceTy<'tcx, M::Provenance>) -> InterpResult<'tcx> {
382-
let (size, align) = self
383+
let (size, _align) = self
383384
.size_and_align_of_mplace(&mplace)?
384385
.unwrap_or((mplace.layout.size, mplace.layout.align.abi));
385-
assert!(mplace.align <= align, "dynamic alignment less strict than static one?");
386-
let align = if M::enforce_alignment(self).should_check() { align } else { Align::ONE };
386+
// Due to packed places, only `mplace.align` matters.
387+
let align =
388+
if M::enforce_alignment(self).should_check() { mplace.align } else { Align::ONE };
387389
self.check_ptr_access_align(mplace.ptr, size, align, CheckInAllocMsg::DerefTest)?;
388390
Ok(())
389391
}

src/tools/miri/tests/fail/unaligned_pointers/dyn_alignment.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
// should find the bug even without validation and stacked borrows, but gets masked by optimizations
2-
//@compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows -Zmir-opt-level=0 -Cdebug-assertions=no
1+
// should find the bug even without, but gets masked by optimizations
2+
//@compile-flags: -Zmiri-disable-stacked-borrows -Zmir-opt-level=0 -Cdebug-assertions=no
3+
//@normalize-stderr-test: "but found [0-9]+" -> "but found $$ALIGN"
34

45
#[repr(align(256))]
56
#[derive(Debug)]
@@ -19,6 +20,6 @@ fn main() {
1920
(&mut ptr as *mut _ as *mut *const u8).write(&buf as *const _ as *const u8);
2021
}
2122
// Re-borrow that. This should be UB.
22-
let _ptr = &*ptr; //~ERROR: alignment 256 is required
23+
let _ptr = &*ptr; //~ERROR: required 256 byte alignment
2324
}
2425
}

src/tools/miri/tests/fail/unaligned_pointers/dyn_alignment.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: Undefined Behavior: accessing memory with alignment ALIGN, but alignment ALIGN is required
1+
error: Undefined Behavior: constructing invalid value: encountered an unaligned reference (required 256 byte alignment but found $ALIGN)
22
--> $DIR/dyn_alignment.rs:LL:CC
33
|
44
LL | let _ptr = &*ptr;
5-
| ^^^^^ accessing memory with alignment ALIGN, but alignment ALIGN is required
5+
| ^^^^^ constructing invalid value: encountered an unaligned reference (required 256 byte alignment but found $ALIGN)
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

0 commit comments

Comments
 (0)