Skip to content

Commit ff75dce

Browse files
committed
Address some more review comments
1 parent 9317d32 commit ff75dce

File tree

2 files changed

+34
-22
lines changed

2 files changed

+34
-22
lines changed

src/intptrcast.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl<'mir, 'tcx> GlobalStateInner {
5757

5858
let pos = global_state.int_to_ptr_map.binary_search_by_key(&addr, |(addr, _)| *addr);
5959

60-
match pos {
60+
let alloc_id = match pos {
6161
Ok(pos) => Some(global_state.int_to_ptr_map[pos].1),
6262
Err(0) => None,
6363
Err(pos) => {
@@ -80,14 +80,13 @@ impl<'mir, 'tcx> GlobalStateInner {
8080
None
8181
}
8282
}
83+
}?;
84+
85+
if global_state.permissive_provenance && !global_state.exposed.contains(&alloc_id) {
86+
None
87+
} else {
88+
Some(alloc_id)
8389
}
84-
.and_then(|alloc_id| {
85-
if global_state.permissive_provenance && !global_state.exposed.contains(&alloc_id) {
86-
None
87-
} else {
88-
Some(alloc_id)
89-
}
90-
})
9190
}
9291

9392
pub fn expose_addr(ecx: &MiriEvalContext<'mir, 'tcx>, alloc_id: AllocId) {
@@ -165,7 +164,7 @@ impl<'mir, 'tcx> GlobalStateInner {
165164

166165
/// Convert a relative (tcx) pointer to an absolute address.
167166
pub fn rel_ptr_to_addr(ecx: &MiriEvalContext<'mir, 'tcx>, ptr: Pointer<AllocId>) -> u64 {
168-
let (alloc_id, offset) = ptr.into_parts(); // offset is relative
167+
let (alloc_id, offset) = ptr.into_parts(); // offset is relative (AllocId provenance)
169168
let base_addr = GlobalStateInner::alloc_base_addr(ecx, alloc_id);
170169

171170
// Add offset with the right kind of pointer-overflowing arithmetic.
@@ -182,10 +181,7 @@ impl<'mir, 'tcx> GlobalStateInner {
182181
let alloc_id = if let Tag::Concrete(concrete) = tag {
183182
concrete.alloc_id
184183
} else {
185-
match GlobalStateInner::alloc_id_from_addr(ecx, addr.bytes()) {
186-
Some(alloc_id) => alloc_id,
187-
None => return None,
188-
}
184+
GlobalStateInner::alloc_id_from_addr(ecx, addr.bytes())?
189185
};
190186

191187
let base_addr = GlobalStateInner::alloc_base_addr(ecx, alloc_id);

tests/run-pass/ptr_int_roundtrip.rs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,48 @@
11
// compile-flags: -Zmiri-permissive-provenance -Zmiri-disable-stacked-borrows
22
#![feature(strict_provenance)]
33

4+
use std::ptr;
5+
6+
/// Ensure we can expose the address of a pointer that is out-of-bounds
47
fn ptr_roundtrip_out_of_bounds() {
58
let x: i32 = 3;
69
let x_ptr = &x as *const i32;
710

8-
let x_usize = x_ptr.wrapping_offset(128) as usize;
11+
let x_usize = x_ptr.wrapping_offset(128).expose_addr();
912

10-
let ptr = (x_usize as *const i32).wrapping_offset(-128);
13+
let ptr = ptr::from_exposed_addr::<i32>(x_usize).wrapping_offset(-128);
1114
assert_eq!(unsafe { *ptr }, 3);
1215
}
1316

14-
fn ptr_roundtrip_out_of_bounds_with_addr() {
17+
/// Ensure that we can move between allocations using when casting
18+
fn ptr_roundtrip_confusion() {
1519
let x: i32 = 0;
1620
let y: i32 = 1;
1721

18-
let x_ptr = &x as *const _;
19-
let y_ptr = &y as *const _;
22+
let x_ptr = &x as *const i32;
23+
let y_ptr = &y as *const i32;
2024

21-
let x_usize = x_ptr as usize;
22-
let y_usize = y_ptr as usize;
25+
let x_usize = x_ptr.expose_addr();
26+
let y_usize = y_ptr.expose_addr();
2327

24-
let ptr = y_usize as *const i32;
28+
let ptr = ptr::from_exposed_addr::<i32>(y_usize);
2529
let ptr = ptr.with_addr(x_usize);
2630
assert_eq!(unsafe { *ptr }, 0);
2731
}
2832

33+
/// Ensure we can cast back a different integer than the one we got when exposing.
34+
fn ptr_roundtrip_imperfect() {
35+
let x: u8 = 3;
36+
let x_ptr = &x as *const u8;
37+
38+
let x_usize = x_ptr.expose_addr() + 128;
39+
40+
let ptr = ptr::from_exposed_addr::<u8>(x_usize).wrapping_offset(-128);
41+
assert_eq!(unsafe { *ptr }, 3);
42+
}
43+
2944
fn main() {
3045
ptr_roundtrip_out_of_bounds();
31-
ptr_roundtrip_out_of_bounds_with_addr();
46+
ptr_roundtrip_confusion();
47+
ptr_roundtrip_imperfect();
3248
}

0 commit comments

Comments
 (0)