Skip to content

Commit c6ab275

Browse files
committed
test that we check dynamic actual size of object
1 parent 521e77d commit c6ab275

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// should find the bug even without these
2+
// compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows
3+
4+
struct SliceWithHead(u8, [u8]);
5+
6+
fn main() {
7+
let buf = [0u32; 1];
8+
// We craft a wide pointer `*const SliceWithHead` such that the unsized tail is only partially allocated.
9+
// That should be UB, as the reference is not fully dereferencable.
10+
let ptr: *const SliceWithHead = unsafe { std::mem::transmute((&buf, 4usize)) };
11+
// Re-borrow that. This should be UB.
12+
let _ptr = unsafe { &*ptr }; //~ ERROR pointer must be in-bounds at offset 5
13+
}

tests/compile-fail/unaligned_pointers/dyn_alignment.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fn main() {
1212
// We craft a wide reference `&dyn Debug` with the vtable for `MuchAlign`. That should be UB,
1313
// as the reference is not aligned to its dynamic alignment requirements.
1414
let mut ptr = &MuchAlign as &dyn std::fmt::Debug;
15-
// Overwrite the data part of `ptr`.
15+
// Overwrite the data part of `ptr` so it points to `buf`.
1616
unsafe { (&mut ptr as *mut _ as *mut *const u8).write(&buf as *const _ as *const u8); }
1717
// Re-borrow that. This should be UB.
1818
let _ptr = &*ptr; //~ ERROR accessing memory with alignment 4, but alignment 256 is required

0 commit comments

Comments
 (0)