-
Notifications
You must be signed in to change notification settings - Fork 391
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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::*; | ||
|
@@ -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> { | ||
|
@@ -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()), | ||
} | ||
} | ||
|
||
|
@@ -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<'_, '_> { | ||
|
@@ -791,6 +823,7 @@ impl VisitTags for MiriMachine<'_, '_> { | |
stack_addr: _, | ||
stack_size: _, | ||
collect_leak_backtraces: _, | ||
allocation_spans: _, | ||
} = self; | ||
|
||
threads.visit_tags(visit); | ||
|
@@ -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() { | ||
saethlin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ecx.machine | ||
.allocation_spans | ||
.borrow_mut() | ||
.insert(id, (Some(ecx.machine.current_span()), None)); | ||
saethlin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
Ok(Cow::Owned(alloc)) | ||
} | ||
|
||
|
@@ -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(()) | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.