Skip to content

Commit f46921d

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

File tree

3 files changed

+46
-24
lines changed

3 files changed

+46
-24
lines changed

tests/fail/leak_in_tls.rs

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
//@error-in-other-file: memory leaked
2+
//@error-in-other-file: memory leaked
23
//@normalize-stderr-test: ".*│.*" -> "$$stripped$$"
34

5+
#![feature(thread_local)]
6+
47
use std::cell::Cell;
58

69
pub fn main() {
710
thread_local! {
8-
static REF: Cell<Option<&'static i32>> = Cell::new(None);
11+
static TLS_KEY: Cell<Option<&'static i32>> = Cell::new(None);
912
}
1013

14+
#[thread_local]
15+
static TLS: Cell<Option<&'static i32>> = Cell::new(None);
16+
1117
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-
})
18+
TLS_KEY.with(|cell| {
19+
cell.set(Some(Box::leak(Box::new(123))));
20+
});
21+
22+
TLS.set(Some(Box::leak(Box::new(123))));
1823
})
1924
.join()
2025
.unwrap();

tests/fail/leak_in_tls.stderr

+23-9
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,38 @@ 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-
| ^^^^^^^^^^^
15+
LL | cell.set(Some(Box::leak(Box::new(123))));
16+
| ^^^^^^^^^^^^^
1717
= 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
1818
= 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
1919
note: inside closure
2020
--> $DIR/leak_in_tls.rs:LL:CC
2121
|
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 | | })
22+
LL | / TLS_KEY.with(|cell| {
23+
LL | | cell.set(Some(Box::leak(Box::new(123))));
24+
LL | | });
2825
| |__________^
2926

27+
error: memory leaked: ALLOC (Rust heap, size: 4, align: 4), allocated here:
28+
--> RUSTLIB/alloc/src/alloc.rs:LL:CC
29+
|
30+
LL | __rust_alloc(layout.size(), layout.align())
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
32+
|
33+
= note: inside `std::alloc::alloc` at RUSTLIB/alloc/src/alloc.rs:LL:CC
34+
= note: inside `std::alloc::Global::alloc_impl` at RUSTLIB/alloc/src/alloc.rs:LL:CC
35+
= note: inside `<std::alloc::Global as std::alloc::Allocator>::allocate` at RUSTLIB/alloc/src/alloc.rs:LL:CC
36+
= note: inside `alloc::alloc::exchange_malloc` at RUSTLIB/alloc/src/alloc.rs:LL:CC
37+
= note: inside `std::boxed::Box::<i32>::new` at RUSTLIB/alloc/src/boxed.rs:LL:CC
38+
note: inside closure
39+
--> $DIR/leak_in_tls.rs:LL:CC
40+
|
41+
LL | TLS.set(Some(Box::leak(Box::new(123))));
42+
| ^^^^^^^^^^^^^
43+
3044
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
3145

3246
note: the evaluated program leaked memory, pass `-Zmiri-ignore-leaks` to disable this check
3347

34-
error: aborting due to previous error
48+
error: aborting due to 2 previous errors
3549

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

+11-8
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)