4 use crate::ty::layout::{self, HasDataLayout, Size};
5 use rustc_macros::HashStable;
8 AllocId, EvalResult, InboundsCheck,
11 ////////////////////////////////////////////////////////////////////////////////
13 ////////////////////////////////////////////////////////////////////////////////
15 pub trait PointerArithmetic: layout::HasDataLayout {
16 // These are not supposed to be overridden.
19 fn pointer_size(&self) -> Size {
20 self.data_layout().pointer_size
23 //// Trunace the given value to the pointer size; also return whether there was an overflow
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)
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) }
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));
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) }
49 // Overflow checking only works properly on the range from -u64 to +u64.
51 fn overflowing_signed_offset(&self, val: u64, i: i128) -> (u64, bool) {
52 // FIXME: is it possible to over/underflow here?
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)
59 self.overflowing_offset(val, i as u64)
64 impl<T: layout::HasDataLayout> PointerArithmetic for T {}
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
71 /// Defaults to the index based and loosely coupled AllocId.
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> {
83 static_assert_size!(Pointer, 16);
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)
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())
97 /// Produces a `Pointer` which points to the beginning of the Allocation
98 impl From<AllocId> for Pointer {
100 fn from(alloc_id: AllocId) -> Self {
101 Pointer::new(alloc_id, Size::ZERO)
105 impl<'tcx> Pointer<()> {
107 pub fn new(alloc_id: AllocId, offset: Size) -> Self {
108 Pointer { alloc_id, offset, tag: () }
112 pub fn with_tag<Tag>(self, tag: Tag) -> Pointer<Tag>
114 Pointer::new_with_tag(self.alloc_id, self.offset, tag)
118 pub fn with_default_tag<Tag>(self) -> Pointer<Tag>
121 self.with_tag(Tag::default())
125 impl<'tcx, Tag> Pointer<Tag> {
127 pub fn new_with_tag(alloc_id: AllocId, offset: Size, tag: Tag) -> Self {
128 Pointer { alloc_id, offset, tag }
132 pub fn offset(self, i: Size, cx: &impl HasDataLayout) -> EvalResult<'tcx, Self> {
133 Ok(Pointer::new_with_tag(
135 Size::from_bytes(cx.data_layout().offset(self.offset.bytes(), i.bytes())?),
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)
147 pub fn wrapping_offset(self, i: Size, cx: &impl HasDataLayout) -> Self {
148 self.overflowing_offset(i, cx).0
152 pub fn signed_offset(self, i: i64, cx: &impl HasDataLayout) -> EvalResult<'tcx, Self> {
153 Ok(Pointer::new_with_tag(
155 Size::from_bytes(cx.data_layout().signed_offset(self.offset.bytes(), i)?),
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)
167 pub fn wrapping_signed_offset(self, i: i64, cx: &impl HasDataLayout) -> Self {
168 self.overflowing_signed_offset(i128::from(i), cx).0
172 pub fn erase_tag(self) -> Pointer {
173 Pointer { alloc_id: self.alloc_id, offset: self.offset, tag: () }
177 pub fn check_in_alloc(
179 allocation_size: Size,
180 check: InboundsCheck,
181 ) -> EvalResult<'tcx, ()> {
182 if self.offset > allocation_size {
183 err!(PointerOutOfBounds {
184 ptr: self.erase_tag(),