Skip to content

Commit 64244e9

Browse files
committed
do full deref-check before reborrowing
1 parent 82ef2bb commit 64244e9

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

src/stacked_borrows.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -533,9 +533,7 @@ trait EvalContextPrivExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
533533
) -> InterpResult<'tcx> {
534534
let this = self.eval_context_mut();
535535
let protector = if protect { Some(this.frame().extra) } else { None };
536-
let ptr = this.memory.check_ptr_access(place.ptr, size, place.align)
537-
.expect("validity checks should have excluded dangling/unaligned pointer")
538-
.expect("we shouldn't get here for ZST");
536+
let ptr = place.ptr.to_ptr().expect("we should have a proper pointer");
539537
trace!("reborrow: {} reference {:?} derived from {:?} (pointee {}): {:?}, size {}",
540538
kind, new_tag, ptr.tag, place.layout.ty, ptr.erase_tag(), size.bytes());
541539

@@ -583,11 +581,13 @@ trait EvalContextPrivExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
583581
let size = this.size_and_align_of_mplace(place)?
584582
.map(|(size, _)| size)
585583
.unwrap_or_else(|| place.layout.size);
584+
// We can see dangling ptrs in here e.g. after a Box's `Unique` was
585+
// updated using "self.0 = ..." (can happen in Box::from_raw); see miri#1050.
586+
let place = this.mplace_access_checked(place)?;
586587
if size == Size::ZERO {
587588
// Nothing to do for ZSTs.
588589
return Ok(*val);
589590
}
590-
let place = this.force_mplace_ptr(place)?;
591591

592592
// Compute new borrow.
593593
let new_tag = match kind {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// error-pattern: pointer must be in-bounds
2+
3+
fn main() { unsafe {
4+
let ptr = Box::into_raw(Box::new(0u16));
5+
Box::from_raw(ptr as *mut u32);
6+
} }
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// error-pattern: dangling pointer was dereferenced
2+
use std::ptr::NonNull;
3+
4+
fn main() { unsafe {
5+
let ptr = NonNull::<i32>::dangling();
6+
Box::from_raw(ptr.as_ptr());
7+
} }

0 commit comments

Comments
 (0)