1 use std::convert::TryFrom;
3 use rustc::ty::{Ty, layout::LayoutOf};
8 pub trait EvalContextExt<'tcx> {
12 ) -> InterpResult<'tcx>;
17 left: ImmTy<'tcx, Tag>,
18 right: ImmTy<'tcx, Tag>,
19 ) -> InterpResult<'tcx, (Scalar<Tag>, bool, Ty<'tcx>)>;
25 ) -> InterpResult<'tcx, bool>;
27 fn pointer_offset_inbounds(
32 ) -> InterpResult<'tcx, Scalar<Tag>>;
35 impl<'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'mir, 'tcx> {
36 /// Test if the pointer is in-bounds of a live allocation.
38 fn pointer_inbounds(&self, ptr: Pointer<Tag>) -> InterpResult<'tcx> {
39 let (size, _align) = self.memory.get_size_and_align(ptr.alloc_id, AllocCheck::Live)?;
40 ptr.check_inbounds_alloc(size, CheckInAllocMsg::InboundsTest)
46 left: ImmTy<'tcx, Tag>,
47 right: ImmTy<'tcx, Tag>,
48 ) -> InterpResult<'tcx, (Scalar<Tag>, bool, Ty<'tcx>)> {
49 use rustc::mir::BinOp::*;
51 trace!("ptr_op: {:?} {:?} {:?}", *left, bin_op, *right);
55 // This supports fat pointers.
56 let eq = match (*left, *right) {
57 (Immediate::Scalar(left), Immediate::Scalar(right)) =>
58 self.ptr_eq(left.not_undef()?, right.not_undef()?)?,
59 (Immediate::ScalarPair(left1, left2), Immediate::ScalarPair(right1, right2)) =>
60 self.ptr_eq(left1.not_undef()?, right1.not_undef()?)? &&
61 self.ptr_eq(left2.not_undef()?, right2.not_undef()?)?,
62 _ => bug!("Type system should not allow comparing Scalar with ScalarPair"),
64 (Scalar::from_bool(if bin_op == Eq { eq } else { !eq }), false, self.tcx.types.bool)
67 Lt | Le | Gt | Ge => {
68 // Just compare the integers.
69 // TODO: Do we really want to *always* do that, even when comparing two live in-bounds pointers?
70 let left = self.force_bits(left.to_scalar()?, left.layout.size)?;
71 let right = self.force_bits(right.to_scalar()?, right.layout.size)?;
72 let res = match bin_op {
77 _ => bug!("We already established it has to be one of these operators."),
79 (Scalar::from_bool(res), false, self.tcx.types.bool)
83 let pointee_ty = left.layout.ty
85 .expect("Offset called on non-ptr type")
87 let ptr = self.pointer_offset_inbounds(
90 right.to_scalar()?.to_isize(self)?,
92 (ptr, false, left.layout.ty)
95 _ => bug!("Invalid operator on pointers: {:?}", bin_op)
103 ) -> InterpResult<'tcx, bool> {
104 let size = self.pointer_size();
105 // Just compare the integers.
106 // TODO: Do we really want to *always* do that, even when comparing two live in-bounds pointers?
107 let left = self.force_bits(left, size)?;
108 let right = self.force_bits(right, size)?;
112 /// Raises an error if the offset moves the pointer outside of its allocation.
113 /// We consider ZSTs their own huge allocation that doesn't overlap with anything (and nothing
114 /// moves in there because the size is 0). We also consider the NULL pointer its own separate
115 /// allocation, and all the remaining integers pointers their own allocation.
116 fn pointer_offset_inbounds(
119 pointee_ty: Ty<'tcx>,
121 ) -> InterpResult<'tcx, Scalar<Tag>> {
122 let pointee_size = i64::try_from(self.layout_of(pointee_ty)?.size.bytes()).unwrap();
124 .checked_mul(pointee_size)
125 .ok_or_else(|| err_panic!(Overflow(mir::BinOp::Mul)))?;
126 // Now let's see what kind of pointer this is.
127 let ptr = if offset == 0 {
129 Scalar::Ptr(ptr) => ptr,
130 Scalar::Raw { .. } => {
131 // Offset 0 on an integer. We accept that, pretending there is
132 // a little zero-sized allocation here.
137 // Offset > 0. We *require* a pointer.
140 // Both old and new pointer must be in-bounds of a *live* allocation.
141 // (Of the same allocation, but that part is trivial with our representation.)
142 self.pointer_inbounds(ptr)?;
143 let ptr = ptr.signed_offset(offset, self)?;
144 self.pointer_inbounds(ptr)?;