Skip to content

Commit be9feae

Browse files
committed
Auto merge of #115184 - saethlin:local-allocated-spans, r=RalfJung
Record allocation spans inside force_allocation This expands #2940 to cover locals r? `@RalfJung`
2 parents 86044e1 + 397c495 commit be9feae

8 files changed

+92
-5
lines changed

src/machine.rs

+18
Original file line numberDiff line numberDiff line change
@@ -1405,4 +1405,22 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for MiriMachine<'mir, 'tcx> {
14051405
}
14061406
res
14071407
}
1408+
1409+
fn after_local_allocated(
1410+
ecx: &mut InterpCx<'mir, 'tcx, Self>,
1411+
frame: usize,
1412+
local: mir::Local,
1413+
mplace: &MPlaceTy<'tcx, Provenance>
1414+
) -> InterpResult<'tcx> {
1415+
let Some(Provenance::Concrete { alloc_id, .. }) = mplace.ptr.provenance else {
1416+
panic!("after_local_allocated should only be called on fresh allocations");
1417+
};
1418+
let local_decl = &ecx.active_thread_stack()[frame].body.local_decls[local];
1419+
let span = local_decl.source_info.span;
1420+
ecx.machine
1421+
.allocation_spans
1422+
.borrow_mut()
1423+
.insert(alloc_id, (span, None));
1424+
Ok(())
1425+
}
14081426
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// The interpreter tries to delay allocating locals until their address is taken.
2+
// This test checks that we correctly use the span associated with the local itself, not the span
3+
// where we take the address of the local and force it to be allocated.
4+
5+
fn main() {
6+
let ptr = {
7+
let x = 0usize; // This line should appear in the helps
8+
&x as *const usize // This line should NOT appear in the helps
9+
};
10+
unsafe {
11+
dbg!(*ptr); //~ ERROR: has been freed
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error: Undefined Behavior: dereferencing pointer failed: ALLOC has been freed, so this pointer is dangling
2+
--> $DIR/dangling_primitive.rs:LL:CC
3+
|
4+
LL | dbg!(*ptr);
5+
| ^^^^^^^^^^ dereferencing pointer failed: ALLOC has been freed, so this pointer is dangling
6+
|
7+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
8+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
9+
help: ALLOC was allocated here:
10+
--> $DIR/dangling_primitive.rs:LL:CC
11+
|
12+
LL | let x = 0usize; // This line should appear in the helps
13+
| ^
14+
help: ALLOC was deallocated here:
15+
--> $DIR/dangling_primitive.rs:LL:CC
16+
|
17+
LL | };
18+
| ^
19+
= note: BACKTRACE (of the first span):
20+
= note: inside `main` at RUSTLIB/std/src/macros.rs:LL:CC
21+
= note: this error originates in the macro `dbg` (in Nightly builds, run with -Z macro-backtrace for more info)
22+
23+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
24+
25+
error: aborting due to previous error
26+

tests/fail/dangling_pointers/deref-partially-dangling.stderr

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ LL | let val = unsafe { (*xptr).1 };
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
9-
= note: BACKTRACE:
9+
help: ALLOC was allocated here:
10+
--> $DIR/deref-partially-dangling.rs:LL:CC
11+
|
12+
LL | let x = (1, 13);
13+
| ^
14+
= note: BACKTRACE (of the first span):
1015
= note: inside `main` at $DIR/deref-partially-dangling.rs:LL:CC
1116

1217
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace

tests/fail/dangling_pointers/dyn_size.stderr

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ LL | let _ptr = unsafe { &*ptr };
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
9-
= note: BACKTRACE:
9+
help: ALLOC was allocated here:
10+
--> $DIR/dyn_size.rs:LL:CC
11+
|
12+
LL | let buf = [0u32; 1];
13+
| ^^^
14+
= note: BACKTRACE (of the first span):
1015
= note: inside `main` at $DIR/dyn_size.rs:LL:CC
1116

1217
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace

tests/fail/dangling_pointers/stack_temporary.stderr

+11-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,17 @@ LL | let val = *x;
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
9-
= note: BACKTRACE:
9+
help: ALLOC was allocated here:
10+
--> $DIR/stack_temporary.rs:LL:CC
11+
|
12+
LL | let x = make_ref(&mut 0); // The temporary storing "0" is deallocated at the ";"!
13+
| ^
14+
help: ALLOC was deallocated here:
15+
--> $DIR/stack_temporary.rs:LL:CC
16+
|
17+
LL | let x = make_ref(&mut 0); // The temporary storing "0" is deallocated at the ";"!
18+
| ^
19+
= note: BACKTRACE (of the first span):
1020
= note: inside `main` at $DIR/stack_temporary.rs:LL:CC
1121

1222
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace

tests/fail/intrinsics/out_of_bounds_ptr_1.stderr

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ LL | let x = unsafe { x.offset(5) };
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
9-
= note: BACKTRACE:
9+
help: ALLOC was allocated here:
10+
--> $DIR/out_of_bounds_ptr_1.rs:LL:CC
11+
|
12+
LL | let v = [0i8; 4];
13+
| ^
14+
= note: BACKTRACE (of the first span):
1015
= note: inside `main` at $DIR/out_of_bounds_ptr_1.rs:LL:CC
1116

1217
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace

tests/fail/intrinsics/out_of_bounds_ptr_3.stderr

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ LL | let x = unsafe { x.offset(-1) };
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
9-
= note: BACKTRACE:
9+
help: ALLOC was allocated here:
10+
--> $DIR/out_of_bounds_ptr_3.rs:LL:CC
11+
|
12+
LL | let v = [0i8; 4];
13+
| ^
14+
= note: BACKTRACE (of the first span):
1015
= note: inside `main` at $DIR/out_of_bounds_ptr_3.rs:LL:CC
1116

1217
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace

0 commit comments

Comments
 (0)