mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-22 06:44:35 +00:00
interpret: simplify pointer arithmetic logic
This commit is contained in:
parent
de78cb56b2
commit
5d5c97aad7
@ -516,7 +516,7 @@ impl Size {
|
||||
/// Truncates `value` to `self` bits and then sign-extends it to 128 bits
|
||||
/// (i.e., if it is negative, fill with 1's on the left).
|
||||
#[inline]
|
||||
pub fn sign_extend(self, value: u128) -> u128 {
|
||||
pub fn sign_extend(self, value: u128) -> i128 {
|
||||
let size = self.bits();
|
||||
if size == 0 {
|
||||
// Truncated until nothing is left.
|
||||
@ -526,7 +526,7 @@ impl Size {
|
||||
let shift = 128 - size;
|
||||
// Shift the unsigned value to the left, then shift back to the right as signed
|
||||
// (essentially fills with sign bit on the left).
|
||||
(((value << shift) as i128) >> shift) as u128
|
||||
((value << shift) as i128) >> shift
|
||||
}
|
||||
|
||||
/// Truncates `value` to `self` bits.
|
||||
@ -544,7 +544,7 @@ impl Size {
|
||||
|
||||
#[inline]
|
||||
pub fn signed_int_min(&self) -> i128 {
|
||||
self.sign_extend(1_u128 << (self.bits() - 1)) as i128
|
||||
self.sign_extend(1_u128 << (self.bits() - 1))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
@ -560,17 +560,6 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
|
||||
self.frame().body
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn sign_extend(&self, value: u128, ty: TyAndLayout<'_>) -> u128 {
|
||||
assert!(ty.abi.is_signed());
|
||||
ty.size.sign_extend(value)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn truncate(&self, value: u128, ty: TyAndLayout<'_>) -> u128 {
|
||||
ty.size.truncate(value)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn type_is_freeze(&self, ty: Ty<'tcx>) -> bool {
|
||||
ty.is_freeze(*self.tcx, self.param_env)
|
||||
|
@ -206,7 +206,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
|
||||
} else {
|
||||
(val_bits >> shift_bits) | (val_bits << inv_shift_bits)
|
||||
};
|
||||
let truncated_bits = self.truncate(result_bits, layout_val);
|
||||
let truncated_bits = layout_val.size.truncate(result_bits);
|
||||
let result = Scalar::from_uint(truncated_bits, layout_val.size);
|
||||
self.write_scalar(result, dest)?;
|
||||
}
|
||||
@ -580,13 +580,10 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
|
||||
ptr: Pointer<Option<M::Provenance>>,
|
||||
offset_bytes: i64,
|
||||
) -> InterpResult<'tcx, Pointer<Option<M::Provenance>>> {
|
||||
// We first compute the pointer with overflow checks, to get a specific error for when it
|
||||
// overflows (though technically this is redundant with the following inbounds check).
|
||||
let result = ptr.signed_offset(offset_bytes, self)?;
|
||||
// The offset must be in bounds starting from `ptr`.
|
||||
self.check_ptr_access_signed(ptr, offset_bytes, CheckInAllocMsg::PointerArithmeticTest)?;
|
||||
// Done.
|
||||
Ok(result)
|
||||
// This also implies that there is no overflow, so we are done.
|
||||
Ok(ptr.wrapping_signed_offset(offset_bytes, self))
|
||||
}
|
||||
|
||||
/// Copy `count*size_of::<T>()` many bytes from `*src` to `*dst`.
|
||||
|
@ -473,7 +473,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
|
||||
throw_ub!(PointerOutOfBounds {
|
||||
alloc_id,
|
||||
alloc_size,
|
||||
ptr_offset: self.target_usize_to_isize(offset),
|
||||
ptr_offset: self.sign_extend_to_target_isize(offset),
|
||||
inbounds_size: size,
|
||||
msg,
|
||||
})
|
||||
|
@ -285,10 +285,8 @@ impl<'tcx, Prov: Provenance> Projectable<'tcx, Prov> for PlaceTy<'tcx, Prov> {
|
||||
// projections are type-checked and bounds-checked.
|
||||
assert!(offset + layout.size <= self.layout.size);
|
||||
|
||||
let new_offset = Size::from_bytes(
|
||||
ecx.data_layout()
|
||||
.offset(old_offset.unwrap_or(Size::ZERO).bytes(), offset.bytes())?,
|
||||
);
|
||||
// Size `+`, ensures no overflow.
|
||||
let new_offset = old_offset.unwrap_or(Size::ZERO) + offset;
|
||||
|
||||
PlaceTy {
|
||||
place: Place::Local { local, offset: Some(new_offset), locals_addr },
|
||||
|
@ -362,7 +362,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
|
||||
// of the first element.
|
||||
let elem_size = first.layout.size;
|
||||
let first_ptr = first.ptr();
|
||||
let rest_ptr = first_ptr.offset(elem_size, self)?;
|
||||
let rest_ptr = first_ptr.wrapping_offset(elem_size, self);
|
||||
// No alignment requirement since `copy_op` above already checked it.
|
||||
self.mem_copy_repeatedly(
|
||||
first_ptr,
|
||||
|
@ -309,11 +309,7 @@ fn report_bin_hex_error(
|
||||
) {
|
||||
let (t, actually) = match ty {
|
||||
attr::IntType::SignedInt(t) => {
|
||||
let actually = if negative {
|
||||
-(size.sign_extend(val) as i128)
|
||||
} else {
|
||||
size.sign_extend(val) as i128
|
||||
};
|
||||
let actually = if negative { -(size.sign_extend(val)) } else { size.sign_extend(val) };
|
||||
(t.name_str(), actually.to_string())
|
||||
}
|
||||
attr::IntType::UnsignedInt(t) => {
|
||||
|
@ -5,7 +5,7 @@ use rustc_data_structures::static_assert_size;
|
||||
use rustc_macros::{HashStable, TyDecodable, TyEncodable};
|
||||
use rustc_target::abi::{HasDataLayout, Size};
|
||||
|
||||
use super::{AllocId, InterpResult};
|
||||
use super::AllocId;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Pointer arithmetic
|
||||
@ -40,62 +40,13 @@ pub trait PointerArithmetic: HasDataLayout {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn target_usize_to_isize(&self, val: u64) -> i64 {
|
||||
let val = val as i64;
|
||||
// Now wrap-around into the machine_isize range.
|
||||
if val > self.target_isize_max() {
|
||||
// This can only happen if the ptr size is < 64, so we know max_usize_plus_1 fits into
|
||||
// i64.
|
||||
debug_assert!(self.pointer_size().bits() < 64);
|
||||
let max_usize_plus_1 = 1u128 << self.pointer_size().bits();
|
||||
val - i64::try_from(max_usize_plus_1).unwrap()
|
||||
} else {
|
||||
val
|
||||
}
|
||||
}
|
||||
|
||||
/// 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!
|
||||
#[inline]
|
||||
fn truncate_to_ptr(&self, (val, over): (u64, bool)) -> (u64, bool) {
|
||||
let val = u128::from(val);
|
||||
let max_ptr_plus_1 = 1u128 << self.pointer_size().bits();
|
||||
(u64::try_from(val % max_ptr_plus_1).unwrap(), over || val >= max_ptr_plus_1)
|
||||
fn truncate_to_target_usize(&self, val: u64) -> u64 {
|
||||
self.pointer_size().truncate(val.into()).try_into().unwrap()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn overflowing_offset(&self, val: u64, i: u64) -> (u64, bool) {
|
||||
// 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.
|
||||
let res = val.overflowing_add(i);
|
||||
self.truncate_to_ptr(res)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn overflowing_signed_offset(&self, val: u64, i: i64) -> (u64, bool) {
|
||||
// We need to make sure that i fits in a machine isize.
|
||||
let n = i.unsigned_abs();
|
||||
if i >= 0 {
|
||||
let (val, over) = self.overflowing_offset(val, n);
|
||||
(val, over || i > self.target_isize_max())
|
||||
} else {
|
||||
let res = val.overflowing_sub(n);
|
||||
let (val, over) = self.truncate_to_ptr(res);
|
||||
(val, over || i < self.target_isize_min())
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn offset<'tcx>(&self, val: u64, i: u64) -> InterpResult<'tcx, u64> {
|
||||
let (res, over) = self.overflowing_offset(val, i);
|
||||
if over { throw_ub!(PointerArithOverflow) } else { Ok(res) }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn signed_offset<'tcx>(&self, val: u64, i: i64) -> InterpResult<'tcx, u64> {
|
||||
let (res, over) = self.overflowing_signed_offset(val, i);
|
||||
if over { throw_ub!(PointerArithOverflow) } else { Ok(res) }
|
||||
fn sign_extend_to_target_isize(&self, val: u64) -> i64 {
|
||||
self.pointer_size().sign_extend(val.into()).try_into().unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
@ -331,7 +282,7 @@ impl<Prov> Pointer<Option<Prov>> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx, Prov> Pointer<Prov> {
|
||||
impl<Prov> Pointer<Prov> {
|
||||
#[inline(always)]
|
||||
pub fn new(provenance: Prov, offset: Size) -> Self {
|
||||
Pointer { provenance, offset }
|
||||
@ -349,43 +300,16 @@ impl<'tcx, Prov> Pointer<Prov> {
|
||||
Pointer { provenance: f(self.provenance), ..self }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn offset(self, i: Size, cx: &impl HasDataLayout) -> InterpResult<'tcx, Self> {
|
||||
Ok(Pointer {
|
||||
offset: Size::from_bytes(cx.data_layout().offset(self.offset.bytes(), i.bytes())?),
|
||||
..self
|
||||
})
|
||||
}
|
||||
|
||||
#[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());
|
||||
let ptr = Pointer { offset: Size::from_bytes(res), ..self };
|
||||
(ptr, over)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn wrapping_offset(self, i: Size, cx: &impl HasDataLayout) -> Self {
|
||||
self.overflowing_offset(i, cx).0
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn signed_offset(self, i: i64, cx: &impl HasDataLayout) -> InterpResult<'tcx, Self> {
|
||||
Ok(Pointer {
|
||||
offset: Size::from_bytes(cx.data_layout().signed_offset(self.offset.bytes(), i)?),
|
||||
..self
|
||||
})
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn overflowing_signed_offset(self, i: i64, cx: &impl HasDataLayout) -> (Self, bool) {
|
||||
let (res, over) = cx.data_layout().overflowing_signed_offset(self.offset.bytes(), i);
|
||||
let ptr = Pointer { offset: Size::from_bytes(res), ..self };
|
||||
(ptr, over)
|
||||
let res =
|
||||
cx.data_layout().truncate_to_target_usize(self.offset.bytes().wrapping_add(i.bytes()));
|
||||
Pointer { offset: Size::from_bytes(res), ..self }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn wrapping_signed_offset(self, i: i64, cx: &impl HasDataLayout) -> Self {
|
||||
self.overflowing_signed_offset(i, cx).0
|
||||
// It's wrapping anyway, so we can just cast to `u64`.
|
||||
self.wrapping_offset(Size::from_bytes(i as u64), cx)
|
||||
}
|
||||
}
|
||||
|
@ -393,7 +393,7 @@ impl<'tcx, Prov: Provenance> Scalar<Prov> {
|
||||
#[inline]
|
||||
pub fn to_int(self, size: Size) -> InterpResult<'tcx, i128> {
|
||||
let b = self.to_bits(size)?;
|
||||
Ok(size.sign_extend(b) as i128)
|
||||
Ok(size.sign_extend(b))
|
||||
}
|
||||
|
||||
/// Converts the scalar to produce an `i8`. Fails if the scalar is a pointer.
|
||||
|
@ -234,7 +234,7 @@ impl ScalarInt {
|
||||
let data = i.into();
|
||||
// `into` performed sign extension, we have to truncate
|
||||
let r = Self::raw(size.truncate(data as u128), size);
|
||||
(r, size.sign_extend(r.data) as i128 != data)
|
||||
(r, size.sign_extend(r.data) != data)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
@ -335,7 +335,7 @@ impl ScalarInt {
|
||||
#[inline]
|
||||
pub fn to_int(self, size: Size) -> i128 {
|
||||
let b = self.to_bits(size);
|
||||
size.sign_extend(b) as i128
|
||||
size.sign_extend(b)
|
||||
}
|
||||
|
||||
/// Converts the `ScalarInt` to i8.
|
||||
|
@ -79,7 +79,7 @@ impl<'tcx> Discr<'tcx> {
|
||||
let (val, oflo) = if signed {
|
||||
let min = size.signed_int_min();
|
||||
let max = size.signed_int_max();
|
||||
let val = size.sign_extend(self.val) as i128;
|
||||
let val = size.sign_extend(self.val);
|
||||
assert!(n < (i128::MAX as u128));
|
||||
let n = n as i128;
|
||||
let oflo = val > max - n;
|
||||
|
@ -11,7 +11,7 @@ use rand::Rng;
|
||||
|
||||
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
|
||||
use rustc_span::Span;
|
||||
use rustc_target::abi::{Align, HasDataLayout, Size};
|
||||
use rustc_target::abi::{Align, Size};
|
||||
|
||||
use crate::{concurrency::VClock, *};
|
||||
|
||||
@ -307,15 +307,15 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
|
||||
|
||||
let (prov, offset) = ptr.into_parts(); // offset is relative (AllocId provenance)
|
||||
let alloc_id = prov.alloc_id();
|
||||
let base_addr = ecx.addr_from_alloc_id(alloc_id, kind)?;
|
||||
|
||||
// Add offset with the right kind of pointer-overflowing arithmetic.
|
||||
let dl = ecx.data_layout();
|
||||
let absolute_addr = dl.overflowing_offset(base_addr, offset.bytes()).0;
|
||||
Ok(interpret::Pointer::new(
|
||||
// Get a pointer to the beginning of this allocation.
|
||||
let base_addr = ecx.addr_from_alloc_id(alloc_id, kind)?;
|
||||
let base_ptr = interpret::Pointer::new(
|
||||
Provenance::Concrete { alloc_id, tag },
|
||||
Size::from_bytes(absolute_addr),
|
||||
))
|
||||
Size::from_bytes(base_addr),
|
||||
);
|
||||
// Add offset with the right kind of pointer-overflowing arithmetic.
|
||||
Ok(base_ptr.wrapping_offset(offset, ecx))
|
||||
}
|
||||
|
||||
/// When a pointer is used for a memory access, this computes where in which allocation the
|
||||
@ -341,12 +341,8 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
|
||||
let base_addr = *ecx.machine.alloc_addresses.borrow().base_addr.get(&alloc_id).unwrap();
|
||||
|
||||
// Wrapping "addr - base_addr"
|
||||
#[allow(clippy::cast_possible_wrap)] // we want to wrap here
|
||||
let neg_base_addr = (base_addr as i64).wrapping_neg();
|
||||
Some((
|
||||
alloc_id,
|
||||
Size::from_bytes(ecx.overflowing_signed_offset(addr.bytes(), neg_base_addr).0),
|
||||
))
|
||||
let rel_offset = ecx.truncate_to_target_usize(addr.bytes().wrapping_sub(base_addr));
|
||||
Some((alloc_id, Size::from_bytes(rel_offset)))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -606,7 +606,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
|
||||
}
|
||||
// The part between the end_ptr and the end of the place is also frozen.
|
||||
// So pretend there is a 0-sized `UnsafeCell` at the end.
|
||||
unsafe_cell_action(&place.ptr().offset(size, this)?, Size::ZERO)?;
|
||||
unsafe_cell_action(&place.ptr().wrapping_offset(size, this), Size::ZERO)?;
|
||||
// Done!
|
||||
return Ok(());
|
||||
|
||||
@ -975,7 +975,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
|
||||
loop {
|
||||
// FIXME: We are re-getting the allocation each time around the loop.
|
||||
// Would be nice if we could somehow "extend" an existing AllocRange.
|
||||
let alloc = this.get_ptr_alloc(ptr.offset(len, this)?, size1)?.unwrap(); // not a ZST, so we will get a result
|
||||
let alloc = this.get_ptr_alloc(ptr.wrapping_offset(len, this), size1)?.unwrap(); // not a ZST, so we will get a result
|
||||
let byte = alloc.read_integer(alloc_range(Size::ZERO, size1))?.to_u8()?;
|
||||
if byte == 0 {
|
||||
break;
|
||||
@ -1039,7 +1039,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
|
||||
break;
|
||||
} else {
|
||||
wchars.push(wchar_int.try_into().unwrap());
|
||||
ptr = ptr.offset(size, this)?;
|
||||
ptr = ptr.wrapping_offset(size, this);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -622,7 +622,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
|
||||
{
|
||||
let idx = u64::try_from(idx).unwrap();
|
||||
#[allow(clippy::arithmetic_side_effects)] // idx < num, so this never wraps
|
||||
let new_ptr = ptr.offset(Size::from_bytes(num - idx - 1), this)?;
|
||||
let new_ptr = ptr.wrapping_offset(Size::from_bytes(num - idx - 1), this);
|
||||
this.write_pointer(new_ptr, dest)?;
|
||||
} else {
|
||||
this.write_null(dest)?;
|
||||
@ -646,7 +646,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
|
||||
.iter()
|
||||
.position(|&c| c == val);
|
||||
if let Some(idx) = idx {
|
||||
let new_ptr = ptr.offset(Size::from_bytes(idx as u64), this)?;
|
||||
let new_ptr = ptr.wrapping_offset(Size::from_bytes(idx as u64), this);
|
||||
this.write_pointer(new_ptr, dest)?;
|
||||
} else {
|
||||
this.write_null(dest)?;
|
||||
|
@ -82,7 +82,7 @@ impl<'tcx> UnixEnvVars<'tcx> {
|
||||
};
|
||||
// The offset is used to strip the "{name}=" part of the string.
|
||||
let var_ptr = var_ptr
|
||||
.offset(Size::from_bytes(u64::try_from(name.len()).unwrap().strict_add(1)), ecx)?;
|
||||
.wrapping_offset(Size::from_bytes(u64::try_from(name.len()).unwrap().strict_add(1)), ecx);
|
||||
Ok(Some(var_ptr))
|
||||
}
|
||||
|
||||
|
@ -996,7 +996,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
|
||||
&this.ptr_to_mplace(entry, dirent64_layout),
|
||||
)?;
|
||||
|
||||
let name_ptr = entry.offset(Size::from_bytes(d_name_offset), this)?;
|
||||
let name_ptr = entry.wrapping_offset(Size::from_bytes(d_name_offset), this);
|
||||
this.write_bytes_ptr(name_ptr, name_bytes.iter().copied())?;
|
||||
|
||||
Some(entry)
|
||||
|
@ -53,7 +53,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
|
||||
// We just allocated this, the access is definitely in-bounds and fits into our address space.
|
||||
// mmap guarantees new mappings are zero-init.
|
||||
this.write_bytes_ptr(
|
||||
ptr.offset(Size::from_bytes(old_size), this).unwrap().into(),
|
||||
ptr.wrapping_offset(Size::from_bytes(old_size), this).into(),
|
||||
std::iter::repeat(0u8).take(usize::try_from(increase).unwrap()),
|
||||
)
|
||||
.unwrap();
|
||||
|
@ -1,6 +0,0 @@
|
||||
fn main() {
|
||||
let v = [0i8; 4];
|
||||
let x = &v as *const i8;
|
||||
let x = unsafe { x.offset(isize::MIN) }; //~ERROR: overflowing in-bounds pointer arithmetic
|
||||
panic!("this should never print: {:?}", x);
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
error: Undefined Behavior: overflowing in-bounds pointer arithmetic
|
||||
--> $DIR/out_of_bounds_ptr_2.rs:LL:CC
|
||||
|
|
||||
LL | let x = unsafe { x.offset(isize::MIN) };
|
||||
| ^^^^^^^^^^^^^^^^^^^^ overflowing in-bounds pointer arithmetic
|
||||
|
|
||||
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
|
||||
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
|
||||
= note: BACKTRACE:
|
||||
= note: inside `main` at $DIR/out_of_bounds_ptr_2.rs:LL:CC
|
||||
|
||||
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
|
||||
|
||||
error: aborting due to 1 previous error
|
||||
|
@ -1,5 +1,5 @@
|
||||
//@compile-flags: -Zmiri-permissive-provenance
|
||||
//@normalize-stderr-test: "to \d+ bytes of memory" -> "to $$BYTES bytes of memory"
|
||||
//@normalize-stderr-test: "\d+ bytes" -> "$$BYTES bytes"
|
||||
|
||||
fn main() {
|
||||
let ptr = Box::into_raw(Box::new(0u32));
|
||||
|
@ -1,7 +1,6 @@
|
||||
fn main() {
|
||||
let v = [0i8; 4];
|
||||
let x = &v as *const i8;
|
||||
// The error is inside another function, so we cannot match it by line
|
||||
let x = unsafe { x.offset(5) }; //~ERROR: expected a pointer to 5 bytes of memory
|
||||
panic!("this should never print: {:?}", x);
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
error: Undefined Behavior: out-of-bounds pointer arithmetic: expected a pointer to 5 bytes of memory, but got ALLOC which is only 4 bytes from the end of the allocation
|
||||
--> $DIR/out_of_bounds_ptr_1.rs:LL:CC
|
||||
--> $DIR/ptr_offset_out_of_bounds.rs:LL:CC
|
||||
|
|
||||
LL | let x = unsafe { x.offset(5) };
|
||||
| ^^^^^^^^^^^ out-of-bounds pointer arithmetic: expected a pointer to 5 bytes of memory, but got ALLOC which is only 4 bytes from the end of the allocation
|
||||
@ -7,12 +7,12 @@ LL | let x = unsafe { x.offset(5) };
|
||||
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
|
||||
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
|
||||
help: ALLOC was allocated here:
|
||||
--> $DIR/out_of_bounds_ptr_1.rs:LL:CC
|
||||
--> $DIR/ptr_offset_out_of_bounds.rs:LL:CC
|
||||
|
|
||||
LL | let v = [0i8; 4];
|
||||
| ^
|
||||
= note: BACKTRACE (of the first span):
|
||||
= note: inside `main` at $DIR/out_of_bounds_ptr_1.rs:LL:CC
|
||||
= note: inside `main` at $DIR/ptr_offset_out_of_bounds.rs:LL:CC
|
||||
|
||||
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
|
||||
|
@ -1,5 +1,5 @@
|
||||
error: Undefined Behavior: out-of-bounds pointer arithmetic: expected a pointer to the end of 1 byte of memory, but got ALLOC which is at the beginning of the allocation
|
||||
--> $DIR/out_of_bounds_ptr_3.rs:LL:CC
|
||||
--> $DIR/ptr_offset_out_of_bounds_neg.rs:LL:CC
|
||||
|
|
||||
LL | let x = unsafe { x.offset(-1) };
|
||||
| ^^^^^^^^^^^^ out-of-bounds pointer arithmetic: expected a pointer to the end of 1 byte of memory, but got ALLOC which is at the beginning of the allocation
|
||||
@ -7,12 +7,12 @@ LL | let x = unsafe { x.offset(-1) };
|
||||
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
|
||||
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
|
||||
help: ALLOC was allocated here:
|
||||
--> $DIR/out_of_bounds_ptr_3.rs:LL:CC
|
||||
--> $DIR/ptr_offset_out_of_bounds_neg.rs:LL:CC
|
||||
|
|
||||
LL | let v = [0i8; 4];
|
||||
| ^
|
||||
= note: BACKTRACE (of the first span):
|
||||
= note: inside `main` at $DIR/out_of_bounds_ptr_3.rs:LL:CC
|
||||
= note: inside `main` at $DIR/ptr_offset_out_of_bounds_neg.rs:LL:CC
|
||||
|
||||
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
|
||||
|
@ -1,5 +1,8 @@
|
||||
//@normalize-stderr-test: "\d+ bytes" -> "$$BYTES bytes"
|
||||
|
||||
fn main() {
|
||||
let v = [1i8, 2];
|
||||
let x = &v[1] as *const i8;
|
||||
let _val = unsafe { x.offset(isize::MIN) }; //~ERROR: overflowing in-bounds pointer arithmetic
|
||||
let v = [0i8; 4];
|
||||
let x = &v as *const i8;
|
||||
let x = unsafe { x.offset(isize::MIN) }; //~ERROR: out-of-bounds pointer arithmetic
|
||||
panic!("this should never print: {:?}", x);
|
||||
}
|
||||
|
@ -1,12 +1,17 @@
|
||||
error: Undefined Behavior: overflowing in-bounds pointer arithmetic
|
||||
error: Undefined Behavior: out-of-bounds pointer arithmetic: expected a pointer to the end of $BYTES bytes of memory, but got ALLOC which is at the beginning of the allocation
|
||||
--> $DIR/ptr_offset_overflow.rs:LL:CC
|
||||
|
|
||||
LL | let _val = unsafe { x.offset(isize::MIN) };
|
||||
| ^^^^^^^^^^^^^^^^^^^^ overflowing in-bounds pointer arithmetic
|
||||
LL | let x = unsafe { x.offset(isize::MIN) };
|
||||
| ^^^^^^^^^^^^^^^^^^^^ out-of-bounds pointer arithmetic: expected a pointer to the end of $BYTES bytes of memory, but got ALLOC which is at the beginning of the allocation
|
||||
|
|
||||
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
|
||||
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
|
||||
= note: BACKTRACE:
|
||||
help: ALLOC was allocated here:
|
||||
--> $DIR/ptr_offset_overflow.rs:LL:CC
|
||||
|
|
||||
LL | let v = [0i8; 4];
|
||||
| ^
|
||||
= note: BACKTRACE (of the first span):
|
||||
= note: inside `main` at $DIR/ptr_offset_overflow.rs:LL:CC
|
||||
|
||||
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
|
||||
|
@ -1,7 +1,7 @@
|
||||
error[E0080]: evaluation of constant value failed
|
||||
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
|
||||
|
|
||||
= note: overflowing in-bounds pointer arithmetic
|
||||
= note: out-of-bounds pointer arithmetic: expected a pointer to the end of 1 byte of memory, but got ALLOC0 which is at the beginning of the allocation
|
||||
|
|
||||
note: inside `std::ptr::const_ptr::<impl *const u8>::offset`
|
||||
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
|
||||
@ -14,7 +14,7 @@ LL | pub const BEFORE_START: *const u8 = unsafe { (&0u8 as *const u8).offset(-1)
|
||||
error[E0080]: evaluation of constant value failed
|
||||
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
|
||||
|
|
||||
= note: out-of-bounds pointer arithmetic: expected a pointer to $BYTES bytes of memory, but got ALLOC0 which is only 1 byte from the end of the allocation
|
||||
= note: out-of-bounds pointer arithmetic: expected a pointer to $BYTES bytes of memory, but got ALLOC1 which is only 1 byte from the end of the allocation
|
||||
|
|
||||
note: inside `std::ptr::const_ptr::<impl *const u8>::offset`
|
||||
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
|
||||
@ -27,7 +27,7 @@ LL | pub const AFTER_END: *const u8 = unsafe { (&0u8 as *const u8).offset(2) };
|
||||
error[E0080]: evaluation of constant value failed
|
||||
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
|
||||
|
|
||||
= note: out-of-bounds pointer arithmetic: expected a pointer to $BYTES bytes of memory, but got ALLOC1 which is only $BYTES bytes from the end of the allocation
|
||||
= note: out-of-bounds pointer arithmetic: expected a pointer to $BYTES bytes of memory, but got ALLOC2 which is only $BYTES bytes from the end of the allocation
|
||||
|
|
||||
note: inside `std::ptr::const_ptr::<impl *const u8>::offset`
|
||||
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
|
||||
@ -66,7 +66,7 @@ LL | pub const UNDERFLOW: *const u16 = unsafe { [0u16; 1].as_ptr().offset(isize:
|
||||
error[E0080]: evaluation of constant value failed
|
||||
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
|
||||
|
|
||||
= note: overflowing in-bounds pointer arithmetic
|
||||
= note: out-of-bounds pointer arithmetic: expected a pointer to $BYTES bytes of memory, but got 0xf..f[noalloc] which is a dangling pointer (it has no provenance)
|
||||
|
|
||||
note: inside `std::ptr::const_ptr::<impl *const u8>::offset`
|
||||
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
|
||||
@ -79,7 +79,7 @@ LL | pub const OVERFLOW_ADDRESS_SPACE: *const u8 = unsafe { (usize::MAX as *cons
|
||||
error[E0080]: evaluation of constant value failed
|
||||
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
|
||||
|
|
||||
= note: overflowing in-bounds pointer arithmetic
|
||||
= note: out-of-bounds pointer arithmetic: expected a pointer to the end of $BYTES bytes of memory, but got 0x1[noalloc] which is a dangling pointer (it has no provenance)
|
||||
|
|
||||
note: inside `std::ptr::const_ptr::<impl *const u8>::offset`
|
||||
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
|
||||
@ -92,7 +92,7 @@ LL | pub const UNDERFLOW_ADDRESS_SPACE: *const u8 = unsafe { (1 as *const u8).of
|
||||
error[E0080]: evaluation of constant value failed
|
||||
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
|
||||
|
|
||||
= note: out-of-bounds pointer arithmetic: expected a pointer to the end of $BYTES bytes of memory, but got ALLOC2-0x2 which points to before the beginning of the allocation
|
||||
= note: out-of-bounds pointer arithmetic: expected a pointer to the end of $BYTES bytes of memory, but got ALLOC3-0x2 which points to before the beginning of the allocation
|
||||
|
|
||||
note: inside `std::ptr::const_ptr::<impl *const u8>::offset`
|
||||
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
|
||||
@ -105,7 +105,7 @@ LL | pub const NEGATIVE_OFFSET: *const u8 = unsafe { [0u8; 1].as_ptr().wrapping_
|
||||
error[E0080]: evaluation of constant value failed
|
||||
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
|
||||
|
|
||||
= note: out-of-bounds pointer arithmetic: expected a pointer to 1 byte of memory, but got ALLOC3 which is at or beyond the end of the allocation of size $BYTES bytes
|
||||
= note: out-of-bounds pointer arithmetic: expected a pointer to 1 byte of memory, but got ALLOC4 which is at or beyond the end of the allocation of size $BYTES bytes
|
||||
|
|
||||
note: inside `std::ptr::const_ptr::<impl *const u8>::offset`
|
||||
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
|
||||
|
Loading…
Reference in New Issue
Block a user