-
Notifications
You must be signed in to change notification settings - Fork 13.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dynamically size sigaltstk in rustc #113528
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1453,13 +1453,13 @@ mod signal_handler { | |
/// When an error signal (such as SIGABRT or SIGSEGV) is delivered to the | ||
/// process, print a stack trace and then exit. | ||
pub(super) fn install() { | ||
use std::alloc::{alloc, Layout}; | ||
|
||
unsafe { | ||
const ALT_STACK_SIZE: usize = libc::MINSIGSTKSZ + 64 * 1024; | ||
let alt_stack_size: usize = min_sigstack_size() + 64 * 1024; | ||
let mut alt_stack: libc::stack_t = std::mem::zeroed(); | ||
alt_stack.ss_sp = | ||
std::alloc::alloc(std::alloc::Layout::from_size_align(ALT_STACK_SIZE, 1).unwrap()) | ||
as *mut libc::c_void; | ||
alt_stack.ss_size = ALT_STACK_SIZE; | ||
alt_stack.ss_sp = alloc(Layout::from_size_align(alt_stack_size, 1).unwrap()).cast(); | ||
alt_stack.ss_size = alt_stack_size; | ||
libc::sigaltstack(&alt_stack, std::ptr::null_mut()); | ||
|
||
let mut sa: libc::sigaction = std::mem::zeroed(); | ||
|
@@ -1469,6 +1469,23 @@ mod signal_handler { | |
libc::sigaction(libc::SIGSEGV, &sa, std::ptr::null_mut()); | ||
} | ||
} | ||
|
||
/// Modern kernels on modern hardware can have dynamic signal stack sizes. | ||
#[cfg(any(target_os = "linux", target_os = "android"))] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I have spoken to people who run rustc on Android. |
||
fn min_sigstack_size() -> usize { | ||
const AT_MINSIGSTKSZ: core::ffi::c_ulong = 51; | ||
let dynamic_sigstksz = unsafe { libc::getauxval(AT_MINSIGSTKSZ) }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. doesn't need to block this PR, but does it make sense to upstream AT_MINSIGSTKSZ to the libc crate so we don't need to hardcode it here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you mean would it be good for rust-lang/libc#3125 to be reviewed and accepted? yes, I think so. |
||
// If getauxval couldn't find the entry, it returns 0, | ||
// so take the higher of the "constant" and auxval. | ||
// This transparently supports older kernels which don't provide AT_MINSIGSTKSZ | ||
libc::MINSIGSTKSZ.max(dynamic_sigstksz as _) | ||
} | ||
|
||
/// Not all OS support hardware where this is needed. | ||
#[cfg(not(any(target_os = "linux", target_os = "android")))] | ||
fn min_sigstack_size() -> usize { | ||
libc::MINSIGSTKSZ | ||
} | ||
} | ||
|
||
#[cfg(not(all(unix, any(target_env = "gnu", target_os = "macos"))))] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might seem like 64KiB is greater than the largest number I mentioned, 16KiB.
...but it's up to 16KiB per vector register, and it has way more than one. 🫠