Skip to content

Commit 8842e28

Browse files
committed
Auto merge of #31684 - tmiasko:alternate-stack, r=alexcrichton
Remove alternate stack with sigaltstack before unmaping it. Also reuse existing signal stack if already set, this is especially useful when working with sanitizers that configure alternate stack themselves. This change depends on SS_DISABLE recently introduced in libc crate and updates this git submodule accordingly.
2 parents de366b5 + 77922b8 commit 8842e28

File tree

3 files changed

+69
-6
lines changed

3 files changed

+69
-6
lines changed

src/liblibc

src/libstd/sys/unix/stack_overflow.rs

+24-5
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ mod imp {
4646
use super::Handler;
4747
use mem;
4848
use ptr;
49-
use libc::{sigaltstack, SIGSTKSZ};
49+
use libc::{sigaltstack, SIGSTKSZ, SS_DISABLE};
5050
use libc::{sigaction, SIGBUS, SIG_DFL,
5151
SA_SIGINFO, SA_ONSTACK, sighandler_t};
5252
use libc;
@@ -169,13 +169,32 @@ mod imp {
169169
}
170170

171171
pub unsafe fn make_handler() -> Handler {
172-
let stack = get_stack();
173-
sigaltstack(&stack, ptr::null_mut());
174-
Handler { _data: stack.ss_sp as *mut libc::c_void }
172+
let mut stack = mem::zeroed();
173+
sigaltstack(ptr::null(), &mut stack);
174+
// Configure alternate signal stack, if one is not already set.
175+
if stack.ss_flags & SS_DISABLE != 0 {
176+
stack = get_stack();
177+
sigaltstack(&stack, ptr::null_mut());
178+
Handler { _data: stack.ss_sp as *mut libc::c_void }
179+
} else {
180+
Handler { _data: ptr::null_mut() }
181+
}
175182
}
176183

177184
pub unsafe fn drop_handler(handler: &mut Handler) {
178-
munmap(handler._data, SIGSTKSZ);
185+
if !handler._data.is_null() {
186+
let stack = libc::stack_t {
187+
ss_sp: ptr::null_mut(),
188+
ss_flags: SS_DISABLE,
189+
// Workaround for bug in MacOS implementation of sigaltstack
190+
// UNIX2003 which returns ENOMEM when disabling a stack while
191+
// passing ss_size smaller than MINSIGSTKSZ. According to POSIX
192+
// both ss_sp and ss_size should be ignored in this case.
193+
ss_size: SIGSTKSZ,
194+
};
195+
sigaltstack(&stack, ptr::null_mut());
196+
munmap(handler._data, SIGSTKSZ);
197+
}
179198
}
180199
}
181200

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Previously memory for alternate signal stack have been unmapped during
12+
// main thread exit while still being in use by signal handlers. This test
13+
// triggers this situation by sending signal from atexit handler.
14+
//
15+
// ignore-windows
16+
17+
#![feature(libc)]
18+
extern crate libc;
19+
20+
use libc::*;
21+
22+
unsafe extern fn signal_handler(signum: c_int, _: *mut siginfo_t, _: *mut c_void) {
23+
assert_eq!(signum, SIGWINCH);
24+
}
25+
26+
extern fn send_signal() {
27+
unsafe {
28+
raise(SIGWINCH);
29+
}
30+
}
31+
32+
fn main() {
33+
unsafe {
34+
// Install signal hander that runs on alternate signal stack.
35+
let mut action: sigaction = std::mem::zeroed();
36+
action.sa_flags = SA_SIGINFO | SA_ONSTACK;
37+
action.sa_sigaction = signal_handler as sighandler_t;
38+
sigaction(SIGWINCH, &action, std::ptr::null_mut());
39+
40+
// Send SIGWINCH on exit.
41+
atexit(send_signal);
42+
}
43+
}
44+

0 commit comments

Comments
 (0)