]> git.lizzy.rs Git - rust.git/blobdiff - src/operator.rs
Add newlines at end of file + use replace.
[rust.git] / src / operator.rs
index c494e4ff8c72484acdae7168383980fb30b6de65..5b86b9a76f6b48125906a9fdbd5fac091b9c9008 100644 (file)
@@ -1,10 +1,6 @@
-use std::convert::TryFrom;
+use log::trace;
 
-use rustc::mir;
-use rustc::ty::{
-    layout::{LayoutOf, Size},
-    Ty,
-};
+use rustc_middle::{mir, ty::Ty};
 
 use crate::*;
 
@@ -17,13 +13,6 @@ fn binary_ptr_op(
     ) -> InterpResult<'tcx, (Scalar<Tag>, bool, Ty<'tcx>)>;
 
     fn ptr_eq(&self, left: Scalar<Tag>, right: Scalar<Tag>) -> InterpResult<'tcx, bool>;
-
-    fn pointer_offset_inbounds(
-        &self,
-        ptr: Scalar<Tag>,
-        pointee_ty: Ty<'tcx>,
-        offset: i64,
-    ) -> InterpResult<'tcx, Scalar<Tag>>;
 }
 
 impl<'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'mir, 'tcx> {
@@ -33,7 +22,7 @@ fn binary_ptr_op(
         left: ImmTy<'tcx, Tag>,
         right: ImmTy<'tcx, Tag>,
     ) -> InterpResult<'tcx, (Scalar<Tag>, bool, Ty<'tcx>)> {
-        use rustc::mir::BinOp::*;
+        use rustc_middle::mir::BinOp::*;
 
         trace!("ptr_op: {:?} {:?} {:?}", *left, bin_op, *right);
 
@@ -43,11 +32,11 @@ fn binary_ptr_op(
                 #[rustfmt::skip]
                 let eq = match (*left, *right) {
                     (Immediate::Scalar(left), Immediate::Scalar(right)) => {
-                        self.ptr_eq(left.not_undef()?, right.not_undef()?)?
+                        self.ptr_eq(left.check_init()?, right.check_init()?)?
                     }
                     (Immediate::ScalarPair(left1, left2), Immediate::ScalarPair(right1, right2)) => {
-                        self.ptr_eq(left1.not_undef()?, right1.not_undef()?)?
-                            && self.ptr_eq(left2.not_undef()?, right2.not_undef()?)?
+                        self.ptr_eq(left1.check_init()?, right1.check_init()?)?
+                            && self.ptr_eq(left2.check_init()?, right2.check_init()?)?
                     }
                     _ => bug!("Type system should not allow comparing Scalar with ScalarPair"),
                 };
@@ -72,7 +61,7 @@ fn binary_ptr_op(
             Offset => {
                 let pointee_ty =
                     left.layout.ty.builtin_deref(true).expect("Offset called on non-ptr type").ty;
-                let ptr = self.pointer_offset_inbounds(
+                let ptr = self.ptr_offset_inbounds(
                     left.to_scalar()?,
                     pointee_ty,
                     right.to_scalar()?.to_machine_isize(self)?,
@@ -92,38 +81,4 @@ fn ptr_eq(&self, left: Scalar<Tag>, right: Scalar<Tag>) -> InterpResult<'tcx, bo
         let right = self.force_bits(right, size)?;
         Ok(left == right)
     }
-
-    /// Raises an error if the offset moves the pointer outside of its allocation.
-    /// For integers, we consider each of them their own tiny allocation of size 0,
-    /// so offset-by-0 is okay for them -- except for NULL, which we rule out entirely.
-    fn pointer_offset_inbounds(
-        &self,
-        ptr: Scalar<Tag>,
-        pointee_ty: Ty<'tcx>,
-        offset: i64,
-    ) -> InterpResult<'tcx, Scalar<Tag>> {
-        let pointee_size = i64::try_from(self.layout_of(pointee_ty)?.size.bytes()).unwrap();
-        let offset = offset.checked_mul(pointee_size).ok_or_else(|| {
-            err_ub_format!("overflow during offset comutation for inbounds pointer arithmetic")
-        })?;
-        // We do this first, to rule out overflows.
-        let offset_ptr = ptr.ptr_signed_offset(offset, self)?;
-        // What we need to check is that starting at `min(ptr, offset_ptr)`,
-        // we could do an access of size `abs(offset)`. Alignment does not matter.
-        let (min_ptr, abs_offset) = if offset >= 0 {
-            (ptr, u64::try_from(offset).unwrap())
-        } else {
-            // Negative offset.
-            // If the negation overflows, the result will be negative so the try_from will fail.
-            (offset_ptr, u64::try_from(-offset).unwrap())
-        };
-        self.memory.check_ptr_access_align(
-            min_ptr,
-            Size::from_bytes(abs_offset),
-            None,
-            CheckInAllocMsg::InboundsTest,
-        )?;
-        // That's it!
-        Ok(offset_ptr)
-    }
 }