Skip to content

Commit 923d912

Browse files
committed
finally we can actually have adjacent allocations :)
1 parent b479f09 commit 923d912

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

src/intptrcast.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,9 @@ impl<'mir, 'tcx> GlobalStateInner {
191191
slack,
192192
);
193193

194-
// Remember next base address. Leave a gap of at least 1 to avoid two zero-sized allocations
195-
// having the same base address, and to avoid ambiguous provenance for the address between two
196-
// allocations (also see https://github.com/rust-lang/unsafe-code-guidelines/issues/313).
197-
let size_plus_1 = size.bytes().checked_add(1).unwrap();
198-
global_state.next_base_addr = base_addr.checked_add(size_plus_1).unwrap();
194+
// Remember next base address. We *do* allow allocations to touch each other,
195+
// and ZST allocations to have the same address.
196+
global_state.next_base_addr = base_addr.checked_add(size.bytes()).unwrap();
199197
// Given that `next_base_addr` increases in each allocation, pushing the
200198
// corresponding tuple keeps `int_to_ptr_map` sorted
201199
global_state.int_to_ptr_map.push((base_addr, alloc_id));

tests/pass/adjacent-allocs.rs

+32
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,35 @@
11
// compile-flags: -Zmiri-permissive-provenance
22

3+
fn ensure_allocs_can_be_adjacent() {
4+
for _ in 0..512 {
5+
let n = 0u64;
6+
let ptr: *const u64 = &n;
7+
let ptr2 = {
8+
let m = 0u64;
9+
&m as *const u64
10+
};
11+
if ptr.wrapping_add(1) == ptr2 {
12+
return;
13+
}
14+
}
15+
panic!("never saw adjacent stack variables?");
16+
}
17+
18+
fn ensure_zst_allocs_can_be_adjacent() {
19+
for _ in 0..512 {
20+
let n = ();
21+
let ptr: *const () = &n;
22+
let ptr2 = {
23+
let m = ();
24+
&m as *const ()
25+
};
26+
if ptr == ptr2 {
27+
return;
28+
}
29+
}
30+
panic!("never saw adjacent zero-sized stack variables?");
31+
}
32+
333
fn test1() {
434
// The slack between allocations is random.
535
// Loop a few times to hit the zero-slack case.
@@ -42,6 +72,8 @@ fn test2() {
4272
}
4373

4474
fn main() {
75+
ensure_allocs_can_be_adjacent();
76+
ensure_zst_allocs_can_be_adjacent();
4577
test1();
4678
test2();
4779
}

0 commit comments

Comments
 (0)