Skip to content

Commit da940eb

Browse files
committed
Auto merge of #1354 - divergentdave:dump-alloc-on-undef-read, r=RalfJung
Print hex dump of alloc on reading undef bytes Here's a small addition I made locally to the UB diagnostics, in case you're interested in it. This PR calls `dump_alloc()` on the relevant allocation if Miri fails on UB due to reading undefined bytes. This came in handy when diagnosing such an issue in a large program using unsafe Rust, in part because it wasn't deterministic enough to use `-Zmiri-track-alloc-id=`. If you'd like to put this behind another -Z flag, let me know.
2 parents 97ae5b6 + e267fb4 commit da940eb

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

src/diagnostics.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,19 @@ pub fn report_error<'tcx, 'mir>(
115115

116116
e.print_backtrace();
117117
let msg = e.to_string();
118-
report_msg(ecx, &format!("{}: {}", title, msg), msg, helps, true)
118+
report_msg(ecx, &format!("{}: {}", title, msg), msg, helps, true);
119+
120+
// Extra output to help debug specific issues.
121+
if let UndefinedBehavior(UndefinedBehaviorInfo::InvalidUndefBytes(Some(ptr))) = e.kind {
122+
eprintln!(
123+
"Uninitialized read occurred at offset 0x{:x} into this allocation:",
124+
ptr.offset.bytes(),
125+
);
126+
ecx.memory.dump_alloc(ptr.alloc_id);
127+
eprintln!();
128+
}
129+
130+
None
119131
}
120132

121133
/// Report an error or note (depending on the `error` argument) at the current frame's current statement.
@@ -126,7 +138,7 @@ fn report_msg<'tcx, 'mir>(
126138
span_msg: String,
127139
mut helps: Vec<String>,
128140
error: bool,
129-
) -> Option<i64> {
141+
) {
130142
let span = if let Some(frame) = ecx.machine.stack.last() {
131143
frame.current_source_info().unwrap().span
132144
} else {
@@ -167,8 +179,6 @@ fn report_msg<'tcx, 'mir>(
167179
trace!(" local {}: {:?}", i, local.value);
168180
}
169181
}
170-
// Let the reported error determine the return code.
171-
return None;
172182
}
173183

174184
thread_local! {
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// error-pattern: reading uninitialized memory
2+
3+
use std::alloc::{alloc, dealloc, Layout};
4+
use std::slice::from_raw_parts;
5+
6+
fn main() {
7+
let layout = Layout::from_size_align(32, 8).unwrap();
8+
unsafe {
9+
let ptr = alloc(layout);
10+
*ptr = 0x41;
11+
*ptr.add(1) = 0x42;
12+
*ptr.add(2) = 0x43;
13+
*ptr.add(3) = 0x44;
14+
*ptr.add(16) = 0x00;
15+
let slice1 = from_raw_parts(ptr, 16);
16+
let slice2 = from_raw_parts(ptr.add(16), 16);
17+
drop(slice1.cmp(slice2));
18+
dealloc(ptr, layout);
19+
}
20+
}

0 commit comments

Comments
 (0)