@@ -25,6 +25,18 @@ impl Drop for Handler {
25
25
}
26
26
}
27
27
28
+ #[ cfg( any(
29
+ target_os = "linux" ,
30
+ target_os = "freebsd" ,
31
+ target_os = "hurd" ,
32
+ target_os = "macos" ,
33
+ target_os = "netbsd" ,
34
+ target_os = "openbsd" ,
35
+ target_os = "solaris" ,
36
+ target_os = "illumos" ,
37
+ ) ) ]
38
+ mod thread_info;
39
+
28
40
#[ cfg( any(
29
41
target_os = "linux" ,
30
42
target_os = "freebsd" ,
@@ -46,22 +58,13 @@ mod imp {
46
58
use libc:: { mmap64, mprotect, munmap} ;
47
59
48
60
use super :: Handler ;
49
- use crate :: cell :: Cell ;
61
+ use super :: thread_info :: { delete_current_info , set_current_info , with_current_info } ;
50
62
use crate :: ops:: Range ;
51
63
use crate :: sync:: OnceLock ;
52
64
use crate :: sync:: atomic:: { Atomic , AtomicBool , AtomicPtr , AtomicUsize , Ordering } ;
53
65
use crate :: sys:: pal:: unix:: os;
54
- use crate :: { io, mem, ptr, thread} ;
55
-
56
- // We use a TLS variable to store the address of the guard page. While TLS
57
- // variables are not guaranteed to be signal-safe, this works out in practice
58
- // since we make sure to write to the variable before the signal stack is
59
- // installed, thereby ensuring that the variable is always allocated when
60
- // the signal handler is called.
61
- thread_local ! {
62
- // FIXME: use `Range` once that implements `Copy`.
63
- static GUARD : Cell <( usize , usize ) > = const { Cell :: new( ( 0 , 0 ) ) } ;
64
- }
66
+ use crate :: thread:: with_current_name;
67
+ use crate :: { io, mem, panic, ptr} ;
65
68
66
69
// Signal handler for the SIGSEGV and SIGBUS handlers. We've got guard pages
67
70
// (unmapped pages) at the end of every thread's stack, so if a thread ends
@@ -93,29 +96,37 @@ mod imp {
93
96
info : * mut libc:: siginfo_t ,
94
97
_data : * mut libc:: c_void ,
95
98
) {
96
- let ( start, end) = GUARD . get ( ) ;
97
99
// SAFETY: this pointer is provided by the system and will always point to a valid `siginfo_t`.
98
- let addr = unsafe { ( * info) . si_addr ( ) . addr ( ) } ;
100
+ let fault_addr = unsafe { ( * info) . si_addr ( ) . addr ( ) } ;
101
+
102
+ // `with_current_info` expects that the process aborts after it is
103
+ // called. If the signal was not caused by a memory access, this might
104
+ // not be true. We detect this by noticing that the `si_addr` field is
105
+ // zero if the signal is synthetic.
106
+ if fault_addr == 0 {
107
+ return ;
108
+ }
99
109
100
- // If the faulting address is within the guard page, then we print a
101
- // message saying so and abort.
102
- if start <= addr && addr < end {
103
- thread:: with_current_name ( |name| {
104
- let name = name. unwrap_or ( "<unknown>" ) ;
110
+ with_current_info ( |thread_info| {
111
+ // If the faulting address is within the guard page, then we print a
112
+ // message saying so and abort.
113
+ if let Some ( thread_info) = thread_info
114
+ && thread_info. guard_page_range . contains ( & fault_addr)
115
+ {
116
+ let name = thread_info. thread_name . as_deref ( ) . unwrap_or ( "<unknown>" ) ;
105
117
rtprintpanic ! ( "\n thread '{name}' has overflowed its stack\n " ) ;
106
- } ) ;
107
-
108
- rtabort ! ( "stack overflow" ) ;
109
- } else {
110
- // Unregister ourselves by reverting back to the default behavior.
111
- // SAFETY: assuming all platforms define struct sigaction as "zero-initializable"
112
- let mut action: sigaction = unsafe { mem:: zeroed ( ) } ;
113
- action. sa_sigaction = SIG_DFL ;
114
- // SAFETY: pray this is a well-behaved POSIX implementation of fn sigaction
115
- unsafe { sigaction ( signum, & action, ptr:: null_mut ( ) ) } ;
116
-
117
- // See comment above for why this function returns.
118
- }
118
+ rtabort ! ( "stack overflow" ) ;
119
+ } else {
120
+ // Unregister ourselves by reverting back to the default behavior.
121
+ // SAFETY: assuming all platforms define struct sigaction as "zero-initializable"
122
+ let mut action: sigaction = unsafe { mem:: zeroed ( ) } ;
123
+ action. sa_sigaction = SIG_DFL ;
124
+ // SAFETY: pray this is a well-behaved POSIX implementation of fn sigaction
125
+ unsafe { sigaction ( signum, & action, ptr:: null_mut ( ) ) } ;
126
+
127
+ // See comment above for why this function returns.
128
+ }
129
+ } )
119
130
}
120
131
121
132
static PAGE_SIZE : Atomic < usize > = AtomicUsize :: new ( 0 ) ;
@@ -128,9 +139,7 @@ mod imp {
128
139
pub unsafe fn init ( ) {
129
140
PAGE_SIZE . store ( os:: page_size ( ) , Ordering :: Relaxed ) ;
130
141
131
- // Always write to GUARD to ensure the TLS variable is allocated.
132
- let guard = unsafe { install_main_guard ( ) . unwrap_or ( 0 ..0 ) } ;
133
- GUARD . set ( ( guard. start , guard. end ) ) ;
142
+ let mut guard_page_range = unsafe { install_main_guard ( ) } ;
134
143
135
144
// SAFETY: assuming all platforms define struct sigaction as "zero-initializable"
136
145
let mut action: sigaction = unsafe { mem:: zeroed ( ) } ;
@@ -145,7 +154,13 @@ mod imp {
145
154
let handler = unsafe { make_handler ( true ) } ;
146
155
MAIN_ALTSTACK . store ( handler. data , Ordering :: Relaxed ) ;
147
156
mem:: forget ( handler) ;
157
+
158
+ if let Some ( guard_page_range) = guard_page_range. take ( ) {
159
+ let thread_name = with_current_name ( |name| name. map ( Box :: from) ) ;
160
+ set_current_info ( guard_page_range, thread_name) ;
161
+ }
148
162
}
163
+
149
164
action. sa_flags = SA_SIGINFO | SA_ONSTACK ;
150
165
action. sa_sigaction = signal_handler as sighandler_t ;
151
166
// SAFETY: only overriding signals if the default is set
@@ -214,9 +229,10 @@ mod imp {
214
229
}
215
230
216
231
if !main_thread {
217
- // Always write to GUARD to ensure the TLS variable is allocated.
218
- let guard = unsafe { current_guard ( ) } . unwrap_or ( 0 ..0 ) ;
219
- GUARD . set ( ( guard. start , guard. end ) ) ;
232
+ if let Some ( guard_page_range) = unsafe { current_guard ( ) } {
233
+ let thread_name = with_current_name ( |name| name. map ( Box :: from) ) ;
234
+ set_current_info ( guard_page_range, thread_name) ;
235
+ }
220
236
}
221
237
222
238
// SAFETY: assuming stack_t is zero-initializable
@@ -261,6 +277,8 @@ mod imp {
261
277
// a mapping that started one page earlier, so walk back a page and unmap from there.
262
278
unsafe { munmap ( data. sub ( page_size) , sigstack_size + page_size) } ;
263
279
}
280
+
281
+ delete_current_info ( ) ;
264
282
}
265
283
266
284
/// Modern kernels on modern hardware can have dynamic signal stack sizes.
0 commit comments