2020-03-30 04:07:32 -05:00
|
|
|
use log::trace;
|
|
|
|
|
2020-04-02 17:05:35 -05:00
|
|
|
use rustc_middle::{mir, ty::Ty};
|
2017-07-25 04:32:48 -05:00
|
|
|
|
2018-11-01 02:56:41 -05:00
|
|
|
use crate::*;
|
2017-07-25 04:32:48 -05:00
|
|
|
|
|
|
|
pub trait EvalContextExt<'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,
|
2021-02-19 18:00:00 -06: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
|
|
|
|
2019-12-23 05:56:23 -06:00
|
|
|
fn ptr_eq(&self, left: Scalar<Tag>, right: Scalar<Tag>) -> InterpResult<'tcx, bool>;
|
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-07-24 09:17:49 -05:00
|
|
|
fn binary_ptr_op(
|
2017-07-25 04:32:48 -05:00
|
|
|
&self,
|
|
|
|
bin_op: mir::BinOp,
|
2021-02-19 18:00:00 -06:00
|
|
|
left: &ImmTy<'tcx, Tag>,
|
|
|
|
right: &ImmTy<'tcx, Tag>,
|
2019-08-10 14:19:25 -05:00
|
|
|
) -> InterpResult<'tcx, (Scalar<Tag>, bool, Ty<'tcx>)> {
|
2020-03-30 04:07:32 -05:00
|
|
|
use rustc_middle::mir::BinOp::*;
|
2018-08-28 11:13:58 -05:00
|
|
|
|
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-12-23 05:56:23 -06:00
|
|
|
#[rustfmt::skip]
|
2021-02-19 18:00:00 -06:00
|
|
|
let eq = match (**left, **right) {
|
2019-12-23 05:56:23 -06:00
|
|
|
(Immediate::Scalar(left), Immediate::Scalar(right)) => {
|
2020-07-23 10:50:45 -05:00
|
|
|
self.ptr_eq(left.check_init()?, right.check_init()?)?
|
2019-12-23 05:56:23 -06:00
|
|
|
}
|
|
|
|
(Immediate::ScalarPair(left1, left2), Immediate::ScalarPair(right1, right2)) => {
|
2020-07-23 10:50:45 -05:00
|
|
|
self.ptr_eq(left1.check_init()?, right1.check_init()?)?
|
|
|
|
&& self.ptr_eq(left2.check_init()?, right2.check_init()?)?
|
2019-12-23 05:56:23 -06:00
|
|
|
}
|
2019-02-08 09:27:00 -06:00
|
|
|
_ => 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.
|
2021-07-15 13:33:08 -05:00
|
|
|
let left = left.to_scalar()?.to_bits(left.layout.size)?;
|
|
|
|
let right = right.to_scalar()?.to_bits(right.layout.size)?;
|
2019-07-24 09:22:48 -05:00
|
|
|
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-12-23 05:56:23 -06:00
|
|
|
let pointee_ty =
|
|
|
|
left.layout.ty.builtin_deref(true).expect("Offset called on non-ptr type").ty;
|
2020-05-15 03:44:41 -05:00
|
|
|
let ptr = self.ptr_offset_inbounds(
|
2021-07-15 13:33:08 -05:00
|
|
|
self.scalar_to_ptr(left.to_scalar()?),
|
2017-08-10 10:48:38 -05:00
|
|
|
pointee_ty,
|
2019-11-08 15:07:52 -06:00
|
|
|
right.to_scalar()?.to_machine_isize(self)?,
|
2017-08-10 10:48:38 -05:00
|
|
|
)?;
|
2021-07-15 13:33:08 -05:00
|
|
|
(Scalar::from_maybe_pointer(ptr, self), false, left.layout.ty)
|
2017-08-10 10:48:38 -05:00
|
|
|
}
|
2019-07-24 09:22:48 -05:00
|
|
|
|
2019-12-23 05:56:23 -06:00
|
|
|
_ => bug!("Invalid operator on pointers: {:?}", bin_op),
|
2019-08-03 03:25:55 -05:00
|
|
|
})
|
2017-07-25 04:32:48 -05:00
|
|
|
}
|
|
|
|
|
2019-12-23 05:56:23 -06:00
|
|
|
fn ptr_eq(&self, left: Scalar<Tag>, right: Scalar<Tag>) -> 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.
|
2021-07-15 13:33:08 -05:00
|
|
|
let left = left.to_bits(size)?;
|
|
|
|
let right = right.to_bits(size)?;
|
2019-07-23 14:38:53 -05:00
|
|
|
Ok(left == right)
|
2018-08-28 11:13:58 -05:00
|
|
|
}
|
2017-07-25 04:32:48 -05:00
|
|
|
}
|