2019-07-24 09:22:48 -05:00
|
|
|
use rustc::ty::{Ty, layout::LayoutOf};
|
2017-07-25 04:32:48 -05:00
|
|
|
use rustc::mir;
|
|
|
|
|
2018-11-01 02:56:41 -05:00
|
|
|
use crate::*;
|
2017-07-25 04:32:48 -05:00
|
|
|
|
|
|
|
pub trait EvalContextExt<'tcx> {
|
2019-06-23 10:26:12 -05:00
|
|
|
fn pointer_inbounds(
|
|
|
|
&self,
|
|
|
|
ptr: Pointer<Tag>
|
|
|
|
) -> InterpResult<'tcx>;
|
|
|
|
|
2019-07-24 09:17:49 -05:00
|
|
|
fn binary_ptr_op(
|
2017-07-25 04:32:48 -05:00
|
|
|
&self,
|
|
|
|
bin_op: mir::BinOp,
|
2019-04-15 08:36:09 -05:00
|
|
|
left: ImmTy<'tcx, Tag>,
|
|
|
|
right: ImmTy<'tcx, Tag>,
|
2019-08-10 14:19:25 -05:00
|
|
|
) -> InterpResult<'tcx, (Scalar<Tag>, bool, Ty<'tcx>)>;
|
2017-07-25 04:32:48 -05:00
|
|
|
|
2018-08-28 11:13:58 -05:00
|
|
|
fn ptr_eq(
|
|
|
|
&self,
|
2019-04-15 08:36:09 -05:00
|
|
|
left: Scalar<Tag>,
|
|
|
|
right: Scalar<Tag>,
|
2019-06-08 15:14:47 -05:00
|
|
|
) -> InterpResult<'tcx, bool>;
|
2018-08-28 11:13:58 -05:00
|
|
|
|
2018-08-15 14:01:40 -05:00
|
|
|
fn pointer_offset_inbounds(
|
|
|
|
&self,
|
2019-04-15 08:36:09 -05:00
|
|
|
ptr: Scalar<Tag>,
|
2018-08-15 14:01:40 -05:00
|
|
|
pointee_ty: Ty<'tcx>,
|
|
|
|
offset: i64,
|
2019-06-08 15:14:47 -05:00
|
|
|
) -> InterpResult<'tcx, Scalar<Tag>>;
|
2017-07-25 04:32:48 -05:00
|
|
|
}
|
|
|
|
|
2019-06-13 01:52:04 -05:00
|
|
|
impl<'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'mir, 'tcx> {
|
2019-06-23 10:26:12 -05:00
|
|
|
/// Test if the pointer is in-bounds of a live allocation.
|
|
|
|
#[inline]
|
|
|
|
fn pointer_inbounds(&self, ptr: Pointer<Tag>) -> InterpResult<'tcx> {
|
2019-10-17 21:11:50 -05:00
|
|
|
let (size, _align) = self.memory.get_size_and_align(ptr.alloc_id, AllocCheck::Live)?;
|
2019-07-28 07:55:55 -05:00
|
|
|
ptr.check_inbounds_alloc(size, CheckInAllocMsg::InboundsTest)
|
2019-06-23 10:26:12 -05:00
|
|
|
}
|
|
|
|
|
2019-07-24 09:17:49 -05:00
|
|
|
fn binary_ptr_op(
|
2017-07-25 04:32:48 -05:00
|
|
|
&self,
|
|
|
|
bin_op: mir::BinOp,
|
2019-04-15 08:36:09 -05:00
|
|
|
left: ImmTy<'tcx, Tag>,
|
|
|
|
right: ImmTy<'tcx, Tag>,
|
2019-08-10 14:19:25 -05:00
|
|
|
) -> InterpResult<'tcx, (Scalar<Tag>, bool, Ty<'tcx>)> {
|
2018-08-28 11:13:58 -05:00
|
|
|
use rustc::mir::BinOp::*;
|
|
|
|
|
2019-02-08 09:27:00 -06:00
|
|
|
trace!("ptr_op: {:?} {:?} {:?}", *left, bin_op, *right);
|
|
|
|
|
2019-07-24 09:22:48 -05:00
|
|
|
Ok(match bin_op {
|
2019-02-08 09:27:00 -06:00
|
|
|
Eq | Ne => {
|
2019-07-24 09:22:48 -05:00
|
|
|
// This supports fat pointers.
|
2019-02-08 09:27:00 -06:00
|
|
|
let eq = match (*left, *right) {
|
|
|
|
(Immediate::Scalar(left), Immediate::Scalar(right)) =>
|
|
|
|
self.ptr_eq(left.not_undef()?, right.not_undef()?)?,
|
|
|
|
(Immediate::ScalarPair(left1, left2), Immediate::ScalarPair(right1, right2)) =>
|
|
|
|
self.ptr_eq(left1.not_undef()?, right1.not_undef()?)? &&
|
|
|
|
self.ptr_eq(left2.not_undef()?, right2.not_undef()?)?,
|
|
|
|
_ => bug!("Type system should not allow comparing Scalar with ScalarPair"),
|
|
|
|
};
|
2019-08-10 14:19:25 -05:00
|
|
|
(Scalar::from_bool(if bin_op == Eq { eq } else { !eq }), false, self.tcx.types.bool)
|
2019-02-08 09:27:00 -06:00
|
|
|
}
|
|
|
|
|
2019-07-24 09:22:48 -05:00
|
|
|
Lt | Le | Gt | Ge => {
|
|
|
|
// Just compare the integers.
|
|
|
|
// TODO: Do we really want to *always* do that, even when comparing two live in-bounds pointers?
|
|
|
|
let left = self.force_bits(left.to_scalar()?, left.layout.size)?;
|
|
|
|
let right = self.force_bits(right.to_scalar()?, right.layout.size)?;
|
|
|
|
let res = match bin_op {
|
|
|
|
Lt => left < right,
|
|
|
|
Le => left <= right,
|
|
|
|
Gt => left > right,
|
|
|
|
Ge => left >= right,
|
|
|
|
_ => bug!("We already established it has to be one of these operators."),
|
|
|
|
};
|
2019-08-10 14:19:25 -05:00
|
|
|
(Scalar::from_bool(res), false, self.tcx.types.bool)
|
2019-07-24 09:22:48 -05:00
|
|
|
}
|
2018-07-15 14:25:03 -05:00
|
|
|
|
2018-08-15 14:01:40 -05:00
|
|
|
Offset => {
|
2019-07-24 09:22:48 -05:00
|
|
|
let pointee_ty = left.layout.ty
|
2018-01-14 11:59:13 -06:00
|
|
|
.builtin_deref(true)
|
2017-08-10 10:48:38 -05:00
|
|
|
.expect("Offset called on non-ptr type")
|
|
|
|
.ty;
|
2018-08-15 14:01:40 -05:00
|
|
|
let ptr = self.pointer_offset_inbounds(
|
2019-07-24 09:22:48 -05:00
|
|
|
left.to_scalar()?,
|
2017-08-10 10:48:38 -05:00
|
|
|
pointee_ty,
|
2019-07-24 09:22:48 -05:00
|
|
|
right.to_scalar()?.to_isize(self)?,
|
2017-08-10 10:48:38 -05:00
|
|
|
)?;
|
2019-08-10 14:19:25 -05:00
|
|
|
(ptr, false, left.layout.ty)
|
2017-08-10 10:48:38 -05:00
|
|
|
}
|
2019-07-24 09:22:48 -05:00
|
|
|
|
|
|
|
_ => bug!("Invalid operator on pointers: {:?}", bin_op)
|
2019-08-03 03:25:55 -05:00
|
|
|
})
|
2017-07-25 04:32:48 -05:00
|
|
|
}
|
|
|
|
|
2018-08-28 11:13:58 -05:00
|
|
|
fn ptr_eq(
|
|
|
|
&self,
|
2019-04-15 08:36:09 -05:00
|
|
|
left: Scalar<Tag>,
|
|
|
|
right: Scalar<Tag>,
|
2019-06-08 15:14:47 -05:00
|
|
|
) -> InterpResult<'tcx, bool> {
|
2019-02-08 09:27:00 -06:00
|
|
|
let size = self.pointer_size();
|
2019-07-23 14:38:53 -05:00
|
|
|
// Just compare the integers.
|
|
|
|
// TODO: Do we really want to *always* do that, even when comparing two live in-bounds pointers?
|
|
|
|
let left = self.force_bits(left, size)?;
|
|
|
|
let right = self.force_bits(right, size)?;
|
|
|
|
Ok(left == right)
|
2018-08-28 11:13:58 -05:00
|
|
|
}
|
|
|
|
|
2019-02-15 19:29:38 -06:00
|
|
|
/// 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.
|
2018-08-15 14:01:40 -05:00
|
|
|
fn pointer_offset_inbounds(
|
|
|
|
&self,
|
2019-04-15 08:36:09 -05:00
|
|
|
ptr: Scalar<Tag>,
|
2018-08-15 14:01:40 -05:00
|
|
|
pointee_ty: Ty<'tcx>,
|
|
|
|
offset: i64,
|
2019-06-08 15:14:47 -05:00
|
|
|
) -> InterpResult<'tcx, Scalar<Tag>> {
|
2019-02-15 19:29:38 -06:00
|
|
|
// FIXME: assuming here that type size is less than `i64::max_value()`.
|
2018-08-15 14:01:40 -05:00
|
|
|
let pointee_size = self.layout_of(pointee_ty)?.size.bytes() as i64;
|
2019-02-15 19:29:38 -06:00
|
|
|
let offset = offset
|
|
|
|
.checked_mul(pointee_size)
|
2019-08-03 03:25:55 -05:00
|
|
|
.ok_or_else(|| err_panic!(Overflow(mir::BinOp::Mul)))?;
|
2019-02-15 19:29:38 -06:00
|
|
|
// Now let's see what kind of pointer this is.
|
2019-07-03 03:54:16 -05:00
|
|
|
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);
|
|
|
|
}
|
2018-10-08 03:22:26 -05:00
|
|
|
}
|
2019-07-03 03:54:16 -05:00
|
|
|
} 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))
|
2018-08-15 14:01:40 -05:00
|
|
|
}
|
2017-07-25 04:32:48 -05:00
|
|
|
}
|