about summary refs log tree commit diff
diff options
context:
space:
mode:
authorwhtahy <whtahy@users.noreply.github.com>2023-04-26 20:35:16 -0400
committerwhtahy <whtahy@users.noreply.github.com>2023-04-26 22:34:30 -0400
commita87359d7c98790e75e71587058cfea9defee78c7 (patch)
tree55fda76dd64f23fed18258fb7ad6a2cf174244d3
parentbfdd1c4e351f1a30f445c33efdfb946c0eae81ba (diff)
downloadrust-a87359d7c98790e75e71587058cfea9defee78c7.tar.gz
rust-a87359d7c98790e75e71587058cfea9defee78c7.zip
add known-bug test for unsound issue 49682
-rw-r--r--tests/ui/thread-local/thread-local-static-ref-use-after-free.rs46
1 files changed, 46 insertions, 0 deletions
diff --git a/tests/ui/thread-local/thread-local-static-ref-use-after-free.rs b/tests/ui/thread-local/thread-local-static-ref-use-after-free.rs
new file mode 100644
index 00000000000..c282e2185bc
--- /dev/null
+++ b/tests/ui/thread-local/thread-local-static-ref-use-after-free.rs
@@ -0,0 +1,46 @@
+// check-pass
+// known-bug: #49682
+// edition:2021
+
+// Should fail. Keeping references to thread local statics can result in a
+// use-after-free.
+
+#![feature(thread_local)]
+
+use std::sync::atomic::{AtomicUsize, Ordering};
+use std::thread;
+
+#[allow(dead_code)]
+#[thread_local]
+static FOO: AtomicUsize = AtomicUsize::new(0);
+
+#[allow(dead_code)]
+async fn bar() {}
+
+#[allow(dead_code)]
+async fn foo() {
+    let r = &FOO;
+    bar().await;
+    r.load(Ordering::SeqCst);
+}
+
+fn main() {
+    // &FOO = 0x7fd1e9cbf6d0
+    _ = thread::spawn(|| {
+        let g = foo();
+        println!("&FOO = {:p}", &FOO);
+        g
+    })
+    .join()
+    .unwrap();
+
+    // &FOO = 0x7fd1e9cc0f50
+    println!("&FOO = {:p}", &FOO);
+
+    // &FOO = 0x7fd1e9cbf6d0
+    thread::spawn(move || {
+        println!("&FOO = {:p}", &FOO);
+    })
+    .join()
+    .unwrap();
+}