Skip to content

Commit 4a6d962

Browse files
dheaton-armmrkajetanp
authored andcommitted
shims/foreign_items: Add hook for updating the canonical address
Where a method modifies the address that should be considered canonical - such as via TBI or when an MTE tag has been set - Miri will need to be notified. This adds a hook to inform Miri of the new address.
1 parent 7b422fe commit 4a6d962

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

src/alloc_addresses/mod.rs

+15
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,21 @@ impl<'tcx> MiriMachine<'tcx> {
460460
}
461461
})
462462
}
463+
464+
/// Updates the current canonical address for the allocation. Note that any access to this allocation *must* use this address from this point on.
465+
pub fn set_alloc_address(&mut self, id: AllocId, new: u64) {
466+
let global_state = self.alloc_addresses.get_mut();
467+
if let Some(addr) = global_state.base_addr.insert(id, new) {
468+
// Remove the old address' int->ptr mapping.
469+
let pos =
470+
global_state.int_to_ptr_map.binary_search_by_key(&addr, |(addr, _)| *addr).unwrap();
471+
let removed = global_state.int_to_ptr_map.remove(pos);
472+
assert_eq!(removed, (addr, id));
473+
}
474+
let new_pos =
475+
global_state.int_to_ptr_map.binary_search_by_key(&new, |(addr, _)| *addr).unwrap_err();
476+
global_state.int_to_ptr_map.insert(new_pos, (new, id));
477+
}
463478
}
464479

465480
#[cfg(test)]

src/shims/foreign_items.rs

+7
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,13 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
423423
}
424424
}
425425
}
426+
"miri_set_canonical_address" => {
427+
let [old_ptr, new_ptr] = this.check_shim(abi, Abi::Rust, link_name, args)?;
428+
let old_ptr = this.read_pointer(old_ptr)?;
429+
let new_ptr = this.read_pointer(new_ptr)?;
430+
let (alloc_id, _, _) = this.ptr_get_alloc_id(old_ptr, 0)?;
431+
this.machine.set_alloc_address(alloc_id, new_ptr.addr().bytes());
432+
}
426433

427434
// Aborting the process.
428435
"exit" => {

tests/utils/miri_extern.rs

+5
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,9 @@ extern "Rust" {
147147
/// "symbolic" alignment checks. Will fail if the pointer is not actually aligned or `align` is
148148
/// not a power of two. Has no effect when alignment checks are concrete (which is the default).
149149
pub fn miri_promise_symbolic_alignment(ptr: *const (), align: usize);
150+
151+
/// Miri-provided extern function to specify that a new address is to be considered the
152+
/// canonical address, where `new` is a valid alias to the `old` allocation,
153+
/// usually due to them having different values in bits that are ignored by hardware.
154+
pub fn miri_set_canonical_address(old: *const (), new: *const ());
150155
}

0 commit comments

Comments
 (0)