Skip to content

When reporting a heap use-after-free, say where the allocation was allocated and deallocated #2940

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,11 +304,21 @@ pub fn report_error<'tcx, 'mir>(
(None, format!("this usually indicates that your program performed an invalid operation and caused Undefined Behavior")),
(None, format!("but due to `-Zmiri-symbolic-alignment-check`, alignment errors can also be false positives")),
],
UndefinedBehavior(_) =>
vec![
UndefinedBehavior(info) => {
let mut helps = vec![
(None, format!("this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior")),
(None, format!("see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information")),
],
];
if let UndefinedBehaviorInfo::PointerUseAfterFree(alloc_id, _) = info {
if let Some(span) = ecx.machine.allocated_span(*alloc_id) {
helps.push((Some(span), format!("{:?} was allocated here:", alloc_id)));
}
if let Some(span) = ecx.machine.deallocated_span(*alloc_id) {
helps.push((Some(span), format!("{:?} was deallocated here:", alloc_id)));
}
}
helps
}
InvalidProgram(
InvalidProgramInfo::AlreadyReported(_)
) => {
Expand Down
49 changes: 48 additions & 1 deletion src/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use rustc_middle::{
},
};
use rustc_span::def_id::{CrateNum, DefId};
use rustc_span::Symbol;
use rustc_span::{Span, SpanData, Symbol};
use rustc_target::abi::{Align, Size};
use rustc_target::spec::abi::Abi;

Expand Down Expand Up @@ -135,6 +135,17 @@ impl MayLeak for MiriMemoryKind {
}
}

impl MiriMemoryKind {
/// Whether we have a useful allocation span for an allocation of this kind.
fn should_save_allocation_span(self) -> bool {
use self::MiriMemoryKind::*;
match self {
Rust | Miri | C | Mmap => true,
Machine | Global | ExternStatic | Tls | WinHeap | Runtime => false,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is WinHeap treated different from the other heap allocations?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I probably pulled it into the false list then never really noticed the mistake... do we have any tests that would show a use-after-free span for WinHeap? I don't think we actually do.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we have such a test, no.

}
}
}

impl fmt::Display for MiriMemoryKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use self::MiriMemoryKind::*;
Expand Down Expand Up @@ -497,6 +508,10 @@ pub struct MiriMachine<'mir, 'tcx> {

/// Whether to collect a backtrace when each allocation is created, just in case it leaks.
pub(crate) collect_leak_backtraces: bool,

/// The spans we will use to report where an allocation was created and deallocated in
/// diagnostics.
pub(crate) allocation_spans: RefCell<FxHashMap<AllocId, (Option<Span>, Option<Span>)>>,
}

impl<'mir, 'tcx> MiriMachine<'mir, 'tcx> {
Expand Down Expand Up @@ -621,6 +636,7 @@ impl<'mir, 'tcx> MiriMachine<'mir, 'tcx> {
stack_addr,
stack_size,
collect_leak_backtraces: config.collect_leak_backtraces,
allocation_spans: RefCell::new(FxHashMap::default()),
}
}

Expand Down Expand Up @@ -742,6 +758,22 @@ impl<'mir, 'tcx> MiriMachine<'mir, 'tcx> {
pub(crate) fn page_align(&self) -> Align {
Align::from_bytes(self.page_size).unwrap()
}

pub(crate) fn allocated_span(&self, alloc_id: AllocId) -> Option<SpanData> {
self.allocation_spans
.borrow()
.get(&alloc_id)
.and_then(|(allocated, _deallocated)| *allocated)
.map(Span::data)
}

pub(crate) fn deallocated_span(&self, alloc_id: AllocId) -> Option<SpanData> {
self.allocation_spans
.borrow()
.get(&alloc_id)
.and_then(|(_allocated, deallocated)| *deallocated)
.map(Span::data)
}
}

impl VisitTags for MiriMachine<'_, '_> {
Expand Down Expand Up @@ -791,6 +823,7 @@ impl VisitTags for MiriMachine<'_, '_> {
stack_addr: _,
stack_size: _,
collect_leak_backtraces: _,
allocation_spans: _,
} = self;

threads.visit_tags(visit);
Expand Down Expand Up @@ -1051,6 +1084,16 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for MiriMachine<'mir, 'tcx> {
},
|ptr| ecx.global_base_pointer(ptr),
)?;

if let MemoryKind::Machine(kind) = kind {
if kind.should_save_allocation_span() {
ecx.machine
.allocation_spans
.borrow_mut()
.insert(id, (Some(ecx.machine.current_span()), None));
}
}

Ok(Cow::Owned(alloc))
}

