From b18990b1e973a1fd0a0318a88655dc1907509b17 Mon Sep 17 00:00:00 2001 From: joboet Date: Wed, 4 Oct 2023 11:49:48 +0200 Subject: [PATCH] std: abort instead of panicking if the global allocator uses TLS --- library/std/src/sys/hermit/thread_local_dtor.rs | 5 ++++- library/std/src/sys/solid/thread_local_dtor.rs | 5 ++++- library/std/src/sys/unix/thread_local_dtor.rs | 5 ++++- library/std/src/sys/windows/thread_local_key.rs | 6 +++++- library/std/src/sys_common/thread_local_dtor.rs | 5 ++++- 5 files changed, 21 insertions(+), 5 deletions(-) diff --git a/library/std/src/sys/hermit/thread_local_dtor.rs b/library/std/src/sys/hermit/thread_local_dtor.rs index 89db78adffc6a..98adaf4bff1aa 100644 --- a/library/std/src/sys/hermit/thread_local_dtor.rs +++ b/library/std/src/sys/hermit/thread_local_dtor.rs @@ -11,7 +11,10 @@ use crate::cell::RefCell; static DTORS: RefCell> = 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 diff --git a/library/std/src/sys/solid/thread_local_dtor.rs b/library/std/src/sys/solid/thread_local_dtor.rs index d0fecede9551f..26918a4fcb012 100644 --- a/library/std/src/sys/solid/thread_local_dtor.rs +++ b/library/std/src/sys/solid/thread_local_dtor.rs @@ -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() { diff --git a/library/std/src/sys/unix/thread_local_dtor.rs b/library/std/src/sys/unix/thread_local_dtor.rs index 38d275de4c8d9..58763e9fce262 100644 --- a/library/std/src/sys/unix/thread_local_dtor.rs +++ b/library/std/src/sys/unix/thread_local_dtor.rs @@ -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(); diff --git a/library/std/src/sys/windows/thread_local_key.rs b/library/std/src/sys/windows/thread_local_key.rs index dd2c46fdfbc38..3c4e1abbba936 100644 --- a/library/std/src/sys/windows/thread_local_key.rs +++ b/library/std/src/sys/windows/thread_local_key.rs @@ -24,7 +24,11 @@ static DESTRUCTORS: crate::cell::RefCell dtors.push((t, dtor)), + Err(_) => rtabort!("global allocator may not use TLS"), + } + HAS_DTORS.store(true, Relaxed); } diff --git a/library/std/src/sys_common/thread_local_dtor.rs b/library/std/src/sys_common/thread_local_dtor.rs index 5d15140a9da84..98382fc6acc23 100644 --- a/library/std/src/sys_common/thread_local_dtor.rs +++ b/library/std/src/sys_common/thread_local_dtor.rs @@ -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() {