]> git.lizzy.rs Git - rust.git/commitdiff
Improve miri's error reporting in check_in_alloc
authorLooMaclin <loo.maclin@protonmail.com>
Mon, 8 Apr 2019 20:34:28 +0000 (23:34 +0300)
committerLooMaclin <loo.maclin@protonmail.com>
Mon, 8 Apr 2019 20:34:28 +0000 (23:34 +0300)
src/librustc/mir/interpret/allocation.rs
src/librustc_mir/hair/pattern/_match.rs
src/librustc_mir/interpret/memory.rs
src/librustc_mir/interpret/operand.rs
src/librustc_mir/interpret/validity.rs

index 3f7730a8bbc078fb730f2e3c971322c8ca0719e2..ca680187b37db13df69100474226cc379bb42cea 100644 (file)
@@ -25,35 +25,19 @@ pub enum InboundsCheck {
 /// Used by `check_in_alloc` to indicate context of check
 #[derive(Debug, Copy, Clone, RustcEncodable, RustcDecodable, HashStable)]
 pub enum CheckInAllocMsg {
-    ReadCStr,
-    CheckBytes,
-    WriteBytes,
-    WriteRepeat,
-    ReadScalar,
-    WriteScalar,
-    SlicePatCoveredByConst,
-    ReadDiscriminant,
-    CheckAlign,
-    ReadBytes,
-    CopyRepeatedly,
-    CheckBounds,
+    MemoryAccess,
+    NullPointer,
+    PointerArithmetic,
+    OutOfBounds,
 }
 
 impl Display for CheckInAllocMsg {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         write!(f, "{}", match *self {
-            CheckInAllocMsg::ReadCStr => "read C str",
-            CheckInAllocMsg::CheckBytes => "check bytes",
-            CheckInAllocMsg::WriteBytes => "write bytes",
-            CheckInAllocMsg::WriteRepeat => "write repeat",
-            CheckInAllocMsg::ReadScalar => "read scalar",
-            CheckInAllocMsg::WriteScalar => "write scalar",
-            CheckInAllocMsg::SlicePatCoveredByConst => "slice pat covered by const",
-            CheckInAllocMsg::ReadDiscriminant => "read discriminant",
-            CheckInAllocMsg::CheckAlign => "check align",
-            CheckInAllocMsg::ReadBytes => "read bytes",
-            CheckInAllocMsg::CopyRepeatedly => "copy repeatedly",
-            CheckInAllocMsg::CheckBounds => "check bounds",
+            CheckInAllocMsg::MemoryAccess => "memory access",
+            CheckInAllocMsg::NullPointer => "null pointer",
+            CheckInAllocMsg::PointerArithmetic => "pointer arithmetic",
+            CheckInAllocMsg::OutOfBounds => "out of bounds",
         })
     }
 }
@@ -311,7 +295,7 @@ pub fn read_c_str<MemoryExtra>(
                 // Go through `get_bytes` for checks and AllocationExtra hooks.
                 // We read the null, so we include it in the request, but we want it removed
                 // from the result!
-                Ok(&self.get_bytes(cx, ptr, size_with_null, CheckInAllocMsg::ReadCStr)?[..size])
+                Ok(&self.get_bytes(cx, ptr, size_with_null, CheckInAllocMsg::NullPointer)?[..size])
             }
             None => err!(UnterminatedCString(ptr.erase_tag())),
         }
