@@ -11,7 +11,7 @@ use libc::{
1111static mut SIGNAL_HANDLER : Option < Box < dyn Fn ( libc:: c_int ) -> !> > = None ;
1212
1313#[ no_coverage]
14- extern "C" fn os_handler ( signal : libc:: c_int , _: libc:: siginfo_t , _: * mut libc:: c_void ) {
14+ extern "C" fn os_handler ( signal : libc:: c_int , _: * mut libc:: siginfo_t , _: * mut libc:: c_void ) {
1515 // Assuming this always succeeds. Can't really handle errors in any meaningful way.
1616 unsafe {
1717 reset_signal_handlers ( ) ;
@@ -23,26 +23,28 @@ extern "C" fn os_handler(signal: libc::c_int, _: libc::siginfo_t, _: *mut libc::
2323 }
2424}
2525
26+ /// Set signal handlers to the given function and return the pointer and layout
27+ /// of the alternative stack used by the signal handlers.
2628#[ no_coverage]
27- pub unsafe fn set_signal_handlers < F : ' static > ( f : F )
29+ pub unsafe fn set_signal_handlers < F : ' static > ( f : F ) -> ( * mut u8 , std :: alloc :: Layout )
2830where
2931 F : Fn ( libc:: c_int ) -> !,
3032{
3133 SIGNAL_HANDLER = Some ( Box :: new ( f) ) ;
3234
33- let stack_size = libc :: SIGSTKSZ ;
34-
35- let stack_pointer = std:: alloc:: alloc_zeroed ( std :: alloc :: Layout :: array :: < std :: ffi :: c_void > ( stack_size) . unwrap ( ) )
36- as * mut std:: ffi :: c_void ;
35+ // Make sure the alternative stack is big enough. ~65_000 bytes should be okay.
36+ let stack_size = std :: cmp :: max ( libc :: SIGSTKSZ , 0b1 << 16 ) ;
37+ let stack_layout = std:: alloc:: Layout :: array :: < u8 > ( stack_size) . unwrap ( ) ;
38+ let stack_pointer = std:: alloc :: alloc_zeroed ( stack_layout ) ;
3739
3840 let signal_stack = libc:: stack_t {
39- ss_sp : stack_pointer,
41+ ss_sp : stack_pointer as * mut std :: ffi :: c_void ,
4042 ss_size : stack_size,
4143 ss_flags : 0 ,
4244 } ;
4345
4446 let stack = libc:: sigaltstack ( & signal_stack, std:: ptr:: null_mut ( ) ) ;
45- if stack == - 1 {
47+ if stack < 0 {
4648 panic ! ( "could not set alternate stack for handling signals" ) ;
4749 }
4850
6062 panic ! ( "Could not set up signal handler" ) ;
6163 }
6264 }
65+
66+ ( stack_pointer, stack_layout)
6367}
6468
6569#[ no_coverage]
0 commit comments