Skip to content

Commit

Permalink
make another test more robust against random alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Aug 17, 2020
1 parent db159b8 commit 5b1bc4b
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 30 deletions.
8 changes: 4 additions & 4 deletions tests/compile-fail/unaligned_pointers/alignment.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// error-pattern: but alignment 4 is required

fn main() {
let mut x = [0u8; 20];
let x_ptr: *mut u8 = x.as_mut_ptr();
// At least one of these is definitely unaligned.
// Currently, we guarantee to complain about the first one already (https://github.com/rust-lang/miri/issues/1074).
unsafe {
*(x_ptr as *mut u64) = 42; //~ ERROR accessing memory with alignment 1, but alignment
*(x_ptr.add(1) as *mut u64) = 42;
*(x_ptr as *mut u32) = 42;
*(x_ptr.add(1) as *mut u32) = 42;
}
panic!("unreachable in miri");
}
14 changes: 8 additions & 6 deletions tests/compile-fail/unaligned_pointers/reference_to_packed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ struct Foo {
}

fn main() {
let foo = Foo {
x: 42,
y: 99,
};
let p = unsafe { &foo.x };
let i = *p; //~ ERROR alignment 4 is required
for _ in 0..10 { // Try many times as this might work by chance.
let foo = Foo {
x: 42,
y: 99,
};
let p = unsafe { &foo.x };
let i = *p; //~ ERROR alignment 4 is required
}
}
10 changes: 6 additions & 4 deletions tests/compile-fail/unaligned_pointers/unaligned_ptr1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
// compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows

fn main() {
let x = [2u16, 3, 4]; // Make it big enough so we don't get an out-of-bounds error.
let x = &x[0] as *const _ as *const u32;
// This must fail because alignment is violated: the allocation's base is not sufficiently aligned.
let _x = unsafe { *x }; //~ ERROR memory with alignment 2, but alignment 4 is required
for _ in 0..10 { // Try many times as this might work by chance.
let x = [2u16, 3, 4]; // Make it big enough so we don't get an out-of-bounds error.
let x = &x[0] as *const _ as *const u32;
// This must fail because alignment is violated: the allocation's base is not sufficiently aligned.
let _x = unsafe { *x }; //~ ERROR memory with alignment 2, but alignment 4 is required
}
}
12 changes: 7 additions & 5 deletions tests/compile-fail/unaligned_pointers/unaligned_ptr2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
// compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows

fn main() {
let x = [2u32, 3]; // Make it big enough so we don't get an out-of-bounds error.
let x = (x.as_ptr() as *const u8).wrapping_offset(3) as *const u32;
// This must fail because alignment is violated: the offset is not sufficiently aligned.
// Also make the offset not a power of 2, that used to ICE.
let _x = unsafe { *x }; //~ ERROR memory with alignment 1, but alignment 4 is required
for _ in 0..10 { // Try many times as this might work by chance.
let x = [2u32, 3]; // Make it big enough so we don't get an out-of-bounds error.
let x = (x.as_ptr() as *const u8).wrapping_offset(3) as *const u32;
// This must fail because alignment is violated: the offset is not sufficiently aligned.
// Also make the offset not a power of 2, that used to ICE.
let _x = unsafe { *x }; //~ ERROR memory with alignment 1, but alignment 4 is required
}
}
14 changes: 8 additions & 6 deletions tests/compile-fail/unaligned_pointers/unaligned_ptr3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
// compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows

fn main() {
let x = [2u16, 3, 4, 5]; // Make it big enough so we don't get an out-of-bounds error.
let x = &x[0] as *const _ as *const *const u8; // cast to ptr-to-ptr, so that we load a ptr
// This must fail because alignment is violated. Test specifically for loading pointers,
// which have special code in miri's memory.
let _x = unsafe { *x };
//~^ ERROR memory with alignment 2, but alignment
for _ in 0..10 { // Try many times as this might work by chance.
let x = [2u16, 3, 4, 5]; // Make it big enough so we don't get an out-of-bounds error.
let x = &x[0] as *const _ as *const *const u8; // cast to ptr-to-ptr, so that we load a ptr
// This must fail because alignment is violated. Test specifically for loading pointers,
// which have special code in miri's memory.
let _x = unsafe { *x };
//~^ ERROR but alignment
}
}
12 changes: 7 additions & 5 deletions tests/compile-fail/unaligned_pointers/unaligned_ptr_addr_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
use std::ptr;

fn main() {
let x = [2u16, 3, 4]; // Make it big enough so we don't get an out-of-bounds error.
let x = &x[0] as *const _ as *const u32;
// This must fail because alignment is violated: the allocation's base is not sufficiently aligned.
// The deref is UB even if we just put the result into a raw pointer.
let _x = unsafe { ptr::raw_const!(*x) }; //~ ERROR memory with alignment 2, but alignment 4 is required
for _ in 0..10 { // Try many times as this might work by chance.
let x = [2u16, 3, 4]; // Make it big enough so we don't get an out-of-bounds error.
let x = &x[0] as *const _ as *const u32;
// This must fail because alignment is violated: the allocation's base is not sufficiently aligned.
// The deref is UB even if we just put the result into a raw pointer.
let _x = unsafe { ptr::raw_const!(*x) }; //~ ERROR memory with alignment 2, but alignment 4 is required
}
}

0 comments on commit 5b1bc4b

Please sign in to comment.