Skip to content

Commit f0fe74f

Browse files
authored
Merge pull request #4008 from JoJoDeveloping/tb-access-state-based-skipping
[TB Optimization] Skip subtrees based on the subtree's root node's permissions
2 parents 994802c + c387247 commit f0fe74f

File tree

4 files changed

+88
-1
lines changed

4 files changed

+88
-1
lines changed

src/borrow_tracker/tree_borrows/perms.rs

+4
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,10 @@ impl Permission {
237237
pub fn is_active(&self) -> bool {
238238
self.inner == Active
239239
}
240+
/// Check if `self` is the never-allow-writes-again state of a pointer (is `Frozen`).
241+
pub fn is_frozen(&self) -> bool {
242+
self.inner == Frozen
243+
}
240244

241245
/// Default initial permission of the root of a new tree at inbounds positions.
242246
/// Must *only* be used for the root, this is not in general an "initial" permission!

src/borrow_tracker/tree_borrows/tree.rs

+24-1
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,31 @@ impl LocationState {
153153
) -> ContinueTraversal {
154154
if rel_pos.is_foreign() {
155155
let happening_now = IdempotentForeignAccess::from_foreign(access_kind);
156-
let new_access_noop =
156+
let mut new_access_noop =
157157
self.idempotent_foreign_access.can_skip_foreign_access(happening_now);
158+
if self.permission.is_disabled() {
159+
// A foreign access to a `Disabled` tag will have almost no observable effect.
160+
// It's a theorem that `Disabled` node have no protected initialized children,
161+
// and so this foreign access will never trigger any protector.
162+
// (Intuition: You're either protected initialized, and thus can't become Disabled
163+
// or you're already Disabled protected, but not initialized, and then can't
164+
// become initialized since that requires a child access, which Disabled blocks.)
165+
// Further, the children will never be able to read or write again, since they
166+
// have a `Disabled` parent. So this only affects diagnostics, such that the
167+
// blocking write will still be identified directly, just at a different tag.
168+
new_access_noop = true;
169+
}
170+
if self.permission.is_frozen() && access_kind == AccessKind::Read {
171+
// A foreign read to a `Frozen` tag will have almost no observable effect.
172+
// It's a theorem that `Frozen` nodes have no active children, so all children
173+
// already survive foreign reads. Foreign reads in general have almost no
174+
// effect, the only further thing they could do is make protected `Reserved`
175+
// nodes become conflicted, i.e. make them reject child writes for the further
176+
// duration of their protector. But such a child write is already rejected
177+
// because this node is frozen. So this only affects diagnostics, but the
178+
// blocking read will still be identified directly, just at a different tag.
179+
new_access_noop = true;
180+
}
158181
if new_access_noop {
159182
// Abort traversal if the new access is indeed guaranteed
160183
// to be noop.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//@compile-flags: -Zmiri-tree-borrows -Zmiri-provenance-gc=0
2+
3+
// Shows the effect of the optimization of #4008.
4+
// The diagnostics change, but not the error itself.
5+
6+
// When this method is called, the tree will be a single line and look like this,
7+
// with other_ptr being the root at the top
8+
// other_ptr = root : Active
9+
// intermediary : Frozen // an intermediary node
10+
// m : Reserved
11+
fn write_to_mut(m: &mut u8, other_ptr: *const u8) {
12+
unsafe {
13+
std::hint::black_box(*other_ptr);
14+
}
15+
// In line 17 above, m should have become Reserved (protected) so that this write is impossible.
16+
// However, that does not happen because the read above is not forwarded to the subtree below
17+
// the Frozen intermediary node. This does not affect UB, however, because the Frozen that blocked
18+
// the read already prevents any child writes.
19+
*m = 42; //~ERROR: /write access through .* is forbidden/
20+
}
21+
22+
fn main() {
23+
let root = 42u8;
24+
unsafe {
25+
let intermediary = &root;
26+
let data = &mut *(core::ptr::addr_of!(*intermediary) as *mut u8);
27+
write_to_mut(data, core::ptr::addr_of!(root));
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
error: Undefined Behavior: write access through <TAG> at ALLOC[0x0] is forbidden
2+
--> tests/fail/tree_borrows/subtree_traversal_skipping_diagnostics.rs:LL:CC
3+
|
4+
LL | *m = 42;
5+
| ^^^^^^^ write access through <TAG> at ALLOC[0x0] is forbidden
6+
|
7+
= help: this indicates a potential bug in the program: it performed an invalid operation, but the Tree Borrows rules it violated are still experimental
8+
= help: the accessed tag <TAG> is a child of the conflicting tag <TAG>
9+
= help: the conflicting tag <TAG> has state Frozen which forbids this child write access
10+
help: the accessed tag <TAG> was created here
11+
--> tests/fail/tree_borrows/subtree_traversal_skipping_diagnostics.rs:LL:CC
12+
|
13+
LL | fn write_to_mut(m: &mut u8, other_ptr: *const u8) {
14+
| ^
15+
help: the conflicting tag <TAG> was created here, in the initial state Frozen
16+
--> tests/fail/tree_borrows/subtree_traversal_skipping_diagnostics.rs:LL:CC
17+
|
18+
LL | let intermediary = &root;
19+
| ^^^^^
20+
= note: BACKTRACE (of the first span):
21+
= note: inside `write_to_mut` at tests/fail/tree_borrows/subtree_traversal_skipping_diagnostics.rs:LL:CC
22+
note: inside `main`
23+
--> tests/fail/tree_borrows/subtree_traversal_skipping_diagnostics.rs:LL:CC
24+
|
25+
LL | write_to_mut(data, core::ptr::addr_of!(root));
26+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
27+
28+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
29+
30+
error: aborting due to 1 previous error
31+

0 commit comments

Comments
 (0)