Skip to content

Commit 2a4c4ad

Browse files
committed
Auto merge of #809 - RalfJung:intptrcast, r=RalfJung
use intptrcast for heap_allocator test; then it should work on Windows
2 parents 72b2e10 + cb6d4f0 commit 2a4c4ad

File tree

5 files changed

+70
-37
lines changed

5 files changed

+70
-37
lines changed

.appveyor.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ install:
2929
- rustc --version
3030

3131
build_script:
32-
- set RUST_TEST_NOCAPTURE=1
33-
- set RUST_BACKTRACE=1
3432
- set RUSTFLAGS=-C debug-assertions
3533
# Build and install miri
3634
- cargo build --release --all-features --all-targets
@@ -40,6 +38,8 @@ build_script:
4038
- set MIRI_SYSROOT=%USERPROFILE%\AppData\Local\rust-lang\miri\cache\HOST
4139

4240
test_script:
41+
- set RUST_TEST_NOCAPTURE=1
42+
- set RUST_BACKTRACE=1
4343
# Test miri
4444
- cargo test --release --all-features
4545
# Test cargo integration

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/intptrcast.rs

+4
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ impl<'mir, 'tcx> GlobalState {
9090
// From next_base_addr + slack, round up to adjust for alignment.
9191
let base_addr = Self::align_addr(global_state.next_base_addr + slack, align.bytes());
9292
entry.insert(base_addr);
93+
trace!(
94+
"Assigning base address {:#x} to allocation {:?} (slack: {}, align: {})",
95+
base_addr, ptr.alloc_id, slack, align.bytes(),
96+
);
9397

9498
// Remember next base address. If this allocation is zero-sized, leave a gap
9599
// of at least 1 to avoid two allocations having the same base address.

src/shims/foreign_items.rs

+18-2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
5151
Ok(Some(this.load_mir(instance.def)?))
5252
}
5353

