-
Notifications
You must be signed in to change notification settings - Fork 349
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement -Zmiri-tag-gc a garbage collector for tags
- Loading branch information
Showing
11 changed files
with
190 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,6 +59,7 @@ mod range_map; | |
mod shims; | ||
mod stacked_borrows; | ||
mod sync; | ||
mod tag_gc; | ||
mod thread; | ||
mod vector_clock; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
use crate::*; | ||
use rustc_data_structures::fx::FxHashSet; | ||
|
||
pub fn garbage_collect_tags<'mir, 'tcx>( | ||
ecx: &mut MiriEvalContext<'mir, 'tcx>, | ||
) -> InterpResult<'tcx> { | ||
// No reason to do anything at all if stacked borrows is off. | ||
if ecx.machine.stacked_borrows.is_none() { | ||
return Ok(()); | ||
} | ||
|
||
let mut tags = FxHashSet::default(); | ||
|
||
ecx.machine.tls.iter(|scalar| { | ||
if let Scalar::Ptr(Pointer { provenance: Provenance::Concrete { sb, .. }, .. }, _) = scalar | ||
{ | ||
tags.insert(*sb); | ||
} | ||
}); | ||
|
||
// Normal memory | ||
ecx.memory.alloc_map().iter(|it| { | ||
for (_id, (_kind, alloc)) in it { | ||
let stacked_borrows = alloc.extra.stacked_borrows.as_ref().unwrap(); | ||
// Base tags: TODO: Do we need this? | ||
for stack in stacked_borrows.borrow_mut().iter_all() { | ||
if let Some(item) = stack.get(0) { | ||
tags.insert(item.tag()); | ||
} | ||
} | ||
for (_size, prov) in alloc.relocations().iter() { | ||
if let Provenance::Concrete { sb, .. } = prov { | ||
tags.insert(*sb); | ||
} | ||
} | ||
} | ||
}); | ||
|
||
// Locals | ||
// This code is a mess because... | ||
// force_allocation only works on locals from the current thread. This is because locals don't | ||
// remember what thread they are from, they're just an index. So attempting to use a local on | ||
// the wrong thread is simply an error. | ||
// Additionally, InterpCx::force_allocation takes out a mutable borrow of the whole InterpCx. | ||
// So we need to do this whole dance with indexes and a clone of the return place in order to | ||
// avoid holding a shared borrow of anything during the force_allocation call. | ||
let tids = | ||
ecx.machine.threads.threads.iter_enumerated().map(|(tid, _thread)| tid).collect::<Vec<_>>(); | ||
let original_tid = ecx.machine.threads.active_thread; | ||
|
||
for tid in tids { | ||
ecx.machine.threads.active_thread = tid; | ||
for f in 0..ecx.active_thread_stack().len() { | ||
// Handle the return place of each frame | ||
let frame = &ecx.active_thread_stack()[f]; | ||
let return_place = ecx.force_allocation(&frame.return_place.clone())?; | ||
return_place.map_provenance(|p| { | ||
if let Some(Provenance::Concrete { sb, .. }) = p { | ||
tags.insert(sb); | ||
} | ||
p | ||
}); | ||
|
||
let frame = &ecx.active_thread_stack()[f]; | ||
for local in frame.locals.iter() { | ||
if let LocalValue::Live(Operand::Immediate(Immediate::Scalar( | ||
ScalarMaybeUninit::Scalar(Scalar::Ptr(ptr, _)), | ||
))) = local.value | ||
{ | ||
if let Provenance::Concrete { sb, .. } = ptr.provenance { | ||
tags.insert(sb); | ||
} | ||
} | ||
if let LocalValue::Live(Operand::Immediate(Immediate::ScalarPair(s1, s2))) = | ||
local.value | ||
{ | ||
if let ScalarMaybeUninit::Scalar(Scalar::Ptr(ptr, _)) = s1 { | ||
if let Provenance::Concrete { sb, .. } = ptr.provenance { | ||
tags.insert(sb); | ||
} | ||
} | ||
if let ScalarMaybeUninit::Scalar(Scalar::Ptr(ptr, _)) = s2 { | ||
if let Provenance::Concrete { sb, .. } = ptr.provenance { | ||
tags.insert(sb); | ||
} | ||
} | ||
} | ||
|
||
if let LocalValue::Live(Operand::Indirect(MemPlace { ptr, .. })) = local.value { | ||
if let Some(Provenance::Concrete { sb, .. }) = ptr.provenance { | ||
tags.insert(sb); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
ecx.machine.threads.active_thread = original_tid; | ||
|
||
ecx.memory.alloc_map().iter(|it| { | ||
for (id, (_kind, alloc)) in it { | ||
// Don't GC any allocation which has been exposed | ||
if ecx.machine.intptrcast.borrow().is_exposed(*id) { | ||
continue; | ||
} | ||
|
||
let stacked_borrows = alloc.extra.stacked_borrows.as_ref().unwrap(); | ||
for stack in stacked_borrows.borrow_mut().iter_all() { | ||
stack.retain(&tags); | ||
} | ||
} | ||
}); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters