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