]> git.lizzy.rs Git - rust.git/commitdiff
Move alignment and bounds check from `Memory` to `Allocation`
authorOliver Scherer <github35764891676564198441@oli-obk.de>
Mon, 12 Nov 2018 07:37:54 +0000 (08:37 +0100)
committerOliver Scherer <github35764891676564198441@oli-obk.de>
Sat, 24 Nov 2018 10:36:31 +0000 (11:36 +0100)
src/librustc/mir/interpret/allocation.rs
src/librustc_mir/interpret/memory.rs

index ad4bf415b8de044db644c6e07b78f1b6fb17e781..b2737ae203fbaac6b4cb148966b00961713f6084 100644 (file)
@@ -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
index d8ae107a22b563bd1cabdafdbea12ef87853f8f0..9f8e8fc921af887610f9d694624b7deb934d25f2 100644 (file)
@@ -284,48 +284,6 @@ pub fn check_align(
             })
         }
     }
-
-    /// 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