-
- fn ptr_int_arithmetic(
- &self,
- bin_op: mir::BinOp,
- left: Pointer<Tag>,
- right: u128,
- signed: bool,
- ) -> InterpResult<'tcx, (Scalar<Tag>, bool)> {
- use rustc::mir::BinOp::*;
-
- fn map_to_primval((res, over): (Pointer<Tag>, bool)) -> (Scalar<Tag>, bool) {
- (Scalar::Ptr(res), over)
- }
-
- Ok(match bin_op {
- Sub =>
- // The only way this can overflow is by underflowing, so signdeness of the right
- // operands does not matter.
- map_to_primval(left.overflowing_signed_offset(-(right as i128), self)),
- Add if signed =>
- map_to_primval(left.overflowing_signed_offset(right as i128, self)),
- Add if !signed =>
- map_to_primval(left.overflowing_offset(Size::from_bytes(right as u64), self)),
-
- BitAnd if !signed => {
- let ptr_base_align = self.memory().get_size_and_align(left.alloc_id, AllocCheck::MaybeDead)
- .expect("alloc info with MaybeDead cannot fail")
- .1.bytes();
- let base_mask = {
- // FIXME: use `interpret::truncate`, once that takes a `Size` instead of a `Layout`.
- let shift = 128 - self.memory().pointer_size().bits();
- let value = !(ptr_base_align as u128 - 1);
- // Truncate (shift left to drop out leftover values, shift right to fill with zeroes).
- (value << shift) >> shift
- };
- let ptr_size = self.memory().pointer_size();
- trace!("ptr BitAnd, align {}, operand {:#010x}, base_mask {:#010x}",
- ptr_base_align, right, base_mask);
- if right & base_mask == base_mask {
- // Case 1: the base address bits are all preserved, i.e., right is all-1 there.
- let offset = (left.offset.bytes() as u128 & right) as u64;
- (
- Scalar::Ptr(Pointer::new_with_tag(
- left.alloc_id,
- Size::from_bytes(offset),
- left.tag,
- )),
- false,
- )
- } else if right & base_mask == 0 {
- // Case 2: the base address bits are all taken away, i.e., right is all-0 there.
- let v = Scalar::from_uint((left.offset.bytes() as u128) & right, ptr_size);
- (v, false)
- } else {
- throw_unsup!(ReadPointerAsBytes);
- }
- }
-
- Rem if !signed => {
- // Doing modulo a divisor of the alignment is allowed.
- // (Intuition: modulo a divisor leaks less information.)
- let ptr_base_align = self.memory().get_size_and_align(left.alloc_id, AllocCheck::MaybeDead)
- .expect("alloc info with MaybeDead cannot fail")
- .1.bytes();
- let right = right as u64;
- let ptr_size = self.memory().pointer_size();
- if right == 1 {
- // Modulo 1 is always 0.
- (Scalar::from_uint(0u32, ptr_size), false)
- } else if ptr_base_align % right == 0 {
- // The base address would be cancelled out by the modulo operation, so we can
- // just take the modulo of the offset.
- (
- Scalar::from_uint((left.offset.bytes() % right) as u128, ptr_size),
- false,
- )
- } else {
- throw_unsup!(ReadPointerAsBytes);
- }
- }
-
- _ => {
- let msg = format!(
- "unimplemented binary op on pointer {:?}: {:?}, {:?} ({})",
- bin_op,
- left,
- right,
- if signed { "signed" } else { "unsigned" }
- );
- throw_unsup!(Unimplemented(msg));
- }
- })
- }
-
- /// Raises an error if the offset moves the pointer outside of its allocation.
- /// We consider ZSTs their own huge allocation that doesn't overlap with anything (and nothing
- /// moves in there because the size is 0). We also consider the NULL pointer its own separate
- /// allocation, and all the remaining integers pointers their own allocation.
- fn pointer_offset_inbounds(
- &self,
- ptr: Scalar<Tag>,
- pointee_ty: Ty<'tcx>,
- offset: i64,
- ) -> InterpResult<'tcx, Scalar<Tag>> {
- // FIXME: assuming here that type size is less than `i64::max_value()`.
- let pointee_size = self.layout_of(pointee_ty)?.size.bytes() as i64;
- let offset = offset
- .checked_mul(pointee_size)
- .ok_or_else(|| err_panic!(Overflow(mir::BinOp::Mul)))?;
- // Now let's see what kind of pointer this is.
- let ptr = if offset == 0 {
- match ptr {
- Scalar::Ptr(ptr) => ptr,
- Scalar::Raw { .. } => {
- // Offset 0 on an integer. We accept that, pretending there is
- // a little zero-sized allocation here.
- return Ok(ptr);
- }
- }
- } else {
- // Offset > 0. We *require* a pointer.
- self.force_ptr(ptr)?
- };
- // Both old and new pointer must be in-bounds of a *live* allocation.
- // (Of the same allocation, but that part is trivial with our representation.)
- self.pointer_inbounds(ptr)?;
- let ptr = ptr.signed_offset(offset, self)?;
- self.pointer_inbounds(ptr)?;
- Ok(Scalar::Ptr(ptr))
- }