Skip to content

Commit 3236092

Browse files
committed
add method to get absolute address of a pointer (useful only for Miri)
1 parent c9e568f commit 3236092

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

compiler/rustc_const_eval/src/interpret/machine.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ pub trait Machine<'mir, 'tcx>: Sized {
126126

127127
/// Whether, when checking alignment, we should `force_int` and thus support
128128
/// custom alignment logic based on whatever the integer address happens to be.
129+
///
130+
/// Requires PointerTag::OFFSET_IS_ADDR to be true.
129131
fn force_int_for_alignment_check(ecx: &InterpCx<'mir, 'tcx, Self>) -> bool;
130132

131133
/// Whether to enforce the validity invariant

compiler/rustc_const_eval/src/interpret/memory.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -446,12 +446,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
446446
// we want the error to be about the bounds.
447447
if let Some(align) = align {
448448
if M::force_int_for_alignment_check(self) {
449-
assert!(
450-
M::PointerTag::OFFSET_IS_ADDR,
451-
"ptr-to-int cast for align check should never fail"
452-
);
453-
let (_, addr) = ptr.into_parts(); // we checked that offset is absolute
454-
check_offset_align(addr.bytes(), align)?;
449+
// `force_int_for_alignment_check` can only be true if `OFFSET_IS_ADDR` is true.
450+
check_offset_align(ptr.addr().bytes(), align)?;
455451
} else {
456452
// Check allocation alignment and offset alignment.
457453
if alloc_align.bytes() < align.bytes() {

compiler/rustc_middle/src/mir/interpret/pointer.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,16 @@ impl<Tag> Pointer<Option<Tag>> {
207207
None => Err(self.offset),
208208
}
209209
}
210+
211+
/// Returns the absolute address the pointer points to.
212+
/// Only works if Tag::OFFSET_IS_ADDR is true!
213+
pub fn addr(self) -> Size
214+
where
215+
Tag: Provenance,
216+
{
217+
assert!(Tag::OFFSET_IS_ADDR);
218+
self.offset
219+
}
210220
}
211221

212222
impl<Tag> Pointer<Option<Tag>> {

0 commit comments

Comments
 (0)