Skip to content

Commit ce0c3e9

Browse files
committed
std: don't call current_os_id from signal handler
1 parent 6bc27a8 commit ce0c3e9

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

library/std/src/sys/pal/unix/stack_overflow.rs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ pub struct Handler {
88
}
99

1010
impl Handler {
11-
pub unsafe fn new(thread_name: Option<Box<str>>) -> Handler {
12-
make_handler(false, thread_name)
11+
pub unsafe fn new() -> Handler {
12+
make_handler(false)
1313
}
1414

1515
fn null() -> Handler {
@@ -118,8 +118,15 @@ mod imp {
118118
if let Some(thread_info) = thread_info
119119
&& thread_info.guard_page_range.contains(&fault_addr)
120120
{
121-
let name = thread_info.thread_name.as_deref().unwrap_or("<unknown>");
122-
let tid = crate::thread::current_os_id();
121+
// Hey you! Yes, you modifying the stack overflow message!
122+
// Please make sure that all functions called here are
123+
// actually async-signal-safe. If they're not, try retrieving
124+
// the information beforehand and storing it in `ThreadInfo`.
125+
// Thank you!
126+
// - says Jonas after having had to watch his carefully
127+
// written code get made unsound again.
128+
let tid = thread_info.tid;
129+
let name = thread_info.name.as_deref().unwrap_or("<unknown>");
123130
rtprintpanic!("\nthread '{name}' ({tid}) has overflowed its stack\n");
124131
rtabort!("stack overflow");
125132
}
@@ -158,12 +165,12 @@ mod imp {
158165
if !NEED_ALTSTACK.load(Ordering::Relaxed) {
159166
// haven't set up our sigaltstack yet
160167
NEED_ALTSTACK.store(true, Ordering::Release);
161-
let handler = unsafe { make_handler(true, None) };
168+
let handler = unsafe { make_handler(true) };
162169
MAIN_ALTSTACK.store(handler.data, Ordering::Relaxed);
163170
mem::forget(handler);
164171

165172
if let Some(guard_page_range) = guard_page_range.take() {
166-
set_current_info(guard_page_range, Some(Box::from("main")));
173+
set_current_info(guard_page_range);
167174
}
168175
}
169176

@@ -229,14 +236,14 @@ mod imp {
229236
/// # Safety
230237
/// Mutates the alternate signal stack
231238
#[forbid(unsafe_op_in_unsafe_fn)]
232-
pub unsafe fn make_handler(main_thread: bool, thread_name: Option<Box<str>>) -> Handler {
239+
pub unsafe fn make_handler(main_thread: bool) -> Handler {
233240
if !NEED_ALTSTACK.load(Ordering::Acquire) {
234241
return Handler::null();
235242
}
236243

237244
if !main_thread {
238245
if let Some(guard_page_range) = unsafe { current_guard() } {
239-
set_current_info(guard_page_range, thread_name);
246+
set_current_info(guard_page_range);
240247
}
241248
}
242249

@@ -632,10 +639,7 @@ mod imp {
632639

633640
pub unsafe fn cleanup() {}
634641

635-
pub unsafe fn make_handler(
636-
_main_thread: bool,
637-
_thread_name: Option<Box<str>>,
638-
) -> super::Handler {
642+
pub unsafe fn make_handler(_main_thread: bool) -> super::Handler {
639643
super::Handler::null()
640644
}
641645

@@ -719,10 +723,7 @@ mod imp {
719723

720724
pub unsafe fn cleanup() {}
721725

722-
pub unsafe fn make_handler(
723-
main_thread: bool,
724-
_thread_name: Option<Box<str>>,
725-
) -> super::Handler {
726+
pub unsafe fn make_handler(main_thread: bool) -> super::Handler {
726727
if !main_thread {
727728
reserve_stack();
728729
}

library/std/src/sys/pal/unix/stack_overflow/thread_info.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ use crate::sync::atomic::{AtomicUsize, Ordering};
3232
use crate::sys::os::errno_location;
3333

3434
pub struct ThreadInfo {
35+
pub tid: u64,
36+
pub name: Option<Box<str>>,
3537
pub guard_page_range: Range<usize>,
36-
pub thread_name: Option<Box<str>>,
3738
}
3839

3940
static LOCK: Mutex<()> = Mutex::new(());
@@ -108,14 +109,17 @@ fn spin_lock_in_setup(this: usize) -> UnlockOnDrop {
108109
}
109110
}
110111

111-
pub fn set_current_info(guard_page_range: Range<usize>, thread_name: Option<Box<str>>) {
112+
pub fn set_current_info(guard_page_range: Range<usize>) {
113+
let tid = crate::thread::current_os_id();
114+
let name = crate::thread::with_current_name(|name| name.map(Box::from));
115+
112116
let this = errno_location().addr();
113117
let _lock_guard = LOCK.lock();
114118
let _spin_guard = spin_lock_in_setup(this);
115119

116120
// SAFETY: we own the spin lock, so `THREAD_INFO` cannot be aliased.
117121
let thread_info = unsafe { &mut *(&raw mut THREAD_INFO) };
118-
thread_info.insert(this, ThreadInfo { guard_page_range, thread_name });
122+
thread_info.insert(this, ThreadInfo { tid, name, guard_page_range });
119123
}
120124

121125
pub fn delete_current_info() {

0 commit comments

Comments
 (0)