rust/src/operator.rs

130 lines
4.9 KiB
Rust
Raw Normal View History

2019-10-24 03:23:44 -05:00
use std::convert::TryFrom;
use rustc::mir;
2019-12-23 05:56:23 -06:00
use rustc::ty::{
layout::{LayoutOf, Size},
Ty,
};
2018-11-01 02:56:41 -05:00
use crate::*;
pub trait EvalContextExt<'tcx> {
fn binary_ptr_op(
&self,
bin_op: mir::BinOp,
left: ImmTy<'tcx, Tag>,
right: ImmTy<'tcx, Tag>,
2019-08-10 14:19:25 -05:00
) -> InterpResult<'tcx, (Scalar<Tag>, bool, Ty<'tcx>)>;
2019-12-23 05:56:23 -06:00
fn ptr_eq(&self, left: Scalar<Tag>, right: Scalar<Tag>) -> InterpResult<'tcx, bool>;
fn pointer_offset_inbounds(
&self,
ptr: Scalar<Tag>,
pointee_ty: Ty<'tcx>,
offset: i64,
2019-06-08 15:14:47 -05:00
) -> InterpResult<'tcx, Scalar<Tag>>;
}
2019-06-13 01:52:04 -05:00
impl<'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'mir, 'tcx> {
fn binary_ptr_op(
&self,
bin_op: mir::BinOp,
left: ImmTy<'tcx, Tag>,
right: ImmTy<'tcx, Tag>,
2019-08-10 14:19:25 -05:00
) -> InterpResult<'tcx, (Scalar<Tag>, bool, Ty<'tcx>)> {
use rustc::mir::BinOp::*;
2019-02-08 09:27:00 -06:00
trace!("ptr_op: {:?} {:?} {:?}", *left, bin_op, *right);
Ok(match bin_op {
2019-02-08 09:27:00 -06:00
Eq | Ne => {
// This supports fat pointers.
2019-12-23 05:56:23 -06:00
#[rustfmt::skip]
2019-02-08 09:27:00 -06:00
let eq = match (*left, *right) {
2019-12-23 05:56:23 -06:00
(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()?)?
}
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
}
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)
}
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;
let ptr = self.pointer_offset_inbounds(
left.to_scalar()?,
pointee_ty,
2019-11-08 15:07:52 -06:00
right.to_scalar()?.to_machine_isize(self)?,
)?;
2019-08-10 14:19:25 -05:00
(ptr, false, left.layout.ty)
}
2019-12-23 05:56:23 -06:00
_ => bug!("Invalid operator on pointers: {:?}", bin_op),
2019-08-03 03:25:55 -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.
// 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)
}
2019-02-15 19:29:38 -06:00
/// Raises an error if the offset moves the pointer outside of its allocation.
/// For integers, we consider each of them their own tiny allocation of size 0,
/// so offset-by-0 is okay for them -- except for NULL, which we rule out entirely.
fn pointer_offset_inbounds(
&self,
ptr: Scalar<Tag>,
pointee_ty: Ty<'tcx>,
offset: i64,
2019-06-08 15:14:47 -05:00
) -> InterpResult<'tcx, Scalar<Tag>> {
2019-10-24 03:23:44 -05:00
let pointee_size = i64::try_from(self.layout_of(pointee_ty)?.size.bytes()).unwrap();
2020-03-01 03:26:24 -06:00
let offset = offset.checked_mul(pointee_size).ok_or_else(|| {
err_ub_format!("overflow during offset comutation for inbounds pointer arithmetic")
})?;
2019-11-04 05:29:15 -06:00
// We do this first, to rule out overflows.
let offset_ptr = ptr.ptr_signed_offset(offset, self)?;
2019-11-06 03:51:06 -06:00
// What we need to check is that starting at `min(ptr, offset_ptr)`,
// we could do an access of size `abs(offset)`. Alignment does not matter.
let (min_ptr, abs_offset) = if offset >= 0 {
(ptr, u64::try_from(offset).unwrap())
} else {
// Negative offset.
// If the negation overflows, the result will be negative so the try_from will fail.
(offset_ptr, u64::try_from(-offset).unwrap())
};
2019-11-04 05:29:15 -06:00
self.memory.check_ptr_access_align(
2019-11-06 03:51:06 -06:00
min_ptr,
Size::from_bytes(abs_offset),
2019-11-04 05:29:15 -06:00
None,
CheckInAllocMsg::InboundsTest,
)?;
// That's it!
Ok(offset_ptr)
}
}