Skip to content

Commit 57dd8ac

Browse files
committed
Restore legacy provenance behavior
1 parent 66e995f commit 57dd8ac

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

src/intptrcast.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,18 @@ impl<'mir, 'tcx> GlobalStateInner {
122122

123123
// Special-case NULL, it is always an invalid pointer.
124124
if addr == 0 || global_state.provenance_mode == ProvenanceMode::Strict {
125-
return Pointer::new(None, Size::from_bytes(addr));
125+
Pointer::new(None, Size::from_bytes(addr))
126+
} else if global_state.provenance_mode == ProvenanceMode::Legacy {
127+
let alloc_id = Self::alloc_id_from_addr(ecx, addr);
128+
129+
Pointer::new(
130+
alloc_id
131+
.map(|alloc_id| Tag::Concrete(ConcreteTag { alloc_id, sb: SbTag::Untagged })),
132+
Size::from_bytes(addr),
133+
)
134+
} else {
135+
Pointer::new(Some(Tag::Wildcard), Size::from_bytes(addr))
126136
}
127-
128-
Pointer::new(Some(Tag::Wildcard), Size::from_bytes(addr))
129137
}
130138

131139
fn alloc_base_addr(ecx: &MiriEvalContext<'mir, 'tcx>, alloc_id: AllocId) -> u64 {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// compile-flags: -Zmiri-disable-stacked-borrows
2+
#![feature(strict_provenance)]
3+
4+
use std::ptr;
5+
6+
// Make sure that with legacy provenance, the allocation id of
7+
// a casted pointer is determined at cast-time
8+
fn main() {
9+
let x: i32 = 0;
10+
let y: i32 = 1;
11+
12+
let x_ptr = &x as *const i32;
13+
let y_ptr = &y as *const i32;
14+
15+
let x_usize = x_ptr.expose_addr();
16+
let y_usize = y_ptr.expose_addr();
17+
18+
let ptr = ptr::from_exposed_addr::<i32>(y_usize);
19+
let ptr = ptr.with_addr(x_usize);
20+
assert_eq!(unsafe { *ptr }, 0); //~ ERROR pointer to 4 bytes starting at offset -20 is out-of-bounds
21+
}

0 commit comments

Comments
 (0)