]> git.lizzy.rs Git - rust.git/blob - miri/operator.rs
fcc3986015d9115ac5bb77e6170095eea1442888
[rust.git] / miri / operator.rs
1 use rustc::ty;
2 use rustc::mir;
3
4 use rustc_miri::interpret::*;
5
6 pub trait EvalContextExt<'tcx> {
7     fn ptr_op(
8         &self,
9         bin_op: mir::BinOp,
10         left: PrimVal,
11         left_ty: ty::Ty<'tcx>,
12         right: PrimVal,
13         right_ty: ty::Ty<'tcx>,
14     ) -> EvalResult<'tcx, Option<(PrimVal, bool)>>;
15
16     fn ptr_int_arithmetic(
17         &self,
18         bin_op: mir::BinOp,
19         left: MemoryPointer,
20         right: i128,
21         signed: bool,
22     ) -> EvalResult<'tcx, (PrimVal, bool)>;
23 }
24
25 impl<'a, 'tcx> EvalContextExt<'tcx> for EvalContext<'a, 'tcx, super::Evaluator> {
26     fn ptr_op(
27         &self,
28         bin_op: mir::BinOp,
29         left: PrimVal,
30         left_ty: ty::Ty<'tcx>,
31         right: PrimVal,
32         right_ty: ty::Ty<'tcx>,
33     ) -> EvalResult<'tcx, Option<(PrimVal, bool)>> {
34         use rustc_miri::interpret::PrimValKind::*;
35         use rustc::mir::BinOp::*;
36         let usize = PrimValKind::from_uint_size(self.memory.pointer_size());
37         let isize = PrimValKind::from_int_size(self.memory.pointer_size());
38         let left_kind  = self.ty_to_primval_kind(left_ty)?;
39         let right_kind = self.ty_to_primval_kind(right_ty)?;
40         match bin_op {
41             Offset if left_kind == Ptr && right_kind == usize => {
42                 let pointee_ty = left_ty.builtin_deref(true, ty::LvaluePreference::NoPreference).expect("Offset called on non-ptr type").ty;
43                 let ptr = self.pointer_offset(left.into(), pointee_ty, right.to_bytes()? as i64)?;
44                 Ok(Some((ptr.into_inner_primval(), false)))
45             },
46             // These work on anything
47             Eq if left_kind == right_kind => {
48                 let result = match (left, right) {
49                     (PrimVal::Bytes(left), PrimVal::Bytes(right)) => left == right,
50                     (PrimVal::Ptr(left), PrimVal::Ptr(right)) => left == right,
51                     (PrimVal::Undef, _) | (_, PrimVal::Undef) => return Err(EvalError::ReadUndefBytes),
52                     _ => false,
53                 };
54                 Ok(Some((PrimVal::from_bool(result), false)))
55             }
56             Ne if left_kind == right_kind => {
57                 let result = match (left, right) {
58                     (PrimVal::Bytes(left), PrimVal::Bytes(right)) => left != right,
59                     (PrimVal::Ptr(left), PrimVal::Ptr(right)) => left != right,
60                     (PrimVal::Undef, _) | (_, PrimVal::Undef) => return Err(EvalError::ReadUndefBytes),
61                     _ => true,
62                 };
63                 Ok(Some((PrimVal::from_bool(result), false)))
64             }
65             // These need both pointers to be in the same allocation
66             Lt | Le | Gt | Ge | Sub
67             if left_kind == right_kind
68             && (left_kind == Ptr || left_kind == usize || left_kind == isize)
69             && left.is_ptr() && right.is_ptr() => {
70                 let left = left.to_ptr()?;
71                 let right = right.to_ptr()?;
72                 if left.alloc_id == right.alloc_id {
73                     let res = match bin_op {
74                         Lt => left.offset < right.offset,
75                         Le => left.offset <= right.offset,
76                         Gt => left.offset > right.offset,
77                         Ge => left.offset >= right.offset,
78                         Sub => return self.binary_op(
79                             Sub,
80                             PrimVal::Bytes(left.offset as u128),
81                             self.tcx.types.usize,
82                             PrimVal::Bytes(right.offset as u128),
83                             self.tcx.types.usize,
84                         ).map(Some),
85                         _ => bug!("We already established it has to be one of these operators."),
86                     };
87                     Ok(Some((PrimVal::from_bool(res), false)))
88                 } else {
89                     // Both are pointers, but from different allocations.
90                     Err(EvalError::InvalidPointerMath)
91                 }
92             }
93             // These work if one operand is a pointer, the other an integer
94             Add | BitAnd | Sub
95             if left_kind == right_kind && (left_kind == usize || left_kind == isize)
96             && left.is_ptr() && right.is_bytes() => {
97                 // Cast to i128 is fine as we checked the kind to be ptr-sized
98                 self.ptr_int_arithmetic(bin_op, left.to_ptr()?, right.to_bytes()? as i128, left_kind == isize).map(Some)
99             }
100             Add | BitAnd
101             if left_kind == right_kind && (left_kind == usize || left_kind == isize)
102             && left.is_bytes() && right.is_ptr() => {
103                 // This is a commutative operation, just swap the operands
104                 self.ptr_int_arithmetic(bin_op, right.to_ptr()?, left.to_bytes()? as i128, left_kind == isize).map(Some)
105             }
106             _ => Ok(None)
107         }
108     }
109
110     fn ptr_int_arithmetic(
111         &self,
112         bin_op: mir::BinOp,
113         left: MemoryPointer,
114         right: i128,
115         signed: bool,
116     ) -> EvalResult<'tcx, (PrimVal, bool)> {
117         use rustc::mir::BinOp::*;
118
119         fn map_to_primval((res, over) : (MemoryPointer, bool)) -> (PrimVal, bool) {
120             (PrimVal::Ptr(res), over)
121         }
122
123         Ok(match bin_op {
124             Sub =>
125                 // The only way this can overflow is by underflowing, so signdeness of the right operands does not matter
126                 map_to_primval(left.overflowing_signed_offset(-right, self)),
127             Add if signed =>
128                 map_to_primval(left.overflowing_signed_offset(right, self)),
129             Add if !signed =>
130                 map_to_primval(left.overflowing_offset(right as u64, self)),
131
132             BitAnd if !signed => {
133                 let base_mask : u64 = !(self.memory.get(left.alloc_id)?.align - 1);
134                 let right = right as u64;
135                 if right & base_mask == base_mask {
136                     // Case 1: The base address bits are all preserved, i.e., right is all-1 there
137                     (PrimVal::Ptr(MemoryPointer::new(left.alloc_id, left.offset & right)), false)
138                 } else if right & base_mask == 0 {
139                     // Case 2: The base address bits are all taken away, i.e., right is all-0 there
140                     (PrimVal::from_u128((left.offset & right) as u128), false)
141                 } else {
142                     return Err(EvalError::ReadPointerAsBytes);
143                 }
144             }
145
146             _ => {
147                 let msg = format!("unimplemented binary op on pointer {:?}: {:?}, {:?} ({})", bin_op, left, right, if signed { "signed" } else { "unsigned" });
148                 return Err(EvalError::Unimplemented(msg));
149             }
150         })
151     }
152 }