-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Use a DFS to compare CTFE snapshots #66946
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 all commits
6faae4d
f0148a5
840e323
fa5ff3a
37844e5
26b9146
345a8d5
e017f57
afb4faa
ba22627
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 |
---|---|---|
|
@@ -22,7 +22,7 @@ use super::{ | |
Machine, AllocMap, MayLeak, ErrorHandled, CheckInAllocMsg, | ||
}; | ||
|
||
#[derive(Debug, PartialEq, Copy, Clone)] | ||
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)] | ||
pub enum MemoryKind<T> { | ||
/// Stack memory. Error if deallocated except during a stack pop. | ||
Stack, | ||
|
@@ -114,24 +114,6 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> HasDataLayout for Memory<'mir, 'tcx, M> | |
} | ||
} | ||
|
||
// FIXME: Really we shouldn't clone memory, ever. Snapshot machinery should instead | ||
// carefully copy only the reachable parts. | ||
impl<'mir, 'tcx, M> Clone for Memory<'mir, 'tcx, M> | ||
where | ||
M: Machine<'mir, 'tcx, PointerTag = (), AllocExtra = (), MemoryExtra = ()>, | ||
M::MemoryMap: AllocMap<AllocId, (MemoryKind<M::MemoryKinds>, Allocation)>, | ||
{ | ||
fn clone(&self) -> Self { | ||
Memory { | ||
alloc_map: self.alloc_map.clone(), | ||
extra_fn_ptr_map: self.extra_fn_ptr_map.clone(), | ||
dead_alloc_map: self.dead_alloc_map.clone(), | ||
extra: (), | ||
tcx: self.tcx, | ||
} | ||
} | ||
} | ||
|
||
impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> { | ||
pub fn new(tcx: TyCtxtAt<'tcx>, extra: M::MemoryExtra) -> Self { | ||
Memory { | ||
|
@@ -950,3 +932,56 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> { | |
} | ||
} | ||
} | ||
|
||
/// A depth-first search over the allocation graph. | ||
/// | ||
/// This is based on the DFS iterator in `rustc_data_structures`, which we cannot use directly | ||
/// because `AllocId` does not implement `Idx` (it is not dense). | ||
pub struct DepthFirstSearch<'mem, 'mir, 'tcx, M: Machine<'mir, 'tcx>> { | ||
memory: &'mem Memory<'mir, 'tcx, M>, | ||
visited: FxHashSet<AllocId>, | ||
stack: Vec<AllocId>, | ||
} | ||
|
||
impl<M: Machine<'mir, 'tcx>> DepthFirstSearch<'mem, 'mir, 'tcx, M> { | ||
/// Returns a new DFS iterator that will traverse all allocations reachable from the given | ||
/// `AllocId`s. | ||
/// | ||
/// The first node in `roots` will be the first node visited by the DFS. | ||
pub fn with_roots( | ||
memory: &'mem Memory<'mir, 'tcx, M>, | ||
roots: impl IntoIterator<Item = AllocId>, | ||
) -> Self { | ||
let mut stack: Vec<_> = roots.into_iter().collect(); | ||
stack.reverse(); | ||
|
||
DepthFirstSearch { | ||
memory, | ||
visited: stack.iter().copied().collect(), | ||
stack, | ||
} | ||
} | ||
} | ||
|
||
impl<M: Machine<'mir, 'tcx>> Iterator for DepthFirstSearch<'mem, 'mir, 'tcx, M> { | ||
type Item = (AllocId, InterpResult<'tcx, &'mem Allocation<M::PointerTag, M::AllocExtra>>); | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
let DepthFirstSearch { stack, visited, memory } = self; | ||
|
||
let id = stack.pop()?; | ||
let alloc = memory.get_raw(id); | ||
|
||
if let Ok(alloc) = alloc { | ||
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. Is this where you are ignoring We should make sure that we don't catch errors requiring allocation though, similar to this. Maybe create a new helper method at the error type for this? 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'm not sure what you mean by "errors requiring allocation". Are there certain errors that we should 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. Any error type that has a So, the idea is to make sure that any error that is caught and acted upon (and not just rethrown) does not allocate 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. Idea: Should we make the set of errors that 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. That makes sense. I'll probably wait for someone else to do this? It feels like y'all have a decent idea of what you want here. |
||
let new_pointers = alloc | ||
.relocations() | ||
.values() | ||
.map(|&(_, id)| id) | ||
.filter(|id| visited.insert(*id)); | ||
|
||
stack.extend(new_pointers); | ||
} | ||
|
||
Some((id, alloc)) | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.