Skip to content

Commit

Permalink
finally we can actually have adjacent allocations :)
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Jun 27, 2022
1 parent b479f09 commit 923d912
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/intptrcast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,9 @@ impl<'mir, 'tcx> GlobalStateInner {
slack,
);

// Remember next base address. Leave a gap of at least 1 to avoid two zero-sized allocations
// having the same base address, and to avoid ambiguous provenance for the address between two
// allocations (also see https://github.com/rust-lang/unsafe-code-guidelines/issues/313).
let size_plus_1 = size.bytes().checked_add(1).unwrap();
global_state.next_base_addr = base_addr.checked_add(size_plus_1).unwrap();
// Remember next base address. We *do* allow allocations to touch each other,
// and ZST allocations to have the same address.
global_state.next_base_addr = base_addr.checked_add(size.bytes()).unwrap();
// Given that `next_base_addr` increases in each allocation, pushing the
// corresponding tuple keeps `int_to_ptr_map` sorted
global_state.int_to_ptr_map.push((base_addr, alloc_id));
Expand Down
32 changes: 32 additions & 0 deletions tests/pass/adjacent-allocs.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
// compile-flags: -Zmiri-permissive-provenance

fn ensure_allocs_can_be_adjacent() {
for _ in 0..512 {
let n = 0u64;
let ptr: *const u64 = &n;
let ptr2 = {
let m = 0u64;
&m as *const u64
};
if ptr.wrapping_add(1) == ptr2 {
return;
}
}
panic!("never saw adjacent stack variables?");
}

fn ensure_zst_allocs_can_be_adjacent() {
for _ in 0..512 {
let n = ();
let ptr: *const () = &n;
let ptr2 = {
let m = ();
&m as *const ()
};
if ptr == ptr2 {
return;
}
}
panic!("never saw adjacent zero-sized stack variables?");
}

fn test1() {
// The slack between allocations is random.
// Loop a few times to hit the zero-slack case.
Expand Down Expand Up @@ -42,6 +72,8 @@ fn test2() {
}

fn main() {
ensure_allocs_can_be_adjacent();
ensure_zst_allocs_can_be_adjacent();
test1();
test2();
}

0 comments on commit 923d912

Please sign in to comment.