Sub topic 5
Reading a Miri error report
This chapter assumes you can run cargo +nightly miri run and understand raw pointers and unsafe at a basic level. Here you will learn to read the report Miri prints when it finds undefined behavior (UB).
The parts of a report
Take the out-of-bounds read from the spine. It reads one element past the end of a three-element Vec.
fn main() {
let v = vec![1, 2, 3];
let p = v.as_ptr();
let x = unsafe { *p.add(3) };
println!("{x}");
}Run it with cargo +nightly miri run. The output has this shape. The exact wording and line numbers vary between Miri versions.
error: Undefined Behavior: memory access failed: expected a pointer to 4 bytes of memory, but got alloc1234 which is only 12 bytes from the end of the allocation
--> src/main.rs:4:22
|
4 | let x = unsafe { *p.add(3) };
| ^^^^^^^^^ Undefined Behavior occurred here
|
= 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: alloc1234 was allocated here:
...
note: inside `main`
--> src/main.rs:4:22A report has four parts.
- The headline. The line starting with
error: Undefined Behavior:names the rule that was broken. This is the most important line. It tells you the category: out-of-bounds access, use-after-free, invalid value, aliasing violation, or data race. - The primary location. The
-->line and the underlined span show the operation that triggered the error. Miri stops here. Nothing after this point has run. - The allocation notes. Lines such as
alloc1234 was allocated hereshow where the memory came from. For a use-after-free there is also a note showing where it was freed. - The backtrace. The
note: inside ...lines list the call stack, innermost call first. They show how execution reached the faulty line.
The error is where Miri noticed, not always where the bug is
Miri reports the first operation that is UB. That is often, but not always, the line you need to change.
Consider the invalid value example.
fn main() {
let b: bool = unsafe { std::mem::transmute(3u8) };
println!("{b}");
}Miri flags the transmute itself, because creating a bool with value 3 is already UB. It does not wait until the value is used. By contrast, the use-after-free example is reported at the dereference, while the cause is the earlier drop. In that case, read the "was freed here" note rather than only the primary location.
A good habit is to ask two questions of every report:
- Which operation does Miri say is invalid?
- Which earlier line made that operation invalid?
The second answer is usually in an allocation note or a borrow note.
Reading the headline vocabulary
Headlines use a small set of recurring phrases. This table maps common ones to their meaning.
| Phrase in the headline | Likely meaning |
|---|---|
| `memory access failed` / `out-of-bounds` | The pointer reaches outside its allocation. |
| `dangling pointer` / `has been freed` | The allocation was deallocated before this access. |
| `constructing invalid value` | A value breaks its type's rules, such as a `bool` that is not 0 or 1. |
| `using uninitialized data` | Bytes that were never written were read as a value. |
| `not granting access` / `borrow stack` / `tree` | An aliasing violation under Stacked Borrows or Tree Borrows. |
| `Data race detected` | Two threads accessed the same memory without synchronization. |
Later chapters cover each category in detail. For now, use the table to decide which chapter's ideas apply.
Backtrace tips
Miri's backtrace includes frames from your code and sometimes from the standard library. Start with the first frame that points into your own crate. Library frames below it only show which std function you called with bad arguments, such as Vec::set_len.
If you need more context, set RUST_BACKTRACE=1. Miri then prints a fuller backtrace.
RUST_BACKTRACE=1 cargo +nightly miri runExercise
Miri reports a use-after-free at a println!("{}", *p) line, and also prints a note saying the allocation was freed at a drop(Box::from_raw(p)) line. Which line should you investigate to find the cause, and why?
Show answer
Investigate the drop(Box::from_raw(p)) line. The dereference is only where Miri noticed the invalid access. The earlier drop freed the allocation, and that is what made the pointer dangling.