Skip to content

Commit 9ca2f6b

Browse files
add test to demonstrate the effect of #4008
1 parent 13f790c commit 9ca2f6b

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed
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> has state Reserved (conflicted) which forbids this child write access
9+
help: the accessed tag <TAG> was created here, in the initial state Reserved
10+
--> tests/fail/tree_borrows/subtree_traversal_skipping_diagnostics.rs:LL:CC
11+
|
12+
LL | fn write_to_mut(m: &mut u8, other_ptr: *const u8) {
13+
| ^
14+
help: the accessed tag <TAG> later transitioned to Reserved (conflicted) due to a foreign read access at offsets [0x0..0x1]
15+
--> tests/fail/tree_borrows/subtree_traversal_skipping_diagnostics.rs:LL:CC
16+
|
17+
LL | std::hint::black_box(*other_ptr);
18+
| ^^^^^^^^^^
19+
= help: this transition corresponds to a temporary loss of write permissions until function exit
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)