about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-08-25 17:03:33 +0000
committerbors <bors@rust-lang.org>2023-08-25 17:03:33 +0000
commit296c7a683cb7faafea3dd6905860cdeeae13598e (patch)
tree371ed0e225dcf8ea32e4fd7e56ea6520909f33f1 /src
parenta8b905cd78c4ddfa4a7a517ada260506af4adfad (diff)
parent8ecdefb3db7e854561fd382ba19675c894f40170 (diff)
downloadrust-296c7a683cb7faafea3dd6905860cdeeae13598e.tar.gz
rust-296c7a683cb7faafea3dd6905860cdeeae13598e.zip
Auto merge of #115184 - saethlin:local-allocated-spans, r=RalfJung
Record allocation spans inside force_allocation

This expands https://github.com/rust-lang/miri/pull/2940 to cover locals

r? `@RalfJung`
Diffstat (limited to 'src')
-rw-r--r--src/tools/miri/src/machine.rs18
-rw-r--r--src/tools/miri/tests/fail/dangling_pointers/dangling_primitive.rs13
-rw-r--r--src/tools/miri/tests/fail/dangling_pointers/dangling_primitive.stderr26
-rw-r--r--src/tools/miri/tests/fail/dangling_pointers/deref-partially-dangling.stderr7
-rw-r--r--src/tools/miri/tests/fail/dangling_pointers/dyn_size.stderr7
-rw-r--r--src/tools/miri/tests/fail/dangling_pointers/stack_temporary.stderr12
-rw-r--r--src/tools/miri/tests/fail/intrinsics/out_of_bounds_ptr_1.stderr7
-rw-r--r--src/tools/miri/tests/fail/intrinsics/out_of_bounds_ptr_3.stderr7
8 files changed, 92 insertions, 5 deletions
diff --git a/src/tools/miri/src/machine.rs b/src/tools/miri/src/machine.rs
index 17cfcbb2018..98f82ec9a0f 100644
--- a/src/tools/miri/src/machine.rs
+++ b/src/tools/miri/src/machine.rs
@@ -1405,4 +1405,22 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for MiriMachine<'mir, 'tcx> {
         }
         res
     }
+
+    fn after_local_allocated(
+        ecx: &mut InterpCx<'mir, 'tcx, Self>,
+        frame: usize,
+        local: mir::Local,
+        mplace: &MPlaceTy<'tcx, Provenance>
+    ) -> InterpResult<'tcx> {
+        let Some(Provenance::Concrete { alloc_id, .. }) = mplace.ptr.provenance else {
+            panic!("after_local_allocated should only be called on fresh allocations");
+        };
+        let local_decl = &ecx.active_thread_stack()[frame].body.local_decls[local];
+        let span = local_decl.source_info.span;
+        ecx.machine
+            .allocation_spans
+            .borrow_mut()
+            .insert(alloc_id, (span, None));
+        Ok(())
+    }
 }
diff --git a/src/tools/miri/tests/fail/dangling_pointers/dangling_primitive.rs b/src/tools/miri/tests/fail/dangling_pointers/dangling_primitive.rs
new file mode 100644
index 00000000000..393127341ca
--- /dev/null
+++ b/src/tools/miri/tests/fail/dangling_pointers/dangling_primitive.rs
@@ -0,0 +1,13 @@
+// The interpreter tries to delay allocating locals until their address is taken.
+// This test checks that we correctly use the span associated with the local itself, not the span
+// where we take the address of the local and force it to be allocated.
+
+fn main() {
+    let ptr = {
+        let x = 0usize; // This line should appear in the helps
+        &x as *const usize // This line should NOT appear in the helps
+    };
+    unsafe {
+        dbg!(*ptr); //~ ERROR: has been freed
+    }
+}
diff --git a/src/tools/miri/tests/fail/dangling_pointers/dangling_primitive.stderr b/src/tools/miri/tests/fail/dangling_pointers/dangling_primitive.stderr
new file mode 100644
index 00000000000..bdc9c31db40
--- /dev/null
+++ b/src/tools/miri/tests/fail/dangling_pointers/dangling_primitive.stderr
@@ -0,0 +1,26 @@
+error: Undefined Behavior: dereferencing pointer failed: ALLOC has been freed, so this pointer is dangling
+  --> $DIR/dangling_primitive.rs:LL:CC
+   |
+LL |         dbg!(*ptr);
+   |         ^^^^^^^^^^ dereferencing pointer failed: ALLOC has been freed, so this pointer is dangling
+   |
+   = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
+   = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
+help: ALLOC was allocated here:
+  --> $DIR/dangling_primitive.rs:LL:CC
+   |
+LL |         let x = 0usize; // This line should appear in the helps
+   |             ^
+help: ALLOC was deallocated here:
+  --> $DIR/dangling_primitive.rs:LL:CC
+   |
+LL |     };
+   |     ^
+   = note: BACKTRACE (of the first span):
+   = note: inside `main` at RUSTLIB/std/src/macros.rs:LL:CC
+   = note: this error originates in the macro `dbg` (in Nightly builds, run with -Z macro-backtrace for more info)
+
+note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
+
+error: aborting due to previous error
+
diff --git a/src/tools/miri/tests/fail/dangling_pointers/deref-partially-dangling.stderr b/src/tools/miri/tests/fail/dangling_pointers/deref-partially-dangling.stderr
index fe039ef3ada..92b1fcb1145 100644
--- a/src/tools/miri/tests/fail/dangling_pointers/deref-partially-dangling.stderr
+++ b/src/tools/miri/tests/fail/dangling_pointers/deref-partially-dangling.stderr
@@ -6,7 +6,12 @@ LL |     let val = unsafe { (*xptr).1 };
    |
    = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
    = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
