1 use std::convert::TryFrom;
5 layout::{LayoutOf, Size},
11 pub trait EvalContextExt<'tcx> {
15 left: ImmTy<'tcx, Tag>,
16 right: ImmTy<'tcx, Tag>,
17 ) -> InterpResult<'tcx, (Scalar<Tag>, bool, Ty<'tcx>)>;
19 fn ptr_eq(&self, left: Scalar<Tag>, right: Scalar<Tag>) -> InterpResult<'tcx, bool>;
21 fn pointer_offset_inbounds(
26 ) -> InterpResult<'tcx, Scalar<Tag>>;
29 impl<'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'mir, 'tcx> {
33 left: ImmTy<'tcx, Tag>,
34 right: ImmTy<'tcx, Tag>,
35 ) -> InterpResult<'tcx, (Scalar<Tag>, bool, Ty<'tcx>)> {
36 use rustc::mir::BinOp::*;
38 trace!("ptr_op: {:?} {:?} {:?}", *left, bin_op, *right);
42 // 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()?)?
48 (Immediate::ScalarPair(left1, left2), Immediate::ScalarPair(right1, right2)) => {
49 self.ptr_eq(left1.not_undef()?, right1.not_undef()?)?
50 && self.ptr_eq(left2.not_undef()?, right2.not_undef()?)?
52 _ => bug!("Type system should not allow comparing Scalar with ScalarPair"),
54 (Scalar::from_bool(if bin_op == Eq { eq } else { !eq }), false, self.tcx.types.bool)
57 Lt | Le | Gt | Ge => {
58 // Just compare the integers.
59 // TODO: Do we really want to *always* do that, even when comparing two live in-bounds pointers?
60 let left = self.force_bits(left.to_scalar()?, left.layout.size)?;
61 let right = self.force_bits(right.to_scalar()?, right.layout.size)?;
62 let res = match bin_op {
67 _ => bug!("We already established it has to be one of these operators."),
69 (Scalar::from_bool(res), false, self.tcx.types.bool)
74 left.layout.ty.builtin_deref(true).expect("Offset called on non-ptr type").ty;
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),
87 fn ptr_eq(&self, left: Scalar<Tag>, right: Scalar<Tag>) -> InterpResult<'tcx, bool> {
88 let size = self.pointer_size();
89 // Just compare the integers.
90 // TODO: Do we really want to *always* do that, even when comparing two live in-bounds pointers?
91 let left = self.force_bits(left, size)?;
92 let right = self.force_bits(right, size)?;
96 /// Raises an error if the offset moves the pointer outside of its allocation.
97 /// For integers, we consider each of them their own tiny allocation of size 0,
98 /// so offset-by-0 is okay for them -- except for NULL, which we rule out entirely.
99 fn pointer_offset_inbounds(
102 pointee_ty: Ty<'tcx>,
104 ) -> InterpResult<'tcx, Scalar<Tag>> {
105 let pointee_size = i64::try_from(self.layout_of(pointee_ty)?.size.bytes()).unwrap();
107 .checked_mul(pointee_size)
108 .ok_or_else(|| err_ub_format!("overflow during offset comutation for inbounds pointer arithmetic"))?;
109 // We do this first, to rule out overflows.
110 let offset_ptr = ptr.ptr_signed_offset(offset, self)?;
111 // What we need to check is that starting at `min(ptr, offset_ptr)`,
112 // we could do an access of size `abs(offset)`. Alignment does not matter.
113 let (min_ptr, abs_offset) = if offset >= 0 {
114 (ptr, u64::try_from(offset).unwrap())
117 // If the negation overflows, the result will be negative so the try_from will fail.
118 (offset_ptr, u64::try_from(-offset).unwrap())
120 self.memory.check_ptr_access_align(
122 Size::from_bytes(abs_offset),
124 CheckInAllocMsg::InboundsTest,