Skip to content

Commit 9ad0f65

Browse files
committed
Auto merge of #3837 - JoJoDeveloping:tb-compacting-provenance-gc, r=RalfJung
Make Tree Borrows Provenance GC compact the tree Follow-up on #3833 and #3835. In these PRs, the TB GC was fixed to no longer cause a stack overflow. One test that motivated it was the test `fill::horizontal_line` in [`tiny-skia`](https://github.com/RazrFalcon/tiny-skia). But not causing stack overflows was not a large improvents, since it did not fix the fundamental issue: The tree was too large. The test now ran, but it required gigabytes of memory and hours of time (only for it to be OOM-killed 🤬), whereas it finishes within 24 seconds in Stacked Borrows. With this merged, it finishes in about 40 seconds under TB. The problem in that test was that it used [`slice::chunked`](https://doc.rust-lang.org/std/primitive.slice.html#method.chunks) to iterate a slice in chunks. That iterator is written to reborrow at each call to `next`, which creates a linear tree with a bunch of intermediary nodes, which also fragments the `RangeMap` for that allocation. The solution is to now compact the tree, so that these interior nodes are removed. Care is taken to not remove nodes that are protected, or that otherwise restrict their children. I am currently only 99% sure that this is sound, and I do also think that this could compact even more. So `@Vanille-N` please also have a look at whether I got the compacting logic right. For a more visual comparison, [here is a gist](https://gist.github.com/JoJoDeveloping/ae4a7f7c29335a4c233ef42d2f267b01) of what the tree looks like at one point during that test, with and without compacting. This new GC requires a different iteration order during accesses (since the current one can make the error messages non-deterministic), so it is rebased on top of #3843 and requires that PR to be merged first.
2 parents 79115f5 + 84134c6 commit 9ad0f65

File tree

8 files changed

+237
-17
lines changed

8 files changed

+237
-17
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# This file is automatically @generated by Cargo.
2+
# It is not intended for manual editing.
3+
version = 3
4+
5+
[[package]]
6+
name = "slice-chunked"
7+
version = "0.1.0"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "slice-chunked"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//! This is a small example using slice::chunks, which creates a very large Tree Borrows tree.
2+
//! Thanks to ##3837, the GC now compacts the tree, so this test can be run in a reasonable time again.
3+
//! The actual code is adapted from tiny_skia, see https://github.com/RazrFalcon/tiny-skia/blob/master/src/pixmap.rs#L121
4+
//! To make this benchmark demonstrate the effectiveness, run with MIRIFLAGS="-Zmiri-tree-borrows -Zmiri-provenance-gc=100"
5+
6+
const N: usize = 1000;
7+
8+
fn input_vec() -> Vec<u8> {
9+
vec![0; N]
10+
}
11+
12+
fn main() {
13+
let data_len = 2 * N;
14+
let mut rgba_data = Vec::with_capacity(data_len);
15+
let img_data = input_vec();
16+
for slice in img_data.chunks(2) {
17+
let gray = slice[0];
18+
let alpha = slice[1];
19+
rgba_data.push(gray);
20+
rgba_data.push(gray);
21+
rgba_data.push(gray);
22+
rgba_data.push(alpha);
23+
}
24+
25+
assert_eq!(rgba_data.len(), data_len);
26+
}

