Skip to content

Commit 9dd1ff5

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 + 53c72ba commit 9dd1ff5

File tree

4 files changed

+54
-25
lines changed

4 files changed

+54
-25
lines changed

.appveyor.yml

Lines changed: 3 additions & 2 deletions
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,9 @@ 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
43+
- set MIRI_LOG=miri::intptrcast
4344
# Test miri
4445
- cargo test --release --all-features
4546
# Test cargo integration

src/intptrcast.rs

Lines changed: 4 additions & 0 deletions
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

Lines changed: 14 additions & 2 deletions
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()))

tests/run-pass/heap_allocator.rs

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,52 @@
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) {
3245
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;
46+
// Greater than `size`, and also greater than `MIN_ALIGN`.
47+
let align = 32;
48+
49+
let iterations = 32;
3750
unsafe {
3851
let pointers: Vec<_> = (0..iterations).map(|_| {
3952
allocator.alloc(Layout::from_size_align(size, align).unwrap()).unwrap()
@@ -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)