2017-07-25 04:32:48 -05:00
|
|
|
use rustc::ty;
|
2018-05-26 10:07:34 -05:00
|
|
|
use rustc::ty::layout::Primitive;
|
2017-07-25 04:32:48 -05:00
|
|
|
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,
|
2018-05-26 10:07:34 -05:00
|
|
|
left: Scalar,
|
2017-07-25 04:32:48 -05:00
|
|
|
left_ty: ty::Ty<'tcx>,
|
2018-05-26 10:07:34 -05:00
|
|
|
right: Scalar,
|
2017-07-25 04:32:48 -05:00
|
|
|
right_ty: ty::Ty<'tcx>,
|
2018-05-26 10:07:34 -05:00
|
|
|
) -> EvalResult<'tcx, Option<(Scalar, bool)>>;
|
2017-07-25 04:32:48 -05:00
|
|
|
|
|
|
|
fn ptr_int_arithmetic(
|
|
|
|
&self,
|
|
|
|
bin_op: mir::BinOp,
|
2018-05-26 10:07:34 -05:00
|
|
|
left: Pointer,
|
2017-07-25 04:32:48 -05:00
|
|
|
right: i128,
|
|
|
|
signed: bool,
|
2018-05-26 10:07:34 -05:00
|
|
|
) -> EvalResult<'tcx, (Scalar, bool)>;
|
2017-07-25 04:32:48 -05:00
|
|
|
}
|
|
|
|
|
2018-01-14 11:59:13 -06:00
|
|
|
impl<'a, 'mir, 'tcx> EvalContextExt<'tcx> for EvalContext<'a, 'mir, 'tcx, super::Evaluator<'tcx>> {
|
2017-07-25 04:32:48 -05:00
|
|
|
fn ptr_op(
|
|
|
|
&self,
|
|
|
|
bin_op: mir::BinOp,
|
2018-05-26 10:07:34 -05:00
|
|
|
left: Scalar,
|
2017-07-25 04:32:48 -05:00
|
|
|
left_ty: ty::Ty<'tcx>,
|
2018-05-26 10:07:34 -05:00
|
|
|
right: Scalar,
|
2017-07-25 04:32:48 -05:00
|
|
|
right_ty: ty::Ty<'tcx>,
|
2018-05-26 10:07:34 -05:00
|
|
|
) -> EvalResult<'tcx, Option<(Scalar, bool)>> {
|
2017-07-25 04:32:48 -05:00
|
|
|
use rustc::mir::BinOp::*;
|
2018-05-26 10:07:34 -05:00
|
|
|
use rustc::ty::layout::Integer::*;
|
|
|
|
let usize = Primitive::Int(match self.memory.pointer_size().bytes() {
|
|
|
|
1 => I8,
|
|
|
|
2 => I16,
|
|
|
|
4 => I32,
|
|
|
|
8 => I64,
|
|
|
|
16 => I128,
|
|
|
|
_ => unreachable!(),
|
|
|
|
}, false);
|
|
|
|
let isize = Primitive::Int(match self.memory.pointer_size().bytes() {
|
|
|
|
1 => I8,
|
|
|
|
2 => I16,
|
|
|
|
4 => I32,
|
|
|
|
8 => I64,
|
|
|
|
16 => I128,
|
|
|
|
_ => unreachable!(),
|
|
|
|
}, true);
|
2018-05-30 07:30:15 -05:00
|
|
|
let left_layout = self.layout_of(left_ty)?;
|
|
|
|
let left_kind = match left_layout.abi {
|
2018-05-26 10:07:34 -05:00
|
|
|
ty::layout::Abi::Scalar(ref scalar) => scalar.value,
|
|
|
|
_ => Err(EvalErrorKind::TypeNotPrimitive(left_ty))?,
|
|
|
|
};
|
2018-05-30 07:30:15 -05:00
|
|
|
let right_layout = self.layout_of(right_ty)?;
|
|
|
|
let right_kind = match right_layout.abi {
|
2018-05-26 10:07:34 -05:00
|
|
|
ty::layout::Abi::Scalar(ref scalar) => scalar.value,
|
|
|
|
_ => Err(EvalErrorKind::TypeNotPrimitive(right_ty))?,
|
|
|
|
};
|
2017-07-25 04:32:48 -05:00
|
|
|
match bin_op {
|
2018-05-26 10:07:34 -05:00
|
|
|
Offset if left_kind == Primitive::Pointer && right_kind == usize => {
|
2017-08-10 10:48:38 -05:00
|
|
|
let pointee_ty = left_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;
|
|
|
|
let ptr = self.pointer_offset(
|
|
|
|
left.into(),
|
|
|
|
pointee_ty,
|
2018-05-26 10:07:34 -05:00
|
|
|
right.to_bits(self.memory.pointer_size())? as i64,
|
2017-08-10 10:48:38 -05:00
|
|
|
)?;
|
2018-05-26 10:07:34 -05:00
|
|
|
Ok(Some((ptr, 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) {
|
2018-05-26 10:07:34 -05:00
|
|
|
(Scalar::Bits { .. }, Scalar::Bits { .. }) => {
|
2018-05-30 07:30:15 -05:00
|
|
|
left.to_bits(left_layout.size)? == right.to_bits(right_layout.size)?
|
2018-05-26 10:07:34 -05:00
|
|
|
},
|
|
|
|
(Scalar::Ptr(left), Scalar::Ptr(right)) => left == right,
|
2017-07-25 04:32:48 -05:00
|
|
|
_ => false,
|
|
|
|
};
|
2018-05-26 10:07:34 -05:00
|
|
|
Ok(Some((Scalar::from_bool(result), false)))
|
2017-07-25 04:32:48 -05:00
|
|
|
}
|
|
|
|
Ne if left_kind == right_kind => {
|
|
|
|
let result = match (left, right) {
|
2018-05-26 10:07:34 -05:00
|
|
|
(Scalar::Bits { .. }, Scalar::Bits { .. }) => {
|
2018-05-30 07:30:15 -05:00
|
|
|
left.to_bits(left_layout.size)? != right.to_bits(right_layout.size)?
|
2018-05-26 10:07:34 -05:00
|
|
|
},
|
|
|
|
(Scalar::Ptr(left), Scalar::Ptr(right)) => left != right,
|
2017-07-25 04:32:48 -05:00
|
|
|
_ => true,
|
|
|
|
};
|
2018-05-26 10:07:34 -05:00
|
|
|
Ok(Some((Scalar::from_bool(result), false)))
|
2017-07-25 04:32:48 -05:00
|
|
|
}
|
|
|
|
// 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 &&
|
2018-05-26 10:07:34 -05:00
|
|
|
(left_kind == Primitive::Pointer || left_kind == usize || left_kind == isize) &&
|
2017-08-10 10:48:38 -05:00
|
|
|
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,
|
2018-05-26 10:07:34 -05:00
|
|
|
Scalar::Bits { bits: left.offset.bytes() as u128, defined: self.memory.pointer_size().bits() as u8 },
|
2017-08-10 10:48:38 -05:00
|
|
|
self.tcx.types.usize,
|
2018-05-26 10:07:34 -05:00
|
|
|
Scalar::Bits { bits: right.offset.bytes() as u128, defined: self.memory.pointer_size().bits() as u8 },
|
2017-08-10 10:48:38 -05:00
|
|
|
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."),
|
|
|
|
};
|
2018-05-26 10:07:34 -05:00
|
|
|
Ok(Some((Scalar::from_bool(res), false)))
|
2017-07-25 04:32:48 -05:00
|
|
|
} 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) &&
|
2018-05-26 10:07:34 -05:00
|
|
|
left.is_ptr() && right.is_bits() => {
|
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()?,
|
2018-05-26 10:07:34 -05:00
|
|
|
right.to_bits(self.memory.pointer_size())? as i128,
|
2017-08-10 10:48:38 -05:00
|
|
|
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) &&
|
2018-05-26 10:07:34 -05:00
|
|
|
left.is_bits() && 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()?,
|
2018-05-26 10:07:34 -05:00
|
|
|
left.to_bits(self.memory.pointer_size())? as i128,
|
2017-08-10 10:48:38 -05:00
|
|
|
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,
|
2018-05-26 10:07:34 -05:00
|
|
|
left: Pointer,
|
2017-07-25 04:32:48 -05:00
|
|
|
right: i128,
|
|
|
|
signed: bool,
|
2018-05-26 10:07:34 -05:00
|
|
|
) -> EvalResult<'tcx, (Scalar, bool)> {
|
2017-07-25 04:32:48 -05:00
|
|
|
use rustc::mir::BinOp::*;
|
|
|
|
|
2018-05-26 10:07:34 -05:00
|
|
|
fn map_to_primval((res, over): (Pointer, bool)) -> (Scalar, bool) {
|
|
|
|
(Scalar::Ptr(res), over)
|
2017-07-25 04:32:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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 =>
|
2018-05-20 04:26:40 -05:00
|
|
|
map_to_primval(left.overflowing_offset(Size::from_bytes(right as u64), self)),
|
2017-07-25 04:32:48 -05:00
|
|
|
|
|
|
|
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
|
2018-05-26 10:07:34 -05:00
|
|
|
(Scalar::Ptr(Pointer::new(left.alloc_id, Size::from_bytes(left.offset.bytes() & right))), false)
|
2017-07-25 04:32:48 -05:00
|
|
|
} else if right & base_mask == 0 {
|
|
|
|
// Case 2: The base address bits are all taken away, i.e., right is all-0 there
|
2018-05-26 10:07:34 -05:00
|
|
|
(Scalar::Bits { bits: (left.offset.bytes() & right) as u128, defined: 128 }, false)
|
2017-07-25 04:32:48 -05:00
|
|
|
} 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
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|