Skip to content

Commit

Permalink
Auto merge of #809 - RalfJung:intptrcast, r=RalfJung
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 + 53c72ba commit 9dd1ff5
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 25 deletions.
5 changes: 3 additions & 2 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,6 +38,9 @@ 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
# Test cargo integration
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
16 changes: 14 additions & 2 deletions src/shims/foreign_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
Ok(Some(this.load_mir(instance.def)?))
}

/// Returns the minimum alignment for the target architecture.
fn min_align(&self) -> Align {
let this = self.eval_context_ref();
// List taken from `libstd/sys_common/alloc.rs`.
let min_align = match this.tcx.tcx.sess.target.target.arch.as_str() {
"x86" | "arm" | "mips" | "powerpc" | "powerpc64" | "asmjs" | "wasm32" => 8,
"x86_64" | "aarch64" | "mips64" | "s390x" | "sparc64" => 16,
arch => bug!("Unsupported target architecture: {}", arch),
};
Align::from_bytes(min_align).unwrap()
}

fn malloc(
&mut self,
size: u64,
Expand All @@ -61,7 +73,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
if size == 0 {
Scalar::from_int(0, this.pointer_size())
} else {
let align = this.tcx.data_layout.pointer_align.abi;
let align = this.min_align();
let ptr = this.memory_mut().allocate(Size::from_bytes(size), align, MiriMemoryKind::C.into());
if zero_init {
// We just allocated this, the access cannot fail
Expand Down Expand Up @@ -94,7 +106,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
new_size: u64,
) -> InterpResult<'tcx, Scalar<Tag>> {
let this = self.eval_context_mut();
let align = this.tcx.data_layout.pointer_align.abi;
let align = this.min_align();
if old_ptr.is_null_ptr(this) {
if new_size == 0 {
Ok(Scalar::from_int(0, this.pointer_size()))
Expand Down
54 changes: 33 additions & 21 deletions tests/run-pass/heap_allocator.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,52 @@
// compile-flags: -Zmiri-seed=
#![feature(allocator_api)]

use std::ptr::NonNull;
use std::alloc::{Global, Alloc, Layout, System};
use std::slice;

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

let p1 = allocator.alloc_zeroed(layout).unwrap();
for _ in 0..32 {
let a = allocator.alloc(layout).unwrap();
assert_eq!(a.as_ptr() as usize % align, 0, "pointer is incorrectly aligned");
allocator.dealloc(a, layout);
}

let p1 = allocator.alloc_zeroed(layout).unwrap();
assert_eq!(p1.as_ptr() as usize % align, 0, "pointer is incorrectly aligned");

let p2 = allocator.realloc(p1, Layout::from_size_align(20, 4).unwrap(), 40).unwrap();
let slice = slice::from_raw_parts(p2.as_ptr(), 20);
assert_eq!(&slice, &[0_u8; 20]);
let p2 = allocator.realloc(p1, layout, 40).unwrap();
let layout = Layout::from_size_align(40, align).unwrap();
assert_eq!(p2.as_ptr() as usize % align, 0, "pointer is incorrectly aligned");
let slice = slice::from_raw_parts(p2.as_ptr(), 20);
assert_eq!(&slice, &[0_u8; 20]);

// old size == new size
let p3 = allocator.realloc(p2, Layout::from_size_align(40, 4).unwrap(), 40).unwrap();
let slice = slice::from_raw_parts(p3.as_ptr(), 20);
assert_eq!(&slice, &[0_u8; 20]);
// old size == new size
let p3 = allocator.realloc(p2, layout, 40).unwrap();
assert_eq!(p3.as_ptr() as usize % align, 0, "pointer is incorrectly aligned");
let slice = slice::from_raw_parts(p3.as_ptr(), 20);
assert_eq!(&slice, &[0_u8; 20]);

// old size > new size
let p4 = allocator.realloc(p3, Layout::from_size_align(40, 4).unwrap(), 10).unwrap();
let slice = slice::from_raw_parts(p4.as_ptr(), 10);
assert_eq!(&slice, &[0_u8; 10]);
// old size > new size
let p4 = allocator.realloc(p3, layout, 10).unwrap();
let layout = Layout::from_size_align(10, align).unwrap();
assert_eq!(p4.as_ptr() as usize % align, 0, "pointer is incorrectly aligned");
let slice = slice::from_raw_parts(p4.as_ptr(), 10);
assert_eq!(&slice, &[0_u8; 10]);

allocator.dealloc(p4, Layout::from_size_align(10, 4).unwrap());
allocator.dealloc(p4, layout);
}
} }

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

let iterations = 32;
unsafe {
let pointers: Vec<_> = (0..iterations).map(|_| {
allocator.alloc(Layout::from_size_align(size, align).unwrap()).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 9dd1ff5

Please sign in to comment.