Skip to content

Commit b01182a

Browse files
committed
Abort execution if miri_get_alloc_id doesn't have provenance
1 parent a68fc75 commit b01182a

File tree

3 files changed

+5
-9
lines changed

3 files changed

+5
-9
lines changed

src/intptrcast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ impl GlobalStateInner {
6565
impl<'mir, 'tcx> GlobalStateInner {
6666
// Returns the exposed `AllocId` that corresponds to the specified addr,
6767
// or `None` if the addr is out of bounds
68-
pub fn alloc_id_from_addr(ecx: &MiriInterpCx<'mir, 'tcx>, addr: u64) -> Option<AllocId> {
68+
fn alloc_id_from_addr(ecx: &MiriInterpCx<'mir, 'tcx>, addr: u64) -> Option<AllocId> {
6969
let global_state = ecx.machine.intptrcast.borrow();
7070
assert!(global_state.provenance_mode != ProvenanceMode::Strict);
7171

src/shims/foreign_items.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -420,13 +420,14 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
420420
"miri_get_alloc_id" => {
421421
let [ptr] = this.check_shim(abi, Abi::Rust, link_name, args)?;
422422
let ptr = this.read_pointer(ptr)?;
423-
let (prov, offset) = ptr.into_parts();
423+
let (prov, _offset) = ptr.into_parts();
424424
let alloc_id = if prov.is_some() {
425425
let (alloc_id, _, _) = this.ptr_get_alloc_id(ptr)?;
426426
alloc_id
427427
} else {
428-
intptrcast::GlobalStateInner::alloc_id_from_addr(this, offset.bytes())
429-
.expect("pointer passed to miri_get_alloc_id must not be dangling")
428+
throw_machine_stop!(TerminationInfo::Abort(
429+
format!("pointer passed to miri_get_alloc_id must not be dangling, got {:?}", ptr)
430+
))
430431
};
431432
this.write_scalar(Scalar::from_u64(alloc_id.0.get()), dest)?;
432433
}

tests/pass/stacked-borrows/stack-printing.rs

-5
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,5 @@ fn main() {
4242
}
4343
print_borrow_stacks(alloc_id);
4444

45-
// Check that we get the right AllocId even if we pass a pointer with no provenance,
46-
// so long as its address corresponds to an existing allocation.
47-
let ptr_without_provenance = core::ptr::null::<u8>().with_addr(ptr.addr());
48-
assert_eq!(get_alloc_id(ptr_without_provenance), alloc_id);
49-
5045
unsafe { alloc::dealloc(ptr, Layout::new::<u8>()) };
5146
}

0 commit comments

Comments
 (0)