6 pub trait EvalContextExt<'tcx> {
10 left: ImmTy<'tcx, Borrow>,
11 right: ImmTy<'tcx, Borrow>,
12 ) -> EvalResult<'tcx, (Scalar<Borrow>, bool)>;
14 fn ptr_int_arithmetic(
17 left: Pointer<Borrow>,
20 ) -> EvalResult<'tcx, (Scalar<Borrow>, bool)>;
25 right: Scalar<Borrow>,
26 ) -> EvalResult<'tcx, bool>;
28 fn pointer_offset_inbounds(
33 ) -> EvalResult<'tcx, Scalar<Borrow>>;
36 impl<'a, 'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'a, 'mir, 'tcx> {
40 left: ImmTy<'tcx, Borrow>,
41 right: ImmTy<'tcx, Borrow>,
42 ) -> EvalResult<'tcx, (Scalar<Borrow>, bool)> {
43 use rustc::mir::BinOp::*;
45 trace!("ptr_op: {:?} {:?} {:?}", *left, bin_op, *right);
47 // Operations that support fat pointers
50 let eq = match (*left, *right) {
51 (Immediate::Scalar(left), Immediate::Scalar(right)) =>
52 self.ptr_eq(left.not_undef()?, right.not_undef()?)?,
53 (Immediate::ScalarPair(left1, left2), Immediate::ScalarPair(right1, right2)) =>
54 self.ptr_eq(left1.not_undef()?, right1.not_undef()?)? &&
55 self.ptr_eq(left2.not_undef()?, right2.not_undef()?)?,
56 _ => bug!("Type system should not allow comparing Scalar with ScalarPair"),
58 return Ok((Scalar::from_bool(if bin_op == Eq { eq } else { !eq }), false));
63 // Now we expect no more fat pointers.
64 let left_layout = left.layout;
65 let left = left.to_scalar()?;
66 let right_layout = right.layout;
67 let right = right.to_scalar()?;
68 debug_assert!(left.is_ptr() || right.is_ptr() || bin_op == Offset);
72 let pointee_ty = left_layout.ty
74 .expect("Offset called on non-ptr type")
76 let ptr = self.pointer_offset_inbounds(
79 right.to_isize(self)?,
83 // These need both to be pointer, and fail if they are not in the same location
84 Lt | Le | Gt | Ge | Sub if left.is_ptr() && right.is_ptr() => {
85 let left = left.to_ptr().expect("we checked is_ptr");
86 let right = right.to_ptr().expect("we checked is_ptr");
87 if left.alloc_id == right.alloc_id {
88 let res = match bin_op {
89 Lt => left.offset < right.offset,
90 Le => left.offset <= right.offset,
91 Gt => left.offset > right.offset,
92 Ge => left.offset >= right.offset,
94 // subtract the offsets
95 let left_offset = Scalar::from_uint(left.offset.bytes(), self.memory().pointer_size());
96 let right_offset = Scalar::from_uint(right.offset.bytes(), self.memory().pointer_size());
97 let layout = self.layout_of(self.tcx.types.usize)?;
98 return self.binary_op(
100 ImmTy::from_scalar(left_offset, layout),
101 ImmTy::from_scalar(right_offset, layout),
104 _ => bug!("We already established it has to be one of these operators."),
106 Ok((Scalar::from_bool(res), false))
108 // Both are pointers, but from different allocations.
109 err!(InvalidPointerMath)
112 // These work if the left operand is a pointer, and the right an integer
113 Add | BitAnd | Sub | Rem if left.is_ptr() && right.is_bits() => {
114 // Cast to i128 is fine as we checked the kind to be ptr-sized
115 self.ptr_int_arithmetic(
117 left.to_ptr().expect("we checked is_ptr"),
118 right.to_bits(self.memory().pointer_size()).expect("we checked is_bits"),
119 right_layout.abi.is_signed(),
122 // Commutative operators also work if the integer is on the left
123 Add | BitAnd if left.is_bits() && right.is_ptr() => {
124 // This is a commutative operation, just swap the operands
125 self.ptr_int_arithmetic(
127 right.to_ptr().expect("we checked is_ptr"),
128 left.to_bits(self.memory().pointer_size()).expect("we checked is_bits"),
129 left_layout.abi.is_signed(),
132 // Nothing else works
133 _ => err!(InvalidPointerMath),
139 left: Scalar<Borrow>,
140 right: Scalar<Borrow>,
141 ) -> EvalResult<'tcx, bool> {
142 let size = self.pointer_size();
143 Ok(match (left, right) {
144 (Scalar::Bits { .. }, Scalar::Bits { .. }) =>
145 left.to_bits(size)? == right.to_bits(size)?,
146 (Scalar::Ptr(left), Scalar::Ptr(right)) => {
147 // Comparison illegal if one of them is out-of-bounds, *unless* they
148 // are in the same allocation.
149 if left.alloc_id == right.alloc_id {
150 left.offset == right.offset
152 // This accepts one-past-the end. Thus, there is still technically
153 // some non-determinism that we do not fully rule out when two
154 // allocations sit right next to each other. The C/C++ standards are
155 // somewhat fuzzy about this case, so I think for now this check is
157 // Dead allocations in miri cannot overlap with live allocations, but
158 // on read hardware this can easily happen. Thus for comparisons we require
159 // both pointers to be live.
160 self.memory().check_bounds_ptr(left, InboundsCheck::Live)?;
161 self.memory().check_bounds_ptr(right, InboundsCheck::Live)?;
162 // Two in-bounds pointers, we can compare across allocations.
166 // Comparing ptr and integer.
167 (Scalar::Ptr(ptr), Scalar::Bits { bits, size }) |
168 (Scalar::Bits { bits, size }, Scalar::Ptr(ptr)) => {
169 assert_eq!(size as u64, self.pointer_size().bytes());
170 let bits = bits as u64;
172 // Case I: Comparing with NULL.
174 // Test if the ptr is in-bounds. Then it cannot be NULL.
175 // Even dangling pointers cannot be NULL.
176 if self.memory().check_bounds_ptr(ptr, InboundsCheck::MaybeDead).is_ok() {
181 let (alloc_size, alloc_align) = self.memory()
182 .get_size_and_align(ptr.alloc_id, InboundsCheck::MaybeDead)
183 .expect("determining size+align of dead ptr cannot fail");
185 // Case II: Alignment gives it away
186 if ptr.offset.bytes() % alloc_align.bytes() == 0 {
187 // The offset maintains the allocation alignment, so we know `base+offset`
188 // is aligned by `alloc_align`.
189 // FIXME: We could be even more general, e.g., offset 2 into a 4-aligned
190 // allocation cannot equal 3.
191 if bits % alloc_align.bytes() != 0 {
192 // The integer is *not* aligned. So they cannot be equal.
196 // Case III: The integer is too big, and the allocation goes on a bit
197 // without wrapping around the address space.
199 // Compute the highest address at which this allocation could live.
200 // Substract one more, because it must be possible to add the size
201 // to the base address without overflowing; that is, the very last address
202 // of the address space is never dereferencable (but it can be in-bounds, i.e.,
203 // one-past-the-end).
205 ((1u128 << self.pointer_size().bits())
206 - u128::from(alloc_size.bytes())
209 if let Some(max_addr) = max_base_addr.checked_add(ptr.offset.bytes()) {
211 // The integer is too big, this cannot possibly be equal.
217 // None of the supported cases.
218 return err!(InvalidPointerMath);
223 fn ptr_int_arithmetic(
226 left: Pointer<Borrow>,
229 ) -> EvalResult<'tcx, (Scalar<Borrow>, bool)> {
230 use rustc::mir::BinOp::*;
232 fn map_to_primval((res, over): (Pointer<Borrow>, bool)) -> (Scalar<Borrow>, bool) {
233 (Scalar::Ptr(res), over)
238 // The only way this can overflow is by underflowing, so signdeness of the right
239 // operands does not matter.
240 map_to_primval(left.overflowing_signed_offset(-(right as i128), self)),
242 map_to_primval(left.overflowing_signed_offset(right as i128, self)),
244 map_to_primval(left.overflowing_offset(Size::from_bytes(right as u64), self)),
246 BitAnd if !signed => {
247 let ptr_base_align = self.memory().get(left.alloc_id)?.align.bytes();
249 // FIXME: use `interpret::truncate`, once that takes a `Size` instead of a `Layout`.
250 let shift = 128 - self.memory().pointer_size().bits();
251 let value = !(ptr_base_align as u128 - 1);
252 // Truncate (shift left to drop out leftover values, shift right to fill with zeroes).
253 (value << shift) >> shift
255 let ptr_size = self.memory().pointer_size().bytes() as u8;
256 trace!("ptr BitAnd, align {}, operand {:#010x}, base_mask {:#010x}",
257 ptr_base_align, right, base_mask);
258 if right & base_mask == base_mask {
259 // Case 1: the base address bits are all preserved, i.e., right is all-1 there.
260 let offset = (left.offset.bytes() as u128 & right) as u64;
262 Scalar::Ptr(Pointer::new_with_tag(
264 Size::from_bytes(offset),
269 } else if right & base_mask == 0 {
270 // Case 2: the base address bits are all taken away, i.e., right is all-0 there.
271 (Scalar::Bits { bits: (left.offset.bytes() as u128) & right, size: ptr_size }, false)
273 return err!(ReadPointerAsBytes);
278 // Doing modulo a divisor of the alignment is allowed.
279 // (Intuition: modulo a divisor leaks less information.)
280 let ptr_base_align = self.memory().get(left.alloc_id)?.align.bytes();
281 let right = right as u64;
282 let ptr_size = self.memory().pointer_size().bytes() as u8;
284 // Modulo 1 is always 0.
285 (Scalar::Bits { bits: 0, size: ptr_size }, false)
286 } else if ptr_base_align % right == 0 {
287 // The base address would be cancelled out by the modulo operation, so we can
288 // just take the modulo of the offset.
291 bits: (left.offset.bytes() % right) as u128,
297 return err!(ReadPointerAsBytes);
303 "unimplemented binary op on pointer {:?}: {:?}, {:?} ({})",
307 if signed { "signed" } else { "unsigned" }
309 return err!(Unimplemented(msg));
314 /// Raises an error if the offset moves the pointer outside of its allocation.
315 /// We consider ZSTs their own huge allocation that doesn't overlap with anything (and nothing
316 /// moves in there because the size is 0). We also consider the NULL pointer its own separate
317 /// allocation, and all the remaining integers pointers their own allocation.
318 fn pointer_offset_inbounds(
321 pointee_ty: Ty<'tcx>,
323 ) -> EvalResult<'tcx, Scalar<Borrow>> {
324 // FIXME: assuming here that type size is less than `i64::max_value()`.
325 let pointee_size = self.layout_of(pointee_ty)?.size.bytes() as i64;
327 .checked_mul(pointee_size)
328 .ok_or_else(|| EvalErrorKind::Overflow(mir::BinOp::Mul))?;
329 // Now let's see what kind of pointer this is.
330 if let Scalar::Ptr(ptr) = ptr {
331 // Both old and new pointer must be in-bounds of a *live* allocation.
332 // (Of the same allocation, but that part is trivial with our representation.)
333 self.memory().check_bounds_ptr(ptr, InboundsCheck::Live)?;
334 let ptr = ptr.signed_offset(offset, self)?;
335 self.memory().check_bounds_ptr(ptr, InboundsCheck::Live)?;
338 // An integer pointer. They can only be offset by 0, and we pretend there
339 // is a little zero-sized allocation here.
343 err!(InvalidPointerMath)