std: abort instead of panicking if the global allocator uses TLS

This commit is contained in:
joboet 2023-10-04 11:49:48 +02:00
parent fd23276ca8
commit b18990b1e9
No known key found for this signature in database
GPG Key ID: 704E0149B0194B3C
5 changed files with 21 additions and 5 deletions

View File

@ -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

View File

@ -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() {

View File

@ -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();

View File

@ -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);
}

View File

@ -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() {