2017-07-25 04:32:48 -05:00
|
|
|
use rustc::ty;
|
|
|
|
use rustc::mir;
|
|
|
|
|
2017-12-14 04:03:55 -06:00
|
|
|
use super::*;
|
2017-07-25 04:32:48 -05:00
|
|
|
|
2017-07-28 06:08:27 -05:00
|
|
|
use helpers::EvalContextExt as HelperEvalContextExt;
|
|
|
|
|
2017-07-25 04:32:48 -05:00
|
|
|
pub trait EvalContextExt<'tcx> {
|
|
|
|
fn ptr_op(
|
|
|
|
&self,
|
|
|
|
bin_op: mir::BinOp,
|
|
|
|
left: PrimVal,
|
|
|
|
left_ty: ty::Ty<'tcx>,
|
|
|
|
right: PrimVal,
|
|
|
|
right_ty: ty::Ty<'tcx>,
|
|
|
|
) -> EvalResult<'tcx, Option<(PrimVal, bool)>>;
|
|
|
|
|
|
|
|
fn ptr_int_arithmetic(
|
|
|
|
&self,
|
|
|
|
bin_op: mir::BinOp,
|
|
|
|
left: MemoryPointer,
|
|
|
|
right: i128,
|
|
|
|
signed: bool,
|
|
|
|
) -> EvalResult<'tcx, (PrimVal, bool)>;
|
|
|
|
}
|
|
|
|
|
2017-12-14 04:03:55 -06:00
|
|
|
impl<'a, 'tcx> EvalContextExt<'tcx> for EvalContext<'a, 'tcx, super::Evaluator<'tcx>> {
|
2017-07-25 04:32:48 -05:00
|
|
|
fn ptr_op(
|
|
|
|
&self,
|
|
|
|
bin_op: mir::BinOp,
|
|
|
|
left: PrimVal,
|
|
|
|
left_ty: ty::Ty<'tcx>,
|
|
|
|
right: PrimVal,
|
|
|
|
right_ty: ty::Ty<'tcx>,
|
|
|
|
) -> EvalResult<'tcx, Option<(PrimVal, bool)>> {
|
2017-10-05 05:31:47 -05:00
|
|
|
use rustc::mir::interpret::PrimValKind::*;
|
2017-07-25 04:32:48 -05:00
|
|
|
use rustc::mir::BinOp::*;
|
|
|
|
let usize = PrimValKind::from_uint_size(self.memory.pointer_size());
|
|
|
|
let isize = PrimValKind::from_int_size(self.memory.pointer_size());
|
2017-08-10 10:48:38 -05:00
|
|
|
let left_kind = self.ty_to_primval_kind(left_ty)?;
|
2017-07-25 04:32:48 -05:00
|
|
|
let right_kind = self.ty_to_primval_kind(right_ty)?;
|
|
|
|
match bin_op {
|
|
|
|
Offset if left_kind == Ptr && right_kind == usize => {
|
2017-08-10 10:48:38 -05:00
|
|
|
let pointee_ty = left_ty
|
|
|
|
.builtin_deref(true, ty::LvaluePreference::NoPreference)
|
|
|
|
.expect("Offset called on non-ptr type")
|
|
|
|
.ty;
|
|
|
|
let ptr = self.pointer_offset(
|
|
|
|
left.into(),
|
|
|
|
pointee_ty,
|
|
|
|
right.to_bytes()? as i64,
|
|
|
|
)?;
|
2017-07-25 04:32:48 -05:00
|
|
|
Ok(Some((ptr.into_inner_primval(), false)))
|
2017-08-10 10:48:38 -05:00
|
|
|
}
|
2017-07-25 04:32:48 -05:00
|
|
|
// These work on anything
|
|
|
|
Eq if left_kind == right_kind => {
|
|
|
|
let result = match (left, right) {
|
|
|
|
(PrimVal::Bytes(left), PrimVal::Bytes(right)) => left == right,
|
|
|
|
(PrimVal::Ptr(left), PrimVal::Ptr(right)) => left == right,
|
2017-08-10 10:48:38 -05:00
|
|
|
(PrimVal::Undef, _) |
|
|
|
|
(_, PrimVal::Undef) => return err!(ReadUndefBytes),
|
2017-07-25 04:32:48 -05:00
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
Ok(Some((PrimVal::from_bool(result), false)))
|
|
|
|
}
|
|
|
|
Ne if left_kind == right_kind => {
|
|
|
|
let result = match (left, right) {
|
|
|
|
(PrimVal::Bytes(left), PrimVal::Bytes(right)) => left != right,
|
|
|
|
(PrimVal::Ptr(left), PrimVal::Ptr(right)) => left != right,
|
2017-08-10 10:48:38 -05:00
|
|
|
(PrimVal::Undef, _) |
|
|
|
|
(_, PrimVal::Undef) => return err!(ReadUndefBytes),
|
2017-07-25 04:32:48 -05:00
|
|
|
_ => true,
|
|
|
|
};
|
|
|
|
Ok(Some((PrimVal::from_bool(result), false)))
|
|
|
|
}
|
|
|
|
// These need both pointers to be in the same allocation
|
|
|
|
Lt | Le | Gt | Ge | Sub
|
2017-08-10 10:48:38 -05:00
|
|
|
if left_kind == right_kind &&
|
|
|
|
(left_kind == Ptr || left_kind == usize || left_kind == isize) &&
|
|
|
|
left.is_ptr() && right.is_ptr() => {
|
2017-07-25 04:32:48 -05:00
|
|
|
let left = left.to_ptr()?;
|
|
|
|
let right = right.to_ptr()?;
|
|
|
|
if left.alloc_id == right.alloc_id {
|
|
|
|
let res = match bin_op {
|
|
|
|
Lt => left.offset < right.offset,
|
|
|
|
Le => left.offset <= right.offset,
|
|
|
|
Gt => left.offset > right.offset,
|
|
|
|
Ge => left.offset >= right.offset,
|
2017-08-10 10:48:38 -05:00
|
|
|
Sub => {
|
|
|
|
return self.binary_op(
|
|
|
|
Sub,
|
|
|
|
PrimVal::Bytes(left.offset as u128),
|
|
|
|
self.tcx.types.usize,
|
|
|
|
PrimVal::Bytes(right.offset as u128),
|
|
|
|
self.tcx.types.usize,
|
|
|
|
).map(Some)
|
|
|
|
}
|
2017-07-25 04:32:48 -05:00
|
|
|
_ => bug!("We already established it has to be one of these operators."),
|
|
|
|
};
|
|
|
|
Ok(Some((PrimVal::from_bool(res), false)))
|
|
|
|
} else {
|
|
|
|
// Both are pointers, but from different allocations.
|
2017-08-02 09:59:01 -05:00
|
|
|
err!(InvalidPointerMath)
|
2017-07-25 04:32:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// These work if one operand is a pointer, the other an integer
|
|
|
|
Add | BitAnd | Sub
|
2017-08-10 10:48:38 -05:00
|
|
|
if left_kind == right_kind && (left_kind == usize || left_kind == isize) &&
|
|
|
|
left.is_ptr() && right.is_bytes() => {
|
2017-07-25 04:32:48 -05:00
|
|
|
// Cast to i128 is fine as we checked the kind to be ptr-sized
|
2017-08-10 10:48:38 -05:00
|
|
|
self.ptr_int_arithmetic(
|
|
|
|
bin_op,
|
|
|
|
left.to_ptr()?,
|
|
|
|
right.to_bytes()? as i128,
|
|
|
|
left_kind == isize,
|
|
|
|
).map(Some)
|
2017-07-25 04:32:48 -05:00
|
|
|
}
|
|
|
|
Add | BitAnd
|
2017-08-10 10:48:38 -05:00
|
|
|
if left_kind == right_kind && (left_kind == usize || left_kind == isize) &&
|
|
|
|
left.is_bytes() && right.is_ptr() => {
|
2017-07-25 04:32:48 -05:00
|
|
|
// This is a commutative operation, just swap the operands
|
2017-08-10 10:48:38 -05:00
|
|
|
self.ptr_int_arithmetic(
|
|
|
|
bin_op,
|
|
|
|
right.to_ptr()?,
|
|
|
|
left.to_bytes()? as i128,
|
|
|
|
left_kind == isize,
|
|
|
|
).map(Some)
|
2017-07-25 04:32:48 -05:00
|
|
|
}
|
2017-08-10 10:48:38 -05:00
|
|
|
_ => Ok(None),
|
2017-07-25 04:32:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ptr_int_arithmetic(
|
|
|
|
&self,
|
|
|
|
bin_op: mir::BinOp,
|
|
|
|
left: MemoryPointer,
|
|
|
|
right: i128,
|
|
|
|
signed: bool,
|
|
|
|
) -> EvalResult<'tcx, (PrimVal, bool)> {
|
|
|
|
use rustc::mir::BinOp::*;
|
|
|
|
|
2017-08-10 10:48:38 -05:00
|
|
|
fn map_to_primval((res, over): (MemoryPointer, bool)) -> (PrimVal, bool) {
|
2017-07-25 04:32:48 -05:00
|
|
|
(PrimVal::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, self)),
|
|
|
|
Add if signed =>
|
|
|
|
map_to_primval(left.overflowing_signed_offset(right, self)),
|
|
|
|
Add if !signed =>
|
|
|
|
map_to_primval(left.overflowing_offset(right as u64, self)),
|
|
|
|
|
|
|
|
BitAnd if !signed => {
|
2018-01-02 16:43:03 -06:00
|
|
|
let base_mask : u64 = !(self.memory.get(left.alloc_id)?.align.abi() - 1);
|
2017-07-25 04:32:48 -05:00
|
|
|
let right = right as u64;
|
|
|
|
if right & base_mask == base_mask {
|
|
|
|
// Case 1: The base address bits are all preserved, i.e., right is all-1 there
|
|
|
|
(PrimVal::Ptr(MemoryPointer::new(left.alloc_id, left.offset & right)), false)
|
|
|
|
} else if right & base_mask == 0 {
|
|
|
|
// Case 2: The base address bits are all taken away, i.e., right is all-0 there
|
|
|
|
(PrimVal::from_u128((left.offset & right) as u128), false)
|
|
|
|
} else {
|
2017-08-02 09:59:01 -05:00
|
|
|
return err!(ReadPointerAsBytes);
|
2017-07-25 04:32:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => {
|
|
|
|
let msg = format!("unimplemented binary op on pointer {:?}: {:?}, {:?} ({})", bin_op, left, right, if signed { "signed" } else { "unsigned" });
|
2017-08-02 09:59:01 -05:00
|
|
|
return err!(Unimplemented(msg));
|
2017-07-25 04:32:48 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|