Skip to content

Commit d4d85a8

Browse files
committed
don't UB on dangling ptr deref, instead check inbounds on projections
1 parent babeda8 commit d4d85a8

File tree

66 files changed

+264
-220
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+264
-220
lines changed

src/helpers.rs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -700,23 +700,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
700700

701701
let mplace = MPlaceTy::from_aligned_ptr(ptr, layout);
702702

703-
this.check_mplace(&mplace)?;
704-
705-
Ok(mplace)
706-
}
707-
708-
/// Deref' a pointer *without* checking that the place is dereferenceable.
709-
fn deref_pointer_unchecked(
710-
&self,
711-
val: &ImmTy<'tcx, Provenance>,
712-
layout: TyAndLayout<'tcx>,
713-
) -> InterpResult<'tcx, MPlaceTy<'tcx, Provenance>> {
714-
let this = self.eval_context_ref();
715-
let mut mplace = this.ref_to_mplace(val)?;
716-
717-
mplace.layout = layout;
718-
mplace.align = layout.align.abi;
719-
720703
Ok(mplace)
721704
}
722705

src/machine.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,6 +1285,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for MiriMachine<'mir, 'tcx> {
12851285
// We do need to write `uninit` so that even after the call ends, the former contents of
12861286
// this place cannot be observed any more. We do the write after retagging so that for
12871287
// Tree Borrows, this is considered to activate the new tag.
1288+
// Conveniently this also ensures that the place actually points to suitable memory.
12881289
ecx.write_uninit(&protected_place)?;
12891290
// Now we throw away the protected place, ensuring its tag is never used again.
12901291
Ok(())

src/shims/unix/linux/sync.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,8 @@ pub fn futex<'tcx>(
8585
return Ok(());
8686
}
8787

88-
// `read_timespec` will check the place when it is not null.
89-
let timeout = this.deref_pointer_unchecked(
90-
&this.read_immediate(&args[3])?,
88+
let timeout = this.deref_pointer_as(
89+
&args[3],
9190
this.libc_ty_layout("timespec"),
9291
)?;
9392
let timeout_time = if this.ptr_is_null(timeout.ptr())? {

tests/compiletest.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ regexes! {
181181
r"0x[0-9a-fA-F]+[0-9a-fA-F]{2,2}" => "$$HEX",
182182
// erase specific alignments
183183
"alignment [0-9]+" => "alignment ALIGN",
184+
"[0-9]+ byte alignment but found [0-9]+" => "ALIGN byte alignment but found ALIGN",
184185
// erase thread caller ids
185186
r"call [0-9]+" => "call ID",
186187
// erase platform module paths

tests/fail-dep/shims/mmap_use_after_munmap.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ LL | libc::munmap(ptr, 4096);
1313
= note: BACKTRACE:
1414
= note: inside `main` at $DIR/mmap_use_after_munmap.rs:LL:CC
1515

16-
error: Undefined Behavior: dereferencing pointer failed: ALLOC has been freed, so this pointer is dangling
16+
error: Undefined Behavior: memory access failed: ALLOC has been freed, so this pointer is dangling
1717
--> $DIR/mmap_use_after_munmap.rs:LL:CC
1818
|
1919
LL | let _x = *(ptr as *mut u8);
20-
| ^^^^^^^^^^^^^^^^^ dereferencing pointer failed: ALLOC has been freed, so this pointer is dangling
20+
| ^^^^^^^^^^^^^^^^^ memory access failed: ALLOC has been freed, so this pointer is dangling
2121
|
2222
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
2323
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

tests/fail/alloc/reallocate-change-alloc.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: Undefined Behavior: dereferencing pointer failed: ALLOC has been freed, so this pointer is dangling
1+
error: Undefined Behavior: memory access failed: ALLOC has been freed, so this pointer is dangling
22
--> $DIR/reallocate-change-alloc.rs:LL:CC
33
|
44
LL | let _z = *x;
5-
| ^^ dereferencing pointer failed: ALLOC has been freed, so this pointer is dangling
5+
| ^^ memory access failed: ALLOC has been freed, so this pointer is dangling
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

tests/fail/concurrency/thread_local_static_dealloc.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: Undefined Behavior: dereferencing pointer failed: ALLOC has been freed, so this pointer is dangling
1+
error: Undefined Behavior: memory access failed: ALLOC has been freed, so this pointer is dangling
22
--> $DIR/thread_local_static_dealloc.rs:LL:CC
33
|
44
LL | let _val = *dangling_ptr.0;
5-
| ^^^^^^^^^^^^^^^ dereferencing pointer failed: ALLOC has been freed, so this pointer is dangling
5+
| ^^^^^^^^^^^^^^^ memory access failed: ALLOC has been freed, so this pointer is dangling
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

tests/fail/dangling_pointers/dangling_pointer_addr_of.rs

Lines changed: 0 additions & 12 deletions
This file was deleted.

tests/fail/dangling_pointers/dangling_pointer_addr_of.stderr

Lines changed: 0 additions & 26 deletions
This file was deleted.

tests/fail/dangling_pointers/dangling_pointer_deref.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: Undefined Behavior: dereferencing pointer failed: ALLOC has been freed, so this pointer is dangling
1+
error: Undefined Behavior: memory access failed: ALLOC has been freed, so this pointer is dangling
22
--> $DIR/dangling_pointer_deref.rs:LL:CC
33
|
44
LL | let x = unsafe { *p };
5-
| ^^ dereferencing pointer failed: ALLOC has been freed, so this pointer is dangling
5+
| ^^ memory access failed: ALLOC has been freed, so this pointer is dangling
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

0 commit comments

Comments
 (0)