]> git.lizzy.rs Git - rust.git/blob - src/operator.rs
2a90d3e12f4c0f41c51c0f4a837051aed579333e
[rust.git] / src / operator.rs
1 use std::convert::TryFrom;
2
3 use rustc::ty::{Ty, layout::{Size, LayoutOf}};
4 use rustc::mir;
5
6 use crate::*;
7
8 pub trait EvalContextExt<'tcx> {
9     fn binary_ptr_op(
10         &self,
11         bin_op: mir::BinOp,
12         left: ImmTy<'tcx, Tag>,
13         right: ImmTy<'tcx, Tag>,
14     ) -> InterpResult<'tcx, (Scalar<Tag>, bool, Ty<'tcx>)>;
15
16     fn ptr_eq(
17         &self,
18         left: Scalar<Tag>,
19         right: Scalar<Tag>,
20     ) -> InterpResult<'tcx, bool>;
21
22     fn pointer_offset_inbounds(
23         &self,
24         ptr: Scalar<Tag>,
25         pointee_ty: Ty<'tcx>,
26         offset: i64,
27     ) -> InterpResult<'tcx, Scalar<Tag>>;
28 }
29
30 impl<'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'mir, 'tcx> {
31     fn binary_ptr_op(
32         &self,
33         bin_op: mir::BinOp,
34         left: ImmTy<'tcx, Tag>,
35         right: ImmTy<'tcx, Tag>,
36     ) -> InterpResult<'tcx, (Scalar<Tag>, bool, Ty<'tcx>)> {
37         use rustc::mir::BinOp::*;
38
39         trace!("ptr_op: {:?} {:?} {:?}", *left, bin_op, *right);
40
41         Ok(match bin_op {
42             Eq | Ne => {
43                 // This supports fat pointers.
44                 let eq = match (*left, *right) {
45                     (Immediate::Scalar(left), Immediate::Scalar(right)) =>
46                         self.ptr_eq(left.not_undef()?, right.not_undef()?)?,
47                     (Immediate::ScalarPair(left1, left2), Immediate::ScalarPair(right1, right2)) =>
48                         self.ptr_eq(left1.not_undef()?, right1.not_undef()?)? &&
49                         self.ptr_eq(left2.not_undef()?, right2.not_undef()?)?,
50                     _ => bug!("Type system should not allow comparing Scalar with ScalarPair"),
51                 };
52                 (Scalar::from_bool(if bin_op == Eq { eq } else { !eq }), false, self.tcx.types.bool)
53             }
54
55             Lt | Le | Gt | Ge => {
56                 // Just compare the integers.
57                 // TODO: Do we really want to *always* do that, even when comparing two live in-bounds pointers?
58                 let left = self.force_bits(left.to_scalar()?, left.layout.size)?;
59                 let right = self.force_bits(right.to_scalar()?, right.layout.size)?;
60                 let res = match bin_op {
61                     Lt => left < right,
62                     Le => left <= right,
63                     Gt => left > right,
64                     Ge => left >= right,
65                     _ => bug!("We already established it has to be one of these operators."),
66                 };
67                 (Scalar::from_bool(res), false, self.tcx.types.bool)
68             }
69
70             Offset => {
71                 let pointee_ty = left.layout.ty
72                     .builtin_deref(true)
73                     .expect("Offset called on non-ptr type")
74                     .ty;
75                 let ptr = self.pointer_offset_inbounds(
76                     left.to_scalar()?,
77                     pointee_ty,
78                     right.to_scalar()?.to_isize(self)?,
79                 )?;
80                 (ptr, false, left.layout.ty)
81             }
82
83             _ => bug!("Invalid operator on pointers: {:?}", bin_op)
84         })
85     }
86
87     fn ptr_eq(
88         &self,
89         left: Scalar<Tag>,
90         right: Scalar<Tag>,
91     ) -> InterpResult<'tcx, bool> {
92         let size = self.pointer_size();
93         // Just compare the integers.
94         // TODO: Do we really want to *always* do that, even when comparing two live in-bounds pointers?
95         let left = self.force_bits(left, size)?;
96         let right = self.force_bits(right, size)?;
97         Ok(left == right)
98     }
99
100     /// Raises an error if the offset moves the pointer outside of its allocation.
101     /// For integers, we consider each of them their own tiny allocation of size 0,
102     /// so offset-by-0 is okay for them -- except for NULL, which we rule out entirely.
103     fn pointer_offset_inbounds(
104         &self,
105         ptr: Scalar<Tag>,
106         pointee_ty: Ty<'tcx>,
107         offset: i64,
108     ) -> InterpResult<'tcx, Scalar<Tag>> {
109         let pointee_size = i64::try_from(self.layout_of(pointee_ty)?.size.bytes()).unwrap();
110         let offset = offset
111             .checked_mul(pointee_size)
112             .ok_or_else(|| err_panic!(Overflow(mir::BinOp::Mul)))?;
113         // We do this first, to rule out overflows.
114         let offset_ptr = ptr.ptr_signed_offset(offset, self)?;
115         // What we need to check is that starting at `min(ptr, offset_ptr)`,
116         // we could do an access of size `abs(offset)`. Alignment does not matter.
117         let (min_ptr, abs_offset) = if offset >= 0 {
118             (ptr, u64::try_from(offset).unwrap())
119         } else {
120             // Negative offset.
121             // If the negation overflows, the result will be negative so the try_from will fail.
122             (offset_ptr, u64::try_from(-offset).unwrap())
123         };
124         self.memory.check_ptr_access_align(
125             min_ptr,
126             Size::from_bytes(abs_offset),
127             None,
128             CheckInAllocMsg::InboundsTest,
129         )?;
130         // That's it!
131         Ok(offset_ptr)
132     }
133 }