Skip to content

Commit a87359d

Browse files
committed
add known-bug test for unsound issue 49682
1 parent bfdd1c4 commit a87359d

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// check-pass
2+
// known-bug: #49682
3+
// edition:2021
4+
5+
// Should fail. Keeping references to thread local statics can result in a
6+
// use-after-free.
7+
8+
#![feature(thread_local)]
9+
10+
use std::sync::atomic::{AtomicUsize, Ordering};
11+
use std::thread;
12+
13+
#[allow(dead_code)]
14+
#[thread_local]
15+
static FOO: AtomicUsize = AtomicUsize::new(0);
16+
17+
#[allow(dead_code)]
18+
async fn bar() {}
19+
20+
#[allow(dead_code)]
21+
async fn foo() {
22+
let r = &FOO;
23+
bar().await;
24+
r.load(Ordering::SeqCst);
25+
}
26+
27+
fn main() {
28+
// &FOO = 0x7fd1e9cbf6d0
29+
_ = thread::spawn(|| {
30+
let g = foo();
31+
println!("&FOO = {:p}", &FOO);
32+
g
33+
})
34+
.join()
35+
.unwrap();
36+
37+
// &FOO = 0x7fd1e9cc0f50
38+
println!("&FOO = {:p}", &FOO);
39+
40+
// &FOO = 0x7fd1e9cbf6d0
41+
thread::spawn(move || {
42+
println!("&FOO = {:p}", &FOO);
43+
})
44+
.join()
45+
.unwrap();
46+
}

0 commit comments

Comments
 (0)