src/tools/miri/src/borrow_tracker/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ pub struct FrameState {
7171

7272
impl VisitProvenance for FrameState {
7373
fn visit_provenance(&self, visit: &mut VisitWith<'_>) {
74+
// Visit all protected tags. At least in Tree Borrows,
75+
// protected tags can not be GC'd because they still have
76+
// an access coming when the protector ends. Additionally,
77+
// the tree compacting mechanism of TB's GC relies on the fact
78+
// that all protected tags are marked as live for correctness,
79+
// so we _have_ to visit them here.
7480
for (id, tag) in &self.protected_tags {
7581
visit(Some(*id), Some(*tag));
7682
}

src/tools/miri/src/borrow_tracker/tree_borrows/perms.rs

+39-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ mod transition {
130130
Active =>
131131
if protected {
132132
// We wrote, someone else reads -- that's bad.
133-
// (If this is initialized, this move-to-protected will mean insta-UB.)
133+
// (Since Active is always initialized, this move-to-protected will mean insta-UB.)
134134
Disabled
135135
} else {
136136
// We don't want to disable here to allow read-read reordering: it is crucial
@@ -267,6 +267,44 @@ impl Permission {
267267
transition::perform_access(kind, rel_pos, old_state, protected)
268268
.map(|new_state| PermTransition { from: old_state, to: new_state })
269269
}
270+
271+
/// During a provenance GC, we want to compact the tree.
272+
/// For this, we want to merge nodes upwards if they have a singleton parent.
273+
/// But we need to be careful: If the parent is Frozen, and the child is Reserved,
274+
/// we can not do such a merge. In general, such a merge is possible if the parent
275+
/// allows similar accesses, and in particular if the parent never causes UB on its
276+
/// own. This is enforced by a test, namely `tree_compacting_is_sound`. See that
277+
/// test for more information.
278+
/// This method is only sound if the parent is not protected. We never attempt to
279+
/// remove protected parents.
280+
pub fn can_be_replaced_by_child(self, child: Self) -> bool {
281+
match (self.inner, child.inner) {
282+
// ReservedIM can be replaced by anything, as it allows all
283+
// transitions.
284+
(ReservedIM, _) => true,
285+
// Reserved (as parent, where conflictedness does not matter)
286+
// can be replaced by all but ReservedIM,
287+
// since ReservedIM alone would survive foreign writes
288+
(ReservedFrz { .. }, ReservedIM) => false,
289+
(ReservedFrz { .. }, _) => true,
290+
// Active can not be replaced by something surviving
291+
// foreign reads and then remaining writable.
292+
(Active, ReservedIM) => false,
293+
(Active, ReservedFrz { .. }) => false,
294+
// Replacing a state by itself is always okay, even if the child state is protected.
295+
(Active, Active) => true,
296+
// Active can be replaced by Frozen, since it is not protected.
297+
(Active, Frozen) => true,
298+
(Active, Disabled) => true,
299+
// Frozen can only be replaced by Disabled (and itself).
300+
(Frozen, Frozen) => true,
301+
(Frozen, Disabled) => true,
302+
(Frozen, _) => false,
303+
// Disabled can not be replaced by anything else.
304+
(Disabled, Disabled) => true,
305+
(Disabled, _) => false,
306+
}
307+
}
270308
}
271309

272310
impl PermTransition {

src/tools/miri/src/borrow_tracker/tree_borrows/tree.rs

+83-15
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,22 @@ impl LocationState {
128128
Ok(transition)
129129
}
130130

131+
/// Like `perform_access`, but ignores the concrete error cause and also uses state-passing
132+
/// rather than a mutable reference. As such, it returns `Some(x)` if the transition succeeded,
133+
/// or `None` if there was an error.
134+
#[cfg(test)]
135+
fn perform_access_no_fluff(
136+
mut self,
137+
access_kind: AccessKind,
138+
rel_pos: AccessRelatedness,
139+
protected: bool,
140+
) -> Option<Self> {
141+
match self.perform_access(access_kind, rel_pos, protected) {
142+
Ok(_) => Some(self),
143+
Err(_) => None,
144+
}
145+
}
146+
131147
// Helper to optimize the tree traversal.
132148
// The optimization here consists of observing thanks to the tests
133149
// `foreign_read_is_noop_after_foreign_write` and `all_transitions_idempotent`,
@@ -840,6 +856,60 @@ impl Tree {
840856
node.children.is_empty() && !live.contains(&node.tag)
841857
}
842858

859+
/// Checks whether a node can be replaced by its only child.
860+
/// If so, returns the index of said only child.
861+
/// If not, returns none.
862+
fn can_be_replaced_by_single_child(
863+
&self,
864+
idx: UniIndex,
865+
live: &FxHashSet<BorTag>,
866+
) -> Option<UniIndex> {
867+
let node = self.nodes.get(idx).unwrap();
868+
869+
// We never want to replace the root node, as it is also kept in `root_ptr_tags`.
870+
if node.children.len() != 1 || live.contains(&node.tag) || node.parent.is_none() {
871+
return None;
872+
}
873+
// Since protected nodes are never GC'd (see `borrow_tracker::FrameExtra::visit_provenance`),
874+
// we know that `node` is not protected because otherwise `live` would
875+
// have contained `node.tag`.
876+
let child_idx = node.children[0];
877+
let child = self.nodes.get(child_idx).unwrap();
878+
// Check that for that one child, `can_be_replaced_by_child` holds for the permission
879+
// on all locations.
880+
for (_, data) in self.rperms.iter_all() {
881+
let parent_perm =
882+
data.get(idx).map(|x| x.permission).unwrap_or_else(|| node.default_initial_perm);
883+
let child_perm = data
884+
.get(child_idx)
885+
.map(|x| x.permission)
886+
.unwrap_or_else(|| child.default_initial_perm);
887+
if !parent_perm.can_be_replaced_by_child(child_perm) {
888+
return None;
889+
}
890+
}
891+
892+
Some(child_idx)
893+
}
894+
895+
/// Properly removes a node.
896+
/// The node to be removed should not otherwise be usable. It also
897+
/// should have no children, but this is not checked, so that nodes
898+
/// whose children were rotated somewhere else can be deleted without
899+
/// having to first modify them to clear that array.
900+
fn remove_useless_node(&mut self, this: UniIndex) {
901+
// Due to the API of UniMap we must make sure to call
902+
// `UniValMap::remove` for the key of this node on *all* maps that used it
903+
// (which are `self.nodes` and every range of `self.rperms`)
904+
// before we can safely apply `UniKeyMap::remove` to truly remove
905+
// this tag from the `tag_mapping`.
906+
let node = self.nodes.remove(this).unwrap();
907+
for (_perms_range, perms) in self.rperms.iter_mut_all() {
908+
perms.remove(this);
909+
}
910+
self.tag_mapping.remove(&node.tag);
911+
}
912+
843913
/// Traverses the entire tree looking for useless tags.
844914
/// Removes from the tree all useless child nodes of root.
845915
/// It will not delete the root itself.
@@ -883,23 +953,21 @@ impl Tree {
883953
// Remove all useless children.
884954
children_of_node.retain_mut(|idx| {
885955
if self.is_useless(*idx, live) {
886-
// Note: In the rest of this comment, "this node" refers to `idx`.
887-
// This node has no more children (if there were any, they have already been removed).
888-
// It is also unreachable as determined by the GC, so we can remove it everywhere.
889-
// Due to the API of UniMap we must make sure to call
890-
// `UniValMap::remove` for the key of this node on *all* maps that used it
891-
// (which are `self.nodes` and every range of `self.rperms`)
892-
// before we can safely apply `UniKeyMap::remove` to truly remove
893-
// this tag from the `tag_mapping`.
894-
let node = self.nodes.remove(*idx).unwrap();
895-
for (_perms_range, perms) in self.rperms.iter_mut_all() {
896-
perms.remove(*idx);
897-
}
898-
self.tag_mapping.remove(&node.tag);
899-
// now delete it
956+
// Delete `idx` node everywhere else.
957+
self.remove_useless_node(*idx);
958+
// And delete it from children_of_node.
900959
false
901960
} else {
902-
// do nothing, but retain
961+
if let Some(nextchild) = self.can_be_replaced_by_single_child(*idx, live) {
962+
// `nextchild` is our grandchild, and will become our direct child.
963+
// Delete the in-between node, `idx`.
964+
self.remove_useless_node(*idx);
965+
// Set the new child's parent.
966+
self.nodes.get_mut(nextchild).unwrap().parent = Some(*tag);
967+
// Save the new child in children_of_node.
968+
*idx = nextchild;
969+
}
970+
// retain it
903971
true
904972
}
905973
});

src/tools/miri/src/borrow_tracker/tree_borrows/tree/tests.rs

+65
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,71 @@ fn all_read_accesses_commute() {
6464
}
6565
}
6666

67+
fn as_foreign_or_child(related: AccessRelatedness) -> &'static str {
68+
if related.is_foreign() { "foreign" } else { "child" }
69+
}
70+
71+
fn as_protected(b: bool) -> &'static str {
72+
if b { " (protected)" } else { "" }
73+
}
74+
75+
fn as_lazy_or_init(b: bool) -> &'static str {
76+
if b { "initialized" } else { "lazy" }
77+
}
78+
79+
/// Test that tree compacting (as performed by the GC) is sound.
80+
/// Specifically, the GC will replace a parent by a child if the parent is not
81+
/// protected, and if `can_be_replaced_by_child(parent, child)` is true.
82+
/// To check that this is sound, the function must be a simulation, i.e.
83+
/// if both are accessed, the results must still be in simulation, and also
84+
/// if an access is UB, it must also be UB if done only at the child.
85+
#[test]
86+
fn tree_compacting_is_sound() {
87+
// The parent is unprotected
88+
let parent_protected = false;
89+
for ([parent, child], child_protected) in <([LocationState; 2], bool)>::exhaustive() {
90+
if child_protected {
91+
precondition!(child.compatible_with_protector())
92+
}
93+
precondition!(parent.permission().can_be_replaced_by_child(child.permission()));
94+
for (kind, rel) in <(AccessKind, AccessRelatedness)>::exhaustive() {
95+
let new_parent = parent.perform_access_no_fluff(kind, rel, parent_protected);
96+
let new_child = child.perform_access_no_fluff(kind, rel, child_protected);
97+
match (new_parent, new_child) {
98+
(Some(np), Some(nc)) => {
99+
assert!(
100+
np.permission().can_be_replaced_by_child(nc.permission()),
101+
"`can_be_replaced_by_child` is not a simulation: on a {} {} to a {} parent and a {} {}{} child, the parent becomes {}, the child becomes {}, and these are not in simulation!",
102+
as_foreign_or_child(rel),
103+
kind,
104+
parent.permission(),
105+
as_lazy_or_init(child.is_initialized()),
106+
child.permission(),
107+
as_protected(child_protected),
108+
np.permission(),
109+
nc.permission()
110+
)
111+
}
112+
(_, None) => {
113+
// the child produced UB, this is fine no matter what the parent does
114+
}
115+
(None, Some(nc)) => {
116+
panic!(
117+
"`can_be_replaced_by_child` does not have the UB property: on a {} {} to a(n) {} parent and a(n) {} {}{} child, only the parent causes UB, while the child becomes {}, and it is not allowed for only the parent to cause UB!",
118+
as_foreign_or_child(rel),
119+
kind,
120+
parent.permission(),
121+
as_lazy_or_init(child.is_initialized()),
122+
child.permission(),
123+
as_protected(child_protected),
124+
nc.permission()
125+
)
126+
}
127+
}
128+
}
129+
}
130+
}
131+
67132
#[test]
68133
#[rustfmt::skip]
69134
// Ensure that of 2 accesses happen, one foreign and one a child, and we are protected, that we

src/tools/miri/tests/pass-dep/concurrency/linux-futex.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,9 @@ fn wait_wake() {
158158
);
159159
}
160160

161-
assert!((200..1000).contains(&start.elapsed().as_millis()));
161+
// When running this in stress-gc mode, things can take quite long.
162+
// So the timeout is 3000 ms.
163+
assert!((200..3000).contains(&start.elapsed().as_millis()));
162164
t.join().unwrap();
163165
}
164166

0 commit comments

Comments
 (0)