1
Fork 0

Rollup merge of #128277 - RalfJung:offset_from_wildcard, r=oli-obk

miri: fix offset_from behavior on wildcard pointers

offset_from wouldn't behave correctly when the "end" pointer was a wildcard pointer (result of an int2ptr cast) just at the end of the allocation. Fix that by expressing the "same allocation" check in terms of two `check_ptr_access_signed` instead of something specific to offset_from, which is both more canonical and works better with wildcard pointers.

The second commit just improves diagnostics: I wanted the "pointer is dangling (has no provenance)" message to say how many bytes of memory it expected to see (since if it were 0 bytes, this would actually be legal, so it's good to tell the user that it's not 0 bytes). And then I was annoying that the error looks so different for when you deref a dangling pointer vs an out-of-bounds pointer so I made them more similar.

Fixes https://github.com/rust-lang/miri/issues/3767
This commit is contained in:
Matthias Krüger 2024-07-29 11:42:34 +02:00 committed by GitHub
commit eb8114bad7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
80 changed files with 301 additions and 239 deletions

View file

@ -329,16 +329,21 @@ pub enum UndefinedBehaviorInfo<'tcx> {
/// Using a pointer after it got freed.
PointerUseAfterFree(AllocId, CheckInAllocMsg),
/// Used a pointer outside the bounds it is valid for.
/// (If `ptr_size > 0`, determines the size of the memory range that was expected to be in-bounds.)
PointerOutOfBounds {
alloc_id: AllocId,
alloc_size: Size,
ptr_offset: i64,
ptr_size: Size,
/// The size of the memory range that was expected to be in-bounds.
inbounds_size: Size,
msg: CheckInAllocMsg,
},
/// Using an integer as a pointer in the wrong way.
DanglingIntPointer(u64, CheckInAllocMsg),
DanglingIntPointer {
addr: u64,
/// The size of the memory range that was expected to be in-bounds (or 0 if we don't know).
inbounds_size: Size,
msg: CheckInAllocMsg,
},
/// Used a pointer with bad alignment.
AlignmentCheckFailed(Misalignment, CheckAlignMsg),
/// Writing to read-only memory.

View file

@ -181,9 +181,12 @@ impl Provenance for CtfeProvenance {
fn fmt(ptr: &Pointer<Self>, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// Print AllocId.
fmt::Debug::fmt(&ptr.provenance.alloc_id(), f)?; // propagates `alternate` flag
// Print offset only if it is non-zero.
if ptr.offset.bytes() > 0 {
write!(f, "+{:#x}", ptr.offset.bytes())?;
// Print offset only if it is non-zero. Print it signed.
let signed_offset = ptr.offset.bytes() as i64;
if signed_offset > 0 {
write!(f, "+{:#x}", signed_offset)?;
} else if signed_offset < 0 {
write!(f, "-{:#x}", signed_offset.unsigned_abs())?;
}
// Print immutable status.
if ptr.provenance.immutable() {