1
Fork 0

add method to get absolute address of a pointer (useful only for Miri)

This commit is contained in:
Ralf Jung 2022-04-18 11:43:13 -04:00
parent c9e568f72e
commit 3236092503
3 changed files with 14 additions and 6 deletions

View file

@ -126,6 +126,8 @@ pub trait Machine<'mir, 'tcx>: Sized {
/// Whether, when checking alignment, we should `force_int` and thus support
/// custom alignment logic based on whatever the integer address happens to be.
///
/// Requires PointerTag::OFFSET_IS_ADDR to be true.
fn force_int_for_alignment_check(ecx: &InterpCx<'mir, 'tcx, Self>) -> bool;
/// Whether to enforce the validity invariant

View file

@ -446,12 +446,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
// we want the error to be about the bounds.
if let Some(align) = align {
if M::force_int_for_alignment_check(self) {
assert!(
M::PointerTag::OFFSET_IS_ADDR,
"ptr-to-int cast for align check should never fail"
);
let (_, addr) = ptr.into_parts(); // we checked that offset is absolute
check_offset_align(addr.bytes(), align)?;
// `force_int_for_alignment_check` can only be true if `OFFSET_IS_ADDR` is true.
check_offset_align(ptr.addr().bytes(), align)?;
} else {
// Check allocation alignment and offset alignment.
if alloc_align.bytes() < align.bytes() {

View file

@ -207,6 +207,16 @@ impl<Tag> Pointer<Option<Tag>> {
None => Err(self.offset),
}
}
/// Returns the absolute address the pointer points to.
/// Only works if Tag::OFFSET_IS_ADDR is true!
pub fn addr(self) -> Size
where
Tag: Provenance,
{
assert!(Tag::OFFSET_IS_ADDR);
self.offset
}
}
impl<Tag> Pointer<Option<Tag>> {