Skip to content

Commit

Permalink
std: abort instead of panicking if the global allocator uses TLS
Browse files Browse the repository at this point in the history
  • Loading branch information
joboet committed Oct 4, 2023
1 parent fd23276 commit b18990b
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 5 deletions.
5 changes: 4 additions & 1 deletion library/std/src/sys/hermit/thread_local_dtor.rs
Expand Up @@ -11,7 +11,10 @@ use crate::cell::RefCell;
static DTORS: RefCell<Vec<(*mut u8, unsafe extern "C" fn(*mut u8))>> = RefCell::new(Vec::new());

pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
DTORS.try_borrow_mut().expect("global allocator may not use TLS").push((t, dtor));
match DTORS.try_borrow_mut() {
Ok(mut dtors) => dtors.push((t, dtor)),
Err(_) => rtabort!("global allocator may not use TLS"),
}
}

// every thread call this function to run through all possible destructors
Expand Down
5 changes: 4 additions & 1 deletion library/std/src/sys/solid/thread_local_dtor.rs
Expand Up @@ -21,7 +21,10 @@ pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
REGISTERED.set(true);
}

DTORS.try_borrow_mut().expect("global allocator may not use TLS").push((t, dtor));
match DTORS.try_borrow_mut() {
Ok(mut dtors) => dtors.push((t, dtor)),
Err(_) => rtabort!("global allocator may not use TLS"),
}
}

pub unsafe fn run_dtors() {
Expand Down
5 changes: 4 additions & 1 deletion library/std/src/sys/unix/thread_local_dtor.rs
Expand Up @@ -68,7 +68,10 @@ pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
fn _tlv_atexit(dtor: unsafe extern "C" fn(*mut u8), arg: *mut u8);
}

DTORS.try_borrow_mut().expect("global allocator may not use TLS").push((t, dtor));
match DTORS.try_borrow_mut() {
Ok(mut dtors) => dtors.push((t, dtor)),
Err(_) => rtabort!("global allocator may not use TLS"),
}

unsafe extern "C" fn run_dtors(_: *mut u8) {
let mut list = DTORS.take();
Expand Down
6 changes: 5 additions & 1 deletion library/std/src/sys/windows/thread_local_key.rs
Expand Up @@ -24,7 +24,11 @@ static DESTRUCTORS: crate::cell::RefCell<Vec<(*mut u8, unsafe extern "C" fn(*mut
#[inline(never)]
#[cfg(target_thread_local)]
pub unsafe fn register_keyless_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
DESTRUCTORS.try_borrow_mut().expect("global allocator may not use TLS").push((t, dtor));
match DESTRUCTORS.try_borrow_mut() {
Ok(mut dtors) => dtors.push((t, dtor)),
Err(_) => rtabort!("global allocator may not use TLS"),
}

HAS_DTORS.store(true, Relaxed);
}

Expand Down
5 changes: 4 additions & 1 deletion library/std/src/sys_common/thread_local_dtor.rs
Expand Up @@ -38,7 +38,10 @@ pub unsafe fn register_dtor_fallback(t: *mut u8, dtor: unsafe extern "C" fn(*mut
DTORS.set(Box::into_raw(v) as *mut u8);
}
let list = &*(DTORS.get() as *const List);
list.try_borrow_mut().expect("global allocator may not use TLS").push((t, dtor));
match list.try_borrow_mut() {
Ok(mut dtors) => dtors.push((t, dtor)),
Err(_) => rtabort!("global allocator may not use TLS"),
}

unsafe extern "C" fn run_dtors(mut ptr: *mut u8) {
while !ptr.is_null() {
Expand Down

0 comments on commit b18990b

Please sign in to comment.