]> git.lizzy.rs Git - rust.git/blob - src/librustc/mir/interpret/pointer.rs
Rollup merge of #61241 - spastorino:check-place-iterative, r=oli-obk
[rust.git] / src / librustc / mir / interpret / pointer.rs
1 use std::fmt;
2
3 use crate::mir;
4 use crate::ty::layout::{self, HasDataLayout, Size};
5 use rustc_macros::HashStable;
6
7 use super::{
8     AllocId, EvalResult, CheckInAllocMsg
9 };
10
11 ////////////////////////////////////////////////////////////////////////////////
12 // Pointer arithmetic
13 ////////////////////////////////////////////////////////////////////////////////
14
15 pub trait PointerArithmetic: layout::HasDataLayout {
16     // These are not supposed to be overridden.
17
18     #[inline(always)]
19     fn pointer_size(&self) -> Size {
20         self.data_layout().pointer_size
21     }
22
23     //// Trunace the given value to the pointer size; also return whether there was an overflow
24     #[inline]
25     fn truncate_to_ptr(&self, val: u128) -> (u64, bool) {
26         let max_ptr_plus_1 = 1u128 << self.pointer_size().bits();
27         ((val % max_ptr_plus_1) as u64, val >= max_ptr_plus_1)
28     }
29
30     #[inline]
31     fn offset<'tcx>(&self, val: u64, i: u64) -> EvalResult<'tcx, u64> {
32         let (res, over) = self.overflowing_offset(val, i);
33         if over { err!(Overflow(mir::BinOp::Add)) } else { Ok(res) }
34     }
35
36     #[inline]
37     fn overflowing_offset(&self, val: u64, i: u64) -> (u64, bool) {
38         let (res, over1) = val.overflowing_add(i);
39         let (res, over2) = self.truncate_to_ptr(u128::from(res));
40         (res, over1 || over2)
41     }
42
43     #[inline]
44     fn signed_offset<'tcx>(&self, val: u64, i: i64) -> EvalResult<'tcx, u64> {
45         let (res, over) = self.overflowing_signed_offset(val, i128::from(i));
46         if over { err!(Overflow(mir::BinOp::Add)) } else { Ok(res) }
47     }
48
49     // Overflow checking only works properly on the range from -u64 to +u64.
50     #[inline]
51     fn overflowing_signed_offset(&self, val: u64, i: i128) -> (u64, bool) {
52         // FIXME: is it possible to over/underflow here?
53         if i < 0 {
54             // trickery to ensure that i64::min_value() works fine
55             // this formula only works for true negative values, it panics for zero!
56             let n = u64::max_value() - (i as u64) + 1;
57             val.overflowing_sub(n)
58         } else {
59             self.overflowing_offset(val, i as u64)
60         }
61     }
62 }
63
64 impl<T: layout::HasDataLayout> PointerArithmetic for T {}
65
66
67 /// Pointer is generic over the type that represents a reference to Allocations,
68 /// thus making it possible for the most convenient representation to be used in
69 /// each context.
70 ///
71 /// Defaults to the index based and loosely coupled AllocId.
72 ///
73 /// Pointer is also generic over the `Tag` associated with each pointer,
74 /// which is used to do provenance tracking during execution.
75 #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd,
76          RustcEncodable, RustcDecodable, Hash, HashStable)]
77 pub struct Pointer<Tag=(),Id=AllocId> {
78     pub alloc_id: Id,
79     pub offset: Size,
80     pub tag: Tag,
81 }
82
83 static_assert_size!(Pointer, 16);
84
85 impl<Tag: fmt::Debug, Id: fmt::Debug> fmt::Debug for Pointer<Tag, Id> {
86     default fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
87         write!(f, "{:?}.{:#x}[{:?}]", self.alloc_id, self.offset.bytes(), self.tag)
88     }
89 }
90 // Specialization for no tag
91 impl<Id: fmt::Debug> fmt::Debug for Pointer<(), Id> {
92     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
93         write!(f, "{:?}.{:#x}", self.alloc_id, self.offset.bytes())
94     }
95 }
96
97 /// Produces a `Pointer` which points to the beginning of the Allocation
98 impl From<AllocId> for Pointer {
99     #[inline(always)]
100     fn from(alloc_id: AllocId) -> Self {
101         Pointer::new(alloc_id, Size::ZERO)
102     }
103 }
104
105 impl<'tcx> Pointer<()> {
106     #[inline(always)]
107     pub fn new(alloc_id: AllocId, offset: Size) -> Self {
108         Pointer { alloc_id, offset, tag: () }
109     }
110
111     #[inline(always)]
112     pub fn with_tag<Tag>(self, tag: Tag) -> Pointer<Tag>
113     {
114         Pointer::new_with_tag(self.alloc_id, self.offset, tag)
115     }
116
117     #[inline(always)]
118     pub fn with_default_tag<Tag>(self) -> Pointer<Tag>
119         where Tag: Default
120     {
121         self.with_tag(Tag::default())
122     }
123 }
124
125 impl<'tcx, Tag> Pointer<Tag> {
126     #[inline(always)]
127     pub fn new_with_tag(alloc_id: AllocId, offset: Size, tag: Tag) -> Self {
128         Pointer { alloc_id, offset, tag }
129     }
130
131     #[inline]
132     pub fn offset(self, i: Size, cx: &impl HasDataLayout) -> EvalResult<'tcx, Self> {
133         Ok(Pointer::new_with_tag(
134             self.alloc_id,
135             Size::from_bytes(cx.data_layout().offset(self.offset.bytes(), i.bytes())?),
136             self.tag
137         ))
138     }
139
140     #[inline]
141     pub fn overflowing_offset(self, i: Size, cx: &impl HasDataLayout) -> (Self, bool) {
142         let (res, over) = cx.data_layout().overflowing_offset(self.offset.bytes(), i.bytes());
143         (Pointer::new_with_tag(self.alloc_id, Size::from_bytes(res), self.tag), over)
144     }
145
146     #[inline(always)]
147     pub fn wrapping_offset(self, i: Size, cx: &impl HasDataLayout) -> Self {
148         self.overflowing_offset(i, cx).0
149     }
150
151     #[inline]
152     pub fn signed_offset(self, i: i64, cx: &impl HasDataLayout) -> EvalResult<'tcx, Self> {
153         Ok(Pointer::new_with_tag(
154             self.alloc_id,
155             Size::from_bytes(cx.data_layout().signed_offset(self.offset.bytes(), i)?),
156             self.tag,
157         ))
158     }
159
160     #[inline]
161     pub fn overflowing_signed_offset(self, i: i128, cx: &impl HasDataLayout) -> (Self, bool) {
162         let (res, over) = cx.data_layout().overflowing_signed_offset(self.offset.bytes(), i);
163         (Pointer::new_with_tag(self.alloc_id, Size::from_bytes(res), self.tag), over)
164     }
165
166     #[inline(always)]
167     pub fn wrapping_signed_offset(self, i: i64, cx: &impl HasDataLayout) -> Self {
168         self.overflowing_signed_offset(i128::from(i), cx).0
169     }
170
171     #[inline(always)]
172     pub fn erase_tag(self) -> Pointer {
173         Pointer { alloc_id: self.alloc_id, offset: self.offset, tag: () }
174     }
175
176     #[inline(always)]
177     pub fn check_in_alloc(
178         self,
179         allocation_size: Size,
180         msg: CheckInAllocMsg,
181     ) -> EvalResult<'tcx, ()> {
182         if self.offset > allocation_size {
183             err!(PointerOutOfBounds {
184                 ptr: self.erase_tag(),
185                 msg,
186                 allocation_size,
187             })
188         } else {
189             Ok(())
190         }
191     }
192 }