Skip to content

Commit bbec8fa

Browse files
committed
std: panic when the global allocator tries to register a TLS destructor
1 parent 36aab8d commit bbec8fa

File tree

5 files changed

+29
-29
lines changed

5 files changed

+29
-29
lines changed

library/std/src/sys/hermit/thread_local_dtor.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,22 @@
55
// The this solution works like the implementation of macOS and
66
// doesn't additional OS support
77

8-
use crate::mem;
8+
use crate::cell::RefCell;
99

1010
#[thread_local]
11-
static mut DTORS: Vec<(*mut u8, unsafe extern "C" fn(*mut u8))> = Vec::new();
11+
static DTORS: RefCell<Vec<(*mut u8, unsafe extern "C" fn(*mut u8))>> = RefCell::new(Vec::new());
1212

1313
pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
14-
let list = &mut DTORS;
15-
list.push((t, dtor));
14+
DTORS.try_borrow_mut().expect("global allocator may not TLS storage").push((t, dtor));
1615
}
1716

1817
// every thread call this function to run through all possible destructors
1918
pub unsafe fn run_dtors() {
20-
let mut list = mem::take(&mut DTORS);
19+
let mut list = DTORS.take();
2120
while !list.is_empty() {
2221
for (ptr, dtor) in list {
2322
dtor(ptr);
2423
}
25-
list = mem::take(&mut DTORS);
24+
list = DTORS.take();
2625
}
2726
}

library/std/src/sys/solid/thread_local_dtor.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44
// Simplify dtor registration by using a list of destructors.
55

66
use super::{abi, itron::task};
7-
use crate::cell::Cell;
8-
use crate::mem;
7+
use crate::cell::{Cell, RefCell};
98

109
#[thread_local]
1110
static REGISTERED: Cell<bool> = Cell::new(false);
1211

1312
#[thread_local]
14-
static mut DTORS: Vec<(*mut u8, unsafe extern "C" fn(*mut u8))> = Vec::new();
13+
static DTORS: RefCell<Vec<(*mut u8, unsafe extern "C" fn(*mut u8))>> = RefCell::new(Vec::new());
1514

1615
pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
1716
if !REGISTERED.get() {
@@ -22,18 +21,17 @@ pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
2221
REGISTERED.set(true);
2322
}
2423

25-
let list = unsafe { &mut DTORS };
26-
list.push((t, dtor));
24+
DTORS.try_borrow_mut().expect("global allocator may not TLS storage").push((t, dtor));
2725
}
2826

2927
pub unsafe fn run_dtors() {
30-
let mut list = mem::take(unsafe { &mut DTORS });
28+
let mut list = DTORS.take();
3129
while !list.is_empty() {
3230
for (ptr, dtor) in list {
3331
unsafe { dtor(ptr) };
3432
}
3533

36-
list = mem::take(unsafe { &mut DTORS });
34+
list = DTORS.take();
3735
}
3836
}
3937

library/std/src/sys/unix/thread_local_dtor.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,14 @@ pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
5050
// _tlv_atexit.
5151
#[cfg(any(target_os = "macos", target_os = "ios", target_os = "watchos"))]
5252
pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
53-
use crate::cell::Cell;
54-
use crate::mem;
53+
use crate::cell::{Cell, RefCell};
5554
use crate::ptr;
5655

5756
#[thread_local]
5857
static REGISTERED: Cell<bool> = Cell::new(false);
5958

6059
#[thread_local]
61-
static mut DTORS: Vec<(*mut u8, unsafe extern "C" fn(*mut u8))> = Vec::new();
60+
static DTORS: RefCell<Vec<(*mut u8, unsafe extern "C" fn(*mut u8))>> = RefCell::new(Vec::new());
6261

6362
if !REGISTERED.get() {
6463
_tlv_atexit(run_dtors, ptr::null_mut());
@@ -69,16 +68,15 @@ pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
6968
fn _tlv_atexit(dtor: unsafe extern "C" fn(*mut u8), arg: *mut u8);
7069
}
7170

72-
let list = &mut DTORS;
73-
list.push((t, dtor));
71+
DTORS.try_borrow_mut().expect("global allocator may not TLS storage").push((t, dtor));
7472

