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