2021-01-13 01:12:08 +00:00
|
|
|
use super::{AllocId, InterpResult};
|
2019-05-24 11:03:28 +00:00
|
|
|
|
2018-12-03 00:14:35 +00:00
|
|
|
use rustc_macros::HashStable;
|
2020-03-31 16:16:47 +00:00
|
|
|
use rustc_target::abi::{HasDataLayout, Size};
|
2018-10-25 16:23:09 +00:00
|
|
|
|
2019-05-17 01:20:14 +00:00
|
|
|
use std::convert::TryFrom;
|
2020-04-30 18:37:58 +00:00
|
|
|
use std::fmt;
|
2019-06-23 14:31:16 +00:00
|
|
|
|
2018-10-25 16:23:09 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Pointer arithmetic
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2020-03-31 16:16:47 +00:00
|
|
|
pub trait PointerArithmetic: HasDataLayout {
|
2018-10-25 16:23:09 +00:00
|
|
|
// These are not supposed to be overridden.
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn pointer_size(&self) -> Size {
|
|
|
|
self.data_layout().pointer_size
|
|
|
|
}
|
|
|
|
|
2019-11-14 09:38:15 +00:00
|
|
|
#[inline]
|
2020-03-28 16:51:11 +00:00
|
|
|
fn machine_usize_max(&self) -> u64 {
|
2019-11-14 09:38:15 +00:00
|
|
|
let max_usize_plus_1 = 1u128 << self.pointer_size().bits();
|
2019-12-22 22:42:04 +00:00
|
|
|
u64::try_from(max_usize_plus_1 - 1).unwrap()
|
2019-11-14 09:38:15 +00:00
|
|
|
}
|
|
|
|
|
2020-05-27 06:14:55 +00:00
|
|
|
#[inline]
|
|
|
|
fn machine_isize_min(&self) -> i64 {
|
|
|
|
let max_isize_plus_1 = 1i128 << (self.pointer_size().bits() - 1);
|
|
|
|
i64::try_from(-max_isize_plus_1).unwrap()
|
|
|
|
}
|
|
|
|
|
2019-11-14 09:38:15 +00:00
|
|
|
#[inline]
|
2020-03-28 16:51:11 +00:00
|
|
|
fn machine_isize_max(&self) -> i64 {
|
2019-12-22 22:42:04 +00:00
|
|
|
let max_isize_plus_1 = 1u128 << (self.pointer_size().bits() - 1);
|
|
|
|
i64::try_from(max_isize_plus_1 - 1).unwrap()
|
2019-11-14 09:38:15 +00:00
|
|
|
}
|
|
|
|
|
2021-07-17 18:12:28 +00:00
|
|
|
#[inline]
|
|
|
|
fn machine_usize_to_isize(&self, val: u64) -> i64 {
|
|
|
|
let val = val as i64;
|
|
|
|
// Now clamp into the machine_isize range.
|
|
|
|
if val > self.machine_isize_max() {
|
|
|
|
// This can only happen the the ptr size is < 64, so we know max_usize_plus_1 fits into
|
|
|
|
// i64.
|
|
|
|
let max_usize_plus_1 = 1u128 << self.pointer_size().bits();
|
|
|
|
val - i64::try_from(max_usize_plus_1).unwrap()
|
|
|
|
} else {
|
|
|
|
val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-26 12:12:54 +00:00
|
|
|
/// Helper function: truncate given value-"overflowed flag" pair to pointer size and
|
|
|
|
/// update "overflowed flag" if there was an overflow.
|
|
|
|
/// This should be called by all the other methods before returning!
|
2018-10-25 16:23:09 +00:00
|
|
|
#[inline]
|
2019-05-26 12:12:54 +00:00
|
|
|
fn truncate_to_ptr(&self, (val, over): (u64, bool)) -> (u64, bool) {
|
2020-03-21 12:49:02 +00:00
|
|
|
let val = u128::from(val);
|
2018-10-25 16:23:09 +00:00
|
|
|
let max_ptr_plus_1 = 1u128 << self.pointer_size().bits();
|
2020-03-21 12:49:02 +00:00
|
|
|
(u64::try_from(val % max_ptr_plus_1).unwrap(), over || val >= max_ptr_plus_1)
|
2018-10-25 16:23:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn overflowing_offset(&self, val: u64, i: u64) -> (u64, bool) {
|
2020-05-27 06:14:55 +00:00
|
|
|
// We do not need to check if i fits in a machine usize. If it doesn't,
|
|
|
|
// either the wrapping_add will wrap or res will not fit in a pointer.
|
2019-05-26 12:12:54 +00:00
|
|
|
let res = val.overflowing_add(i);
|
|
|
|
self.truncate_to_ptr(res)
|
2018-10-25 16:23:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2020-03-24 09:16:39 +00:00
|
|
|
fn overflowing_signed_offset(&self, val: u64, i: i64) -> (u64, bool) {
|
2020-05-27 06:14:55 +00:00
|
|
|
// We need to make sure that i fits in a machine isize.
|
2021-01-13 01:12:08 +00:00
|
|
|
let n = i.unsigned_abs();
|
2020-05-26 08:57:49 +00:00
|
|
|
if i >= 0 {
|
2020-05-27 06:14:55 +00:00
|
|
|
let (val, over) = self.overflowing_offset(val, n);
|
|
|
|
(val, over || i > self.machine_isize_max())
|
2020-05-26 08:57:49 +00:00
|
|
|
} else {
|
2019-05-26 12:12:54 +00:00
|
|
|
let res = val.overflowing_sub(n);
|
2020-05-27 06:14:55 +00:00
|
|
|
let (val, over) = self.truncate_to_ptr(res);
|
|
|
|
(val, over || i < self.machine_isize_min())
|
2018-10-25 16:23:09 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-26 12:12:54 +00:00
|
|
|
|
|
|
|
#[inline]
|
2019-06-07 16:56:27 +00:00
|
|
|
fn offset<'tcx>(&self, val: u64, i: u64) -> InterpResult<'tcx, u64> {
|
2019-05-26 12:12:54 +00:00
|
|
|
let (res, over) = self.overflowing_offset(val, i);
|
2019-12-01 11:08:05 +00:00
|
|
|
if over { throw_ub!(PointerArithOverflow) } else { Ok(res) }
|
2019-05-26 12:12:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-06-07 16:56:27 +00:00
|
|
|
fn signed_offset<'tcx>(&self, val: u64, i: i64) -> InterpResult<'tcx, u64> {
|
2020-03-24 09:16:39 +00:00
|
|
|
let (res, over) = self.overflowing_signed_offset(val, i);
|
2019-12-01 11:08:05 +00:00
|
|
|
if over { throw_ub!(PointerArithOverflow) } else { Ok(res) }
|
2019-05-26 12:12:54 +00:00
|
|
|
}
|
2018-10-25 16:23:09 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 16:16:47 +00:00
|
|
|
impl<T: HasDataLayout> PointerArithmetic for T {}
|
2018-10-25 16:23:09 +00:00
|
|
|
|
2021-07-12 16:22:15 +00:00
|
|
|
/// This trait abstracts over the kind of provenance that is associated with a `Pointer`. It is
|
|
|
|
/// mostly opaque; the `Machine` trait extends it with some more operations that also have access to
|
|
|
|
/// some global state.
|
2021-07-16 17:50:59 +00:00
|
|
|
/// We don't actually care about this `Debug` bound (we use `Provenance::fmt` to format the entire
|
|
|
|
/// pointer), but `derive` adds some unecessary bounds.
|
|
|
|
pub trait Provenance: Copy + fmt::Debug {
|
2021-07-12 18:29:05 +00:00
|
|
|
/// Says whether the `offset` field of `Pointer`s with this provenance is the actual physical address.
|
2021-07-12 16:22:15 +00:00
|
|
|
/// If `true, ptr-to-int casts work by simply discarding the provenance.
|
2021-07-16 07:39:35 +00:00
|
|
|
/// If `false`, ptr-to-int casts are not supported. The offset *must* be relative in that case.
|
2021-07-12 16:22:15 +00:00
|
|
|
const OFFSET_IS_ADDR: bool;
|
|
|
|
|
|
|
|
/// Determines how a pointer should be printed.
|
|
|
|
fn fmt(ptr: &Pointer<Self>, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
|
|
|
where
|
|
|
|
Self: Sized;
|
|
|
|
|
2021-07-16 07:39:35 +00:00
|
|
|
/// Provenance must always be able to identify the allocation this ptr points to.
|
|
|
|
/// (Identifying the offset in that allocation, however, is harder -- use `Memory::ptr_get_alloc` for that.)
|
|
|
|
fn get_alloc_id(self) -> AllocId;
|
2018-10-25 16:23:09 +00:00
|
|
|
}
|
|
|
|
|
2021-07-12 16:22:15 +00:00
|
|
|
impl Provenance for AllocId {
|
|
|
|
// With the `AllocId` as provenance, the `offset` is interpreted *relative to the allocation*,
|
|
|
|
// so ptr-to-int casts are not possible (since we do not know the global physical offset).
|
|
|
|
const OFFSET_IS_ADDR: bool = false;
|
2019-01-08 11:40:25 +00:00
|
|
|
|
2021-07-12 16:22:15 +00:00
|
|
|
fn fmt(ptr: &Pointer<Self>, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
// Forward `alternate` flag to `alloc_id` printing.
|
|
|
|
if f.alternate() {
|
|
|
|
write!(f, "{:#?}", ptr.provenance)?;
|
|
|
|
} else {
|
|
|
|
write!(f, "{:?}", ptr.provenance)?;
|
|
|
|
}
|
|
|
|
// Print offset only if it is non-zero.
|
|
|
|
if ptr.offset.bytes() > 0 {
|
|
|
|
write!(f, "+0x{:x}", ptr.offset.bytes())?;
|
|
|
|
}
|
|
|
|
Ok(())
|
2020-05-01 10:10:06 +00:00
|
|
|
}
|
2021-07-12 16:22:15 +00:00
|
|
|
|
2021-07-16 07:39:35 +00:00
|
|
|
fn get_alloc_id(self) -> AllocId {
|
2021-07-12 16:22:15 +00:00
|
|
|
self
|
2020-05-01 10:10:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-12 16:22:15 +00:00
|
|
|
/// Represents a pointer in the Miri engine.
|
|
|
|
///
|
|
|
|
/// Pointers are "tagged" with provenance information; typically the `AllocId` they belong to.
|
|
|
|
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, TyEncodable, TyDecodable, Hash)]
|
|
|
|
#[derive(HashStable)]
|
|
|
|
pub struct Pointer<Tag = AllocId> {
|
|
|
|
pub(super) offset: Size, // kept private to avoid accidental misinterpretation (meaning depends on `Tag` type)
|
|
|
|
pub provenance: Tag,
|
|
|
|
}
|
|
|
|
|
2021-07-13 06:58:59 +00:00
|
|
|
static_assert_size!(Pointer, 16);
|
2021-07-12 16:22:15 +00:00
|
|
|
|
2020-04-26 16:59:20 +00:00
|
|
|
// We want the `Debug` output to be readable as it is used by `derive(Debug)` for
|
|
|
|
// all the Miri types.
|
2021-07-12 16:22:15 +00:00
|
|
|
impl<Tag: Provenance> fmt::Debug for Pointer<Tag> {
|
2019-05-24 11:03:28 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2021-07-16 17:50:59 +00:00
|
|
|
Provenance::fmt(self, f)
|
2020-04-26 16:14:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-12 16:22:15 +00:00
|
|
|
impl<Tag: Provenance> fmt::Debug for Pointer<Option<Tag>> {
|
2020-04-26 16:59:20 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2021-07-12 16:22:15 +00:00
|
|
|
match self.provenance {
|
2021-07-16 17:50:59 +00:00
|
|
|
Some(tag) => Provenance::fmt(&Pointer::new(tag, self.offset), f),
|
2021-07-12 16:22:15 +00:00
|
|
|
None => write!(f, "0x{:x}", self.offset.bytes()),
|
|
|
|
}
|
2019-05-24 11:03:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-17 01:20:14 +00:00
|
|
|
/// Produces a `Pointer` that points to the beginning of the `Allocation`.
|
2018-10-25 16:23:09 +00:00
|
|
|
impl From<AllocId> for Pointer {
|
|
|
|
#[inline(always)]
|
|
|
|
fn from(alloc_id: AllocId) -> Self {
|
|
|
|
Pointer::new(alloc_id, Size::ZERO)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-12 16:22:15 +00:00
|
|
|
impl<Tag> From<Pointer<Tag>> for Pointer<Option<Tag>> {
|
2018-10-25 16:23:09 +00:00
|
|
|
#[inline(always)]
|
2021-07-12 16:22:15 +00:00
|
|
|
fn from(ptr: Pointer<Tag>) -> Self {
|
|
|
|
let (tag, offset) = ptr.into_parts();
|
|
|
|
Pointer::new(Some(tag), offset)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<Tag> Pointer<Option<Tag>> {
|
2021-07-14 20:10:17 +00:00
|
|
|
pub fn into_pointer_or_addr(self) -> Result<Pointer<Tag>, Size> {
|
2021-07-12 16:22:15 +00:00
|
|
|
match self.provenance {
|
|
|
|
Some(tag) => Ok(Pointer::new(tag, self.offset)),
|
|
|
|
None => Err(self.offset),
|
|
|
|
}
|
2018-10-25 16:23:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-14 20:10:17 +00:00
|
|
|
impl<Tag> Pointer<Option<Tag>> {
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn null() -> Self {
|
|
|
|
Pointer { provenance: None, offset: Size::ZERO }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-25 16:23:09 +00:00
|
|
|
impl<'tcx, Tag> Pointer<Tag> {
|
|
|
|
#[inline(always)]
|
2021-07-12 16:22:15 +00:00
|
|
|
pub fn new(provenance: Tag, offset: Size) -> Self {
|
|
|
|
Pointer { provenance, offset }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Obtain the constituents of this pointer. Not that the meaning of the offset depends on the type `Tag`!
|
|
|
|
/// This function must only be used in the implementation of `Machine::ptr_get_alloc`,
|
|
|
|
/// and when a `Pointer` is taken apart to be stored efficiently in an `Allocation`.
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn into_parts(self) -> (Tag, Size) {
|
|
|
|
(self.provenance, self.offset)
|
|
|
|
}
|
|
|
|
|
2021-07-14 20:10:17 +00:00
|
|
|
pub fn map_provenance(self, f: impl FnOnce(Tag) -> Tag) -> Self {
|
|
|
|
Pointer { provenance: f(self.provenance), ..self }
|
|
|
|
}
|
|
|
|
|
2018-10-25 16:23:09 +00:00
|
|
|
#[inline]
|
2019-06-07 16:56:27 +00:00
|
|
|
pub fn offset(self, i: Size, cx: &impl HasDataLayout) -> InterpResult<'tcx, Self> {
|
2021-07-12 16:22:15 +00:00
|
|
|
Ok(Pointer {
|
|
|
|
offset: Size::from_bytes(cx.data_layout().offset(self.offset.bytes(), i.bytes())?),
|
|
|
|
..self
|
|
|
|
})
|
2018-10-25 16:23:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn overflowing_offset(self, i: Size, cx: &impl HasDataLayout) -> (Self, bool) {
|
|
|
|
let (res, over) = cx.data_layout().overflowing_offset(self.offset.bytes(), i.bytes());
|
2021-07-12 16:22:15 +00:00
|
|
|
let ptr = Pointer { offset: Size::from_bytes(res), ..self };
|
|
|
|
(ptr, over)
|
2018-10-25 16:23:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn wrapping_offset(self, i: Size, cx: &impl HasDataLayout) -> Self {
|
|
|
|
self.overflowing_offset(i, cx).0
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-06-07 16:56:27 +00:00
|
|
|
pub fn signed_offset(self, i: i64, cx: &impl HasDataLayout) -> InterpResult<'tcx, Self> {
|
2021-07-12 16:22:15 +00:00
|
|
|
Ok(Pointer {
|
|
|
|
offset: Size::from_bytes(cx.data_layout().signed_offset(self.offset.bytes(), i)?),
|
|
|
|
..self
|
|
|
|
})
|
2018-10-25 16:23:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2020-03-24 09:16:39 +00:00
|
|
|
pub fn overflowing_signed_offset(self, i: i64, cx: &impl HasDataLayout) -> (Self, bool) {
|
2018-10-25 16:23:09 +00:00
|
|
|
let (res, over) = cx.data_layout().overflowing_signed_offset(self.offset.bytes(), i);
|
2021-07-12 16:22:15 +00:00
|
|
|
let ptr = Pointer { offset: Size::from_bytes(res), ..self };
|
|
|
|
(ptr, over)
|
2018-10-25 16:23:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn wrapping_signed_offset(self, i: i64, cx: &impl HasDataLayout) -> Self {
|
2020-03-24 09:16:39 +00:00
|
|
|
self.overflowing_signed_offset(i, cx).0
|
2018-10-25 16:23:09 +00:00
|
|
|
}
|
2018-11-07 15:19:51 +00:00
|
|
|
}
|