1
Fork 0

Rollup merge of #98860 - RalfJung:dangling-int-ptr, r=davidtwco

adjust dangling-int-ptr error message

based on suggestions by `@saethlin` in https://github.com/rust-lang/miri/issues/2163

Fixes https://github.com/rust-lang/miri/issues/2163

I also did a bit of refactoring on this, so we have a helper method to create a `Pointer` with `None` provenance.
This commit is contained in:
Matthias Krüger 2022-07-05 17:08:10 +02:00 committed by GitHub
commit 69195c026e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 60 additions and 47 deletions

View file

@ -513,7 +513,7 @@ pub macro compile_time_machine(<$mir: lifetime, $tcx: lifetime>) {
_ecx: &InterpCx<$mir, $tcx, Self>,
addr: u64,
) -> Pointer<Option<AllocId>> {
Pointer::new(None, Size::from_bytes(addr))
Pointer::from_addr(addr)
}
#[inline(always)]
@ -523,7 +523,7 @@ pub macro compile_time_machine(<$mir: lifetime, $tcx: lifetime>) {
) -> InterpResult<$tcx, Pointer<Option<AllocId>>> {
// Allow these casts, but make the pointer not dereferenceable.
// (I.e., they behave like transmutation.)
Ok(Pointer::new(None, Size::from_bytes(addr)))
Ok(Pointer::from_addr(addr))
}
#[inline(always)]

View file

@ -188,7 +188,7 @@ impl<'tcx, Tag: Provenance> MPlaceTy<'tcx, Tag> {
#[inline]
pub fn dangling(layout: TyAndLayout<'tcx>) -> Self {
let align = layout.align.abi;
let ptr = Pointer::new(None, Size::from_bytes(align.bytes())); // no provenance, absolute address
let ptr = Pointer::from_addr(align.bytes()); // no provenance, absolute address
// `Poison` this to make sure that the pointer value `ptr` is never observable by the program.
MPlaceTy { mplace: MemPlace { ptr, meta: MemPlaceMeta::Poison }, layout, align }
}

View file

@ -186,7 +186,7 @@ pub enum CheckInAllocMsg {
impl fmt::Display for CheckInAllocMsg {
/// When this is printed as an error the context looks like this:
/// "{msg}0x01 is not a valid pointer".
/// "{msg}{pointer} is a dangling pointer".
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
@ -194,9 +194,9 @@ impl fmt::Display for CheckInAllocMsg {
match *self {
CheckInAllocMsg::DerefTest => "dereferencing pointer failed: ",
CheckInAllocMsg::MemoryAccessTest => "memory access failed: ",
CheckInAllocMsg::PointerArithmeticTest => "pointer arithmetic failed: ",
CheckInAllocMsg::PointerArithmeticTest => "out-of-bounds pointer arithmetic: ",
CheckInAllocMsg::OffsetFromTest => "out-of-bounds offset_from: ",
CheckInAllocMsg::InboundsTest => "",
CheckInAllocMsg::InboundsTest => "out-of-bounds pointer use: ",
}
)
}
@ -350,14 +350,12 @@ impl fmt::Display for UndefinedBehaviorInfo<'_> {
ptr_size = ptr_size.bytes(),
ptr_size_p = pluralize!(ptr_size.bytes()),
),
DanglingIntPointer(0, CheckInAllocMsg::InboundsTest) => {
write!(f, "null pointer is not a valid pointer for this operation")
}
DanglingIntPointer(0, msg) => {
write!(f, "{msg}null pointer is not a valid pointer")
}
DanglingIntPointer(i, msg) => {
write!(f, "{msg}{i:#x} is not a valid pointer")
write!(
f,
"{msg}{pointer} is a dangling pointer (it has no provenance)",
pointer = Pointer::<Option<AllocId>>::from_addr(*i),
)
}
AlignmentCheckFailed { required, has } => write!(
f,

View file

@ -181,7 +181,17 @@ impl<Tag: Provenance> fmt::Debug for Pointer<Option<Tag>> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.provenance {
Some(tag) => Provenance::fmt(&Pointer::new(tag, self.offset), f),
None => write!(f, "{:#x}", self.offset.bytes()),
None => write!(f, "{:#x}[noalloc]", self.offset.bytes()),
}
}
}
impl<Tag: Provenance> fmt::Display for Pointer<Option<Tag>> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.provenance.is_none() && self.offset.bytes() == 0 {
write!(f, "null pointer")
} else {
fmt::Debug::fmt(self, f)
}
}
}
@ -226,9 +236,14 @@ impl<Tag> Pointer<Option<Tag>> {
}
impl<Tag> Pointer<Option<Tag>> {
#[inline(always)]
pub fn from_addr(addr: u64) -> Self {
Pointer { provenance: None, offset: Size::from_bytes(addr) }
}
#[inline(always)]
pub fn null() -> Self {
Pointer { provenance: None, offset: Size::ZERO }
Pointer::from_addr(0)
}
}