Auto merge of #1853 - RalfJung:negative-offsets, r=RalfJung

better errors for negative out-of-bounds offsets

This is the Miri side of https://github.com/rust-lang/rust/pull/87224
This commit is contained in:
bors 2021-07-20 11:39:12 +00:00
commit e2872a3f2a
3 changed files with 13 additions and 6 deletions

View File

@ -1 +1 @@
a72c360a30f9a8160e4f40340cecc9b1ce979cd7
718d53b0cb7dde93499cb92950d60b412f5a3d05

View File

@ -623,14 +623,14 @@ trait EvalContextPrivExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
let orig_tag = ptr.provenance.sb;
// Ensure we bail out if the pointer goes out-of-bounds (see miri#1050).
let (allocation_size, _) =
let (alloc_size, _) =
this.memory.get_size_and_align(alloc_id, AllocCheck::Dereferenceable)?;
if base_offset + size > allocation_size {
if base_offset + size > alloc_size {
throw_ub!(PointerOutOfBounds {
alloc_id,
offset: base_offset,
size,
allocation_size,
alloc_size,
ptr_offset: this.machine_usize_to_isize(base_offset.bytes()),
ptr_size: size,
msg: CheckInAllocMsg::InboundsTest
});
}

View File

@ -0,0 +1,7 @@
// error-pattern: pointer to 1 byte starting at offset -1 is out-of-bounds
fn main() {
let v = [0i8; 4];
let x = &v as *const i8;
let x = unsafe { x.offset(-1) };
panic!("this should never print: {:?}", x);
}