54+
/// Returns the minimum alignment for the target architecture.
55+
fn min_align(&self) -> Align {
56+
let this = self.eval_context_ref();
57+
// List taken from `libstd/sys_common/alloc.rs`.
58+
let min_align = match this.tcx.tcx.sess.target.target.arch.as_str() {
59+
"x86" | "arm" | "mips" | "powerpc" | "powerpc64" | "asmjs" | "wasm32" => 8,
60+
"x86_64" | "aarch64" | "mips64" | "s390x" | "sparc64" => 16,
61+
arch => bug!("Unsupported target architecture: {}", arch),
62+
};
63+
Align::from_bytes(min_align).unwrap()
64+
}
65+
5466
fn malloc(
5567
&mut self,
5668
size: u64,
@@ -61,7 +73,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
6173
if size == 0 {
6274
Scalar::from_int(0, this.pointer_size())
6375
} else {
64-
let align = this.tcx.data_layout.pointer_align.abi;
76+
let align = this.min_align();
6577
let ptr = this.memory_mut().allocate(Size::from_bytes(size), align, MiriMemoryKind::C.into());
6678
if zero_init {
6779
// We just allocated this, the access cannot fail
@@ -94,7 +106,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
94106
new_size: u64,
95107
) -> InterpResult<'tcx, Scalar<Tag>> {
96108
let this = self.eval_context_mut();
97-
let align = this.tcx.data_layout.pointer_align.abi;
109+
let align = this.min_align();
98110
if old_ptr.is_null_ptr(this) {
99111
if new_size == 0 {
100112
Ok(Scalar::from_int(0, this.pointer_size()))
@@ -191,12 +203,16 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
191203
if !align.is_power_of_two() {
192204
return err!(HeapAllocNonPowerOfTwoAlignment(align));
193205
}
206+
/*
207+
FIXME: This check is disabled because rustc violates it.
208+
See <https://github.com/rust-lang/rust/issues/62251>.
194209
if align < this.pointer_size().bytes() {
195210
return err!(MachineError(format!(
196211
"posix_memalign: alignment must be at least the size of a pointer, but is {}",
197212
align,
198213
)));
199214
}
215+
*/
200216
if size == 0 {
201217
this.write_null(ret.into())?;
202218
} else {

tests/run-pass/heap_allocator.rs

+45-33
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,64 @@
1+
// compile-flags: -Zmiri-seed=
12
#![feature(allocator_api)]
23

34
use std::ptr::NonNull;
45
use std::alloc::{Global, Alloc, Layout, System};
56
use std::slice;
67

78
fn check_alloc<T: Alloc>(mut allocator: T) { unsafe {
8-
let layout = Layout::from_size_align(20, 4).unwrap();
9-
let a = allocator.alloc(layout).unwrap();
10-
allocator.dealloc(a, layout);
9+
for &align in &[4, 8, 16, 32] {
10+
let layout = Layout::from_size_align(20, align).unwrap();
1111

12-
let p1 = allocator.alloc_zeroed(layout).unwrap();
12+
for _ in 0..32 {
13+
let a = allocator.alloc(layout).unwrap();
14+
assert_eq!(a.as_ptr() as usize % align, 0, "pointer is incorrectly aligned");
15+
allocator.dealloc(a, layout);
16+
}
17+
18+
let p1 = allocator.alloc_zeroed(layout).unwrap();
19+
assert_eq!(p1.as_ptr() as usize % align, 0, "pointer is incorrectly aligned");
1320

14-
let p2 = allocator.realloc(p1, Layout::from_size_align(20, 4).unwrap(), 40).unwrap();
15-
let slice = slice::from_raw_parts(p2.as_ptr(), 20);
16-
assert_eq!(&slice, &[0_u8; 20]);
21+
let p2 = allocator.realloc(p1, layout, 40).unwrap();
22+
let layout = Layout::from_size_align(40, align).unwrap();
23+
assert_eq!(p2.as_ptr() as usize % align, 0, "pointer is incorrectly aligned");
24+
let slice = slice::from_raw_parts(p2.as_ptr(), 20);
25+
assert_eq!(&slice, &[0_u8; 20]);
1726

18-
// old size == new size
19-
let p3 = allocator.realloc(p2, Layout::from_size_align(40, 4).unwrap(), 40).unwrap();
20-
let slice = slice::from_raw_parts(p3.as_ptr(), 20);
21-
assert_eq!(&slice, &[0_u8; 20]);
27+
// old size == new size
28+
let p3 = allocator.realloc(p2, layout, 40).unwrap();
29+
assert_eq!(p3.as_ptr() as usize % align, 0, "pointer is incorrectly aligned");
30+
let slice = slice::from_raw_parts(p3.as_ptr(), 20);
31+
assert_eq!(&slice, &[0_u8; 20]);
2232

23-
// old size > new size
24-
let p4 = allocator.realloc(p3, Layout::from_size_align(40, 4).unwrap(), 10).unwrap();
25-
let slice = slice::from_raw_parts(p4.as_ptr(), 10);
26-
assert_eq!(&slice, &[0_u8; 10]);
33+
// old size > new size
34+
let p4 = allocator.realloc(p3, layout, 10).unwrap();
35+
let layout = Layout::from_size_align(10, align).unwrap();
36+
assert_eq!(p4.as_ptr() as usize % align, 0, "pointer is incorrectly aligned");
37+
let slice = slice::from_raw_parts(p4.as_ptr(), 10);
38+
assert_eq!(&slice, &[0_u8; 10]);
2739

28-
allocator.dealloc(p4, Layout::from_size_align(10, 4).unwrap());
40+
allocator.dealloc(p4, layout);
41+
}
2942
} }
3043

3144
fn check_overalign_requests<T: Alloc>(mut allocator: T) {
32-
let size = 8;
33-
// Greater than `size`.
34-
let align = 16;
35-
// Miri is deterministic; no need to try many times.
36-
let iterations = 1;
37-
unsafe {
38-
let pointers: Vec<_> = (0..iterations).map(|_| {
39-
allocator.alloc(Layout::from_size_align(size, align).unwrap()).unwrap()
40-
}).collect();
41-
for &ptr in &pointers {
42-
assert_eq!((ptr.as_ptr() as usize) % align, 0,
43-
"Got a pointer less aligned than requested")
44-
}
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+
}
4556

46-
// Clean up.
47-
for &ptr in &pointers {
48-
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+
}
4962
}
5063
}
5164
}
@@ -75,7 +88,6 @@ fn box_to_global() {
7588
fn main() {
7689
check_alloc(System);
7790
check_alloc(Global);
78-
#[cfg(not(target_os = "windows"))] // TODO: Inspects allocation base address on Windows; needs intptrcast model
7991
check_overalign_requests(System);
8092
check_overalign_requests(Global);
8193
global_to_box();

0 commit comments

Comments
 (0)