7573
unsafe extern "C" fn run_dtors(_: *mut u8) {
76-
let mut list = mem::take(&mut DTORS);
74+
let mut list = DTORS.take();
7775
while !list.is_empty() {
7876
for (ptr, dtor) in list {
7977
dtor(ptr);
8078
}
81-
list = mem::take(&mut DTORS);
79+
list = DTORS.take();
8280
}
8381
}
8482
}

library/std/src/sys/windows/thread_local_key.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@ static HAS_DTORS: AtomicBool = AtomicBool::new(false);
1616
// Using a per-thread list avoids the problems in synchronizing global state.
1717
#[thread_local]
1818
#[cfg(target_thread_local)]
19-
static mut DESTRUCTORS: Vec<(*mut u8, unsafe extern "C" fn(*mut u8))> = Vec::new();
19+
static DESTRUCTORS: crate::cell::RefCell<Vec<(*mut u8, unsafe extern "C" fn(*mut u8))>> =
20+
crate::cell::RefCell::new(Vec::new());
2021

2122
// Ensure this can never be inlined because otherwise this may break in dylibs.
2223
// See #44391.
2324
#[inline(never)]
2425
#[cfg(target_thread_local)]
2526
pub unsafe fn register_keyless_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
26-
DESTRUCTORS.push((t, dtor));
27+
DESTRUCTORS.try_borrow_mut().expect("global allocator may not TLS storage").push((t, dtor));
2728
HAS_DTORS.store(true, Relaxed);
2829
}
2930

@@ -37,11 +38,11 @@ unsafe fn run_keyless_dtors() {
3738
// the case that this loop always terminates because we provide the
3839
// guarantee that a TLS key cannot be set after it is flagged for
3940
// destruction.
40-
while let Some((ptr, dtor)) = DESTRUCTORS.pop() {
41+
while let Some((ptr, dtor)) = DESTRUCTORS.borrow_mut().pop() {
4142
(dtor)(ptr);
4243
}
4344
// We're done so free the memory.
44-
DESTRUCTORS = Vec::new();
45+
DESTRUCTORS.replace(Vec::new());
4546
}
4647

4748
type Key = c::DWORD;

library/std/src/sys_common/thread_local_dtor.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#![unstable(feature = "thread_local_internals", issue = "none")]
1414
#![allow(dead_code)]
1515

16+
use crate::cell::RefCell;
1617
use crate::ptr;
1718
use crate::sys_common::thread_local_key::StaticKey;
1819

@@ -28,17 +29,20 @@ pub unsafe fn register_dtor_fallback(t: *mut u8, dtor: unsafe extern "C" fn(*mut
2829
// flagged for destruction.
2930

3031
static DTORS: StaticKey = StaticKey::new(Some(run_dtors));
31-
type List = Vec<(*mut u8, unsafe extern "C" fn(*mut u8))>;
32+
// FIXME(joboet): integrate RefCell into pointer to avoid infinite recursion
33+
// when the global allocator tries to register a destructor and just panic
34+
// instead.
35+
type List = RefCell<Vec<(*mut u8, unsafe extern "C" fn(*mut u8))>>;
3236
if DTORS.get().is_null() {
33-
let v: Box<List> = Box::new(Vec::new());
37+
let v: Box<List> = Box::new(RefCell::new(Vec::new()));
3438
DTORS.set(Box::into_raw(v) as *mut u8);
3539
}
36-
let list: &mut List = &mut *(DTORS.get() as *mut List);
37-
list.push((t, dtor));
40+
let list = &*(DTORS.get() as *const List);
41+
list.try_borrow_mut().expect("global allocator may not TLS storage").push((t, dtor));
3842

3943
unsafe extern "C" fn run_dtors(mut ptr: *mut u8) {
4044
while !ptr.is_null() {
41-
let list: Box<List> = Box::from_raw(ptr as *mut List);
45+
let list = Box::from_raw(ptr as *mut List).into_inner();
4246
for (ptr, dtor) in list.into_iter() {
4347
dtor(ptr);
4448
}

0 commit comments

Comments
 (0)