Skip to content

Commit 162ed4d

Browse files
committed
Use signal handler only on supported platforms
1 parent ec6a85a commit 162ed4d

File tree

1 file changed

+42
-37
lines changed
  • compiler/rustc_driver/src

1 file changed

+42
-37
lines changed

compiler/rustc_driver/src/lib.rs

+42-37
Original file line numberDiff line numberDiff line change
@@ -1312,55 +1312,60 @@ pub fn init_env_logger(env: &str) {
13121312
tracing::subscriber::set_global_default(subscriber).unwrap();
13131313
}
13141314

1315-
#[cfg(unix)]
1316-
extern "C" {
1317-
fn backtrace_symbols_fd(buffer: *const *mut libc::c_void, size: libc::c_int, fd: libc::c_int);
1318-
}
1315+
#[cfg(all(unix, any(target_env = "gnu", target_os = "macos")))]
1316+
mod signal_handler {
1317+
extern "C" {
1318+
fn backtrace_symbols_fd(
1319+
buffer: *const *mut libc::c_void,
1320+
size: libc::c_int,
1321+
fd: libc::c_int,
1322+
);
1323+
}
13191324

1320-
#[cfg(unix)]
1321-
extern "C" fn print_stack_trace(_: libc::c_int) {
1322-
const MAX_FRAMES: usize = 256;
1323-
static mut STACK_TRACE: [*mut libc::c_void; MAX_FRAMES] = [std::ptr::null_mut(); MAX_FRAMES];
1324-
unsafe {
1325-
let depth = libc::backtrace(STACK_TRACE.as_mut_ptr(), MAX_FRAMES as i32);
1326-
if depth == 0 {
1327-
return;
1325+
extern "C" fn print_stack_trace(_: libc::c_int) {
1326+
const MAX_FRAMES: usize = 256;
1327+
static mut STACK_TRACE: [*mut libc::c_void; MAX_FRAMES] =
1328+
[std::ptr::null_mut(); MAX_FRAMES];
1329+
unsafe {
1330+
let depth = libc::backtrace(STACK_TRACE.as_mut_ptr(), MAX_FRAMES as i32);
1331+
if depth == 0 {
1332+
return;
1333+
}
1334+
backtrace_symbols_fd(STACK_TRACE.as_ptr(), depth, 2);
13281335
}
1329-
backtrace_symbols_fd(STACK_TRACE.as_ptr(), depth, 2);
13301336
}
1331-
}
13321337

1333-
#[cfg(unix)]
1334-
// When an error signal (such as SIGABRT or SIGSEGV) is delivered to the
1335-
// process, print a stack trace and then exit.
1336-
fn install_signal_handler() {
1337-
unsafe {
1338-
const ALT_STACK_SIZE: usize = libc::MINSIGSTKSZ + 64 * 1024;
1339-
let mut alt_stack: libc::stack_t = std::mem::zeroed();
1340-
alt_stack.ss_sp =
1341-
std::alloc::alloc(std::alloc::Layout::from_size_align(ALT_STACK_SIZE, 1).unwrap())
1342-
as *mut libc::c_void;
1343-
alt_stack.ss_size = ALT_STACK_SIZE;
1344-
libc::sigaltstack(&mut alt_stack, std::ptr::null_mut());
1345-
1346-
let mut sa: libc::sigaction = std::mem::zeroed();
1347-
sa.sa_sigaction = print_stack_trace as libc::sighandler_t;
1348-
sa.sa_flags = libc::SA_NODEFER | libc::SA_RESETHAND | libc::SA_ONSTACK;
1349-
libc::sigemptyset(&mut sa.sa_mask);
1350-
libc::sigaction(libc::SIGSEGV, &sa, std::ptr::null_mut());
1338+
// When an error signal (such as SIGABRT or SIGSEGV) is delivered to the
1339+
// process, print a stack trace and then exit.
1340+
pub(super) fn install() {
1341+
unsafe {
1342+
const ALT_STACK_SIZE: usize = libc::MINSIGSTKSZ + 64 * 1024;
1343+
let mut alt_stack: libc::stack_t = std::mem::zeroed();
1344+
alt_stack.ss_sp =
1345+
std::alloc::alloc(std::alloc::Layout::from_size_align(ALT_STACK_SIZE, 1).unwrap())
1346+
as *mut libc::c_void;
1347+
alt_stack.ss_size = ALT_STACK_SIZE;
1348+
libc::sigaltstack(&mut alt_stack, std::ptr::null_mut());
1349+
1350+
let mut sa: libc::sigaction = std::mem::zeroed();
1351+
sa.sa_sigaction = print_stack_trace as libc::sighandler_t;
1352+
sa.sa_flags = libc::SA_NODEFER | libc::SA_RESETHAND | libc::SA_ONSTACK;
1353+
libc::sigemptyset(&mut sa.sa_mask);
1354+
libc::sigaction(libc::SIGSEGV, &sa, std::ptr::null_mut());
1355+
}
13511356
}
13521357
}
13531358

1354-
#[cfg(not(unix))]
1355-
// When an error signal (such as SIGABRT or SIGSEGV) is delivered to the
1356-
// process, print a stack trace and then exit.
1357-
fn install_signal_handler() {}
1359+
#[cfg(not(all(unix, any(target_env = "gnu", target_os = "macos"))))]
1360+
mod signal_handler {
1361+
pub(super) fn install() {}
1362+
}
13581363

13591364
pub fn main() -> ! {
13601365
let start_time = Instant::now();
13611366
let start_rss = get_resident_set_size();
13621367
init_rustc_env_logger();
1363-
install_signal_handler();
1368+
signal_handler::install();
13641369
let mut callbacks = TimePassesCallbacks::default();
13651370
install_ice_hook();
13661371
let exit_code = catch_with_exit_code(|| {

0 commit comments

Comments
 (0)