Skip to content

Commit

Permalink
Auto merge of #809 - RalfJung:intptrcast, r=<try>
Browse files Browse the repository at this point in the history
use intptrcast for heap_allocator test; then it should work on Windows
  • Loading branch information
bors committed Jun 30, 2019
2 parents 72b2e10 + 7d2bfd4 commit a09bafe
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
8 changes: 5 additions & 3 deletions .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ install:
- rustc --version

build_script:
- set RUST_TEST_NOCAPTURE=1
- set RUST_BACKTRACE=1
- set RUSTFLAGS=-C debug-assertions
# Build and install miri
- cargo build --release --all-features --all-targets
Expand All @@ -40,8 +38,12 @@ build_script:
- set MIRI_SYSROOT=%USERPROFILE%\AppData\Local\rust-lang\miri\cache\HOST

test_script:
- set RUST_TEST_NOCAPTURE=1
- set RUST_BACKTRACE=1
- set MIRI_LOG=miri::intptrcast
# Test miri
- cargo test --release --all-features
- cargo test --release --all-features heap
- false
# Test cargo integration
- cd test-cargo-miri
- '"C:\msys64\mingw64\bin\python3.exe" run-test.py'
Expand Down
4 changes: 4 additions & 0 deletions src/intptrcast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ impl<'mir, 'tcx> GlobalState {
// From next_base_addr + slack, round up to adjust for alignment.
let base_addr = Self::align_addr(global_state.next_base_addr + slack, align.bytes());
entry.insert(base_addr);
trace!(
"Assigning base address {:#x} to allocation {:?} (slack: {}, align: {})",
base_addr, ptr.alloc_id, slack, align.bytes(),
);

// Remember next base address. If this allocation is zero-sized, leave a gap
// of at least 1 to avoid two allocations having the same base address.
Expand Down
26 changes: 19 additions & 7 deletions tests/run-pass/heap_allocator.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// compile-flags: -Zmiri-seed=
#![feature(allocator_api)]

use std::ptr::NonNull;
Expand Down Expand Up @@ -28,24 +29,36 @@ fn check_alloc<T: Alloc>(mut allocator: T) { unsafe {
allocator.dealloc(p4, Layout::from_size_align(10, 4).unwrap());
} }

fn check_overalign_requests<T: Alloc>(mut allocator: T) {
unsafe fn align_ptr(ptr: *mut u8, align: usize) -> *mut u8 {
ptr.add(align - (ptr as usize & (align - 1)))
}

fn allocate_with_align<T: Alloc>(mut allocator: T, size: usize, align: usize) -> *mut u8 { unsafe {
let ptr = allocator.alloc(Layout::from_size_align(size + align, 1).unwrap()).unwrap().as_ptr();
eprintln!("{}", format!("Before aligning: {:?}", ptr));
let ptr = align_ptr(ptr, align);
eprintln!("{}", format!("After aligning: {:?}", ptr));
ptr
} }

fn check_overalign_requests<T: Alloc+Copy>(mut allocator: T) {
let size = 8;
// Greater than `size`.
let align = 16;
// Miri is deterministic; no need to try many times.
let iterations = 1;

let iterations = 5;
unsafe {
let pointers: Vec<_> = (0..iterations).map(|_| {
allocator.alloc(Layout::from_size_align(size, align).unwrap()).unwrap()
allocate_with_align(allocator, size, align)
}).collect();
for &ptr in &pointers {
assert_eq!((ptr.as_ptr() as usize) % align, 0,
assert_eq!((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())
allocator.dealloc(NonNull::new(ptr).unwrap(), Layout::from_size_align(size, 1).unwrap())
}
}
}
Expand Down Expand Up @@ -75,7 +88,6 @@ fn box_to_global() {
fn main() {
check_alloc(System);
check_alloc(Global);
#[cfg(not(target_os = "windows"))] // TODO: Inspects allocation base address on Windows; needs intptrcast model
check_overalign_requests(System);
check_overalign_requests(Global);
global_to_box();
Expand Down

0 comments on commit a09bafe

Please sign in to comment.