Expand Down Expand Up @@ -1181,6 +1224,10 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for MiriMachine<'mir, 'tcx> {
if let Some(borrow_tracker) = &mut alloc_extra.borrow_tracker {
borrow_tracker.before_memory_deallocation(alloc_id, prove_extra, range, machine)?;
}
if let Some((_, deallocated_at)) = machine.allocation_spans.borrow_mut().get_mut(&alloc_id)
{
*deallocated_at = Some(machine.current_span());
}
Ok(())
}

Expand Down
12 changes: 11 additions & 1 deletion tests/fail/alloc/deallocate-twice.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,17 @@ LL | unsafe { __rust_dealloc(ptr, layout.size(), layout.align()) }
|
= 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/deallocate-twice.rs:LL:CC
|
LL | let x = alloc(Layout::from_size_align_unchecked(1, 1));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: ALLOC was deallocated here:
--> $DIR/deallocate-twice.rs:LL:CC
|
LL | dealloc(x, Layout::from_size_align_unchecked(1, 1));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: BACKTRACE (of the first span):
= note: inside `std::alloc::dealloc` at RUSTLIB/alloc/src/alloc.rs:LL:CC
note: inside `main`
--> $DIR/deallocate-twice.rs:LL:CC
Expand Down
12 changes: 11 additions & 1 deletion tests/fail/alloc/reallocate-change-alloc.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,17 @@ LL | let _z = *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/reallocate-change-alloc.rs:LL:CC
|
LL | let x = alloc(Layout::from_size_align_unchecked(1, 1));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: ALLOC was deallocated here:
--> $DIR/reallocate-change-alloc.rs:LL:CC
|
LL | let _y = realloc(x, Layout::from_size_align_unchecked(1, 1), 1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: BACKTRACE (of the first span):
= note: inside `main` at $DIR/reallocate-change-alloc.rs:LL:CC

note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
Expand Down
12 changes: 11 additions & 1 deletion tests/fail/alloc/reallocate-dangling.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,17 @@ LL | unsafe { __rust_realloc(ptr, layout.size(), layout.align(), new_size) }
|
= 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/reallocate-dangling.rs:LL:CC
|
LL | let x = alloc(Layout::from_size_align_unchecked(1, 1));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: ALLOC was deallocated here:
--> $DIR/reallocate-dangling.rs:LL:CC
|
LL | dealloc(x, Layout::from_size_align_unchecked(1, 1));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: BACKTRACE (of the first span):
= note: inside `std::alloc::realloc` at RUSTLIB/alloc/src/alloc.rs:LL:CC
note: inside `main`
--> $DIR/reallocate-dangling.rs:LL:CC
Expand Down
12 changes: 11 additions & 1 deletion tests/fail/dangling_pointers/dangling_pointer_addr_of.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,17 @@ LL | let x = unsafe { ptr::addr_of!(*p) };
|
= 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/dangling_pointer_addr_of.rs:LL:CC
|
LL | let b = Box::new(42);
| ^^^^^^^^^^^^
help: ALLOC was deallocated here:
--> $DIR/dangling_pointer_addr_of.rs:LL:CC
|
LL | };
| ^
= note: BACKTRACE (of the first span):
= note: inside `main` at RUSTLIB/core/src/ptr/mod.rs:LL:CC
= note: this error originates in the macro `ptr::addr_of` (in Nightly builds, run with -Z macro-backtrace for more info)

Expand Down
12 changes: 11 additions & 1 deletion tests/fail/dangling_pointers/dangling_pointer_deref.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,17 @@ LL | let x = unsafe { *p };
|
= 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/dangling_pointer_deref.rs:LL:CC
|
LL | let b = Box::new(42);
| ^^^^^^^^^^^^
help: ALLOC was deallocated here:
--> $DIR/dangling_pointer_deref.rs:LL:CC
|
LL | };
| ^
= note: BACKTRACE (of the first span):
= note: inside `main` at $DIR/dangling_pointer_deref.rs:LL:CC

note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
Expand Down
12 changes: 11 additions & 1 deletion tests/fail/dangling_pointers/dangling_pointer_offset.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,17 @@ LL | let x = unsafe { p.offset(42) };
|
= 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/dangling_pointer_offset.rs:LL:CC
|
LL | let b = Box::new(42);
| ^^^^^^^^^^^^
help: ALLOC was deallocated here:
--> $DIR/dangling_pointer_offset.rs:LL:CC
|
LL | };
| ^
= note: BACKTRACE (of the first span):
= note: inside `main` at $DIR/dangling_pointer_offset.rs:LL:CC

note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,17 @@ LL | let _ = *p;
|
= 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/dangling_pointer_project_underscore.rs:LL:CC
|
LL | let b = Box::new(42);
| ^^^^^^^^^^^^
help: ALLOC was deallocated here:
--> $DIR/dangling_pointer_project_underscore.rs:LL:CC
|
LL | };
| ^
= note: BACKTRACE (of the first span):
= note: inside `main` at $DIR/dangling_pointer_project_underscore.rs:LL:CC

