Skip to content

Commit 664640f

Browse files
Add testcase for rust-lang#3846
1 parent 25e5ac4 commit 664640f

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//@compile-flags: -Zmiri-tree-borrows
2+
3+
use std::ptr::addr_of_mut;
4+
5+
fn do_something(_: u8) {}
6+
7+
unsafe fn access_after_sub_1(x: &mut u8, orig_ptr: *mut u8) {
8+
// causes a second access, which should make the lazy part of `x` be `Reserved {conflicted: true}`
9+
do_something(*orig_ptr);
10+
// read from the conflicted pointer
11+
*(x as *mut u8).byte_sub(1) = 42; //~ ERROR: /write access through .* is forbidden/
12+
}
13+
14+
pub fn main() {
15+
unsafe {
16+
let mut alloc = [0u8, 0u8];
17+
let orig_ptr = addr_of_mut!(alloc) as *mut u8;
18+
let foo = &mut *orig_ptr;
19+
// cause a foreign read access to foo
20+
do_something(alloc[0]);
21+
access_after_sub_1(&mut *(foo as *mut u8).byte_add(1), orig_ptr);
22+
}
23+
}
Lines changed: 31 additions & 0 deletions
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+
--> $DIR/repeated_foreign_read_lazy_conflicted.rs:LL:CC
3+
|
4+
LL | *(x as *mut u8).byte_sub(1) = 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+
--> $DIR/repeated_foreign_read_lazy_conflicted.rs:LL:CC
11+
|
12+
LL | unsafe fn access_after_sub_1(x: &mut u8, orig_ptr: *mut u8) {
13+
| ^
14+
help: the accessed tag <TAG> later transitioned to Reserved (conflicted) due to a foreign read access at offsets [0x0..0x1]
15+
--> $DIR/repeated_foreign_read_lazy_conflicted.rs:LL:CC
16+
|
17+
LL | do_something(*orig_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 `access_after_sub_1` at $DIR/repeated_foreign_read_lazy_conflicted.rs:LL:CC
22+
note: inside `main`
23+
--> $DIR/repeated_foreign_read_lazy_conflicted.rs:LL:CC
24+
|
25+
LL | access_after_sub_1(&mut *(foo as *mut u8).byte_add(1), orig_ptr);
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)