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