Skip to content

Commit 6f688d5

Browse files
committed
test both kinds of TLS
1 parent 81ea21b commit 6f688d5

File tree

5 files changed

+71
-31
lines changed

5 files changed

+71
-31
lines changed

tests/fail/leak_in_os_tls.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//@error-in-other-file: memory leaked
2+
//@normalize-stderr-test: ".*│.*" -> "$$stripped$$"
3+
4+
use std::cell::Cell;
5+
6+
pub fn main() {
7+
thread_local! {
8+
static TLS: Cell<Option<&'static i32>> = Cell::new(None);
9+
}
10+
11+
std::thread::spawn(|| {
12+
TLS.with(|cell| {
13+
cell.set(Some(Box::leak(Box::new(123))));
14+
});
15+
})
16+
.join()
17+
.unwrap();
18+
19+
// Imagine the program running for a long time while the thread is gone
20+
// and this memory still sits around, unused -- leaked.
21+
}

tests/fail/leak_in_os_tls.stderr

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
error: memory leaked: ALLOC (Rust heap, size: 4, align: 4), allocated here:
2+
--> RUSTLIB/alloc/src/alloc.rs:LL:CC
3+
|
4+
LL | __rust_alloc(layout.size(), layout.align())
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: inside `std::alloc::alloc` at RUSTLIB/alloc/src/alloc.rs:LL:CC
8+
= note: inside `std::alloc::Global::alloc_impl` at RUSTLIB/alloc/src/alloc.rs:LL:CC
9+
= note: inside `<std::alloc::Global as std::alloc::Allocator>::allocate` at RUSTLIB/alloc/src/alloc.rs:LL:CC
10+
= note: inside `alloc::alloc::exchange_malloc` at RUSTLIB/alloc/src/alloc.rs:LL:CC
11+
= note: inside `std::boxed::Box::<i32>::new` at RUSTLIB/alloc/src/boxed.rs:LL:CC
12+
note: inside closure
13+
--> $DIR/leak_in_os_tls.rs:LL:CC
14+
|
15+
LL | cell.set(Some(Box::leak(Box::new(123))));
16+
| ^^^^^^^^^^^^^
17+
= note: inside `std::thread::LocalKey::<std::cell::Cell<std::option::Option<&i32>>>::try_with::<[closure@$DIR/leak_in_os_tls.rs:LL:CC], ()>` at RUSTLIB/std/src/thread/local.rs:LL:CC
18+
= note: inside `std::thread::LocalKey::<std::cell::Cell<std::option::Option<&i32>>>::with::<[closure@$DIR/leak_in_os_tls.rs:LL:CC], ()>` at RUSTLIB/std/src/thread/local.rs:LL:CC
19+
note: inside closure
20+
--> $DIR/leak_in_os_tls.rs:LL:CC
21+
|
22+
LL | / TLS.with(|cell| {
23+
LL | | cell.set(Some(Box::leak(Box::new(123))));
24+
LL | | });
25+
| |__________^
26+
27+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
28+
29+
note: the evaluated program leaked memory, pass `-Zmiri-ignore-leaks` to disable this check
30+
31+
error: aborting due to previous error
32+

tests/fail/leak_in_tls.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
//@error-in-other-file: memory leaked
22
//@normalize-stderr-test: ".*│.*" -> "$$stripped$$"
33

4+
#![feature(thread_local)]
5+
46
use std::cell::Cell;
57

68
pub fn main() {
7-
thread_local! {
8-
static REF: Cell<Option<&'static i32>> = Cell::new(None);
9-
}
9+
#[thread_local]
10+
static TLS: Cell<Option<&'static i32>> = Cell::new(None);
1011

1112
std::thread::spawn(|| {
12-
REF.with(|cell| {
13-
let a = 123;
14-
let b = Box::new(a);
15-
let r = Box::leak(b);
16-
cell.set(Some(r));
17-
})
13+
TLS.set(Some(Box::leak(Box::new(123))));
1814
})
1915
.join()
2016
.unwrap();

tests/fail/leak_in_tls.stderr

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,8 @@ LL | __rust_alloc(layout.size(), layout.align())
1212
note: inside closure
1313
--> $DIR/leak_in_tls.rs:LL:CC
1414
|
15-
LL | let b = Box::new(a);
16-
| ^^^^^^^^^^^
17-
= note: inside `std::thread::LocalKey::<std::cell::Cell<std::option::Option<&i32>>>::try_with::<[closure@$DIR/leak_in_tls.rs:LL:CC], ()>` at RUSTLIB/std/src/thread/local.rs:LL:CC
18-
= note: inside `std::thread::LocalKey::<std::cell::Cell<std::option::Option<&i32>>>::with::<[closure@$DIR/leak_in_tls.rs:LL:CC], ()>` at RUSTLIB/std/src/thread/local.rs:LL:CC
19-
note: inside closure
20-
--> $DIR/leak_in_tls.rs:LL:CC
21-
|
22-
LL | / REF.with(|cell| {
23-
LL | | let a = 123;
24-
LL | | let b = Box::new(a);
25-
LL | | let r = Box::leak(b);
26-
LL | | cell.set(Some(r));
27-
LL | | })
28-
| |__________^
15+
LL | TLS.set(Some(Box::leak(Box::new(123))));
16+
| ^^^^^^^^^^^^^
2917

3018
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
3119

tests/pass/issues/issue-miri-2881.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1+
#![feature(thread_local)]
2+
13
use std::cell::Cell;
24

35
pub fn main() {
4-
let a = 123;
5-
let b = Box::new(a);
6-
let r = Box::leak(b);
7-
86
thread_local! {
9-
static REF: Cell<Option<&'static i32>> = Cell::new(None);
7+
static TLS_KEY: Cell<Option<&'static i32>> = Cell::new(None);
108
}
119

12-
REF.with(|cell| {
13-
cell.set(Some(r));
14-
})
10+
TLS_KEY.with(|cell| {
11+
cell.set(Some(Box::leak(Box::new(123))));
12+
});
13+
14+
#[thread_local]
15+
static TLS: Cell<Option<&'static i32>> = Cell::new(None);
16+
17+
TLS.set(Some(Box::leak(Box::new(123))));
1518
}

0 commit comments

Comments
 (0)