note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
Expand Down
12 changes: 11 additions & 1 deletion tests/fail/dangling_pointers/dangling_zst_deref.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,17 @@ LL | let _x = unsafe { *p };
|
= 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/dangling_zst_deref.rs:LL:CC
|
LL | let b = Box::new(42);
| ^^^^^^^^^^^^
help: ALLOC was deallocated here:
--> $DIR/dangling_zst_deref.rs:LL:CC
|
LL | };
| ^
= note: BACKTRACE (of the first span):
= note: inside `main` at $DIR/dangling_zst_deref.rs:LL:CC

note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
Expand Down
16 changes: 15 additions & 1 deletion tests/fail/data_race/dealloc_read_race2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,21 @@ LL | *ptr.0
|
= 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/dealloc_read_race2.rs:LL:CC
|
LL | let pointer: *mut usize = Box::into_raw(Box::new(0usize));
| ^^^^^^^^^^^^^^^^
help: ALLOC was deallocated here:
--> $DIR/dealloc_read_race2.rs:LL:CC
|
LL | / __rust_dealloc(
LL | | ptr.0 as *mut _,
LL | | std::mem::size_of::<usize>(),
LL | | std::mem::align_of::<usize>(),
LL | | )
| |_____________^
= note: BACKTRACE (of the first span):
= note: inside closure at $DIR/dealloc_read_race2.rs:LL:CC

note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
Expand Down
16 changes: 15 additions & 1 deletion tests/fail/data_race/dealloc_write_race2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,21 @@ LL | *ptr.0 = 2;
|
= 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/dealloc_write_race2.rs:LL:CC
|
LL | let pointer: *mut usize = Box::into_raw(Box::new(0usize));
| ^^^^^^^^^^^^^^^^
help: ALLOC was deallocated here:
--> $DIR/dealloc_write_race2.rs:LL:CC
|
LL | / __rust_dealloc(
LL | | ptr.0 as *mut _,
LL | | std::mem::size_of::<usize>(),
LL | | std::mem::align_of::<usize>(),
LL | | );
| |_____________^
= note: BACKTRACE (of the first span):
= note: inside closure at $DIR/dealloc_write_race2.rs:LL:CC

note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
Expand Down
12 changes: 11 additions & 1 deletion tests/fail/generator-pinned-moved.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,17 @@ LL | *num += 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/generator-pinned-moved.rs:LL:CC
|
LL | let mut generator_iterator = Box::new(GeneratorIteratorAdapter(firstn()));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: ALLOC was deallocated here:
--> $DIR/generator-pinned-moved.rs:LL:CC
|
LL | }; // *deallocate* generator_iterator
| ^
= note: BACKTRACE (of the first span):
= note: inside closure at $DIR/generator-pinned-moved.rs:LL:CC
note: inside `<GeneratorIteratorAdapter<[static generator@$DIR/generator-pinned-moved.rs:LL:CC]> as std::iter::Iterator>::next`
--> $DIR/generator-pinned-moved.rs:LL:CC
Expand Down
12 changes: 11 additions & 1 deletion tests/fail/rc_as_ptr.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,17 @@ LL | assert_eq!(42, **unsafe { &*Weak::as_ptr(&weak) });
|
= 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/rc_as_ptr.rs:LL:CC
|
LL | let strong = Rc::new(Box::new(42));
| ^^^^^^^^^^^^
help: ALLOC was deallocated here:
--> $DIR/rc_as_ptr.rs:LL:CC
|
LL | drop(strong);
| ^^^^^^^^^^^^
= note: BACKTRACE (of the first span):
= note: inside `main` at RUSTLIB/core/src/macros/mod.rs:LL:CC
= note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)

Expand Down
19 changes: 18 additions & 1 deletion tests/fail/shims/mmap_use_after_munmap.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,24 @@ LL | let _x = *(ptr as *mut u8);
|
= 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/mmap_use_after_munmap.rs:LL:CC
|
LL | let ptr = libc::mmap(
| ___________________^
LL | | std::ptr::null_mut(),
LL | | 4096,
LL | | libc::PROT_READ | libc::PROT_WRITE,
... |
LL | | 0,
LL | | );
| |_________^
help: ALLOC was deallocated here:
--> $DIR/mmap_use_after_munmap.rs:LL:CC
|
LL | libc::munmap(ptr, 4096);
| ^^^^^^^^^^^^^^^^^^^^^^^
= note: BACKTRACE (of the first span):
= note: inside `main` at $DIR/mmap_use_after_munmap.rs:LL:CC

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