From 6f688d5907b93c0fad3664ec0e113328732a6290 Mon Sep 17 00:00:00 2001 From: max-heller Date: Mon, 17 Jul 2023 19:19:02 -0400 Subject: [PATCH] test both kinds of TLS --- tests/fail/leak_in_os_tls.rs | 21 ++++++++++++++++++ tests/fail/leak_in_os_tls.stderr | 32 ++++++++++++++++++++++++++++ tests/fail/leak_in_tls.rs | 14 +++++------- tests/fail/leak_in_tls.stderr | 16 ++------------ tests/pass/issues/issue-miri-2881.rs | 19 ++++++++++------- 5 files changed, 71 insertions(+), 31 deletions(-) create mode 100644 tests/fail/leak_in_os_tls.rs create mode 100644 tests/fail/leak_in_os_tls.stderr diff --git a/tests/fail/leak_in_os_tls.rs b/tests/fail/leak_in_os_tls.rs new file mode 100644 index 0000000000..996d7ed4a2 --- /dev/null +++ b/tests/fail/leak_in_os_tls.rs @@ -0,0 +1,21 @@ +//@error-in-other-file: memory leaked +//@normalize-stderr-test: ".*│.*" -> "$$stripped$$" + +use std::cell::Cell; + +pub fn main() { + thread_local! { + static TLS: Cell> = Cell::new(None); + } + + std::thread::spawn(|| { + TLS.with(|cell| { + cell.set(Some(Box::leak(Box::new(123)))); + }); + }) + .join() + .unwrap(); + + // Imagine the program running for a long time while the thread is gone + // and this memory still sits around, unused -- leaked. +} diff --git a/tests/fail/leak_in_os_tls.stderr b/tests/fail/leak_in_os_tls.stderr new file mode 100644 index 0000000000..b5a2e5e094 --- /dev/null +++ b/tests/fail/leak_in_os_tls.stderr @@ -0,0 +1,32 @@ +error: memory leaked: ALLOC (Rust heap, size: 4, align: 4), allocated here: + --> RUSTLIB/alloc/src/alloc.rs:LL:CC + | +LL | __rust_alloc(layout.size(), layout.align()) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: inside `std::alloc::alloc` at RUSTLIB/alloc/src/alloc.rs:LL:CC + = note: inside `std::alloc::Global::alloc_impl` at RUSTLIB/alloc/src/alloc.rs:LL:CC + = note: inside `::allocate` at RUSTLIB/alloc/src/alloc.rs:LL:CC + = note: inside `alloc::alloc::exchange_malloc` at RUSTLIB/alloc/src/alloc.rs:LL:CC + = note: inside `std::boxed::Box::::new` at RUSTLIB/alloc/src/boxed.rs:LL:CC +note: inside closure + --> $DIR/leak_in_os_tls.rs:LL:CC + | +LL | cell.set(Some(Box::leak(Box::new(123)))); + | ^^^^^^^^^^^^^ + = note: inside `std::thread::LocalKey::>>::try_with::<[closure@$DIR/leak_in_os_tls.rs:LL:CC], ()>` at RUSTLIB/std/src/thread/local.rs:LL:CC + = note: inside `std::thread::LocalKey::>>::with::<[closure@$DIR/leak_in_os_tls.rs:LL:CC], ()>` at RUSTLIB/std/src/thread/local.rs:LL:CC +note: inside closure + --> $DIR/leak_in_os_tls.rs:LL:CC + | +LL | / TLS.with(|cell| { +LL | | cell.set(Some(Box::leak(Box::new(123)))); +LL | | }); + | |__________^ + +note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace + +note: the evaluated program leaked memory, pass `-Zmiri-ignore-leaks` to disable this check + +error: aborting due to previous error + diff --git a/tests/fail/leak_in_tls.rs b/tests/fail/leak_in_tls.rs index 78a085271c..be066624d6 100644 --- a/tests/fail/leak_in_tls.rs +++ b/tests/fail/leak_in_tls.rs @@ -1,20 +1,16 @@ //@error-in-other-file: memory leaked //@normalize-stderr-test: ".*│.*" -> "$$stripped$$" +#![feature(thread_local)] + use std::cell::Cell; pub fn main() { - thread_local! { - static REF: Cell> = Cell::new(None); - } + #[thread_local] + static TLS: Cell> = Cell::new(None); std::thread::spawn(|| { - REF.with(|cell| { - let a = 123; - let b = Box::new(a); - let r = Box::leak(b); - cell.set(Some(r)); - }) + TLS.set(Some(Box::leak(Box::new(123)))); }) .join() .unwrap(); diff --git a/tests/fail/leak_in_tls.stderr b/tests/fail/leak_in_tls.stderr index 2dac698a21..ef2aacf4ac 100644 --- a/tests/fail/leak_in_tls.stderr +++ b/tests/fail/leak_in_tls.stderr @@ -12,20 +12,8 @@ LL | __rust_alloc(layout.size(), layout.align()) note: inside closure --> $DIR/leak_in_tls.rs:LL:CC | -LL | let b = Box::new(a); - | ^^^^^^^^^^^ - = note: inside `std::thread::LocalKey::>>::try_with::<[closure@$DIR/leak_in_tls.rs:LL:CC], ()>` at RUSTLIB/std/src/thread/local.rs:LL:CC - = note: inside `std::thread::LocalKey::>>::with::<[closure@$DIR/leak_in_tls.rs:LL:CC], ()>` at RUSTLIB/std/src/thread/local.rs:LL:CC -note: inside closure - --> $DIR/leak_in_tls.rs:LL:CC - | -LL | / REF.with(|cell| { -LL | | let a = 123; -LL | | let b = Box::new(a); -LL | | let r = Box::leak(b); -LL | | cell.set(Some(r)); -LL | | }) - | |__________^ +LL | TLS.set(Some(Box::leak(Box::new(123)))); + | ^^^^^^^^^^^^^ note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace diff --git a/tests/pass/issues/issue-miri-2881.rs b/tests/pass/issues/issue-miri-2881.rs index 38a9d65a6f..423a307b7e 100644 --- a/tests/pass/issues/issue-miri-2881.rs +++ b/tests/pass/issues/issue-miri-2881.rs @@ -1,15 +1,18 @@ +#![feature(thread_local)] + use std::cell::Cell; pub fn main() { - let a = 123; - let b = Box::new(a); - let r = Box::leak(b); - thread_local! { - static REF: Cell> = Cell::new(None); + static TLS_KEY: Cell> = Cell::new(None); } - REF.with(|cell| { - cell.set(Some(r)); - }) + TLS_KEY.with(|cell| { + cell.set(Some(Box::leak(Box::new(123)))); + }); + + #[thread_local] + static TLS: Cell> = Cell::new(None); + + TLS.set(Some(Box::leak(Box::new(123)))); }