]> git.lizzy.rs Git - rust.git/commitdiff
add method to get absolute address of a pointer (useful only for Miri)
authorRalf Jung <post@ralfj.de>
Mon, 18 Apr 2022 15:43:13 +0000 (11:43 -0400)
committerRalf Jung <post@ralfj.de>
Mon, 18 Apr 2022 16:30:17 +0000 (12:30 -0400)
compiler/rustc_const_eval/src/interpret/machine.rs
compiler/rustc_const_eval/src/interpret/memory.rs
compiler/rustc_middle/src/mir/interpret/pointer.rs

index f386c7f36dc5c44661d705be01d36b9c23aa5cca..7721485771b3b0a121ea08e321b326889869d982 100644 (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
index 262a53fc2151835a62f2f025d937636a9644a39d..d19a9d7056012a6ca9e7611307a4070c9dbdbe6a 100644 (file)
@@ -446,12 +446,8 @@ fn check_offset_align(offset: u64, align: Align) -> InterpResult<'static> {
                 // 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() {
index 5ad2f80f06dbb13c144d5315ca9fc3e8ace91625..4461f4e0568738deccbc4c32c00b134b8debf47f 100644 (file)
@@ -207,6 +207,16 @@ pub fn into_pointer_or_addr(self) -> Result<Pointer<Tag>, Size> {
             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>> {