Skip to content

Commit a62e8f6

Browse files
authored
Merge pull request #4657 from RalfJung/tracking-and-backtrace
print accessed range in alloc tracking; omit 1-frame backtraces
2 parents 65771d6 + d40514f commit a62e8f6

File tree

461 files changed

+52
-1005
lines changed

Some content is hidden

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

461 files changed

+52
-1005
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,9 @@ Try running `cargo miri clean`.
292292
Miri adds its own set of `-Z` flags, which are usually set via the `MIRIFLAGS`
293293
environment variable. We first document the most relevant and most commonly used flags:
294294

295+
* `-Zmiri-backtrace=<0|1|full>` configures how Miri prints backtraces: `1` is the default,
296+
where backtraces are printed in pruned form; `full` prints backtraces without pruning, and `0`
297+
disables backtraces entirely.
295298
* `-Zmiri-deterministic-concurrency` makes Miri's concurrency-related behavior fully deterministic.
296299
Strictly speaking, Miri is always fully deterministic when isolation is enabled (the default
297300
mode), but this determinism is achieved by using an RNG with a fixed seed. Seemingly harmless

src/diagnostics.rs

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ pub enum NonHaltingDiagnostic {
128128
PoppedPointerTag(Item, String),
129129
TrackingAlloc(AllocId, Size, Align),
130130
FreedAlloc(AllocId),
131-
AccessedAlloc(AllocId, AccessKind),
131+
AccessedAlloc(AllocId, AllocRange, AccessKind),
132132
RejectedIsolatedOp(String),
133133
ProgressReport {
134134
block_count: u64, // how many basic blocks have been run so far
@@ -604,28 +604,30 @@ pub fn report_msg<'tcx>(
604604
}
605605

606606
// Add backtrace
607-
let mut backtrace_title = String::from("BACKTRACE");
608-
if extra_span {
609-
write!(backtrace_title, " (of the first span)").unwrap();
610-
}
611-
if let Some(thread) = thread {
612-
let thread_name = machine.threads.get_thread_display_name(thread);
613-
if thread_name != "main" {
614-
// Only print thread name if it is not `main`.
615-
write!(backtrace_title, " on thread `{thread_name}`").unwrap();
616-
};
617-
}
618-
write!(backtrace_title, ":").unwrap();
619-
err.note(backtrace_title);
620-
for (idx, frame_info) in stacktrace.iter().enumerate() {
621-
let is_local = machine.is_local(frame_info);
622-
// No span for non-local frames and the first frame (which is the error site).
623-
if is_local && idx > 0 {
624-
err.subdiagnostic(frame_info.as_note(machine.tcx));
625-
} else {
626-
let sm = sess.source_map();
627-
let span = sm.span_to_embeddable_string(frame_info.span);
628-
err.note(format!("{frame_info} at {span}"));
607+
if stacktrace.len() > 1 {
608+
let mut backtrace_title = String::from("BACKTRACE");
609+
if extra_span {
610+
write!(backtrace_title, " (of the first span)").unwrap();
611+
}
612+
if let Some(thread) = thread {
613+
let thread_name = machine.threads.get_thread_display_name(thread);
614+
if thread_name != "main" {
615+
// Only print thread name if it is not `main`.
616+
write!(backtrace_title, " on thread `{thread_name}`").unwrap();
617+
};
618+
}
619+
write!(backtrace_title, ":").unwrap();
620+
err.note(backtrace_title);
621+
for (idx, frame_info) in stacktrace.iter().enumerate() {
622+
let is_local = machine.is_local(frame_info);
623+
// No span for non-local frames and the first frame (which is the error site).
624+
if is_local && idx > 0 {
625+
err.subdiagnostic(frame_info.as_note(machine.tcx));
626+
} else {
627+
let sm = sess.source_map();
628+
let span = sm.span_to_embeddable_string(frame_info.span);
629+
err.note(format!("{frame_info} at {span}"));
630+
}
629631
}
630632
}
631633

@@ -673,15 +675,15 @@ impl<'tcx> MiriMachine<'tcx> {
673675
"created tag {tag:?} with {perm} at {alloc_id:?}{range:?} derived from {orig_tag:?}"
674676
),
675677
PoppedPointerTag(item, cause) => format!("popped tracked tag for item {item:?}{cause}"),
676-
TrackingAlloc(AllocId(id), size, align) =>
678+
TrackingAlloc(id, size, align) =>
677679
format!(
678-
"now tracking allocation of {size} bytes (alignment {align} bytes) with id {id}",
680+
"now tracking allocation {id:?} of {size} bytes (alignment {align} bytes)",
679681
size = size.bytes(),
680682
align = align.bytes(),
681683
),
682-
AccessedAlloc(AllocId(id), access_kind) =>
683-
format!("{access_kind} to allocation with id {id}"),
684-
FreedAlloc(AllocId(id)) => format!("freed allocation with id {id}"),
684+
AccessedAlloc(id, range, access_kind) =>
685+
format!("{access_kind} at {id:?}[{}..{}]", range.start.bytes(), range.end().bytes()),
686+
FreedAlloc(id) => format!("freed allocation {id:?}"),
685687
RejectedIsolatedOp(op) => format!("{op} was made to return an error due to isolation"),
686688
ProgressReport { .. } =>
687689
format!("progress report: current operation being executed is here"),

src/machine.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1523,8 +1523,11 @@ impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> {
15231523
range: AllocRange,
15241524
) -> InterpResult<'tcx> {
15251525
if machine.track_alloc_accesses && machine.tracked_alloc_ids.contains(&alloc_id) {
1526-
machine
1527-
.emit_diagnostic(NonHaltingDiagnostic::AccessedAlloc(alloc_id, AccessKind::Read));
1526+
machine.emit_diagnostic(NonHaltingDiagnostic::AccessedAlloc(
1527+
alloc_id,
1528+
range,
1529+
AccessKind::Read,
1530+
));
15281531
}
15291532
// The order of checks is deliberate, to prefer reporting a data race over a borrow tracker error.
15301533
match &machine.data_race {
@@ -1559,8 +1562,11 @@ impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> {
15591562
range: AllocRange,
15601563
) -> InterpResult<'tcx> {
15611564
if machine.track_alloc_accesses && machine.tracked_alloc_ids.contains(&alloc_id) {
1562-
machine
1563-
.emit_diagnostic(NonHaltingDiagnostic::AccessedAlloc(alloc_id, AccessKind::Write));
1565+
machine.emit_diagnostic(NonHaltingDiagnostic::AccessedAlloc(
1566+
alloc_id,
1567+
range,
1568+
AccessKind::Write,
1569+
));
15641570
}
15651571
match &machine.data_race {
15661572
GlobalDataRaceHandler::None => {}

tests/fail-dep/concurrency/apple_os_unfair_lock_assert_not_owner.stderr

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ error: abnormal termination: called os_unfair_lock_assert_not_owner on an os_unf
33
|
44
LL | libc::os_unfair_lock_assert_not_owner(lock.get());
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ abnormal termination occurred here
6-
|
7-
= note: BACKTRACE:
8-
= note: inside `main` at tests/fail-dep/concurrency/apple_os_unfair_lock_assert_not_owner.rs:LL:CC
96

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

tests/fail-dep/concurrency/apple_os_unfair_lock_assert_owner.stderr

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ error: abnormal termination: called os_unfair_lock_assert_owner on an os_unfair_
33
|
44
LL | libc::os_unfair_lock_assert_owner(lock.get());
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ abnormal termination occurred here
6-
|
7-
= note: BACKTRACE:
8-
= note: inside `main` at tests/fail-dep/concurrency/apple_os_unfair_lock_assert_owner.rs:LL:CC
96

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

tests/fail-dep/concurrency/apple_os_unfair_lock_move_deadlock.stderr

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ error: the evaluated program deadlocked
33
|
44
LL | unsafe { libc::os_unfair_lock_lock(lock.get()) };
55
| ^ this thread got stuck here
6-
|
7-
= note: BACKTRACE:
8-
= note: inside `main` at tests/fail-dep/concurrency/apple_os_unfair_lock_move_deadlock.rs:LL:CC
96

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

tests/fail-dep/concurrency/apple_os_unfair_lock_reentrant.stderr

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ error: abnormal termination: attempted to lock an os_unfair_lock that is already
33
|
44
LL | libc::os_unfair_lock_lock(lock.get());
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ abnormal termination occurred here
6-
|
7-
= note: BACKTRACE:
8-
= note: inside `main` at tests/fail-dep/concurrency/apple_os_unfair_lock_reentrant.rs:LL:CC
96

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

tests/fail-dep/concurrency/apple_os_unfair_lock_unowned.stderr

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ error: abnormal termination: attempted to unlock an os_unfair_lock not owned by
33
|
44
LL | libc::os_unfair_lock_unlock(lock.get());
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ abnormal termination occurred here
6-
|
7-
= note: BACKTRACE:
8-
= note: inside `main` at tests/fail-dep/concurrency/apple_os_unfair_lock_unowned.rs:LL:CC
96

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

tests/fail-dep/concurrency/libc_pthread_cond_double_destroy.stderr

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ LL | libc::pthread_cond_destroy(cond.as_mut_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:
10-
= note: inside `main` at tests/fail-dep/concurrency/libc_pthread_cond_double_destroy.rs:LL:CC
119

1210
Uninitialized memory occurred at ALLOC[0xX..0xY], in this allocation:
1311
ALLOC DUMP

tests/fail-dep/concurrency/libc_pthread_condattr_double_destroy.stderr

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ LL | libc::pthread_condattr_destroy(attr.as_mut_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:
10-
= note: inside `main` at tests/fail-dep/concurrency/libc_pthread_condattr_double_destroy.rs:LL:CC
119

1210
Uninitialized memory occurred at ALLOC[0xX..0xY], in this allocation:
1311
ALLOC DUMP

0 commit comments

Comments
 (0)