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