]> git.lizzy.rs Git - rust.git/blob - src/librustc/mir/interpret/pointer.rs
Rollup merge of #69656 - matthiaskrgr:iter_nth_zero, r=oli-obk
[rust.git] / src / librustc / mir / interpret / pointer.rs
1 use super::{AllocId, InterpResult};
2
3 use crate::ty::layout::{self, HasDataLayout, Size};
4
5 use rustc_macros::HashStable;
6
7 use std::convert::TryFrom;
8 use std::fmt::{self, Display};
9
10 /// Used by `check_in_alloc` to indicate context of check
11 #[derive(Debug, Copy, Clone, RustcEncodable, RustcDecodable, HashStable)]
12 pub enum CheckInAllocMsg {
13     MemoryAccessTest,
14     NullPointerTest,
15     PointerArithmeticTest,
16     InboundsTest,
17 }
18
19 impl Display for CheckInAllocMsg {
20     /// When this is printed as an error the context looks like this
21     /// "{test name} failed: pointer must be in-bounds at offset..."
22     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23         write!(
24             f,
25             "{}",
26             match *self {
27                 CheckInAllocMsg::MemoryAccessTest => "Memory access",
28                 CheckInAllocMsg::NullPointerTest => "Null pointer test",
29                 CheckInAllocMsg::PointerArithmeticTest => "Pointer arithmetic",
30                 CheckInAllocMsg::InboundsTest => "Inbounds test",
31             }
32         )
33     }
34 }
35
36 ////////////////////////////////////////////////////////////////////////////////
37 // Pointer arithmetic
38 ////////////////////////////////////////////////////////////////////////////////
39
40 pub trait PointerArithmetic: layout::HasDataLayout {
41     // These are not supposed to be overridden.
42
43     #[inline(always)]
44     fn pointer_size(&self) -> Size {
45         self.data_layout().pointer_size
46     }
47
48     #[inline]
49     fn usize_max(&self) -> u64 {
50         let max_usize_plus_1 = 1u128 << self.pointer_size().bits();
51         u64::try_from(max_usize_plus_1 - 1).unwrap()
52     }
53
54     #[inline]
55     fn isize_max(&self) -> i64 {
56         let max_isize_plus_1 = 1u128 << (self.pointer_size().bits() - 1);
57         i64::try_from(max_isize_plus_1 - 1).unwrap()
58     }
59
60     /// Helper function: truncate given value-"overflowed flag" pair to pointer size and
61     /// update "overflowed flag" if there was an overflow.
62     /// This should be called by all the other methods before returning!
63     #[inline]
64     fn truncate_to_ptr(&self, (val, over): (u64, bool)) -> (u64, bool) {
65         let val = val as u128;
66         let max_ptr_plus_1 = 1u128 << self.pointer_size().bits();
67         ((val % max_ptr_plus_1) as u64, over || val >= max_ptr_plus_1)
68     }
69
70     #[inline]
71     fn overflowing_offset(&self, val: u64, i: u64) -> (u64, bool) {
72         let res = val.overflowing_add(i);
73         self.truncate_to_ptr(res)
74     }
75
76     // Overflow checking only works properly on the range from -u64 to +u64.
77     #[inline]
78     fn overflowing_signed_offset(&self, val: u64, i: i128) -> (u64, bool) {
79         // FIXME: is it possible to over/underflow here?
80         if i < 0 {
81             // Trickery to ensure that `i64::MIN` works fine: compute `n = -i`.
82             // This formula only works for true negative values; it overflows for zero!
83             let n = u64::MAX - (i as u64) + 1;
84             let res = val.overflowing_sub(n);
85             self.truncate_to_ptr(res)
86         } else {
87             self.overflowing_offset(val, i as u64)
88         }
89     }
90
91     #[inline]
92     fn offset<'tcx>(&self, val: u64, i: u64) -> InterpResult<'tcx, u64> {
93         let (res, over) = self.overflowing_offset(val, i);
94         if over { throw_ub!(PointerArithOverflow) } else { Ok(res) }
95     }
96
97     #[inline]
98     fn signed_offset<'tcx>(&self, val: u64, i: i64) -> InterpResult<'tcx, u64> {
99         let (res, over) = self.overflowing_signed_offset(val, i128::from(i));
100         if over { throw_ub!(PointerArithOverflow) } else { Ok(res) }
101     }
102 }
103
104 impl<T: layout::HasDataLayout> PointerArithmetic for T {}
105
106 /// `Pointer` is generic over the type that represents a reference to `Allocation`s,
107 /// thus making it possible for the most convenient representation to be used in
108 /// each context.
109 ///
110 /// Defaults to the index based and loosely coupled `AllocId`.
111 ///
112 /// `Pointer` is also generic over the `Tag` associated with each pointer,
113 /// which is used to do provenance tracking during execution.
114 #[derive(
115     Copy,
116     Clone,
117     Eq,
118     PartialEq,
119     Ord,
120     PartialOrd,
121     RustcEncodable,
122     RustcDecodable,
123     Hash,
124     HashStable
125 )]
126 pub struct Pointer<Tag = (), Id = AllocId> {
127     pub alloc_id: Id,
128     pub offset: Size,
129     pub tag: Tag,
130 }
131
132 static_assert_size!(Pointer, 16);
133
134 impl<Tag: fmt::Debug, Id: fmt::Debug> fmt::Debug for Pointer<Tag, Id> {
135     default fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
136         write!(f, "{:?}+{:x}[{:?}]", self.alloc_id, self.offset.bytes(), self.tag)
137     }
138 }
139 // Specialization for no tag
140 impl<Id: fmt::Debug> fmt::Debug for Pointer<(), Id> {
141     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
142         write!(f, "{:?}+{:x}", self.alloc_id, self.offset.bytes())
143     }
144 }
145
146 /// Produces a `Pointer` that points to the beginning of the `Allocation`.
147 impl From<AllocId> for Pointer {
148     #[inline(always)]
149     fn from(alloc_id: AllocId) -> Self {
150         Pointer::new(alloc_id, Size::ZERO)
151     }
152 }
153
154 impl Pointer<()> {
155     #[inline(always)]
156     pub fn new(alloc_id: AllocId, offset: Size) -> Self {
157         Pointer { alloc_id, offset, tag: () }
158     }
159
160     #[inline(always)]
161     pub fn with_tag<Tag>(self, tag: Tag) -> Pointer<Tag> {
162         Pointer::new_with_tag(self.alloc_id, self.offset, tag)
163     }
164 }
165
166 impl<'tcx, Tag> Pointer<Tag> {
167     #[inline(always)]
168     pub fn new_with_tag(alloc_id: AllocId, offset: Size, tag: Tag) -> Self {
169         Pointer { alloc_id, offset, tag }
170     }
171
172     #[inline]
173     pub fn offset(self, i: Size, cx: &impl HasDataLayout) -> InterpResult<'tcx, Self> {
174         Ok(Pointer::new_with_tag(
175             self.alloc_id,
176             Size::from_bytes(cx.data_layout().offset(self.offset.bytes(), i.bytes())?),
177             self.tag,
178         ))
179     }
180
181     #[inline]
182     pub fn overflowing_offset(self, i: Size, cx: &impl HasDataLayout) -> (Self, bool) {
183         let (res, over) = cx.data_layout().overflowing_offset(self.offset.bytes(), i.bytes());
184         (Pointer::new_with_tag(self.alloc_id, Size::from_bytes(res), self.tag), over)
185     }
186
187     #[inline(always)]
188     pub fn wrapping_offset(self, i: Size, cx: &impl HasDataLayout) -> Self {
189         self.overflowing_offset(i, cx).0
190     }
191
192     #[inline]
193     pub fn signed_offset(self, i: i64, cx: &impl HasDataLayout) -> InterpResult<'tcx, Self> {
194         Ok(Pointer::new_with_tag(
195             self.alloc_id,
196             Size::from_bytes(cx.data_layout().signed_offset(self.offset.bytes(), i)?),
197             self.tag,
198         ))
199     }
200
201     #[inline]
202     pub fn overflowing_signed_offset(self, i: i128, cx: &impl HasDataLayout) -> (Self, bool) {
203         let (res, over) = cx.data_layout().overflowing_signed_offset(self.offset.bytes(), i);
204         (Pointer::new_with_tag(self.alloc_id, Size::from_bytes(res), self.tag), over)
205     }
206
207     #[inline(always)]
208     pub fn wrapping_signed_offset(self, i: i64, cx: &impl HasDataLayout) -> Self {
209         self.overflowing_signed_offset(i128::from(i), cx).0
210     }
211
212     #[inline(always)]
213     pub fn erase_tag(self) -> Pointer {
214         Pointer { alloc_id: self.alloc_id, offset: self.offset, tag: () }
215     }
216
217     /// Test if the pointer is "inbounds" of an allocation of the given size.
218     /// A pointer is "inbounds" even if its offset is equal to the size; this is
219     /// a "one-past-the-end" pointer.
220     #[inline(always)]
221     pub fn check_inbounds_alloc(
222         self,
223         allocation_size: Size,
224         msg: CheckInAllocMsg,
225     ) -> InterpResult<'tcx, ()> {
226         if self.offset > allocation_size {
227             throw_unsup!(PointerOutOfBounds { ptr: self.erase_tag(), msg, allocation_size })
228         } else {
229             Ok(())
230         }
231     }
232 }