Skip to content

Commit

Permalink
test even more size-alignment combinations. found a bug in libstd!
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Jun 30, 2019
1 parent 709b474 commit cb6d4f0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ Definite bugs found:
* [Futures turning a shared reference into a mutable one](https://github.com/rust-lang/rust/pull/56319)
* [`str` turning a shared reference into a mutable one](https://github.com/rust-lang/rust/pull/58200)
* [`rand` performing unaligned reads](https://github.com/rust-random/rand/issues/779)
* [The Unix allocator calling `posix_memalign` in an invalid way](https://github.com/rust-lang/rust/issues/62251)

Violations of Stacked Borrows found that are likely bugs (but Stacked Borrows is currently just an experiment):

Expand Down
4 changes: 4 additions & 0 deletions src/shims/foreign_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,16 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
if !align.is_power_of_two() {
return err!(HeapAllocNonPowerOfTwoAlignment(align));
}
/*
FIXME: This check is disabled because rustc violates it.
See <https://github.com/rust-lang/rust/issues/62251>.
if align < this.pointer_size().bytes() {
return err!(MachineError(format!(
"posix_memalign: alignment must be at least the size of a pointer, but is {}",
align,
)));
}
*/
if size == 0 {
this.write_null(ret.into())?;
} else {
Expand Down
32 changes: 16 additions & 16 deletions tests/run-pass/heap_allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,23 @@ fn check_alloc<T: Alloc>(mut allocator: T) { unsafe {
} }

fn check_overalign_requests<T: Alloc>(mut allocator: T) {
let size = 8;
// Greater than `size`, and also greater than `MIN_ALIGN`.
let align = 32;
for &size in &[2, 8, 64] { // size less than and bigger than alignment
for &align in &[4, 8, 16, 32] { // Be sure to cover less than and bigger than `MIN_ALIGN` for all architectures
let iterations = 32;
unsafe {
let pointers: Vec<_> = (0..iterations).map(|_| {
allocator.alloc(Layout::from_size_align(size, align).unwrap()).unwrap()
}).collect();
for &ptr in &pointers {
assert_eq!((ptr.as_ptr() as usize) % align, 0,
"Got a pointer less aligned than requested")
}

let iterations = 32;
unsafe {
let pointers: Vec<_> = (0..iterations).map(|_| {
allocator.alloc(Layout::from_size_align(size, align).unwrap()).unwrap()
}).collect();
for &ptr in &pointers {
assert_eq!((ptr.as_ptr() as usize) % align, 0,
"Got a pointer less aligned than requested")
}

// Clean up.
for &ptr in &pointers {
allocator.dealloc(ptr, Layout::from_size_align(size, align).unwrap())
// Clean up.
for &ptr in &pointers {
allocator.dealloc(ptr, Layout::from_size_align(size, align).unwrap())
}
}
}
}
}
Expand Down

0 comments on commit cb6d4f0

Please sign in to comment.