1 use rustc::ty::{Ty, layout::LayoutOf};
6 pub trait EvalContextExt<'tcx> {
10 ) -> InterpResult<'tcx>;
15 left: ImmTy<'tcx, Tag>,
16 right: ImmTy<'tcx, Tag>,
17 ) -> InterpResult<'tcx, (Scalar<Tag>, bool)>;
23 ) -> InterpResult<'tcx, bool>;
25 fn pointer_offset_inbounds(
30 ) -> InterpResult<'tcx, Scalar<Tag>>;
33 impl<'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'mir, 'tcx> {
34 /// Test if the pointer is in-bounds of a live allocation.
36 fn pointer_inbounds(&self, ptr: Pointer<Tag>) -> InterpResult<'tcx> {
37 let (size, _align) = self.memory().get_size_and_align(ptr.alloc_id, AllocCheck::Live)?;
38 ptr.check_in_alloc(size, CheckInAllocMsg::InboundsTest)
44 left: ImmTy<'tcx, Tag>,
45 right: ImmTy<'tcx, Tag>,
46 ) -> InterpResult<'tcx, (Scalar<Tag>, bool)> {
47 use rustc::mir::BinOp::*;
49 trace!("ptr_op: {:?} {:?} {:?}", *left, bin_op, *right);
53 // This supports fat pointers.
54 let eq = match (*left, *right) {
55 (Immediate::Scalar(left), Immediate::Scalar(right)) =>
56 self.ptr_eq(left.not_undef()?, right.not_undef()?)?,
57 (Immediate::ScalarPair(left1, left2), Immediate::ScalarPair(right1, right2)) =>
58 self.ptr_eq(left1.not_undef()?, right1.not_undef()?)? &&
59 self.ptr_eq(left2.not_undef()?, right2.not_undef()?)?,
60 _ => bug!("Type system should not allow comparing Scalar with ScalarPair"),
62 (Scalar::from_bool(if bin_op == Eq { eq } else { !eq }), false)
65 Lt | Le | Gt | Ge => {
66 // Just compare the integers.
67 // TODO: Do we really want to *always* do that, even when comparing two live in-bounds pointers?
68 let left = self.force_bits(left.to_scalar()?, left.layout.size)?;
69 let right = self.force_bits(right.to_scalar()?, right.layout.size)?;
70 let res = match bin_op {
75 _ => bug!("We already established it has to be one of these operators."),
77 (Scalar::from_bool(res), false)
81 let pointee_ty = left.layout.ty
83 .expect("Offset called on non-ptr type")
85 let ptr = self.pointer_offset_inbounds(
88 right.to_scalar()?.to_isize(self)?,
93 _ => bug!("Invalid operator on pointers: {:?}", bin_op)
101 ) -> InterpResult<'tcx, bool> {
102 let size = self.pointer_size();
103 // Just compare the integers.
104 // TODO: Do we really want to *always* do that, even when comparing two live in-bounds pointers?
105 let left = self.force_bits(left, size)?;
106 let right = self.force_bits(right, size)?;
110 /// Raises an error if the offset moves the pointer outside of its allocation.
111 /// We consider ZSTs their own huge allocation that doesn't overlap with anything (and nothing
112 /// moves in there because the size is 0). We also consider the NULL pointer its own separate
113 /// allocation, and all the remaining integers pointers their own allocation.
114 fn pointer_offset_inbounds(
117 pointee_ty: Ty<'tcx>,
119 ) -> InterpResult<'tcx, Scalar<Tag>> {
120 // FIXME: assuming here that type size is less than `i64::max_value()`.
121 let pointee_size = self.layout_of(pointee_ty)?.size.bytes() as i64;
123 .checked_mul(pointee_size)
124 .ok_or_else(|| err_panic!(Overflow(mir::BinOp::Mul)))?;
125 // Now let's see what kind of pointer this is.
126 let ptr = if offset == 0 {
128 Scalar::Ptr(ptr) => ptr,
129 Scalar::Raw { .. } => {
130 // Offset 0 on an integer. We accept that, pretending there is
131 // a little zero-sized allocation here.
136 // Offset > 0. We *require* a pointer.
139 // Both old and new pointer must be in-bounds of a *live* allocation.
140 // (Of the same allocation, but that part is trivial with our representation.)
141 self.pointer_inbounds(ptr)?;
142 let ptr = ptr.signed_offset(offset, self)?;
143 self.pointer_inbounds(ptr)?;