Skip to content

Commit cb6d4f0

Browse files
committed
test even more size-alignment combinations. found a bug in libstd!
1 parent 709b474 commit cb6d4f0

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ Definite bugs found:
333333
* [Futures turning a shared reference into a mutable one](https://github.com/rust-lang/rust/pull/56319)
334334
* [`str` turning a shared reference into a mutable one](https://github.com/rust-lang/rust/pull/58200)
335335
* [`rand` performing unaligned reads](https://github.com/rust-random/rand/issues/779)
336+
* [The Unix allocator calling `posix_memalign` in an invalid way](https://github.com/rust-lang/rust/issues/62251)
336337

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

src/shims/foreign_items.rs

+4
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,16 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
203203
if !align.is_power_of_two() {
204204
return err!(HeapAllocNonPowerOfTwoAlignment(align));
205205
}
206+
/*
207+
FIXME: This check is disabled because rustc violates it.
208+
See <https://github.com/rust-lang/rust/issues/62251>.
206209
if align < this.pointer_size().bytes() {
207210
return err!(MachineError(format!(
208211
"posix_memalign: alignment must be at least the size of a pointer, but is {}",
209212
align,
210213
)));
211214
}
215+
*/
212216
if size == 0 {
213217
this.write_null(ret.into())?;
214218
} else {

tests/run-pass/heap_allocator.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,23 @@ fn check_alloc<T: Alloc>(mut allocator: T) { unsafe {
4242
} }
4343

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

49-
let iterations = 32;
50-
unsafe {
51-
let pointers: Vec<_> = (0..iterations).map(|_| {
52-
allocator.alloc(Layout::from_size_align(size, align).unwrap()).unwrap()
53-
}).collect();
54-
for &ptr in &pointers {
55-
assert_eq!((ptr.as_ptr() as usize) % align, 0,
56-
"Got a pointer less aligned than requested")
57-
}
58-
59-
// Clean up.
60-
for &ptr in &pointers {
61-
allocator.dealloc(ptr, Layout::from_size_align(size, align).unwrap())
57+
// Clean up.
58+
for &ptr in &pointers {
59+
allocator.dealloc(ptr, Layout::from_size_align(size, align).unwrap())
60+
}
61+
}
6262
}
6363
}
6464
}

0 commit comments

Comments
 (0)