about summary refs log tree commit diff
diff options
context:
space:
mode:
authorOliver Scherer <github35764891676564198441@oli-obk.de>2018-11-12 08:37:54 +0100
committerOliver Scherer <github35764891676564198441@oli-obk.de>2018-11-24 11:36:31 +0100
commit7c9d786e5007004917d73749fad32bc3bff94cce (patch)
tree7d9557eb2ad9726bc32244cd7ec2673ea5e84b64
parentd98c46ce57d562ebcfb01ec814ff8d90d47ff7ea (diff)
downloadrust-7c9d786e5007004917d73749fad32bc3bff94cce.tar.gz
rust-7c9d786e5007004917d73749fad32bc3bff94cce.zip
Move alignment and bounds check from `Memory` to `Allocation`
-rw-r--r--src/librustc/mir/interpret/allocation.rs45
-rw-r--r--src/librustc_mir/interpret/memory.rs42
2 files changed, 45 insertions, 42 deletions
diff --git a/src/librustc/mir/interpret/allocation.rs b/src/librustc/mir/interpret/allocation.rs
index ad4bf415b8d..b2737ae203f 100644
--- a/src/librustc/mir/interpret/allocation.rs
+++ b/src/librustc/mir/interpret/allocation.rs
@@ -49,6 +49,51 @@ pub struct Allocation<Tag=(),Extra=()> {
     pub extra: Extra,
 }
 
+/// Alignment and bounds checks
+impl<'tcx, Tag, Extra> Allocation<Tag, Extra> {
+    /// Check if the pointer is "in-bounds". Notice that a pointer pointing at the end
+    /// of an allocation (i.e., at the first *inaccessible* location) *is* considered
+    /// in-bounds!  This follows C's/LLVM's rules.  `check` indicates whether we
+    /// additionally require the pointer to be pointing to a *live* (still allocated)
+    /// allocation.
+    /// If you want to check bounds before doing a memory access, better use `check_bounds`.
+    pub fn check_bounds_ptr(
+        &self,
+        ptr: Pointer<M::PointerTag>,
+        check: InboundsCheck,
+    ) -> EvalResult<'tcx> {
+        let allocation_size = match check {
+            InboundsCheck::Live => {
+                let alloc = self.get(ptr.alloc_id)?;
+                alloc.bytes.len() as u64
+            }
+            InboundsCheck::MaybeDead => {
+                self.get_size_and_align(ptr.alloc_id).0.bytes()
+            }
+        };
+        if ptr.offset.bytes() > allocation_size {
+            return err!(PointerOutOfBounds {
+                ptr: ptr.erase_tag(),
+                check,
+                allocation_size: Size::from_bytes(allocation_size),
+            });
+        }
+        Ok(())
+    }
+
+    /// Check if the memory range beginning at `ptr` and of size `Size` is "in-bounds".
+    #[inline(always)]
+    pub fn check_bounds(
+        &self,
+        ptr: Pointer<M::PointerTag>,
+        size: Size,
+        check: InboundsCheck,
+    ) -> EvalResult<'tcx> {
+        // if ptr.offset is in bounds, then so is ptr (because offset checks for overflow)
+        self.check_bounds_ptr(ptr.offset(size, &*self)?, check)
+    }
+}
+
 /// Byte accessors
 impl<'tcx, Tag, Extra> Allocation<Tag, Extra> {
     /// The last argument controls whether we error out when there are undefined
diff --git a/src/librustc_mir/interpret/memory.rs b/src/librustc_mir/interpret/memory.rs
index d8ae107a22b..9f8e8fc921a 100644
--- a/src/librustc_mir/interpret/memory.rs
+++ b/src/librustc_mir/interpret/memory.rs
@@ -284,48 +284,6 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
             })
         }
     }
-
-    /// Check if the pointer is "in-bounds". Notice that a pointer pointing at the end
-    /// of an allocation (i.e., at the first *inaccessible* location) *is* considered
-    /// in-bounds!  This follows C's/LLVM's rules.  `check` indicates whether we
-    /// additionally require the pointer to be pointing to a *live* (still allocated)
-    /// allocation.
-    /// If you want to check bounds before doing a memory access, better use `check_bounds`.
-    pub fn check_bounds_ptr(
-        &self,
-        ptr: Pointer<M::PointerTag>,
-        check: InboundsCheck,
-    ) -> EvalResult<'tcx> {
-        let allocation_size = match check {
-            InboundsCheck::Live => {
-                let alloc = self.get(ptr.alloc_id)?;
-                alloc.bytes.len() as u64
-            }
-            InboundsCheck::MaybeDead => {
-                self.get_size_and_align(ptr.alloc_id).0.bytes()
-            }
-        };
-        if ptr.offset.bytes() > allocation_size {
-            return err!(PointerOutOfBounds {
-                ptr: ptr.erase_tag(),
-                check,
-                allocation_size: Size::from_bytes(allocation_size),
-            });
-        }
-        Ok(())
-    }
-
-    /// Check if the memory range beginning at `ptr` and of size `Size` is "in-bounds".
-    #[inline(always)]
-    pub fn check_bounds(
-        &self,
-        ptr: Pointer<M::PointerTag>,
-        size: Size,
-        check: InboundsCheck,
-    ) -> EvalResult<'tcx> {
-        // if ptr.offset is in bounds, then so is ptr (because offset checks for overflow)
-        self.check_bounds_ptr(ptr.offset(size, &*self)?, check)
-    }
 }
 
 /// Allocation accessors