-   = note: BACKTRACE:
+help: ALLOC was allocated here:
+  --> $DIR/deref-partially-dangling.rs:LL:CC
+   |
+LL |     let x = (1, 13);
+   |         ^
+   = note: BACKTRACE (of the first span):
    = note: inside `main` at $DIR/deref-partially-dangling.rs:LL:CC
 
 note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
diff --git a/src/tools/miri/tests/fail/dangling_pointers/dyn_size.stderr b/src/tools/miri/tests/fail/dangling_pointers/dyn_size.stderr
index 33aa6c84410..95a50bc8750 100644
--- a/src/tools/miri/tests/fail/dangling_pointers/dyn_size.stderr
+++ b/src/tools/miri/tests/fail/dangling_pointers/dyn_size.stderr
@@ -6,7 +6,12 @@ LL |     let _ptr = unsafe { &*ptr };
    |
    = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
    = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
-   = note: BACKTRACE:
+help: ALLOC was allocated here:
+  --> $DIR/dyn_size.rs:LL:CC
+   |
+LL |     let buf = [0u32; 1];
+   |         ^^^
+   = note: BACKTRACE (of the first span):
    = note: inside `main` at $DIR/dyn_size.rs:LL:CC
 
 note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
diff --git a/src/tools/miri/tests/fail/dangling_pointers/stack_temporary.stderr b/src/tools/miri/tests/fail/dangling_pointers/stack_temporary.stderr
index 500f28a3cbc..4d2dfe28aed 100644
--- a/src/tools/miri/tests/fail/dangling_pointers/stack_temporary.stderr
+++ b/src/tools/miri/tests/fail/dangling_pointers/stack_temporary.stderr
@@ -6,7 +6,17 @@ LL |         let val = *x;
    |
    = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
    = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
-   = note: BACKTRACE:
+help: ALLOC was allocated here:
+  --> $DIR/stack_temporary.rs:LL:CC
+   |
+LL |         let x = make_ref(&mut 0); // The temporary storing "0" is deallocated at the ";"!
+   |                               ^
+help: ALLOC was deallocated here:
+  --> $DIR/stack_temporary.rs:LL:CC
+   |
+LL |         let x = make_ref(&mut 0); // The temporary storing "0" is deallocated at the ";"!
+   |                                 ^
+   = note: BACKTRACE (of the first span):
    = note: inside `main` at $DIR/stack_temporary.rs:LL:CC
 
 note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
diff --git a/src/tools/miri/tests/fail/intrinsics/out_of_bounds_ptr_1.stderr b/src/tools/miri/tests/fail/intrinsics/out_of_bounds_ptr_1.stderr
index 4422310870a..e1102a6d216 100644
--- a/src/tools/miri/tests/fail/intrinsics/out_of_bounds_ptr_1.stderr
+++ b/src/tools/miri/tests/fail/intrinsics/out_of_bounds_ptr_1.stderr
@@ -6,7 +6,12 @@ LL |     let x = unsafe { x.offset(5) };
    |
    = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
    = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
-   = note: BACKTRACE:
+help: ALLOC was allocated here:
+  --> $DIR/out_of_bounds_ptr_1.rs:LL:CC
+   |
+LL |     let v = [0i8; 4];
+   |         ^
+   = note: BACKTRACE (of the first span):
    = note: inside `main` at $DIR/out_of_bounds_ptr_1.rs:LL:CC
 
 note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
diff --git a/src/tools/miri/tests/fail/intrinsics/out_of_bounds_ptr_3.stderr b/src/tools/miri/tests/fail/intrinsics/out_of_bounds_ptr_3.stderr
index 1364e0f9009..99f28b3e4f8 100644
--- a/src/tools/miri/tests/fail/intrinsics/out_of_bounds_ptr_3.stderr
+++ b/src/tools/miri/tests/fail/intrinsics/out_of_bounds_ptr_3.stderr
@@ -6,7 +6,12 @@ LL |     let x = unsafe { x.offset(-1) };
    |
    = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
    = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
-   = note: BACKTRACE:
+help: ALLOC was allocated here:
+  --> $DIR/out_of_bounds_ptr_3.rs:LL:CC
+   |
+LL |     let v = [0i8; 4];
+   |         ^
+   = note: BACKTRACE (of the first span):
    = note: inside `main` at $DIR/out_of_bounds_ptr_3.rs:LL:CC
 
 note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace