1 use std::convert::TryFrom;
3 use rustc::ty::{Ty, layout::{Size, LayoutOf}};
8 pub trait EvalContextExt<'tcx> {
12 left: ImmTy<'tcx, Tag>,
13 right: ImmTy<'tcx, Tag>,
14 ) -> InterpResult<'tcx, (Scalar<Tag>, bool, Ty<'tcx>)>;
20 ) -> InterpResult<'tcx, bool>;
22 fn pointer_offset_inbounds(
27 ) -> InterpResult<'tcx, Scalar<Tag>>;
30 impl<'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'mir, 'tcx> {
34 left: ImmTy<'tcx, Tag>,
35 right: ImmTy<'tcx, Tag>,
36 ) -> InterpResult<'tcx, (Scalar<Tag>, bool, Ty<'tcx>)> {
37 use rustc::mir::BinOp::*;
39 trace!("ptr_op: {:?} {:?} {:?}", *left, bin_op, *right);
43 // This supports fat pointers.
44 let eq = match (*left, *right) {
45 (Immediate::Scalar(left), Immediate::Scalar(right)) =>
46 self.ptr_eq(left.not_undef()?, right.not_undef()?)?,
47 (Immediate::ScalarPair(left1, left2), Immediate::ScalarPair(right1, right2)) =>
48 self.ptr_eq(left1.not_undef()?, right1.not_undef()?)? &&
49 self.ptr_eq(left2.not_undef()?, right2.not_undef()?)?,
50 _ => bug!("Type system should not allow comparing Scalar with ScalarPair"),
52 (Scalar::from_bool(if bin_op == Eq { eq } else { !eq }), false, self.tcx.types.bool)
55 Lt | Le | Gt | Ge => {
56 // Just compare the integers.
57 // TODO: Do we really want to *always* do that, even when comparing two live in-bounds pointers?
58 let left = self.force_bits(left.to_scalar()?, left.layout.size)?;
59 let right = self.force_bits(right.to_scalar()?, right.layout.size)?;
60 let res = match bin_op {
65 _ => bug!("We already established it has to be one of these operators."),
67 (Scalar::from_bool(res), false, self.tcx.types.bool)
71 let pointee_ty = left.layout.ty
73 .expect("Offset called on non-ptr type")
75 let ptr = self.pointer_offset_inbounds(
78 right.to_scalar()?.to_machine_isize(self)?,
80 (ptr, false, left.layout.ty)
83 _ => bug!("Invalid operator on pointers: {:?}", bin_op)
91 ) -> InterpResult<'tcx, bool> {
92 let size = self.pointer_size();
93 // Just compare the integers.
94 // TODO: Do we really want to *always* do that, even when comparing two live in-bounds pointers?
95 let left = self.force_bits(left, size)?;
96 let right = self.force_bits(right, size)?;
100 /// Raises an error if the offset moves the pointer outside of its allocation.
101 /// For integers, we consider each of them their own tiny allocation of size 0,
102 /// so offset-by-0 is okay for them -- except for NULL, which we rule out entirely.
103 fn pointer_offset_inbounds(
106 pointee_ty: Ty<'tcx>,
108 ) -> InterpResult<'tcx, Scalar<Tag>> {
109 let pointee_size = i64::try_from(self.layout_of(pointee_ty)?.size.bytes()).unwrap();
111 .checked_mul(pointee_size)
112 .ok_or_else(|| err_panic!(Overflow(mir::BinOp::Mul)))?;
113 // We do this first, to rule out overflows.
114 let offset_ptr = ptr.ptr_signed_offset(offset, self)?;
115 // What we need to check is that starting at `min(ptr, offset_ptr)`,
116 // we could do an access of size `abs(offset)`. Alignment does not matter.
117 let (min_ptr, abs_offset) = if offset >= 0 {
118 (ptr, u64::try_from(offset).unwrap())
121 // If the negation overflows, the result will be negative so the try_from will fail.
122 (offset_ptr, u64::try_from(-offset).unwrap())
124 self.memory.check_ptr_access_align(
126 Size::from_bytes(abs_offset),
128 CheckInAllocMsg::InboundsTest,