1 use super::{AllocId, InterpResult};
3 use crate::ty::layout::{self, HasDataLayout, Size};
5 use rustc_macros::HashStable;
7 use std::convert::TryFrom;
8 use std::fmt::{self, Display};
10 /// Used by `check_in_alloc` to indicate context of check
11 #[derive(Debug, Copy, Clone, RustcEncodable, RustcDecodable, HashStable)]
12 pub enum CheckInAllocMsg {
15 PointerArithmeticTest,
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 {
27 CheckInAllocMsg::MemoryAccessTest => "Memory access",
28 CheckInAllocMsg::NullPointerTest => "Null pointer test",
29 CheckInAllocMsg::PointerArithmeticTest => "Pointer arithmetic",
30 CheckInAllocMsg::InboundsTest => "Inbounds test",
36 ////////////////////////////////////////////////////////////////////////////////
38 ////////////////////////////////////////////////////////////////////////////////
40 pub trait PointerArithmetic: layout::HasDataLayout {
41 // These are not supposed to be overridden.
44 fn pointer_size(&self) -> Size {
45 self.data_layout().pointer_size
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()
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()
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!
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)
71 fn overflowing_offset(&self, val: u64, i: u64) -> (u64, bool) {
72 let res = val.overflowing_add(i);
73 self.truncate_to_ptr(res)
76 // Overflow checking only works properly on the range from -u64 to +u64.
78 fn overflowing_signed_offset(&self, val: u64, i: i128) -> (u64, bool) {
79 // FIXME: is it possible to over/underflow here?
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)
87 self.overflowing_offset(val, i as u64)
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) }
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) }
104 impl<T: layout::HasDataLayout> PointerArithmetic for T {}
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
110 /// Defaults to the index based and loosely coupled `AllocId`.
112 /// `Pointer` is also generic over the `Tag` associated with each pointer,
113 /// which is used to do provenance tracking during execution.
126 pub struct Pointer<Tag = (), Id = AllocId> {
132 static_assert_size!(Pointer, 16);
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)
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())
146 /// Produces a `Pointer` that points to the beginning of the `Allocation`.
147 impl From<AllocId> for Pointer {
149 fn from(alloc_id: AllocId) -> Self {
150 Pointer::new(alloc_id, Size::ZERO)
156 pub fn new(alloc_id: AllocId, offset: Size) -> Self {
157 Pointer { alloc_id, offset, tag: () }
161 pub fn with_tag<Tag>(self, tag: Tag) -> Pointer<Tag> {
162 Pointer::new_with_tag(self.alloc_id, self.offset, tag)
166 impl<'tcx, Tag> Pointer<Tag> {
168 pub fn new_with_tag(alloc_id: AllocId, offset: Size, tag: Tag) -> Self {
169 Pointer { alloc_id, offset, tag }
173 pub fn offset(self, i: Size, cx: &impl HasDataLayout) -> InterpResult<'tcx, Self> {
174 Ok(Pointer::new_with_tag(
176 Size::from_bytes(cx.data_layout().offset(self.offset.bytes(), i.bytes())?),
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)
188 pub fn wrapping_offset(self, i: Size, cx: &impl HasDataLayout) -> Self {
189 self.overflowing_offset(i, cx).0
193 pub fn signed_offset(self, i: i64, cx: &impl HasDataLayout) -> InterpResult<'tcx, Self> {
194 Ok(Pointer::new_with_tag(
196 Size::from_bytes(cx.data_layout().signed_offset(self.offset.bytes(), i)?),
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)
208 pub fn wrapping_signed_offset(self, i: i64, cx: &impl HasDataLayout) -> Self {
209 self.overflowing_signed_offset(i128::from(i), cx).0
213 pub fn erase_tag(self) -> Pointer {
214 Pointer { alloc_id: self.alloc_id, offset: self.offset, tag: () }
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.
221 pub fn check_inbounds_alloc(
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 })