1 /* Page fault handling library.
2 Copyright (C) 1993-2021 Free Software Foundation, Inc.
3 Copyright (C) 2018 Nylon Chen <nylon7@andestech.com>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 /* Written by Bruno Haible and Nylon Chen. */
26 #include <stdio.h> /* declares perror */
27 #include <stdint.h> /* defines uintptr_t */
31 # include <sys/resource.h> /* declares struct rlimit */
35 # include <sys/param.h> /* defines macro OpenBSD */
40 int libsigsegv_version = LIBSIGSEGV_VERSION;
43 /* ======================= Fault handler information ======================= */
47 SIGSEGV_FAULT_HANDLER_ARGLIST
48 is the argument list for the actual fault handler.
50 and if available (optional):
53 is a macro for fetching the fault address.
56 is a macro giving a pointer to the entire fault context (i.e.
57 the register set etc.).
59 SIGSEGV_FAULT_STACKPOINTER
60 is a macro for fetching the stackpointer at the moment the fault
64 #if defined __linux__ || defined __ANDROID__ /* Linux */
66 # define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, siginfo_t *sip, void *ucp
67 # define SIGSEGV_FAULT_ADDRESS sip->si_addr
68 # define SIGSEGV_FAULT_CONTEXT ((ucontext_t *) ucp)
69 # define SIGSEGV_FAULT_ADDRESS_FROM_SIGINFO
71 # if defined __alpha__
73 /* See glibc/sysdeps/unix/sysv/linux/alpha/sys/ucontext.h
74 and the definition of GET_STACK in
75 glibc/sysdeps/unix/sysv/linux/alpha/sigcontextinfo.h.
76 Note that the 'mcontext_t' defined in
77 glibc/sysdeps/unix/sysv/linux/alpha/sys/ucontext.h
78 and the 'struct sigcontext' defined in <asm/sigcontext.h>
79 are actually the same. */
81 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.sc_regs[30]
83 # elif defined __arm64__ /* 64-bit */
85 /* See glibc/sysdeps/unix/sysv/linux/aarch64/sys/ucontext.h.
86 Note that the 'mcontext_t' defined in
87 glibc/sysdeps/unix/sysv/linux/aarch64/sys/ucontext.h
88 and the 'struct sigcontext' defined in <asm/sigcontext.h>
89 are actually the same. */
91 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.sp
93 # elif defined __arm__ || defined __armhf__ /* 32-bit */
95 /* See glibc/sysdeps/unix/sysv/linux/arm/sys/ucontext.h
96 and the definition of GET_STACK in
97 glibc/sysdeps/unix/sysv/linux/arm/sigcontextinfo.h.
98 Note that the 'mcontext_t' defined in
99 glibc/sysdeps/unix/sysv/linux/arm/sys/ucontext.h
100 and the 'struct sigcontext' defined in <asm/sigcontext.h>
101 are actually the same. */
103 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.arm_sp
105 # elif defined __cris__
107 /* See glibc-ports/sysdeps/unix/sysv/linux/cris/sys/ucontext.h.
108 Note that the 'mcontext_t' defined in
109 glibc-ports/sysdeps/unix/sysv/linux/cris/sys/ucontext.h
110 and the 'struct sigcontext' defined in <asm/sigcontext.h>
111 are actually the same. */
113 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.usp
115 # elif defined __hppa__
117 /* See glibc/sysdeps/unix/sysv/linux/hppa/sys/ucontext.h.
118 Note that the 'mcontext_t' defined in
119 glibc/sysdeps/unix/sysv/linux/hppa/sys/ucontext.h
120 and the 'struct sigcontext' defined in <asm/sigcontext.h>
121 are actually the same. */
123 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.sc_gr[30]
125 # elif defined __x86_64__ /* 64 bit registers */
127 /* See glibc/sysdeps/unix/sysv/linux/x86/sys/ucontext.h
128 and the definition of GET_STACK in
129 glibc/sysdeps/unix/sysv/linux/x86_64/sigcontextinfo.h.
130 Note that the 'mcontext_t' defined in
131 glibc/sysdeps/unix/sysv/linux/x86/sys/ucontext.h
132 and the 'struct sigcontext' defined in
133 glibc/sysdeps/unix/sysv/linux/x86/bits/sigcontext.h
134 (see also <asm/sigcontext.h>)
135 are effectively the same. */
137 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.gregs[REG_RSP]
139 # elif defined __i386__ /* 32 bit registers */
141 /* See glibc/sysdeps/unix/sysv/linux/x86/sys/ucontext.h
142 and the definition of GET_STACK in
143 glibc/sysdeps/unix/sysv/linux/i386/sigcontextinfo.h.
144 Note that the 'mcontext_t' defined in
145 glibc/sysdeps/unix/sysv/linux/x86/sys/ucontext.h
146 and the 'struct sigcontext_ia32' defined in <asm/sigcontext32.h>
147 are effectively the same. */
149 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.gregs[REG_ESP]
150 /* same value as ((ucontext_t *) ucp)->uc_mcontext.gregs[REG_UESP] */
152 # elif defined __ia64__
154 /* See glibc/sysdeps/unix/sysv/linux/ia64/sys/ucontext.h.
155 Note that the 'mcontext_t' defined in
156 glibc/sysdeps/unix/sysv/linux/ia64/sys/ucontext.h
157 and the 'struct sigcontext' defined in
158 glibc/sysdeps/unix/sysv/linux/ia64/bits/sigcontext.h
159 (see also <asm/sigcontext.h>)
160 are actually the same. */
162 /* IA-64 has two stack pointers, one that grows down, called $r12, and one
163 that grows up, called $bsp/$bspstore. */
164 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.sc_gr[12]
166 /* It would be better to access $bspstore instead of $bsp but I don't know
167 where to find it in 'struct sigcontext'. Anyway, it doesn't matter
168 because $bsp and $bspstore never differ by more than ca. 1 KB. */
169 # define SIGSEGV_FAULT_BSP_POINTER ((ucontext_t *) ucp)->uc_mcontext.sc_ar_bsp
171 # elif defined __m68k__
173 /* See glibc/sysdeps/unix/sysv/linux/m68k/sys/ucontext.h
174 and the definition of GET_STACK in
175 glibc/sysdeps/unix/sysv/linux/m68k/sigcontextinfo.h.
176 Note that the 'mcontext_t' defined in
177 glibc/sysdeps/unix/sysv/linux/m68k/sys/ucontext.h
178 and the 'struct sigcontext' defined in <asm/sigcontext.h>
179 are quite different types. */
181 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.gregs[R_SP]
183 # elif defined __mips__ || defined __mipsn32__ || defined __mips64__
185 /* See glibc/sysdeps/unix/sysv/linux/mips/sys/ucontext.h
186 and the definition of GET_STACK in
187 glibc/sysdeps/unix/sysv/linux/mips/sigcontextinfo.h.
188 Note that the 'mcontext_t' defined in
189 glibc/sysdeps/unix/sysv/linux/mips/sys/ucontext.h
190 and the 'struct sigcontext' defined in
191 glibc/sysdeps/unix/sysv/linux/mips/bits/sigcontext.h
192 (see also <asm/sigcontext.h>)
193 are effectively the same. */
195 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.gregs[29]
197 # elif defined __nds32__
199 /* See glibc/sysdeps/unix/sysv/linux/nds32/sys/ucontext.h
200 and the definition of GET_STACK in
201 glibc/sysdeps/unix/sysv/linux/nds32/sigcontextinfo.h.
202 Both are found in <https://patches-gcc.linaro.org/cover/4409/> part 08/11
203 <https://sourceware.org/ml/libc-alpha/2018-05/msg00125.html>. */
205 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.nds32_sp
207 # elif defined __powerpc__ || defined __powerpc64__ || defined __powerpc64_elfv2__
209 /* See glibc/sysdeps/unix/sysv/linux/powerpc/sys/ucontext.h
210 and the definition of GET_STACK in
211 glibc/sysdeps/unix/sysv/linux/powerpc/sigcontextinfo.h.
212 Note that the 'mcontext_t' defined in
213 glibc/sysdeps/unix/sysv/linux/powerpc/sys/ucontext.h,
214 the 'struct sigcontext' defined in <asm/sigcontext.h>,
215 and the 'struct pt_regs' defined in <asm/ptrace.h>
216 are quite different types. */
218 # if defined __powerpc64__ || defined __powerpc64_elfv2__ /* 64-bit */
219 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.gp_regs[1]
221 /* both should be equivalent */
223 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.regs->gpr[1]
225 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.uc_regs->gregs[1]
229 # elif defined __riscv32__ || __riscv64__
231 /* See glibc/sysdeps/unix/sysv/linux/riscv/sys/ucontext.h
232 and the definition of GET_STACK in
233 glibc/sysdeps/unix/sysv/linux/riscv/sigcontextinfo.h.
234 Note that the 'mcontext_t' defined in
235 glibc/sysdeps/unix/sysv/linux/riscv/sys/ucontext.h
236 and the 'struct sigcontext' defined in
237 glibc/sysdeps/unix/sysv/linux/riscv/bits/sigcontext.h
238 start with the same block of 32 general-purpose registers. */
240 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.__gregs[REG_SP]
242 # elif defined __s390__ || defined __s390x__
244 /* See glibc/sysdeps/unix/sysv/linux/s390/sys/ucontext.h
245 and the definition of GET_STACK in
246 glibc/sysdeps/unix/sysv/linux/s390/sigcontextinfo.h.
247 Note that the 'mcontext_t' defined in
248 glibc/sysdeps/unix/sysv/linux/s390/sys/ucontext.h
249 and the '_sigregs' type, indirect part of 'struct sigcontext', defined
250 in <asm/sigcontext.h>, are effectively the same. */
252 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.gregs[15]
254 # elif defined __sh__
256 /* See glibc/sysdeps/unix/sysv/linux/sh/sys/ucontext.h
257 and the definition of GET_STACK in
258 glibc/sysdeps/unix/sysv/linux/sh/sigcontextinfo.h.
259 Note that the 'mcontext_t' defined in
260 glibc/sysdeps/unix/sysv/linux/sh/sys/ucontext.h
261 and the 'struct sigcontext' defined in <asm/sigcontext.h>
262 are effectively the same. */
264 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.gregs[15]
266 # elif defined __sparc__ || defined __sparc64__
268 /* See glibc/sysdeps/unix/sysv/linux/sparc/sys/ucontext.h
269 and the definition of GET_STACK in
270 glibc/sysdeps/unix/sysv/linux/sparc/{sparc32,sparc64}/sigcontextinfo.h.
271 Note that the 'mcontext_t' defined in
272 glibc/sysdeps/unix/sysv/linux/sparc/sys/ucontext.h
273 and the 'struct sigcontext' defined in
274 glibc/sysdeps/unix/sysv/linux/sparc/bits/sigcontext.h
275 (see also <asm/sigcontext.h>)
276 are quite different types. */
278 # if defined __sparc64__/* 64-bit */
279 /* From linux-4.8.1/arch/sparc/kernel/signal_64.c, function setup_rt_frame, we
280 see that ucp is not an 'ucontext_t *' but rather a 'struct sigcontext *'
281 that happens to have the same value as sip (which is possible because a
282 'struct sigcontext' starts with 128 bytes room for the siginfo_t). */
283 # define SIGSEGV_FAULT_STACKPOINTER (((struct sigcontext *) ucp)->sigc_regs.u_regs[14] + 2047)
285 /* From linux-4.8.1/arch/sparc/kernel/signal_32.c, function setup_rt_frame,
286 and linux-4.8.1/arch/sparc/kernel/signal32.c, function setup_rt_frame32, we
287 see that ucp is a 'struct pt_regs *' or 'struct pt_regs32 *', respectively.
288 In userland, this is a 'struct sigcontext *'. */
289 # define SIGSEGV_FAULT_STACKPOINTER ((struct sigcontext *) ucp)->si_regs.u_regs[14]
292 /* The sip->si_addr field is correct for a normal fault, but unusable in case
293 of a stack overflow. What I observe (when running
294 tests/test-sigsegv-catch-stackoverflow1, with a printf right at the beginning
295 of sigsegv_handler) is that sip->si_addr is near 0:
296 - in 64-bit mode: sip->si_addr = 0x000000000000030F, and gdb shows me that
297 the fault occurs in an instruction 'stx %o3,[%fp+0x30f]' and %fp is 0.
298 In fact, all registers %l0..%l7 and %i0..%i7 are 0.
299 - in 32-bit mode: sip->si_addr = 0xFFFFFA64, and gdb shows me that
300 the fault occurs in an instruction 'st %g2,[%fp-1436]' and %fp is 0.
301 In fact, all registers %l0..%l7 and %i0..%i7 are 0.
302 Apparently when the stack overflow occurred, some trap has tried to move the
303 contents of the registers %l0..%l7 and %i0..%i7 (a "window" in SPARC
304 terminology) to the stack, did not succeed in doing this, replaced all these
305 register values with 0, and resumed execution at the fault location. This
306 time, due to %fp = 0, a different fault was triggered. Now it is impossible
307 to determine the real (previous) fault address because, even if know the
308 faulting instruction, the previous register values have been lost. */
309 # define BOGUS_FAULT_ADDRESS_UPON_STACK_OVERFLOW
313 /* When adding support for other CPUs here: */
315 /* For SIGSEGV_FAULT_HANDLER_ARGLIST, see the definition of SIGCONTEXT in
316 glibc/sysdeps/unix/sysv/linux/<cpu>/sigcontextinfo.h. */
318 /* For SIGSEGV_FAULT_STACKPOINTER, see the definition of GET_STACK in
319 glibc/sysdeps/unix/sysv/linux/<cpu>/sigcontextinfo.h. */
325 #if defined __GNU__ /* Hurd */
327 # define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp
328 # define SIGSEGV_FAULT_ADDRESS (unsigned long) code
329 # define SIGSEGV_FAULT_CONTEXT scp
331 # if defined __i386__
333 /* scp points to a 'struct sigcontext' (defined in
334 glibc/sysdeps/mach/hurd/i386/bits/sigcontext.h).
335 The registers of this struct get pushed on the stack through
336 gnumach/i386/i386/locore.S:trapall. */
337 /* Both sc_esp and sc_uesp appear to have the same value.
338 It appears more reliable to use sc_uesp because it is labelled as
339 "old esp, if trapped from user". */
340 # define SIGSEGV_FAULT_STACKPOINTER scp->sc_uesp
346 #if defined __FreeBSD_kernel__ || defined __FreeBSD__ || defined __DragonFly__ /* GNU/kFreeBSD, FreeBSD */
348 # if defined __arm__ || defined __armhf__ || defined __arm64__
350 # define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, siginfo_t *sip, void *ucp
351 # define SIGSEGV_FAULT_ADDRESS sip->si_addr
352 # define SIGSEGV_FAULT_CONTEXT ((ucontext_t *) ucp)
354 # if defined __arm64__ /* 64-bit */
356 /* See sys/arm64/include/ucontext.h. */
358 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.mc_gpregs.gp_sp
360 # elif defined __arm__ || defined __armhf__ /* 32-bit */
362 /* See sys/arm/include/ucontext.h. */
364 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.__gregs[_REG_SP]
370 /* On FreeBSD 12, both of these approaches work. On FreeBSD derivatives, the
371 first one has more chances to work. */
373 /* Use signal handlers without SA_SIGINFO. */
375 # define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp, void *addr
376 # define SIGSEGV_FAULT_ADDRESS addr
377 # define SIGSEGV_FAULT_CONTEXT scp
379 /* See sys/x86/include/signal.h. */
381 # if defined __x86_64__
382 /* 64 bit registers */
384 # define SIGSEGV_FAULT_STACKPOINTER scp->sc_rsp
386 # elif defined __i386__
387 /* 32 bit registers */
389 # define SIGSEGV_FAULT_STACKPOINTER scp->sc_esp
394 /* Use signal handlers with SA_SIGINFO. */
396 # define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, siginfo_t *sip, void *scp
397 # define SIGSEGV_FAULT_ADDRESS sip->si_addr
398 # define SIGSEGV_FAULT_CONTEXT ((struct sigcontext *) scp)
399 # define SIGSEGV_FAULT_ADDRESS_FROM_SIGINFO
401 /* See sys/x86/include/signal.h. */
403 # if defined __x86_64__
404 /* 64 bit registers */
406 # define SIGSEGV_FAULT_STACKPOINTER ((struct sigcontext *) scp)->sc_rsp
408 # elif defined __i386__
409 /* 32 bit registers */
411 # define SIGSEGV_FAULT_STACKPOINTER ((struct sigcontext *) scp)->sc_esp
421 #if defined __NetBSD__ /* NetBSD */
423 # define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, siginfo_t *sip, void *ucp
424 # define SIGSEGV_FAULT_ADDRESS sip->si_addr
425 # define SIGSEGV_FAULT_CONTEXT ((ucontext_t *) ucp)
426 # define SIGSEGV_FAULT_ADDRESS_FROM_SIGINFO
428 /* _UC_MACHINE_SP is a platform independent macro.
429 Defined in <machine/mcontext.h>, see
430 http://cvsweb.netbsd.org/bsdweb.cgi/src/sys/arch/$arch/include/mcontext.h
431 Supported on alpha, amd64, i386, ia64, m68k, mips, powerpc, sparc since
433 On i386, _UC_MACHINE_SP is the same as ->uc_mcontext.__gregs[_REG_UESP],
434 and apparently the same value as ->uc_mcontext.__gregs[_REG_ESP]. */
435 # ifdef _UC_MACHINE_SP
436 # define SIGSEGV_FAULT_STACKPOINTER _UC_MACHINE_SP ((ucontext_t *) ucp)
441 #if defined __OpenBSD__ /* OpenBSD */
443 # define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, siginfo_t *sip, struct sigcontext *scp
444 # define SIGSEGV_FAULT_ADDRESS sip->si_addr
445 # define SIGSEGV_FAULT_CONTEXT scp
446 # define SIGSEGV_FAULT_ADDRESS_FROM_SIGINFO
448 # if defined __alpha__
450 /* See the definition of 'struct sigcontext' in
451 openbsd-src/sys/arch/alpha/include/signal.h. */
453 # define SIGSEGV_FAULT_STACKPOINTER scp->sc_regs[30]
455 # elif defined __arm__ || defined __armhf__
457 /* See the definition of 'struct sigcontext' in
458 openbsd-src/sys/arch/arm/include/signal.h. */
460 # define SIGSEGV_FAULT_STACKPOINTER scp->sc_usr_sp
462 # elif defined __hppa__ || defined __hppa64__
464 /* See the definition of 'struct sigcontext' in
465 openbsd-src/sys/arch/hppa/include/signal.h
467 openbsd-src/sys/arch/hppa64/include/signal.h. */
469 # define SIGSEGV_FAULT_STACKPOINTER scp->sc_regs[30]
471 # elif defined __x86_64__
472 /* 64 bit registers */
474 /* See the definition of 'struct sigcontext' in
475 openbsd-src/sys/arch/amd64/include/signal.h. */
477 # define SIGSEGV_FAULT_STACKPOINTER scp->sc_rsp
479 # elif defined __i386__
480 /* 32 bit registers */
482 /* See the definition of 'struct sigcontext' in
483 openbsd-src/sys/arch/i386/include/signal.h. */
485 # define SIGSEGV_FAULT_STACKPOINTER scp->sc_esp
487 # elif defined __m68k__
489 /* See the definition of 'struct sigcontext' in
490 openbsd-src/sys/arch/m68k/include/signal.h. */
492 # define SIGSEGV_FAULT_STACKPOINTER scp->sc_sp
494 # elif defined __m88k__
496 /* See the definition of 'struct sigcontext' in
497 openbsd-src/sys/arch/m88k/include/signal.h
498 and the definition of 'struct reg' in
499 openbsd-src/sys/arch/m88k/include/reg.h. */
501 # if OpenBSD >= 201211 /* OpenBSD version >= 5.2 */
502 # define SIGSEGV_FAULT_STACKPOINTER scp->sc_regs[31]
504 # define SIGSEGV_FAULT_STACKPOINTER scp->sc_regs.r[31]
507 # elif defined __mips__ || defined __mipsn32__ || defined __mips64__
509 /* See the definition of 'struct sigcontext' in
510 openbsd-src/sys/arch/mips64/include/signal.h. */
512 # define SIGSEGV_FAULT_STACKPOINTER scp->sc_regs[29]
514 # elif defined __powerpc__ || defined __powerpc64__
516 /* See the definition of 'struct sigcontext' and 'struct trapframe' in
517 openbsd-src/sys/arch/powerpc/include/signal.h. */
519 # define SIGSEGV_FAULT_STACKPOINTER scp->sc_frame.fixreg[1]
521 # elif defined __sh__
523 /* See the definition of 'struct sigcontext' in
524 openbsd-src/sys/arch/sh/include/signal.h
525 and the definition of 'struct reg' in
526 openbsd-src/sys/arch/sh/include/reg.h. */
528 # if OpenBSD >= 201211 /* OpenBSD version >= 5.2 */
529 # define SIGSEGV_FAULT_STACKPOINTER scp->sc_reg[20-15]
531 # define SIGSEGV_FAULT_STACKPOINTER scp->sc_reg.r_r15
534 # elif defined __sparc__ || defined __sparc64__
536 /* See the definition of 'struct sigcontext' in
537 openbsd-src/sys/arch/sparc/include/signal.h
539 openbsd-src/sys/arch/sparc64/include/signal.h. */
541 # define SIGSEGV_FAULT_STACKPOINTER scp->sc_sp
543 # elif defined __vax__
545 /* See the definition of 'struct sigcontext' in
546 openbsd-src/sys/arch/vax/include/signal.h. */
548 # define SIGSEGV_FAULT_STACKPOINTER scp->sc_sp
554 #if (defined __APPLE__ && defined __MACH__) /* macOS */
556 # define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, siginfo_t *sip, void *ucp
557 # define SIGSEGV_FAULT_ADDRESS sip->si_addr
558 # define SIGSEGV_FAULT_CONTEXT ((ucontext_t *) ucp)
559 # define SIGSEGV_FAULT_ADDRESS_FROM_SIGINFO
561 # if defined __x86_64__
563 /* See the definitions of
564 - 'ucontext_t' and 'struct __darwin_ucontext' in <sys/_types/_ucontext.h>,
565 - 'struct __darwin_mcontext64' in <i386/_mcontext.h>, and
566 - 'struct __darwin_x86_thread_state64' in <mach/i386/_structs.h>. */
567 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext->__ss.__rsp
569 # elif defined __i386__
571 /* See the definitions of
572 - 'ucontext_t' and 'struct __darwin_ucontext' in <sys/_types/_ucontext.h>,
573 - 'struct __darwin_mcontext32' in <i386/_mcontext.h>, and
574 - 'struct __darwin_i386_thread_state' in <mach/i386/_structs.h>. */
575 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext->__ss.__esp
577 # elif defined __arm64__
579 /* See the definitions of
580 - 'ucontext_t' and 'struct __darwin_ucontext' in <sys/_types/_ucontext.h>,
581 - 'struct __darwin_mcontext64' in <arm/_mcontext.h>, and
582 - 'struct __darwin_arm_thread_state64' in <mach/arm/_structs.h>. */
583 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext->__ss.__sp
585 # elif defined __powerpc__
587 /* See the definitions of
588 - 'ucontext_t' and 'struct __darwin_ucontext' in <sys/_structs.h>,
589 - 'struct __darwin_mcontext' in <ppc/_structs.h>, and
590 - 'struct __darwin_ppc_thread_state' in <mach/ppc/_structs.h>. */
591 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext->__ss.__r1
597 #if defined _AIX /* AIX */
599 # define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, siginfo_t *sip, void *ucp
600 # define SIGSEGV_FAULT_ADDRESS sip->si_addr
601 # define SIGSEGV_FAULT_CONTEXT ((ucontext_t *) ucp)
602 # define SIGSEGV_FAULT_ADDRESS_FROM_SIGINFO
604 # if defined __powerpc__ || defined __powerpc64__
605 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.jmp_context.gpr[1]
610 #if defined __sgi /* IRIX */
612 # define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp
613 # define SIGSEGV_FAULT_ADDRESS (unsigned long) scp->sc_badvaddr
614 # define SIGSEGV_FAULT_CONTEXT scp
616 # if defined __mips__ || defined __mipsn32__ || defined __mips64__
617 # define SIGSEGV_FAULT_STACKPOINTER scp->sc_regs[29]
622 #if defined __sun /* Solaris */
624 # define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, siginfo_t *sip, void *ucp
625 # define SIGSEGV_FAULT_ADDRESS sip->si_addr
626 # define SIGSEGV_FAULT_CONTEXT ((ucontext_t *) ucp)
627 # define SIGSEGV_FAULT_ADDRESS_FROM_SIGINFO
629 # if defined __x86_64__
630 /* 64 bit registers */
632 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.gregs[REG_RSP]
634 # elif defined __i386__
635 /* 32 bit registers */
637 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.gregs[ESP]
639 # elif defined __sparc__ || defined __sparc64__
641 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.gregs[REG_O6]
645 /* On Solaris 11.3/SPARC, both in 32-bit and 64-bit mode, when catching
646 stack overflow, the fault address is correct the first time, but is zero
647 or near zero the second time.
648 'truss tests/test-sigsegv-catch-stackoverflow1' shows it:
652 Incurred fault #6, FLTBOUNDS %pc = 0x000116E8
653 siginfo: SIGSEGV SEGV_MAPERR addr=0xFFB00000
654 Received signal #11, SIGSEGV [caught]
655 siginfo: SIGSEGV SEGV_MAPERR addr=0xFFB00000
657 Incurred fault #6, FLTBOUNDS %pc = 0x000116E8
658 siginfo: SIGSEGV SEGV_MAPERR addr=0x00000008
659 Received signal #11, SIGSEGV [caught]
660 siginfo: SIGSEGV SEGV_MAPERR addr=0x00000008
664 Incurred fault #6, FLTBOUNDS %pc = 0x100001C58
665 siginfo: SIGSEGV SEGV_MAPERR addr=0xFFFFFFFF7FF00000
666 Received signal #11, SIGSEGV [caught]
667 siginfo: SIGSEGV SEGV_MAPERR addr=0xFFFFFFFF7FF00000
669 Incurred fault #6, FLTBOUNDS %pc = 0x100001C58
670 siginfo: SIGSEGV SEGV_MAPERR addr=0x00000000
671 Received signal #11, SIGSEGV [caught]
672 siginfo: SIGSEGV SEGV_MAPERR addr=0x00000000
674 # define BOGUS_FAULT_ADDRESS_UPON_STACK_OVERFLOW
682 #if defined __CYGWIN__ /* Cygwin */
684 # define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, siginfo_t *sip, void *ucp
685 # define SIGSEGV_FAULT_ADDRESS sip->si_addr
686 # define SIGSEGV_FAULT_CONTEXT ((ucontext_t *) ucp)
687 # define SIGSEGV_FAULT_ADDRESS_FROM_SIGINFO
689 /* See the definition of 'ucontext_t' in <sys/ucontext.h> and
690 of 'struct __mcontext' in <cygwin/signal.h>. */
691 # if defined __x86_64__
692 /* 64 bit registers */
693 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.rsp
694 # elif defined __i386__
695 /* 32 bit registers */
696 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.esp
701 #if defined __HAIKU__ /* Haiku */
703 # define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, siginfo_t *sip, void *ucp
704 # define SIGSEGV_FAULT_ADDRESS sip->si_addr
705 # define SIGSEGV_FAULT_CONTEXT ((ucontext_t *) ucp)
706 # define SIGSEGV_FAULT_ADDRESS_FROM_SIGINFO
708 # if defined __x86_64__
709 /* 64 bit registers */
711 /* See the definition of 'ucontext_t' in <signal.h> and
712 of 'struct vregs' in <arch/x86_64/signal.h>. */
714 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.rsp
716 # elif defined __i386__
717 /* 32 bit registers */
719 /* See the definition of 'ucontext_t' in <signal.h> and
720 of 'struct vregs' in <arch/x86/signal.h>. */
722 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.esp
728 /* ========================================================================== */
730 /* List of signals that are sent when an invalid virtual memory address
731 is accessed, or when the stack overflows. */
732 #if defined __GNU__ \
733 || defined __FreeBSD_kernel__ || defined __FreeBSD__ || defined __DragonFly__ \
734 || defined __NetBSD__ || defined __OpenBSD__ \
735 || (defined __APPLE__ && defined __MACH__)
736 # define SIGSEGV_FOR_ALL_SIGNALS(var,body) \
737 { int var; var = SIGSEGV; { body } var = SIGBUS; { body } }
739 # define SIGSEGV_FOR_ALL_SIGNALS(var,body) \
740 { int var; var = SIGSEGV; { body } }
743 /* ========================================================================== */
745 /* Determine the virtual memory area of a given address. */
746 #include "stackvma.h"
748 /* ========================================================================== */
750 /* On the average Unix platform, we define
752 HAVE_SIGSEGV_RECOVERY
753 if there is a fault-*.h include file which defines
754 SIGSEGV_FAULT_HANDLER_ARGLIST and SIGSEGV_FAULT_ADDRESS.
756 HAVE_STACK_OVERFLOW_RECOVERY
757 if HAVE_SIGALTSTACK is set and
758 at least two of the following are true:
759 A) There is a fault-*.h include file which defines
760 SIGSEGV_FAULT_HANDLER_ARGLIST and SIGSEGV_FAULT_ADDRESS.
761 B) There is a fault-*.h include file which defines
762 SIGSEGV_FAULT_HANDLER_ARGLIST and SIGSEGV_FAULT_STACKPOINTER.
763 C) There is a stackvma-*.c, other than stackvma-none.c, which
764 defines sigsegv_get_vma.
766 Why? Obviously, to catch stack overflow, we need an alternate signal
767 stack; this requires kernel support. But we also need to distinguish
768 (with a reasonable confidence) a stack overflow from a regular SIGSEGV.
769 If we have A) and B), we use the
770 Heuristic AB: If the fault address is near the stack pointer, it's a
772 If we have A) and C), we use the
773 Heuristic AC: If the fault address is near and beyond the bottom of
774 the stack's virtual memory area, it's a stack overflow.
775 If we have B) and C), we use the
776 Heuristic BC: If the stack pointer is near the bottom of the stack's
777 virtual memory area, it's a stack overflow.
778 This heuristic comes in two flavours: On OSes which let the stack's
779 VMA grow continuously, we determine the bottom by use of getrlimit().
780 On OSes which preallocate the stack's VMA with its maximum size
781 (like BeOS), we use the stack's VMA directly.
784 #if HAVE_SIGSEGV_RECOVERY \
785 && !(defined SIGSEGV_FAULT_HANDLER_ARGLIST && defined SIGSEGV_FAULT_ADDRESS)
786 # error "You need to define SIGSEGV_FAULT_HANDLER_ARGLIST and SIGSEGV_FAULT_ADDRESS before you can define HAVE_SIGSEGV_RECOVERY."
788 #if !HAVE_SIGSEGV_RECOVERY \
789 && (defined SIGSEGV_FAULT_HANDLER_ARGLIST && defined SIGSEGV_FAULT_ADDRESS) \
790 && !(defined __FreeBSD__ && (defined __sparc__ || defined __sparc64__))
791 # if __GNUC__ || (__clang_major__ >= 4)
792 # warning "You can define HAVE_SIGSEGV_RECOVERY on this platform."
794 # error "You can define HAVE_SIGSEGV_RECOVERY on this platform."
798 #if HAVE_STACK_OVERFLOW_RECOVERY \
799 && !(defined SIGSEGV_FAULT_ADDRESS + defined SIGSEGV_FAULT_STACKPOINTER + HAVE_STACKVMA >= 2)
800 # error "You need to define two of SIGSEGV_FAULT_ADDRESS, SIGSEGV_FAULT_STACKPOINTER, HAVE_STACKVMA, before you can define HAVE_STACK_OVERFLOW_RECOVERY."
802 #if !HAVE_STACK_OVERFLOW_RECOVERY \
803 && (defined SIGSEGV_FAULT_ADDRESS + defined SIGSEGV_FAULT_STACKPOINTER + HAVE_STACKVMA >= 2) \
804 && !(defined __FreeBSD__ && (defined __sparc__ || defined __sparc64__)) \
805 && !(defined __NetBSD__ && (defined __sparc__ || defined __sparc64__))
806 # if __GNUC__ || (__clang_major__ >= 4)
807 # warning "You can define HAVE_STACK_OVERFLOW_RECOVERY on this platform."
809 # error "You can define HAVE_STACK_OVERFLOW_RECOVERY on this platform."
813 /* ========================================================================== */
815 #if HAVE_STACK_OVERFLOW_RECOVERY
817 /* ======= Leaving a signal handler executing on the alternate stack ======= */
819 /* Platform dependent:
820 Leaving a signal handler executing on the alternate stack. */
821 static void sigsegv_reset_onstack_flag (void);
823 /* -------------------------- leave-sigaltstack.c -------------------------- */
825 # if defined __GNU__ \
826 || defined __FreeBSD_kernel__ || defined __FreeBSD__ || defined __DragonFly__ \
827 || defined __NetBSD__ || defined __OpenBSD__
830 sigsegv_reset_onstack_flag (void)
834 if (sigaltstack (NULL, &ss) >= 0)
836 ss.ss_flags &= ~SS_ONSTACK;
837 sigaltstack (&ss, NULL);
841 /* --------------------------- leave-setcontext.c --------------------------- */
843 # elif defined __sgi || defined __sun /* IRIX, Solaris */
845 # include <ucontext.h>
848 sigsegv_reset_onstack_flag (void)
852 if (getcontext (&uc) >= 0)
853 /* getcontext returns twice. We are interested in the returned context
854 only the first time, i.e. when the SS_ONSTACK bit is set. */
855 if (uc.uc_stack.ss_flags & SS_ONSTACK)
857 uc.uc_stack.ss_flags &= ~SS_ONSTACK;
858 /* Note that setcontext() does not refill uc. Therefore if
859 setcontext() keeps SS_ONSTACK set in the kernel, either
860 setcontext() will return -1 or getcontext() will return a
861 second time, with the SS_ONSTACK bit being cleared. */
866 /* ------------------------------ leave-nop.c ------------------------------ */
871 sigsegv_reset_onstack_flag (void)
873 /* Nothing to do. sigaltstack() simply looks at the stack pointer,
874 therefore SS_ONSTACK is not sticky. */
879 /* ========================================================================== */
883 /* Address of the last byte belonging to the stack vma. */
884 static uintptr_t stack_top = 0;
886 /* Needs to be called once only. */
888 remember_stack_top (void *some_variable_on_stack)
890 struct vma_struct vma;
892 if (sigsegv_get_vma ((uintptr_t) some_variable_on_stack, &vma) >= 0)
893 stack_top = vma.end - 1;
896 # endif /* HAVE_STACKVMA */
898 static stackoverflow_handler_t stk_user_handler = (stackoverflow_handler_t)NULL;
899 static uintptr_t stk_extra_stack;
900 static size_t stk_extra_stack_size;
902 #endif /* HAVE_STACK_OVERFLOW_RECOVERY */
904 #if HAVE_SIGSEGV_RECOVERY
906 /* User's SIGSEGV handler. */
907 static sigsegv_handler_t user_handler = (sigsegv_handler_t)NULL;
909 #endif /* HAVE_SIGSEGV_RECOVERY */
912 /* Our SIGSEGV handler, with OS dependent argument list. */
914 #if HAVE_SIGSEGV_RECOVERY
917 sigsegv_handler (SIGSEGV_FAULT_HANDLER_ARGLIST)
919 void *address = (void *) (SIGSEGV_FAULT_ADDRESS);
921 # if HAVE_STACK_OVERFLOW_RECOVERY
922 # if !(HAVE_STACKVMA || defined SIGSEGV_FAULT_STACKPOINTER)
923 #error "Insufficient heuristics for detecting a stack overflow. Either define CFG_STACKVMA and HAVE_STACKVMA correctly, or define SIGSEGV_FAULT_STACKPOINTER correctly, or undefine HAVE_STACK_OVERFLOW_RECOVERY!"
926 /* Call user's handler. */
927 if (user_handler && (*user_handler) (address, 0))
929 /* Handler successful. */
933 /* Handler declined responsibility. */
935 /* Did the user install a stack overflow handler? */
936 if (stk_user_handler)
938 /* See whether it was a stack overflow. If so, longjump away. */
939 # ifdef SIGSEGV_FAULT_STACKPOINTER
940 uintptr_t old_sp = (uintptr_t) (SIGSEGV_FAULT_STACKPOINTER);
942 uintptr_t old_bsp = (uintptr_t) (SIGSEGV_FAULT_BSP_POINTER);
947 /* Were we able to determine the stack top? */
950 /* Determine stack bounds. */
952 struct vma_struct vma;
956 ret = sigsegv_get_vma (stack_top, &vma);
960 # ifndef BOGUS_FAULT_ADDRESS_UPON_STACK_OVERFLOW
961 /* Heuristic AC: If the fault_address is nearer to the stack
962 segment's [start,end] than to the previous segment, we
963 consider it a stack overflow.
964 In the case of IA-64, we know that the previous segment
965 is the up-growing bsp segment, and either of the two
966 stacks can overflow. */
967 uintptr_t addr = (uintptr_t) address;
970 if (addr >= vma.prev_end && addr <= vma.end - 1)
972 # if STACK_DIRECTION < 0
973 if (addr >= vma.start
974 ? (addr <= vma.end - 1)
975 : vma.is_near_this (addr, &vma))
977 if (addr <= vma.end - 1
978 ? (addr >= vma.start)
979 : vma.is_near_this (addr, &vma))
983 # else /* BOGUS_FAULT_ADDRESS_UPON_STACK_OVERFLOW */
984 # if HAVE_GETRLIMIT && defined RLIMIT_STACK
985 /* Heuristic BC: If the stack size has reached its maximal size,
986 and old_sp is near the low end, we consider it a stack
991 ret = getrlimit (RLIMIT_STACK, &rl);
995 uintptr_t current_stack_size = vma.end - vma.start;
996 uintptr_t max_stack_size = rl.rlim_cur;
997 if (current_stack_size <= max_stack_size + 4096
998 && max_stack_size <= current_stack_size + 4096
1003 # ifdef SIGSEGV_FAULT_STACKPOINTER
1004 /* Heuristic BC: If we know old_sp, and it is neither
1005 near the low end, nor in the alternate stack, then
1006 it's probably not a stack overflow. */
1007 && ((old_sp >= stk_extra_stack
1008 && old_sp <= stk_extra_stack + stk_extra_stack_size)
1009 # if STACK_DIRECTION < 0
1010 || (old_sp <= vma.start + 4096
1011 && vma.start <= old_sp + 4096))
1013 || (old_sp <= vma.end + 4096
1014 && vma.end <= old_sp + 4096))
1018 # endif /* BOGUS_FAULT_ADDRESS_UPON_STACK_OVERFLOW */
1019 # else /* !HAVE_STACKVMA */
1020 /* Heuristic AB: If the fault address is near the stack pointer,
1021 it's a stack overflow. */
1022 uintptr_t addr = (uintptr_t) address;
1024 if ((addr <= old_sp + 4096 && old_sp <= addr + 4096)
1026 || (addr <= old_bsp + 4096 && old_bsp <= addr + 4096)
1032 # endif /* !HAVE_STACKVMA */
1034 # ifdef SIGSEGV_FAULT_STACKPOINTER
1036 (old_sp >= stk_extra_stack
1037 && old_sp <= stk_extra_stack + stk_extra_stack_size);
1038 stackoverflow_context_t context = (SIGSEGV_FAULT_CONTEXT);
1041 stackoverflow_context_t context = (void *) 0;
1043 /* Call user's handler. */
1044 (*stk_user_handler) (emergency, context);
1050 # endif /* HAVE_STACK_OVERFLOW_RECOVERY */
1052 if (user_handler && (*user_handler) (address, 1))
1054 /* Handler successful. */
1058 /* Handler declined responsibility for real. */
1060 /* Remove ourselves and dump core. */
1061 SIGSEGV_FOR_ALL_SIGNALS (signo, signal (signo, SIG_DFL);)
1064 # if HAVE_STACK_OVERFLOW_RECOVERY
1066 # endif /* HAVE_STACK_OVERFLOW_RECOVERY */
1069 #elif HAVE_STACK_OVERFLOW_RECOVERY
1072 # ifdef SIGSEGV_FAULT_STACKPOINTER
1073 sigsegv_handler (SIGSEGV_FAULT_HANDLER_ARGLIST)
1075 sigsegv_handler (int sig)
1078 # if !((HAVE_GETRLIMIT && defined RLIMIT_STACK) || defined SIGSEGV_FAULT_STACKPOINTER)
1079 # error "Insufficient heuristics for detecting a stack overflow. Either define SIGSEGV_FAULT_STACKPOINTER correctly, or undefine HAVE_STACK_OVERFLOW_RECOVERY!"
1082 /* Did the user install a handler? */
1083 if (stk_user_handler)
1085 /* See whether it was a stack overflow. If so, longjump away. */
1086 # ifdef SIGSEGV_FAULT_STACKPOINTER
1087 uintptr_t old_sp = (uintptr_t) (SIGSEGV_FAULT_STACKPOINTER);
1090 /* Were we able to determine the stack top? */
1093 /* Determine stack bounds. */
1095 struct vma_struct vma;
1098 saved_errno = errno;
1099 ret = sigsegv_get_vma (stack_top, &vma);
1100 errno = saved_errno;
1103 # if HAVE_GETRLIMIT && defined RLIMIT_STACK
1104 /* Heuristic BC: If the stack size has reached its maximal size,
1105 and old_sp is near the low end, we consider it a stack
1109 saved_errno = errno;
1110 ret = getrlimit (RLIMIT_STACK, &rl);
1111 errno = saved_errno;
1114 uintptr_t current_stack_size = vma.end - vma.start;
1115 uintptr_t max_stack_size = rl.rlim_cur;
1116 if (current_stack_size <= max_stack_size + 4096
1117 && max_stack_size <= current_stack_size + 4096
1122 # ifdef SIGSEGV_FAULT_STACKPOINTER
1123 /* Heuristic BC: If we know old_sp, and it is neither
1124 near the low end, nor in the alternate stack, then
1125 it's probably not a stack overflow. */
1126 && ((old_sp >= stk_extra_stack
1127 && old_sp <= stk_extra_stack + stk_extra_stack_size)
1128 # if STACK_DIRECTION < 0
1129 || (old_sp <= vma.start + 4096
1130 && vma.start <= old_sp + 4096))
1132 || (old_sp <= vma.end + 4096
1133 && vma.end <= old_sp + 4096))
1138 # ifdef SIGSEGV_FAULT_STACKPOINTER
1140 (old_sp >= stk_extra_stack
1141 && old_sp <= stk_extra_stack + stk_extra_stack_size);
1142 stackoverflow_context_t context = (SIGSEGV_FAULT_CONTEXT);
1145 stackoverflow_context_t context = (void *) 0;
1147 /* Call user's handler. */
1148 (*stk_user_handler)(emergency,context);
1155 /* Remove ourselves and dump core. */
1156 SIGSEGV_FOR_ALL_SIGNALS (signo, signal (signo, SIG_DFL);)
1162 #if HAVE_SIGSEGV_RECOVERY || HAVE_STACK_OVERFLOW_RECOVERY
1165 install_for (int sig)
1167 struct sigaction action;
1169 # ifdef SIGSEGV_FAULT_ADDRESS_FROM_SIGINFO
1170 action.sa_sigaction = &sigsegv_handler;
1172 action.sa_handler = (void (*) (int)) &sigsegv_handler;
1174 /* Block most signals while SIGSEGV is being handled. */
1175 /* Signals SIGKILL, SIGSTOP cannot be blocked. */
1176 /* Signals SIGCONT, SIGTSTP, SIGTTIN, SIGTTOU are not blocked because
1177 dealing with these signals seems dangerous. */
1178 /* Signals SIGILL, SIGABRT, SIGFPE, SIGSEGV, SIGTRAP, SIGIOT, SIGEMT, SIGBUS,
1179 SIGSYS, SIGSTKFLT are not blocked because these are synchronous signals,
1180 which may require immediate intervention, otherwise the process may
1182 sigemptyset (&action.sa_mask);
1184 sigaddset (&action.sa_mask,SIGHUP);
1187 sigaddset (&action.sa_mask,SIGINT);
1190 sigaddset (&action.sa_mask,SIGQUIT);
1193 sigaddset (&action.sa_mask,SIGPIPE);
1196 sigaddset (&action.sa_mask,SIGALRM);
1199 sigaddset (&action.sa_mask,SIGTERM);
1202 sigaddset (&action.sa_mask,SIGUSR1);
1205 sigaddset (&action.sa_mask,SIGUSR2);
1208 sigaddset (&action.sa_mask,SIGCHLD);
1211 sigaddset (&action.sa_mask,SIGCLD);
1214 sigaddset (&action.sa_mask,SIGURG);
1217 sigaddset (&action.sa_mask,SIGIO);
1220 sigaddset (&action.sa_mask,SIGPOLL);
1223 sigaddset (&action.sa_mask,SIGXCPU);
1226 sigaddset (&action.sa_mask,SIGXFSZ);
1229 sigaddset (&action.sa_mask,SIGVTALRM);
1232 sigaddset (&action.sa_mask,SIGPROF);
1235 sigaddset (&action.sa_mask,SIGPWR);
1238 sigaddset (&action.sa_mask,SIGLOST);
1241 sigaddset (&action.sa_mask,SIGWINCH);
1243 /* Note that sigaction() implicitly adds sig itself to action.sa_mask. */
1244 /* Ask the OS to provide a structure siginfo_t to the handler. */
1245 # ifdef SIGSEGV_FAULT_ADDRESS_FROM_SIGINFO
1246 action.sa_flags = SA_SIGINFO;
1248 action.sa_flags = 0;
1250 # if HAVE_STACK_OVERFLOW_RECOVERY && HAVE_SIGALTSTACK /* not BeOS */
1251 /* Work around Linux 2.2.5 bug: If SA_ONSTACK is specified but sigaltstack()
1252 has not been called, the kernel will busy loop, eating CPU time. So
1253 avoid setting SA_ONSTACK until the user has requested stack overflow
1255 if (stk_user_handler)
1256 action.sa_flags |= SA_ONSTACK;
1258 sigaction (sig, &action, (struct sigaction *) NULL);
1261 #endif /* HAVE_SIGSEGV_RECOVERY || HAVE_STACK_OVERFLOW_RECOVERY */
1264 sigsegv_install_handler (sigsegv_handler_t handler)
1266 #if HAVE_SIGSEGV_RECOVERY
1267 user_handler = handler;
1269 SIGSEGV_FOR_ALL_SIGNALS (sig, install_for (sig);)
1278 sigsegv_deinstall_handler (void)
1280 #if HAVE_SIGSEGV_RECOVERY
1281 user_handler = (sigsegv_handler_t)NULL;
1283 # if HAVE_STACK_OVERFLOW_RECOVERY
1284 if (!stk_user_handler)
1287 SIGSEGV_FOR_ALL_SIGNALS (sig, signal (sig, SIG_DFL);)
1293 sigsegv_leave_handler (void (*continuation) (void*, void*, void*),
1294 void* cont_arg1, void* cont_arg2, void* cont_arg3)
1296 #if HAVE_STACK_OVERFLOW_RECOVERY
1298 * Reset the system's knowledge that we are executing on the alternate
1299 * stack. If we didn't do that, siglongjmp would be needed instead of
1300 * longjmp to leave the signal handler.
1302 sigsegv_reset_onstack_flag ();
1304 (*continuation) (cont_arg1, cont_arg2, cont_arg3);
1309 stackoverflow_install_handler (stackoverflow_handler_t handler,
1310 void *extra_stack, size_t extra_stack_size)
1312 #if HAVE_STACK_OVERFLOW_RECOVERY
1317 remember_stack_top (&dummy);
1323 stk_user_handler = handler;
1324 stk_extra_stack = (uintptr_t) extra_stack;
1325 stk_extra_stack_size = extra_stack_size;
1328 # if SIGALTSTACK_SS_REVERSED
1329 ss.ss_sp = (char *) extra_stack + extra_stack_size - sizeof (void *);
1330 ss.ss_size = extra_stack_size - sizeof (void *);
1332 ss.ss_sp = extra_stack;
1333 ss.ss_size = extra_stack_size;
1335 ss.ss_flags = 0; /* no SS_DISABLE */
1336 if (sigaltstack (&ss, (stack_t*)0) < 0)
1340 /* Install the signal handlers with SA_ONSTACK. */
1341 SIGSEGV_FOR_ALL_SIGNALS (sig, install_for (sig);)
1349 stackoverflow_deinstall_handler (void)
1351 #if HAVE_STACK_OVERFLOW_RECOVERY
1352 stk_user_handler = (stackoverflow_handler_t) NULL;
1354 # if HAVE_SIGSEGV_RECOVERY
1357 /* Reinstall the signal handlers without SA_ONSTACK, to avoid Linux
1359 SIGSEGV_FOR_ALL_SIGNALS (sig, install_for (sig);)
1364 SIGSEGV_FOR_ALL_SIGNALS (sig, signal (sig, SIG_DFL);)
1369 ss.ss_flags = SS_DISABLE;
1370 if (sigaltstack (&ss, (stack_t *) 0) < 0)
1371 perror ("gnulib sigsegv (stackoverflow_deinstall_handler)");