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