1 use std::convert::TryFrom;
6 use rustc_middle::ty::{
7 layout::{LayoutOf, Size},
13 pub trait EvalContextExt<'tcx> {
17 left: ImmTy<'tcx, Tag>,
18 right: ImmTy<'tcx, Tag>,
19 ) -> InterpResult<'tcx, (Scalar<Tag>, bool, Ty<'tcx>)>;
21 fn ptr_eq(&self, left: Scalar<Tag>, right: Scalar<Tag>) -> InterpResult<'tcx, bool>;
23 fn pointer_offset_inbounds(
28 ) -> InterpResult<'tcx, Scalar<Tag>>;
31 impl<'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'mir, 'tcx> {
35 left: ImmTy<'tcx, Tag>,
36 right: ImmTy<'tcx, Tag>,
37 ) -> InterpResult<'tcx, (Scalar<Tag>, bool, Ty<'tcx>)> {
38 use rustc_middle::mir::BinOp::*;
40 trace!("ptr_op: {:?} {:?} {:?}", *left, bin_op, *right);
44 // This supports fat pointers.
46 let eq = match (*left, *right) {
47 (Immediate::Scalar(left), Immediate::Scalar(right)) => {
48 self.ptr_eq(left.not_undef()?, right.not_undef()?)?
50 (Immediate::ScalarPair(left1, left2), Immediate::ScalarPair(right1, right2)) => {
51 self.ptr_eq(left1.not_undef()?, right1.not_undef()?)?
52 && self.ptr_eq(left2.not_undef()?, right2.not_undef()?)?
54 _ => bug!("Type system should not allow comparing Scalar with ScalarPair"),
56 (Scalar::from_bool(if bin_op == Eq { eq } else { !eq }), false, self.tcx.types.bool)
59 Lt | Le | Gt | Ge => {
60 // Just compare the integers.
61 // TODO: Do we really want to *always* do that, even when comparing two live in-bounds pointers?
62 let left = self.force_bits(left.to_scalar()?, left.layout.size)?;
63 let right = self.force_bits(right.to_scalar()?, right.layout.size)?;
64 let res = match bin_op {
69 _ => bug!("We already established it has to be one of these operators."),
71 (Scalar::from_bool(res), false, self.tcx.types.bool)
76 left.layout.ty.builtin_deref(true).expect("Offset called on non-ptr type").ty;
77 let ptr = self.pointer_offset_inbounds(
80 right.to_scalar()?.to_machine_isize(self)?,
82 (ptr, false, left.layout.ty)
85 _ => bug!("Invalid operator on pointers: {:?}", bin_op),
89 fn ptr_eq(&self, left: Scalar<Tag>, right: Scalar<Tag>) -> InterpResult<'tcx, bool> {
90 let size = self.pointer_size();
91 // Just compare the integers.
92 // TODO: Do we really want to *always* do that, even when comparing two live in-bounds pointers?
93 let left = self.force_bits(left, size)?;
94 let right = self.force_bits(right, size)?;
98 /// Raises an error if the offset moves the pointer outside of its allocation.
99 /// For integers, we consider each of them their own tiny allocation of size 0,
100 /// so offset-by-0 is okay for them -- except for NULL, which we rule out entirely.
101 fn pointer_offset_inbounds(
104 pointee_ty: Ty<'tcx>,
106 ) -> InterpResult<'tcx, Scalar<Tag>> {
107 let pointee_size = i64::try_from(self.layout_of(pointee_ty)?.size.bytes()).unwrap();
108 let offset = offset.checked_mul(pointee_size).ok_or_else(|| {
109 err_ub_format!("overflow during offset comutation for inbounds pointer arithmetic")
111 // We do this first, to rule out overflows.
112 let offset_ptr = ptr.ptr_signed_offset(offset, self)?;
113 // What we need to check is that starting at `min(ptr, offset_ptr)`,
114 // we could do an access of size `abs(offset)`. Alignment does not matter.
115 let (min_ptr, abs_offset) = if offset >= 0 {
116 (ptr, u64::try_from(offset).unwrap())
119 // If the negation overflows, the result will be negative so the try_from will fail.
120 (offset_ptr, u64::try_from(-offset).unwrap())
122 self.memory.check_ptr_access_align(
124 Size::from_bytes(abs_offset),
126 CheckInAllocMsg::InboundsTest,