@@ -331,7 +315,7 @@ pub fn check_bytes<MemoryExtra>(
         where Extra: AllocationExtra<Tag, MemoryExtra>
     {
         // Check bounds and relocations on the edges
-        self.get_bytes_with_undef_and_ptr(cx, ptr, size, CheckInAllocMsg::CheckBytes)?;
+        self.get_bytes_with_undef_and_ptr(cx, ptr, size, CheckInAllocMsg::OutOfBounds)?;
         // Check undef and ptr
         if !allow_ptr_and_undef {
             self.check_defined(ptr, size)?;
@@ -353,7 +337,7 @@ pub fn write_bytes<MemoryExtra>(
         where Extra: AllocationExtra<Tag, MemoryExtra>
     {
         let bytes = self.get_bytes_mut(cx, ptr, Size::from_bytes(src.len() as u64),
-                                       CheckInAllocMsg::WriteBytes)?;
+                                       CheckInAllocMsg::MemoryAccess)?;
         bytes.clone_from_slice(src);
         Ok(())
     }
@@ -369,7 +353,7 @@ pub fn write_repeat<MemoryExtra>(
         // FIXME: Working around https://github.com/rust-lang/rust/issues/56209
         where Extra: AllocationExtra<Tag, MemoryExtra>
     {
-        let bytes = self.get_bytes_mut(cx, ptr, count, CheckInAllocMsg::WriteRepeat)?;
+        let bytes = self.get_bytes_mut(cx, ptr, count, CheckInAllocMsg::MemoryAccess)?;
         for b in bytes {
             *b = val;
         }
@@ -394,7 +378,7 @@ pub fn read_scalar<MemoryExtra>(
         where Extra: AllocationExtra<Tag, MemoryExtra>
     {
         // get_bytes_unchecked tests relocation edges
-        let bytes = self.get_bytes_with_undef_and_ptr(cx, ptr, size, CheckInAllocMsg::ReadScalar)?;
+        let bytes = self.get_bytes_with_undef_and_ptr(cx, ptr, size, CheckInAllocMsg::PointerArithmetic)?;
         // Undef check happens *after* we established that the alignment is correct.
         // We must not return Ok() for unaligned pointers!
         if self.check_defined(ptr, size).is_err() {
@@ -471,7 +455,7 @@ pub fn write_scalar<MemoryExtra>(
         };
 
         let endian = cx.data_layout().endian;
-        let dst = self.get_bytes_mut(cx, ptr, type_size, CheckInAllocMsg::WriteScalar)?;
+        let dst = self.get_bytes_mut(cx, ptr, type_size, CheckInAllocMsg::PointerArithmetic)?;
         write_target_uint(endian, dst, bytes).unwrap();
 
         // See if we have to also write a relocation
index 561ac207b6bcba53569835c637dd6781392a1289..1e2ffa5db440b41fa181df41baed7c3bac746bef 100644 (file)
@@ -1419,7 +1419,7 @@ fn slice_pat_covered_by_const<'tcx>(
             }
             let n = n.assert_usize(tcx).unwrap();
             alloc.get_bytes(&tcx, ptr, Size::from_bytes(n),
-                            CheckInAllocMsg::SlicePatCoveredByConst).unwrap()
+                            CheckInAllocMsg::OutOfBounds).unwrap()
         },
         // a slice fat pointer to a zero length slice
         (ConstValue::Slice(Scalar::Bits { .. }, 0), ty::Slice(t)) => {
@@ -1444,7 +1444,7 @@ fn slice_pat_covered_by_const<'tcx>(
             tcx.alloc_map
                 .lock()
                 .unwrap_memory(ptr.alloc_id)
-                .get_bytes(&tcx, ptr, Size::from_bytes(n), CheckInAllocMsg::SlicePatCoveredByConst)
+                .get_bytes(&tcx, ptr, Size::from_bytes(n), CheckInAllocMsg::OutOfBounds)
                 .unwrap()
         },
         _ => bug!(
index b5e3f818699194fb1a72911ec05bdb7195827c17..a911479a15a23211d624de8696dc36e93d95e504 100644 (file)
@@ -252,7 +252,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(ptr, CheckInAllocMsg::CheckAlign)?;
+                let align = self.check_bounds_ptr(ptr, CheckInAllocMsg::NullPointer)?;
                 (ptr.offset.bytes(), align)
             }
             Scalar::Bits { bits, size } => {
@@ -440,7 +440,7 @@ pub fn get_size_and_align(
                 Ok((layout.size, layout.align.abi))
             }
             _ => match msg {
-                CheckInAllocMsg::CheckAlign | CheckInAllocMsg::ReadDiscriminant => {
+                CheckInAllocMsg::NullPointer | CheckInAllocMsg::OutOfBounds => {
                     // Must be a deallocated pointer
                     Ok(*self.dead_alloc_map.get(&id).expect(
                         "allocation missing in dead_alloc_map"
@@ -604,7 +604,7 @@ pub fn read_bytes(
             Ok(&[])
         } else {
             let ptr = ptr.to_ptr()?;
-            self.get(ptr.alloc_id)?.get_bytes(self, ptr, size, CheckInAllocMsg::ReadBytes)
+            self.get(ptr.alloc_id)?.get_bytes(self, ptr, size, CheckInAllocMsg::MemoryAccess)
         }
     }
 }
@@ -729,10 +729,10 @@ pub fn copy_repeatedly(
 
         // This checks relocation edges on the src.
         let src_bytes = self.get(src.alloc_id)?
-            .get_bytes_with_undef_and_ptr(&tcx, src, size, CheckInAllocMsg::CopyRepeatedly)?
+            .get_bytes_with_undef_and_ptr(&tcx, src, size, CheckInAllocMsg::MemoryAccess)?
             .as_ptr();
         let dest_bytes = self.get_mut(dest.alloc_id)?
-            .get_bytes_mut(&tcx, dest, size * length, CheckInAllocMsg::CopyRepeatedly)?
+            .get_bytes_mut(&tcx, dest, size * length, CheckInAllocMsg::MemoryAccess)?
             .as_mut_ptr();
 
         // SAFE: The above indexing would have panicked if there weren't at least `size` bytes
index 166e79c12d806cca06377cb2f20e8bd32b53a061..e7e22a12cd68f53657b26975c3e367228e96a7c7 100644 (file)
@@ -668,7 +668,7 @@ pub fn read_discriminant(
                         // 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(ptr,
-                                                         CheckInAllocMsg::ReadDiscriminant).is_ok();
+                                                         CheckInAllocMsg::OutOfBounds).is_ok();
                         if !ptr_valid {
                             return err!(InvalidDiscriminant(raw_discr.erase_tag()));
                         }
index 37c880ccf72bf4af0fe980b4d0cf86f65e3b2251..965b2898cad53a92f2b0c42726a057bdb0555b5d 100644 (file)
@@ -394,7 +394,7 @@ fn visit_primitive(&mut self, value: OpTy<'tcx, M::PointerTag>) -> EvalResult<'t
                         try_validation!(
                             self.ecx.memory
                                 .get(ptr.alloc_id)?
-                                .check_bounds(self.ecx, ptr, size, CheckInAllocMsg::CheckBounds),
+                                .check_bounds(self.ecx, ptr, size, CheckInAllocMsg::OutOfBounds),
                             "dangling (not entirely in bounds) reference", self.path);
                     }
                     // Check if we have encountered this pointer+layout combination