]> git.lizzy.rs Git - rust.git/commitdiff
Allow testing pointers for inboundedness while forbidding dangling pointers
authorOliver Scherer <github35764891676564198441@oli-obk.de>
Wed, 19 Dec 2018 15:26:46 +0000 (16:26 +0100)
committerOliver Scherer <github35764891676564198441@oli-obk.de>
Wed, 19 Dec 2018 15:48:31 +0000 (16:48 +0100)
src/librustc_mir/interpret/memory.rs
src/librustc_mir/interpret/operand.rs

index 420fe26426321909d04583b086cb8aee15a3e6ea..de7ad1651c1667b5d781a8e30dd709448f728be6 100644 (file)
@@ -262,7 +262,7 @@ pub fn check_align(
             Scalar::Ptr(ptr) => {
                 // check this is not NULL -- which we can ensure only if this is in-bounds
                 // of some (potentially dead) allocation.
-                let align = self.check_bounds_ptr_maybe_dead(ptr)?;
+                let align = self.check_bounds_ptr(ptr, InboundsCheck::MaybeDead)?;
                 (ptr.offset.bytes(), align)
             }
             Scalar::Bits { bits, size } => {
@@ -297,17 +297,15 @@ 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.
-    /// This function also works for deallocated allocations.
-    /// Use `.get(ptr.alloc_id)?.check_bounds_ptr(ptr)` if you want to force the allocation
-    /// to still be live.
     /// If you want to check bounds before doing a memory access, better first obtain
     /// an `Allocation` and call `check_bounds`.
-    pub fn check_bounds_ptr_maybe_dead(
+    pub fn check_bounds_ptr(
         &self,
         ptr: Pointer<M::PointerTag>,
+        liveness: InboundsCheck,
     ) -> EvalResult<'tcx, Align> {
         let (allocation_size, align) = self.get_size_and_align(ptr.alloc_id);
-        ptr.check_in_alloc(allocation_size, InboundsCheck::MaybeDead)?;
+        ptr.check_in_alloc(allocation_size, liveness)?;
         Ok(align)
     }
 }
index 83ceadada65ce68f0e1a62c47a064e6f65a47aee..76f851a958c5997aea9f2172d96898015718a3a4 100644 (file)
@@ -17,7 +17,7 @@
 use rustc::ty::layout::{self, Size, LayoutOf, TyLayout, HasDataLayout, IntegerExt, VariantIdx};
 
 use rustc::mir::interpret::{
-    GlobalId, AllocId,
+    GlobalId, AllocId, InboundsCheck,
     ConstValue, Pointer, Scalar,
     EvalResult, EvalErrorKind,
 };
@@ -647,7 +647,7 @@ pub fn read_discriminant(
                     ScalarMaybeUndef::Scalar(Scalar::Ptr(ptr)) => {
                         // The niche must be just 0 (which an inbounds pointer value never is)
                         let ptr_valid = niche_start == 0 && variants_start == variants_end &&
-                            self.memory.check_bounds_ptr_maybe_dead(ptr).is_ok();
+                            self.memory.check_bounds_ptr(ptr, InboundsCheck::MaybeDead).is_ok();
                         if !ptr_valid {
                             return err!(InvalidDiscriminant(raw_discr.erase_tag()));
                         }