]> git.lizzy.rs Git - rust.git/blob - src/operator.rs
f047f4f4fad01d64ff5fcff5766b16dd911f7ab1
[rust.git] / src / operator.rs
1 use rustc::ty::{Ty, layout::LayoutOf};
2 use rustc::mir;
3
4 use crate::*;
5
6 pub trait EvalContextExt<'tcx> {
7     fn pointer_inbounds(
8         &self,
9         ptr: Pointer<Tag>
10     ) -> InterpResult<'tcx>;
11
12     fn binary_ptr_op(
13         &self,
14         bin_op: mir::BinOp,
15         left: ImmTy<'tcx, Tag>,
16         right: ImmTy<'tcx, Tag>,
17     ) -> InterpResult<'tcx, (Scalar<Tag>, bool)>;
18
19     fn ptr_eq(
20         &self,
21         left: Scalar<Tag>,
22         right: Scalar<Tag>,
23     ) -> InterpResult<'tcx, bool>;
24
25     fn pointer_offset_inbounds(
26         &self,
27         ptr: Scalar<Tag>,
28         pointee_ty: Ty<'tcx>,
29         offset: i64,
30     ) -> InterpResult<'tcx, Scalar<Tag>>;
31 }
32
33 impl<'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'mir, 'tcx> {
34     /// Test if the pointer is in-bounds of a live allocation.
35     #[inline]
36     fn pointer_inbounds(&self, ptr: Pointer<Tag>) -> InterpResult<'tcx> {
37         let (size, _align) = self.memory().get_size_and_align(ptr.alloc_id, AllocCheck::Live)?;
38         ptr.check_in_alloc(size, CheckInAllocMsg::InboundsTest)
39     }
40
41     fn binary_ptr_op(
42         &self,
43         bin_op: mir::BinOp,
44         left: ImmTy<'tcx, Tag>,
45         right: ImmTy<'tcx, Tag>,
46     ) -> InterpResult<'tcx, (Scalar<Tag>, bool)> {
47         use rustc::mir::BinOp::*;
48
49         trace!("ptr_op: {:?} {:?} {:?}", *left, bin_op, *right);
50
51         Ok(match bin_op {
52             Eq | Ne => {
53                 // This supports fat pointers.
54                 let eq = match (*left, *right) {
55                     (Immediate::Scalar(left), Immediate::Scalar(right)) =>
56                         self.ptr_eq(left.not_undef()?, right.not_undef()?)?,
57                     (Immediate::ScalarPair(left1, left2), Immediate::ScalarPair(right1, right2)) =>
58                         self.ptr_eq(left1.not_undef()?, right1.not_undef()?)? &&
59                         self.ptr_eq(left2.not_undef()?, right2.not_undef()?)?,
60                     _ => bug!("Type system should not allow comparing Scalar with ScalarPair"),
61                 };
62                 (Scalar::from_bool(if bin_op == Eq { eq } else { !eq }), false)
63             }
64
65             Lt | Le | Gt | Ge => {
66                 // Just compare the integers.
67                 // TODO: Do we really want to *always* do that, even when comparing two live in-bounds pointers?
68                 let left = self.force_bits(left.to_scalar()?, left.layout.size)?;
69                 let right = self.force_bits(right.to_scalar()?, right.layout.size)?;
70                 let res = match bin_op {
71                     Lt => left < right,
72                     Le => left <= right,
73                     Gt => left > right,
74                     Ge => left >= right,
75                     _ => bug!("We already established it has to be one of these operators."),
76                 };
77                 (Scalar::from_bool(res), false)
78             }
79
80             Offset => {
81                 let pointee_ty = left.layout.ty
82                     .builtin_deref(true)
83                     .expect("Offset called on non-ptr type")
84                     .ty;
85                 let ptr = self.pointer_offset_inbounds(
86                     left.to_scalar()?,
87                     pointee_ty,
88                     right.to_scalar()?.to_isize(self)?,
89                 )?;
90                 (ptr, false)
91             }
92
93             _ => bug!("Invalid operator on pointers: {:?}", bin_op)
94         })
95     }
96
97     fn ptr_eq(
98         &self,
99         left: Scalar<Tag>,
100         right: Scalar<Tag>,
101     ) -> InterpResult<'tcx, bool> {
102         let size = self.pointer_size();
103         // Just compare the integers.
104         // TODO: Do we really want to *always* do that, even when comparing two live in-bounds pointers?
105         let left = self.force_bits(left, size)?;
106         let right = self.force_bits(right, size)?;
107         Ok(left == right)
108     }
109
110     /// Raises an error if the offset moves the pointer outside of its allocation.
111     /// We consider ZSTs their own huge allocation that doesn't overlap with anything (and nothing
112     /// moves in there because the size is 0). We also consider the NULL pointer its own separate
113     /// allocation, and all the remaining integers pointers their own allocation.
114     fn pointer_offset_inbounds(
115         &self,
116         ptr: Scalar<Tag>,
117         pointee_ty: Ty<'tcx>,
118         offset: i64,
119     ) -> InterpResult<'tcx, Scalar<Tag>> {
120         // FIXME: assuming here that type size is less than `i64::max_value()`.
121         let pointee_size = self.layout_of(pointee_ty)?.size.bytes() as i64;
122         let offset = offset
123             .checked_mul(pointee_size)
124             .ok_or_else(|| err_panic!(Overflow(mir::BinOp::Mul)))?;
125         // Now let's see what kind of pointer this is.
126         let ptr = if offset == 0 {
127             match ptr {
128                 Scalar::Ptr(ptr) => ptr,
129                 Scalar::Raw { .. } => {
130                     // Offset 0 on an integer. We accept that, pretending there is
131                     // a little zero-sized allocation here.
132                     return Ok(ptr);
133                 }
134             }
135         } else {
136             // Offset > 0. We *require* a pointer.
137             self.force_ptr(ptr)?
138         };
139         // Both old and new pointer must be in-bounds of a *live* allocation.
140         // (Of the same allocation, but that part is trivial with our representation.)
141         self.pointer_inbounds(ptr)?;
142         let ptr = ptr.signed_offset(offset, self)?;
143         self.pointer_inbounds(ptr)?;
144         Ok(Scalar::Ptr(ptr))
145     }
146 }