| 1 |
/* Page fault handling library. |
| 2 |
Copyright (C) 1993-2025 Free Software Foundation, Inc. |
| 3 |
Copyright (C) 2018 Nylon Chen <[email protected]> |
| 4 |
|
| 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. |
| 9 |
|
| 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. |
| 14 |
|
| 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/>. */ |
| 17 |
|
| 18 |
/* Written by Bruno Haible and Nylon Chen. */ |
| 19 |
|
| 20 |
#include <config.h> |
| 21 |
|
| 22 |
/* Specification. */ |
| 23 |
#include "sigsegv.h" |
| 24 |
|
| 25 |
#include <errno.h> |
| 26 |
#include <stdio.h> /* declares perror */ |
| 27 |
#include <stdint.h> /* defines uintptr_t */ |
| 28 |
#include <stdlib.h> |
| 29 |
#include <signal.h> |
| 30 |
#if HAVE_GETRLIMIT |
| 31 |
# include <sys/resource.h> /* declares struct rlimit */ |
| 32 |
#endif |
| 33 |
|
| 34 |
#ifdef __OpenBSD__ |
| 35 |
# include <sys/param.h> /* defines macro OpenBSD */ |
| 36 |
#endif |
| 37 |
|
| 38 |
|
| 39 |
/* Version number. */ |
| 40 |
int libsigsegv_version = LIBSIGSEGV_VERSION; |
| 41 |
|
| 42 |
|
| 43 |
/* ======================= Fault handler information ======================= */ |
| 44 |
|
| 45 |
/* Define: |
| 46 |
|
| 47 |
SIGSEGV_FAULT_HANDLER_ARGLIST |
| 48 |
is the argument list for the actual fault handler. |
| 49 |
|
| 50 |
and if available (optional): |
| 51 |
|
| 52 |
SIGSEGV_FAULT_ADDRESS |
| 53 |
is a macro for fetching the fault address. |
| 54 |
|
| 55 |
SIGSEGV_FAULT_CONTEXT |
| 56 |
is a macro giving a pointer to the entire fault context (i.e. |
| 57 |
the register set etc.). |
| 58 |
|
| 59 |
SIGSEGV_FAULT_STACKPOINTER |
| 60 |
is a macro for fetching the stackpointer at the moment the fault |
| 61 |
occurred. |
| 62 |
*/ |
| 63 |
|
| 64 |
#if defined __linux__ && !defined __ANDROID__ /* Linux */ |
| 65 |
|
| 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 |
| 70 |
|
| 71 |
# if defined __alpha__ |
| 72 |
|
| 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. */ |
| 80 |
|
| 81 |
# define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.sc_regs[30] |
| 82 |
|
| 83 |
# elif defined __arm64__ /* 64-bit */ |
| 84 |
|
| 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. */ |
| 90 |
|
| 91 |
# define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.sp |
| 92 |
|
| 93 |
# elif defined __arm__ || defined __armhf__ /* 32-bit */ |
| 94 |
|
| 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. */ |
| 102 |
|
| 103 |
# define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.arm_sp |
| 104 |
|
| 105 |
# elif defined __cris__ |
| 106 |
|
| 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. */ |
| 112 |
|
| 113 |
# define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.usp |
| 114 |
|
| 115 |
# elif defined __hppa__ |
| 116 |
|
| 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. */ |
| 122 |
|
| 123 |
# define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.sc_gr[30] |
| 124 |
|
| 125 |
# elif defined __x86_64__ /* 64 bit registers */ |
| 126 |
|
| 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. */ |
| 136 |
|
| 137 |
# define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.gregs[REG_RSP] |
| 138 |
|
| 139 |
# elif defined __i386__ /* 32 bit registers */ |
| 140 |
|
| 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. */ |
| 148 |
|
| 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] */ |
| 151 |
|
| 152 |
# elif defined __ia64__ |
| 153 |
|
| 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. */ |
| 161 |
|
| 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] |
| 165 |
|
| 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 |
| 170 |
|
| 171 |
# elif defined __loongarch__ |
| 172 |
|
| 173 |
/* See <sys/ucontext.h>. |
| 174 |
Note that the 'mcontext_t' defined in <sys/ucontext.h> |
| 175 |
and the 'struct sigcontext' defined in <bits/sigcontext.h> |
| 176 |
(see also <asm/sigcontext.h>) are effectively the same. */ |
| 177 |
|
| 178 |
# define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.__gregs[3] |
| 179 |
|
| 180 |
# elif defined __m68k__ |
| 181 |
|
| 182 |
/* See glibc/sysdeps/unix/sysv/linux/m68k/sys/ucontext.h |
| 183 |
and the definition of GET_STACK in |
| 184 |
glibc/sysdeps/unix/sysv/linux/m68k/sigcontextinfo.h. |
| 185 |
Note that the 'mcontext_t' defined in |
| 186 |
glibc/sysdeps/unix/sysv/linux/m68k/sys/ucontext.h |
| 187 |
and the 'struct sigcontext' defined in <asm/sigcontext.h> |
| 188 |
are quite different types. */ |
| 189 |
|
| 190 |
# define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.gregs[R_SP] |
| 191 |
|
| 192 |
# elif defined __mips__ || defined __mipsn32__ || defined __mips64__ |
| 193 |
|
| 194 |
/* See glibc/sysdeps/unix/sysv/linux/mips/sys/ucontext.h |
| 195 |
and the definition of GET_STACK in |
| 196 |
glibc/sysdeps/unix/sysv/linux/mips/sigcontextinfo.h. |
| 197 |
Note that the 'mcontext_t' defined in |
| 198 |
glibc/sysdeps/unix/sysv/linux/mips/sys/ucontext.h |
| 199 |
and the 'struct sigcontext' defined in |
| 200 |
glibc/sysdeps/unix/sysv/linux/mips/bits/sigcontext.h |
| 201 |
(see also <asm/sigcontext.h>) |
| 202 |
are effectively the same. */ |
| 203 |
|
| 204 |
# define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.gregs[29] |
| 205 |
|
| 206 |
# elif defined __nds32__ |
| 207 |
|
| 208 |
/* See glibc/sysdeps/unix/sysv/linux/nds32/sys/ucontext.h |
| 209 |
and the definition of GET_STACK in |
| 210 |
glibc/sysdeps/unix/sysv/linux/nds32/sigcontextinfo.h. |
| 211 |
Both are found in <https://patches-gcc.linaro.org/cover/4409/> part 08/11 |
| 212 |
<https://sourceware.org/ml/libc-alpha/2018-05/msg00125.html>. */ |
| 213 |
|
| 214 |
# define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.nds32_sp |
| 215 |
|
| 216 |
# elif defined __powerpc__ || defined __powerpc64__ || defined __powerpc64_elfv2__ |
| 217 |
|
| 218 |
/* See glibc/sysdeps/unix/sysv/linux/powerpc/sys/ucontext.h |
| 219 |
and the definition of GET_STACK in |
| 220 |
glibc/sysdeps/unix/sysv/linux/powerpc/sigcontextinfo.h. |
| 221 |
Note that the 'mcontext_t' defined in |
| 222 |
glibc/sysdeps/unix/sysv/linux/powerpc/sys/ucontext.h, |
| 223 |
the 'struct sigcontext' defined in <asm/sigcontext.h>, |
| 224 |
and the 'struct pt_regs' defined in <asm/ptrace.h> |
| 225 |
are quite different types. */ |
| 226 |
|
| 227 |
# if defined __powerpc64__ || defined __powerpc64_elfv2__ /* 64-bit */ |
| 228 |
# define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.gp_regs[1] |
| 229 |
# else /* 32-bit */ |
| 230 |
# if MUSL_LIBC |
| 231 |
/* musl libc has a different structure of ucontext_t in |
| 232 |
musl/arch/powerpc/bits/signal.h. */ |
| 233 |
/* The glibc comments say: |
| 234 |
"Different versions of the kernel have stored the registers on signal |
| 235 |
delivery at different offsets from the ucontext struct. Programs should |
| 236 |
thus use the uc_mcontext.uc_regs pointer to find where the registers are |
| 237 |
actually stored." */ |
| 238 |
# if 0 |
| 239 |
# define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.gregs[1] |
| 240 |
# else |
| 241 |
# define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_regs->gregs[1] |
| 242 |
# endif |
| 243 |
# else |
| 244 |
/* Assume the structure of ucontext_t in |
| 245 |
glibc/sysdeps/unix/sysv/linux/powerpc/sys/ucontext.h. */ |
| 246 |
/* Because of the union, both definitions should be equivalent. */ |
| 247 |
# if 0 |
| 248 |
# define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.regs->gpr[1] |
| 249 |
# else |
| 250 |
# define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.uc_regs->gregs[1] |
| 251 |
# endif |
| 252 |
# endif |
| 253 |
# endif |
| 254 |
|
| 255 |
# elif defined __riscv32__ || __riscv64__ |
| 256 |
|
| 257 |
/* See glibc/sysdeps/unix/sysv/linux/riscv/sys/ucontext.h |
| 258 |
and the definition of GET_STACK in |
| 259 |
glibc/sysdeps/unix/sysv/linux/riscv/sigcontextinfo.h. |
| 260 |
Note that the 'mcontext_t' defined in |
| 261 |
glibc/sysdeps/unix/sysv/linux/riscv/sys/ucontext.h |
| 262 |
and the 'struct sigcontext' defined in |
| 263 |
glibc/sysdeps/unix/sysv/linux/riscv/bits/sigcontext.h |
| 264 |
start with the same block of 32 general-purpose registers. */ |
| 265 |
|
| 266 |
# define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.__gregs[REG_SP] |
| 267 |
|
| 268 |
# elif defined __s390__ || defined __s390x__ |
| 269 |
|
| 270 |
/* See glibc/sysdeps/unix/sysv/linux/s390/sys/ucontext.h |
| 271 |
and the definition of GET_STACK in |
| 272 |
glibc/sysdeps/unix/sysv/linux/s390/sigcontextinfo.h. |
| 273 |
Note that the 'mcontext_t' defined in |
| 274 |
glibc/sysdeps/unix/sysv/linux/s390/sys/ucontext.h |
| 275 |
and the '_sigregs' type, indirect part of 'struct sigcontext', defined |
| 276 |
in <asm/sigcontext.h>, are effectively the same. */ |
| 277 |
|
| 278 |
# define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.gregs[15] |
| 279 |
|
| 280 |
# elif defined __sh__ |
| 281 |
|
| 282 |
/* See glibc/sysdeps/unix/sysv/linux/sh/sys/ucontext.h |
| 283 |
and the definition of GET_STACK in |
| 284 |
glibc/sysdeps/unix/sysv/linux/sh/sigcontextinfo.h. |
| 285 |
Note that the 'mcontext_t' defined in |
| 286 |
glibc/sysdeps/unix/sysv/linux/sh/sys/ucontext.h |
| 287 |
and the 'struct sigcontext' defined in <asm/sigcontext.h> |
| 288 |
are effectively the same. */ |
| 289 |
|
| 290 |
# define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.gregs[15] |
| 291 |
|
| 292 |
# elif defined __sparc__ || defined __sparc64__ |
| 293 |
|
| 294 |
/* See glibc/sysdeps/unix/sysv/linux/sparc/sys/ucontext.h |
| 295 |
and the definition of GET_STACK in |
| 296 |
glibc/sysdeps/unix/sysv/linux/sparc/{sparc32,sparc64}/sigcontextinfo.h. |
| 297 |
Note that the 'mcontext_t' defined in |
| 298 |
glibc/sysdeps/unix/sysv/linux/sparc/sys/ucontext.h |
| 299 |
and the 'struct sigcontext' defined in |
| 300 |
glibc/sysdeps/unix/sysv/linux/sparc/bits/sigcontext.h |
| 301 |
(see also <asm/sigcontext.h>) |
| 302 |
are quite different types. */ |
| 303 |
|
| 304 |
# if defined __sparc64__/* 64-bit */ |
| 305 |
/* From linux-4.8.1/arch/sparc/kernel/signal_64.c, function setup_rt_frame, we |
| 306 |
see that ucp is not an 'ucontext_t *' but rather a 'struct sigcontext *' |
| 307 |
that happens to have the same value as sip (which is possible because a |
| 308 |
'struct sigcontext' starts with 128 bytes room for the siginfo_t). */ |
| 309 |
# define SIGSEGV_FAULT_STACKPOINTER (((struct sigcontext *) ucp)->sigc_regs.u_regs[14] + 2047) |
| 310 |
# else /* 32-bit */ |
| 311 |
/* From linux-4.8.1/arch/sparc/kernel/signal_32.c, function setup_rt_frame, |
| 312 |
and linux-4.8.1/arch/sparc/kernel/signal32.c, function setup_rt_frame32, we |
| 313 |
see that ucp is a 'struct pt_regs *' or 'struct pt_regs32 *', respectively. |
| 314 |
In userland, this is a 'struct sigcontext *'. */ |
| 315 |
# define SIGSEGV_FAULT_STACKPOINTER ((struct sigcontext *) ucp)->si_regs.u_regs[14] |
| 316 |
# endif |
| 317 |
|
| 318 |
/* The sip->si_addr field is correct for a normal fault, but unusable in case |
| 319 |
of a stack overflow. What I observe (when running |
| 320 |
tests/test-sigsegv-catch-stackoverflow1, with a printf right at the beginning |
| 321 |
of sigsegv_handler) is that sip->si_addr is near 0: |
| 322 |
- in 64-bit mode: sip->si_addr = 0x000000000000030F, and gdb shows me that |
| 323 |
the fault occurs in an instruction 'stx %o3,[%fp+0x30f]' and %fp is 0. |
| 324 |
In fact, all registers %l0..%l7 and %i0..%i7 are 0. |
| 325 |
- in 32-bit mode: sip->si_addr = 0xFFFFFA64, and gdb shows me that |
| 326 |
the fault occurs in an instruction 'st %g2,[%fp-1436]' and %fp is 0. |
| 327 |
In fact, all registers %l0..%l7 and %i0..%i7 are 0. |
| 328 |
Apparently when the stack overflow occurred, some trap has tried to move the |
| 329 |
contents of the registers %l0..%l7 and %i0..%i7 (a "window" in SPARC |
| 330 |
terminology) to the stack, did not succeed in doing this, replaced all these |
| 331 |
register values with 0, and resumed execution at the fault location. This |
| 332 |
time, due to %fp = 0, a different fault was triggered. Now it is impossible |
| 333 |
to determine the real (previous) fault address because, even if know the |
| 334 |
faulting instruction, the previous register values have been lost. */ |
| 335 |
# define BOGUS_FAULT_ADDRESS_UPON_STACK_OVERFLOW |
| 336 |
|
| 337 |
# else |
| 338 |
|
| 339 |
/* When adding support for other CPUs here: */ |
| 340 |
|
| 341 |
/* For SIGSEGV_FAULT_HANDLER_ARGLIST, see the definition of SIGCONTEXT in |
| 342 |
glibc/sysdeps/unix/sysv/linux/<cpu>/sigcontextinfo.h. */ |
| 343 |
|
| 344 |
/* For SIGSEGV_FAULT_STACKPOINTER, see the definition of GET_STACK in |
| 345 |
glibc/sysdeps/unix/sysv/linux/<cpu>/sigcontextinfo.h. */ |
| 346 |
|
| 347 |
# endif |
| 348 |
|
| 349 |
#endif |
| 350 |
|
| 351 |
#if defined __ANDROID__ /* Android */ |
| 352 |
/* A platform that supports the POSIX:2008 (XPG 7) way, without |
| 353 |
'struct sigcontext' nor 'ucontext_t'. */ |
| 354 |
|
| 355 |
# define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, siginfo_t *sip, void *context |
| 356 |
# define SIGSEGV_FAULT_ADDRESS sip->si_addr |
| 357 |
# define SIGSEGV_FAULT_CONTEXT context |
| 358 |
# define SIGSEGV_FAULT_ADDRESS_FROM_SIGINFO |
| 359 |
|
| 360 |
#endif |
| 361 |
|
| 362 |
#if defined __gnu_hurd__ /* Hurd */ |
| 363 |
|
| 364 |
# define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, long code, struct sigcontext *scp |
| 365 |
# define SIGSEGV_FAULT_ADDRESS (unsigned long) code |
| 366 |
# define SIGSEGV_FAULT_CONTEXT scp |
| 367 |
|
| 368 |
# if defined __x86_64__ |
| 369 |
/* 64 bit registers */ |
| 370 |
|
| 371 |
/* scp points to a 'struct sigcontext' (defined in |
| 372 |
glibc/sysdeps/mach/hurd/x86_64/bits/sigcontext.h). |
| 373 |
The registers, at the moment the signal occurred, get pushed on the kernel |
| 374 |
stack through gnumach/x86_64/locore.S:alltraps. They are denoted by a |
| 375 |
'struct i386_saved_state' (defined in gnumach/i386/i386/thread.h). |
| 376 |
Upon invocation of the Mach interface function thread_get_state |
| 377 |
<https://www.gnu.org/software/hurd/gnumach-doc/Thread-Execution.html> |
| 378 |
(= __thread_get_state in glibc), defined in gnumach/kern/thread.c, |
| 379 |
the function thread_getstatus, defined in gnumach/i386/i386/pcb.c, copies the |
| 380 |
register values in a different arrangement into a 'struct i386_thread_state', |
| 381 |
defined in gnumach/i386/include/mach/i386/thread_status.h. (Different |
| 382 |
arrangement: trapno, err get dropped; different order of r8...r15; also rsp |
| 383 |
gets set to 0.) |
| 384 |
This 'struct i386_thread_state' is actually the 'basic' part of a |
| 385 |
'struct machine_thread_all_state', defined in |
| 386 |
glibc/sysdeps/mach/x86/thread_state.h. |
| 387 |
From there, the function _hurd_setup_sighandler, defined in |
| 388 |
glibc/sysdeps/mach/hurd/x86/trampoline.c, |
| 389 |
1. sets rsp to the same value as ursp, |
| 390 |
2. copies the 'struct i386_thread_state' into the appropriate part of a |
| 391 |
'struct sigcontext', defined in |
| 392 |
glibc/sysdeps/mach/hurd/x86_64/bits/sigcontext.h. */ |
| 393 |
/* Both sc_rsp and sc_ursp have the same value. |
| 394 |
It appears more reliable to use sc_ursp because sc_rsp is marked as |
| 395 |
"not used". */ |
| 396 |
# define SIGSEGV_FAULT_STACKPOINTER scp->sc_ursp |
| 397 |
|
| 398 |
# elif defined __i386__ |
| 399 |
/* 32 bit registers */ |
| 400 |
|
| 401 |
/* scp points to a 'struct sigcontext' (defined in |
| 402 |
glibc/sysdeps/mach/hurd/i386/bits/sigcontext.h). |
| 403 |
The registers, at the moment the signal occurred, get pushed on the kernel |
| 404 |
stack through gnumach/i386/i386/locore.S:alltraps. They are denoted by a |
| 405 |
'struct i386_saved_state' (defined in gnumach/i386/i386/thread.h). |
| 406 |
Upon invocation of the Mach interface function thread_get_state |
| 407 |
<https://www.gnu.org/software/hurd/gnumach-doc/Thread-Execution.html> |
| 408 |
(= __thread_get_state in glibc), defined in gnumach/kern/thread.c, |
| 409 |
the function thread_getstatus, defined in gnumach/i386/i386/pcb.c, copies the |
| 410 |
register values in a different arrangement into a 'struct i386_thread_state', |
| 411 |
defined in gnumach/i386/include/mach/i386/thread_status.h. (Different |
| 412 |
arrangement: trapno, err get dropped; also esp gets set to 0.) |
| 413 |
This 'struct i386_thread_state' is actually the 'basic' part of a |
| 414 |
'struct machine_thread_all_state', defined in |
| 415 |
glibc/sysdeps/mach/x86/thread_state.h. |
| 416 |
From there, the function _hurd_setup_sighandler, defined in |
| 417 |
glibc/sysdeps/mach/hurd/x86/trampoline.c, |
| 418 |
1. sets esp to the same value as uesp, |
| 419 |
2. copies the 'struct i386_thread_state' into the appropriate part of a |
| 420 |
'struct sigcontext', defined in |
| 421 |
glibc/sysdeps/mach/hurd/i386/bits/sigcontext.h. */ |
| 422 |
/* Both sc_esp and sc_uesp have the same value. |
| 423 |
It appears more reliable to use sc_uesp because sc_esp is marked as |
| 424 |
"not used". */ |
| 425 |
# define SIGSEGV_FAULT_STACKPOINTER scp->sc_uesp |
| 426 |
|
| 427 |
# endif |
| 428 |
|
| 429 |
#endif |
| 430 |
|
| 431 |
#if defined __FreeBSD_kernel__ || defined __FreeBSD__ || defined __DragonFly__ /* GNU/kFreeBSD, FreeBSD */ |
| 432 |
|
| 433 |
# if defined __arm__ || defined __armhf__ || (defined __arm64__ || defined __aarch64__) |
| 434 |
|
| 435 |
# define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, siginfo_t *sip, void *ucp |
| 436 |
# define SIGSEGV_FAULT_ADDRESS sip->si_addr |
| 437 |
# define SIGSEGV_FAULT_CONTEXT ((ucontext_t *) ucp) |
| 438 |
|
| 439 |
# if defined __arm64__ || defined __aarch64__ /* 64-bit */ |
| 440 |
|
| 441 |
/* See sys/arm64/include/ucontext.h. */ |
| 442 |
|
| 443 |
# if defined __CHERI_PURE_CAPABILITY__ |
| 444 |
# define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.mc_capregs.cap_sp |
| 445 |
# else |
| 446 |
# define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.mc_gpregs.gp_sp |
| 447 |
# endif |
| 448 |
|
| 449 |
# elif defined __arm__ || defined __armhf__ /* 32-bit */ |
| 450 |
|
| 451 |
/* See sys/arm/include/ucontext.h. */ |
| 452 |
|
| 453 |
# define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.__gregs[_REG_SP] |
| 454 |
|
| 455 |
# endif |
| 456 |
|
| 457 |
# else |
| 458 |
|
| 459 |
/* On FreeBSD 12, both of these approaches work. On FreeBSD derivatives, the |
| 460 |
first one has more chances to work. */ |
| 461 |
# if 1 |
| 462 |
/* Use signal handlers without SA_SIGINFO. */ |
| 463 |
|
| 464 |
# define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp, void *addr |
| 465 |
# define SIGSEGV_FAULT_ADDRESS addr |
| 466 |
# define SIGSEGV_FAULT_CONTEXT scp |
| 467 |
|
| 468 |
/* See sys/x86/include/signal.h. */ |
| 469 |
|
| 470 |
# if defined __x86_64__ |
| 471 |
/* 64 bit registers */ |
| 472 |
|
| 473 |
# define SIGSEGV_FAULT_STACKPOINTER scp->sc_rsp |
| 474 |
|
| 475 |
# elif defined __i386__ |
| 476 |
/* 32 bit registers */ |
| 477 |
|
| 478 |
# define SIGSEGV_FAULT_STACKPOINTER scp->sc_esp |
| 479 |
|
| 480 |
# endif |
| 481 |
|
| 482 |
# else |
| 483 |
/* Use signal handlers with SA_SIGINFO. */ |
| 484 |
|
| 485 |
# define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, siginfo_t *sip, void *scp |
| 486 |
# define SIGSEGV_FAULT_ADDRESS sip->si_addr |
| 487 |
# define SIGSEGV_FAULT_CONTEXT ((struct sigcontext *) scp) |
| 488 |
# define SIGSEGV_FAULT_ADDRESS_FROM_SIGINFO |
| 489 |
|
| 490 |
/* See sys/x86/include/signal.h. */ |
| 491 |
|
| 492 |
# if defined __x86_64__ |
| 493 |
/* 64 bit registers */ |
| 494 |
|
| 495 |
# define SIGSEGV_FAULT_STACKPOINTER ((struct sigcontext *) scp)->sc_rsp |
| 496 |
|
| 497 |
# elif defined __i386__ |
| 498 |
/* 32 bit registers */ |
| 499 |
|
| 500 |
# define SIGSEGV_FAULT_STACKPOINTER ((struct sigcontext *) scp)->sc_esp |
| 501 |
|
| 502 |
# endif |
| 503 |
|
| 504 |
# endif |
| 505 |
|
| 506 |
# endif |
| 507 |
|
| 508 |
#endif |
| 509 |
|
| 510 |
#if defined __NetBSD__ /* NetBSD */ |
| 511 |
|
| 512 |
# define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, siginfo_t *sip, void *ucp |
| 513 |
# define SIGSEGV_FAULT_ADDRESS sip->si_addr |
| 514 |
# define SIGSEGV_FAULT_CONTEXT ((ucontext_t *) ucp) |
| 515 |
# define SIGSEGV_FAULT_ADDRESS_FROM_SIGINFO |
| 516 |
|
| 517 |
/* _UC_MACHINE_SP is a platform independent macro. |
| 518 |
Defined in <machine/mcontext.h>, see |
| 519 |
https://cvsweb.netbsd.org/bsdweb.cgi/src/sys/arch/$arch/include/mcontext.h |
| 520 |
Supported on alpha, amd64, i386, ia64, m68k, mips, powerpc, sparc since |
| 521 |
NetBSD 2.0. |
| 522 |
On i386, _UC_MACHINE_SP is the same as ->uc_mcontext.__gregs[_REG_UESP], |
| 523 |
and apparently the same value as ->uc_mcontext.__gregs[_REG_ESP]. */ |
| 524 |
# ifdef _UC_MACHINE_SP |
| 525 |
# define SIGSEGV_FAULT_STACKPOINTER _UC_MACHINE_SP ((ucontext_t *) ucp) |
| 526 |
# endif |
| 527 |
|
| 528 |
#endif |
| 529 |
|
| 530 |
#if defined __OpenBSD__ /* OpenBSD */ |
| 531 |
|
| 532 |
# define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, siginfo_t *sip, struct sigcontext *scp |
| 533 |
# define SIGSEGV_FAULT_ADDRESS sip->si_addr |
| 534 |
# define SIGSEGV_FAULT_CONTEXT scp |
| 535 |
# define SIGSEGV_FAULT_ADDRESS_FROM_SIGINFO |
| 536 |
|
| 537 |
# if defined __alpha__ |
| 538 |
|
| 539 |
/* See the definition of 'struct sigcontext' in |
| 540 |
openbsd-src/sys/arch/alpha/include/signal.h. */ |
| 541 |
|
| 542 |
# define SIGSEGV_FAULT_STACKPOINTER scp->sc_regs[30] |
| 543 |
|
| 544 |
# elif defined __arm__ || defined __armhf__ |
| 545 |
|
| 546 |
/* See the definition of 'struct sigcontext' in |
| 547 |
openbsd-src/sys/arch/arm/include/signal.h. */ |
| 548 |
|
| 549 |
# define SIGSEGV_FAULT_STACKPOINTER scp->sc_usr_sp |
| 550 |
|
| 551 |
# elif defined __hppa__ || defined __hppa64__ |
| 552 |
|
| 553 |
/* See the definition of 'struct sigcontext' in |
| 554 |
openbsd-src/sys/arch/hppa/include/signal.h |
| 555 |
and |
| 556 |
openbsd-src/sys/arch/hppa64/include/signal.h. */ |
| 557 |
|
| 558 |
# define SIGSEGV_FAULT_STACKPOINTER scp->sc_regs[30] |
| 559 |
|
| 560 |
# elif defined __x86_64__ |
| 561 |
/* 64 bit registers */ |
| 562 |
|
| 563 |
/* See the definition of 'struct sigcontext' in |
| 564 |
openbsd-src/sys/arch/amd64/include/signal.h. */ |
| 565 |
|
| 566 |
# define SIGSEGV_FAULT_STACKPOINTER scp->sc_rsp |
| 567 |
|
| 568 |
# elif defined __i386__ |
| 569 |
/* 32 bit registers */ |
| 570 |
|
| 571 |
/* See the definition of 'struct sigcontext' in |
| 572 |
openbsd-src/sys/arch/i386/include/signal.h. */ |
| 573 |
|
| 574 |
# define SIGSEGV_FAULT_STACKPOINTER scp->sc_esp |
| 575 |
|
| 576 |
# elif defined __m68k__ |
| 577 |
|
| 578 |
/* See the definition of 'struct sigcontext' in |
| 579 |
openbsd-src/sys/arch/m68k/include/signal.h. */ |
| 580 |
|
| 581 |
# define SIGSEGV_FAULT_STACKPOINTER scp->sc_sp |
| 582 |
|
| 583 |
# elif defined __m88k__ |
| 584 |
|
| 585 |
/* See the definition of 'struct sigcontext' in |
| 586 |
openbsd-src/sys/arch/m88k/include/signal.h |
| 587 |
and the definition of 'struct reg' in |
| 588 |
openbsd-src/sys/arch/m88k/include/reg.h. */ |
| 589 |
|
| 590 |
# if OpenBSD >= 201211 /* OpenBSD version >= 5.2 */ |
| 591 |
# define SIGSEGV_FAULT_STACKPOINTER scp->sc_regs[31] |
| 592 |
# else |
| 593 |
# define SIGSEGV_FAULT_STACKPOINTER scp->sc_regs.r[31] |
| 594 |
# endif |
| 595 |
|
| 596 |
# elif defined __mips__ || defined __mipsn32__ || defined __mips64__ |
| 597 |
|
| 598 |
/* See the definition of 'struct sigcontext' in |
| 599 |
openbsd-src/sys/arch/mips64/include/signal.h. */ |
| 600 |
|
| 601 |
# define SIGSEGV_FAULT_STACKPOINTER scp->sc_regs[29] |
| 602 |
|
| 603 |
# elif defined __powerpc64__ |
| 604 |
|
| 605 |
/* See the definition of 'struct sigcontext' in |
| 606 |
openbsd-src/sys/arch/powerpc64/include/signal.h. */ |
| 607 |
|
| 608 |
# define SIGSEGV_FAULT_STACKPOINTER scp->sc_sp |
| 609 |
|
| 610 |
# elif defined __powerpc__ |
| 611 |
|
| 612 |
/* See the definition of 'struct sigcontext' and 'struct trapframe' in |
| 613 |
openbsd-src/sys/arch/powerpc/include/signal.h. */ |
| 614 |
|
| 615 |
# define SIGSEGV_FAULT_STACKPOINTER scp->sc_frame.fixreg[1] |
| 616 |
|
| 617 |
# elif defined __sh__ |
| 618 |
|
| 619 |
/* See the definition of 'struct sigcontext' in |
| 620 |
openbsd-src/sys/arch/sh/include/signal.h |
| 621 |
and the definition of 'struct reg' in |
| 622 |
openbsd-src/sys/arch/sh/include/reg.h. */ |
| 623 |
|
| 624 |
# if OpenBSD >= 201211 /* OpenBSD version >= 5.2 */ |
| 625 |
# define SIGSEGV_FAULT_STACKPOINTER scp->sc_reg[20-15] |
| 626 |
# else |
| 627 |
# define SIGSEGV_FAULT_STACKPOINTER scp->sc_reg.r_r15 |
| 628 |
# endif |
| 629 |
|
| 630 |
# elif defined __sparc__ || defined __sparc64__ |
| 631 |
|
| 632 |
/* See the definition of 'struct sigcontext' in |
| 633 |
openbsd-src/sys/arch/sparc/include/signal.h |
| 634 |
and |
| 635 |
openbsd-src/sys/arch/sparc64/include/signal.h. */ |
| 636 |
|
| 637 |
# define SIGSEGV_FAULT_STACKPOINTER scp->sc_sp |
| 638 |
|
| 639 |
# elif defined __vax__ |
| 640 |
|
| 641 |
/* See the definition of 'struct sigcontext' in |
| 642 |
openbsd-src/sys/arch/vax/include/signal.h. */ |
| 643 |
|
| 644 |
# define SIGSEGV_FAULT_STACKPOINTER scp->sc_sp |
| 645 |
|
| 646 |
# endif |
| 647 |
|
| 648 |
#endif |
| 649 |
|
| 650 |
#if (defined __APPLE__ && defined __MACH__) /* macOS */ |
| 651 |
|
| 652 |
# define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, siginfo_t *sip, void *ucp |
| 653 |
# define SIGSEGV_FAULT_ADDRESS sip->si_addr |
| 654 |
# define SIGSEGV_FAULT_CONTEXT ((ucontext_t *) ucp) |
| 655 |
# define SIGSEGV_FAULT_ADDRESS_FROM_SIGINFO |
| 656 |
|
| 657 |
# if defined __x86_64__ |
| 658 |
|
| 659 |
/* See the definitions of |
| 660 |
- 'ucontext_t' and 'struct __darwin_ucontext' in <sys/_types/_ucontext.h>, |
| 661 |
- 'struct __darwin_mcontext64' in <i386/_mcontext.h>, and |
| 662 |
- 'struct __darwin_x86_thread_state64' in <mach/i386/_structs.h>. */ |
| 663 |
# define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext->__ss.__rsp |
| 664 |
|
| 665 |
# elif defined __i386__ |
| 666 |
|
| 667 |
/* See the definitions of |
| 668 |
- 'ucontext_t' and 'struct __darwin_ucontext' in <sys/_types/_ucontext.h>, |
| 669 |
- 'struct __darwin_mcontext32' in <i386/_mcontext.h>, and |
| 670 |
- 'struct __darwin_i386_thread_state' in <mach/i386/_structs.h>. */ |
| 671 |
# define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext->__ss.__esp |
| 672 |
|
| 673 |
# elif defined __arm64__ |
| 674 |
|
| 675 |
/* See the definitions of |
| 676 |
- 'ucontext_t' and 'struct __darwin_ucontext' in <sys/_types/_ucontext.h>, |
| 677 |
- 'struct __darwin_mcontext64' in <arm/_mcontext.h>, and |
| 678 |
- 'struct __darwin_arm_thread_state64' in <mach/arm/_structs.h>. */ |
| 679 |
# define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext->__ss.__sp |
| 680 |
|
| 681 |
# elif defined __powerpc__ |
| 682 |
|
| 683 |
/* See the definitions of |
| 684 |
- 'ucontext_t' and 'struct __darwin_ucontext' in <sys/_structs.h>, |
| 685 |
- 'struct __darwin_mcontext' in <ppc/_structs.h>, and |
| 686 |
- 'struct __darwin_ppc_thread_state' in <mach/ppc/_structs.h>. */ |
| 687 |
# define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext->__ss.__r1 |
| 688 |
|
| 689 |
# endif |
| 690 |
|
| 691 |
#endif |
| 692 |
|
| 693 |
#if defined _AIX /* AIX */ |
| 694 |
|
| 695 |
# define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, siginfo_t *sip, void *ucp |
| 696 |
# define SIGSEGV_FAULT_ADDRESS sip->si_addr |
| 697 |
# define SIGSEGV_FAULT_CONTEXT ((ucontext_t *) ucp) |
| 698 |
# define SIGSEGV_FAULT_ADDRESS_FROM_SIGINFO |
| 699 |
|
| 700 |
# if defined __powerpc__ || defined __powerpc64__ |
| 701 |
# define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.jmp_context.gpr[1] |
| 702 |
# endif |
| 703 |
|
| 704 |
#endif |
| 705 |
|
| 706 |
#if defined __sgi /* IRIX */ |
| 707 |
|
| 708 |
# define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp |
| 709 |
# define SIGSEGV_FAULT_ADDRESS (unsigned long) scp->sc_badvaddr |
| 710 |
# define SIGSEGV_FAULT_CONTEXT scp |
| 711 |
|
| 712 |
# if defined __mips__ || defined __mipsn32__ || defined __mips64__ |
| 713 |
# define SIGSEGV_FAULT_STACKPOINTER scp->sc_regs[29] |
| 714 |
# endif |
| 715 |
|
| 716 |
#endif |
| 717 |
|
| 718 |
#if defined __sun /* Solaris */ |
| 719 |
|
| 720 |
# define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, siginfo_t *sip, void *ucp |
| 721 |
# define SIGSEGV_FAULT_ADDRESS sip->si_addr |
| 722 |
# define SIGSEGV_FAULT_CONTEXT ((ucontext_t *) ucp) |
| 723 |
# define SIGSEGV_FAULT_ADDRESS_FROM_SIGINFO |
| 724 |
|
| 725 |
# if defined __x86_64__ |
| 726 |
/* 64 bit registers */ |
| 727 |
|
| 728 |
# define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.gregs[REG_RSP] |
| 729 |
|
| 730 |
# elif defined __i386__ |
| 731 |
/* 32 bit registers */ |
| 732 |
|
| 733 |
# define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.gregs[ESP] |
| 734 |
|
| 735 |
# elif defined __sparc__ || defined __sparc64__ |
| 736 |
|
| 737 |
# define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.gregs[REG_O6] |
| 738 |
|
| 739 |
# if SOLARIS11 |
| 740 |
|
| 741 |
/* On Solaris 11.3/SPARC, both in 32-bit and 64-bit mode, when catching |
| 742 |
stack overflow, the fault address is correct the first time, but is zero |
| 743 |
or near zero the second time. |
| 744 |
'truss tests/test-sigsegv-catch-stackoverflow1' shows it: |
| 745 |
|
| 746 |
In 32-bit mode: |
| 747 |
|
| 748 |
Incurred fault #6, FLTBOUNDS %pc = 0x000116E8 |
| 749 |
siginfo: SIGSEGV SEGV_MAPERR addr=0xFFB00000 |
| 750 |
Received signal #11, SIGSEGV [caught] |
| 751 |
siginfo: SIGSEGV SEGV_MAPERR addr=0xFFB00000 |
| 752 |
then |
| 753 |
Incurred fault #6, FLTBOUNDS %pc = 0x000116E8 |
| 754 |
siginfo: SIGSEGV SEGV_MAPERR addr=0x00000008 |
| 755 |
Received signal #11, SIGSEGV [caught] |
| 756 |
siginfo: SIGSEGV SEGV_MAPERR addr=0x00000008 |
| 757 |
|
| 758 |
In 64-bit mode: |
| 759 |
|
| 760 |
Incurred fault #6, FLTBOUNDS %pc = 0x100001C58 |
| 761 |
siginfo: SIGSEGV SEGV_MAPERR addr=0xFFFFFFFF7FF00000 |
| 762 |
Received signal #11, SIGSEGV [caught] |
| 763 |
siginfo: SIGSEGV SEGV_MAPERR addr=0xFFFFFFFF7FF00000 |
| 764 |
then |
| 765 |
Incurred fault #6, FLTBOUNDS %pc = 0x100001C58 |
| 766 |
siginfo: SIGSEGV SEGV_MAPERR addr=0x00000000 |
| 767 |
Received signal #11, SIGSEGV [caught] |
| 768 |
siginfo: SIGSEGV SEGV_MAPERR addr=0x00000000 |
| 769 |
*/ |
| 770 |
# define BOGUS_FAULT_ADDRESS_UPON_STACK_OVERFLOW |
| 771 |
|
| 772 |
# endif |
| 773 |
|
| 774 |
# endif |
| 775 |
|
| 776 |
#endif |
| 777 |
|
| 778 |
#if defined __CYGWIN__ /* Cygwin */ |
| 779 |
|
| 780 |
# define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, siginfo_t *sip, void *ucp |
| 781 |
# define SIGSEGV_FAULT_ADDRESS sip->si_addr |
| 782 |
# define SIGSEGV_FAULT_CONTEXT ((ucontext_t *) ucp) |
| 783 |
# define SIGSEGV_FAULT_ADDRESS_FROM_SIGINFO |
| 784 |
|
| 785 |
/* See the definition of 'ucontext_t' in <sys/ucontext.h> and |
| 786 |
of 'struct __mcontext' in <cygwin/signal.h>. */ |
| 787 |
# if defined __x86_64__ |
| 788 |
/* 64 bit registers */ |
| 789 |
# define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.rsp |
| 790 |
# elif defined __i386__ |
| 791 |
/* 32 bit registers */ |
| 792 |
# define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.esp |
| 793 |
# endif |
| 794 |
|
| 795 |
#endif |
| 796 |
|
| 797 |
#if defined __HAIKU__ /* Haiku */ |
| 798 |
|
| 799 |
# define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, siginfo_t *sip, void *ucp |
| 800 |
# define SIGSEGV_FAULT_ADDRESS sip->si_addr |
| 801 |
# define SIGSEGV_FAULT_CONTEXT ((ucontext_t *) ucp) |
| 802 |
# define SIGSEGV_FAULT_ADDRESS_FROM_SIGINFO |
| 803 |
|
| 804 |
# if defined __x86_64__ |
| 805 |
/* 64 bit registers */ |
| 806 |
|
| 807 |
/* See the definition of 'ucontext_t' in <signal.h> and |
| 808 |
of 'struct vregs' in <arch/x86_64/signal.h>. */ |
| 809 |
|
| 810 |
# define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.rsp |
| 811 |
|
| 812 |
# elif defined __i386__ |
| 813 |
/* 32 bit registers */ |
| 814 |
|
| 815 |
/* See the definition of 'ucontext_t' in <signal.h> and |
| 816 |
of 'struct vregs' in <arch/x86/signal.h>. */ |
| 817 |
|
| 818 |
# define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.esp |
| 819 |
|
| 820 |
# endif |
| 821 |
|
| 822 |
#endif |
| 823 |
|
| 824 |
/* ========================================================================== */ |
| 825 |
|
| 826 |
/* List of signals that are sent when an invalid virtual memory address |
| 827 |
is accessed, or when the stack overflows. */ |
| 828 |
#if defined __gnu_hurd__ \ |
| 829 |
|| defined __FreeBSD_kernel__ || defined __FreeBSD__ || defined __DragonFly__ \ |
| 830 |
|| defined __NetBSD__ || defined __OpenBSD__ \ |
| 831 |
|| (defined __APPLE__ && defined __MACH__) |
| 832 |
# if defined __CHERI__ |
| 833 |
# define SIGSEGV_FOR_ALL_SIGNALS(var,body) \ |
| 834 |
{ int var; \ |
| 835 |
var = SIGSEGV; { body } \ |
| 836 |
var = SIGBUS; { body } \ |
| 837 |
var = SIGPROT; { body } \ |
| 838 |
} |
| 839 |
# else |
| 840 |
# define SIGSEGV_FOR_ALL_SIGNALS(var,body) \ |
| 841 |
{ int var; \ |
| 842 |
var = SIGSEGV; { body } \ |
| 843 |
var = SIGBUS; { body } \ |
| 844 |
} |
| 845 |
# endif |
| 846 |
#else |
| 847 |
# define SIGSEGV_FOR_ALL_SIGNALS(var,body) \ |
| 848 |
{ int var; var = SIGSEGV; { body } } |
| 849 |
#endif |
| 850 |
|
| 851 |
/* ========================================================================== */ |
| 852 |
|
| 853 |
/* Determine the virtual memory area of a given address. */ |
| 854 |
#include "stackvma.h" |
| 855 |
|
| 856 |
/* ========================================================================== */ |
| 857 |
|
| 858 |
/* On the average Unix platform, we define |
| 859 |
|
| 860 |
HAVE_SIGSEGV_RECOVERY |
| 861 |
if there is a fault-*.h include file which defines |
| 862 |
SIGSEGV_FAULT_HANDLER_ARGLIST and SIGSEGV_FAULT_ADDRESS. |
| 863 |
|
| 864 |
HAVE_STACK_OVERFLOW_RECOVERY |
| 865 |
if HAVE_SIGALTSTACK is set and |
| 866 |
at least two of the following are true: |
| 867 |
A) There is a fault-*.h include file which defines |
| 868 |
SIGSEGV_FAULT_HANDLER_ARGLIST and SIGSEGV_FAULT_ADDRESS. |
| 869 |
B) There is a fault-*.h include file which defines |
| 870 |
SIGSEGV_FAULT_HANDLER_ARGLIST and SIGSEGV_FAULT_STACKPOINTER. |
| 871 |
C) There is a stackvma-*.c, other than stackvma-none.c, which |
| 872 |
defines sigsegv_get_vma. |
| 873 |
|
| 874 |
Why? Obviously, to catch stack overflow, we need an alternate signal |
| 875 |
stack; this requires kernel support. But we also need to distinguish |
| 876 |
(with a reasonable confidence) a stack overflow from a regular SIGSEGV. |
| 877 |
If we have A) and B), we use the |
| 878 |
Heuristic AB: If the fault address is near the stack pointer, it's a |
| 879 |
stack overflow. |
| 880 |
If we have A) and C), we use the |
| 881 |
Heuristic AC: If the fault address is near and beyond the bottom of |
| 882 |
the stack's virtual memory area, it's a stack overflow. |
| 883 |
If we have B) and C), we use the |
| 884 |
Heuristic BC: If the stack pointer is near the bottom of the stack's |
| 885 |
virtual memory area, it's a stack overflow. |
| 886 |
This heuristic comes in two flavours: On OSes which let the stack's |
| 887 |
VMA grow continuously, we determine the bottom by use of getrlimit(). |
| 888 |
On OSes which preallocate the stack's VMA with its maximum size |
| 889 |
(like BeOS), we use the stack's VMA directly. |
| 890 |
*/ |
| 891 |
|
| 892 |
#if HAVE_SIGSEGV_RECOVERY \ |
| 893 |
&& !(defined SIGSEGV_FAULT_HANDLER_ARGLIST && defined SIGSEGV_FAULT_ADDRESS) |
| 894 |
# error "You need to define SIGSEGV_FAULT_HANDLER_ARGLIST and SIGSEGV_FAULT_ADDRESS before you can define HAVE_SIGSEGV_RECOVERY." |
| 895 |
#endif |
| 896 |
#if !HAVE_SIGSEGV_RECOVERY \ |
| 897 |
&& (defined SIGSEGV_FAULT_HANDLER_ARGLIST && defined SIGSEGV_FAULT_ADDRESS) \ |
| 898 |
&& !(defined __FreeBSD__ && (defined __sparc__ || defined __sparc64__)) |
| 899 |
# if __GNUC__ || (__clang_major__ >= 4) |
| 900 |
# warning "You can define HAVE_SIGSEGV_RECOVERY on this platform." |
| 901 |
# else |
| 902 |
# error "You can define HAVE_SIGSEGV_RECOVERY on this platform." |
| 903 |
# endif |
| 904 |
#endif |
| 905 |
|
| 906 |
#if HAVE_STACK_OVERFLOW_RECOVERY \ |
| 907 |
&& !(defined SIGSEGV_FAULT_ADDRESS + defined SIGSEGV_FAULT_STACKPOINTER + HAVE_STACKVMA >= 2) |
| 908 |
# error "You need to define two of SIGSEGV_FAULT_ADDRESS, SIGSEGV_FAULT_STACKPOINTER, HAVE_STACKVMA, before you can define HAVE_STACK_OVERFLOW_RECOVERY." |
| 909 |
#endif |
| 910 |
#if !HAVE_STACK_OVERFLOW_RECOVERY \ |
| 911 |
&& (defined SIGSEGV_FAULT_ADDRESS + defined SIGSEGV_FAULT_STACKPOINTER + HAVE_STACKVMA >= 2) \ |
| 912 |
&& !(defined __FreeBSD__ && (defined __sparc__ || defined __sparc64__)) \ |
| 913 |
&& !(defined __NetBSD__ && (defined __sparc__ || defined __sparc64__)) |
| 914 |
# if __GNUC__ || (__clang_major__ >= 4) |
| 915 |
# warning "You can define HAVE_STACK_OVERFLOW_RECOVERY on this platform." |
| 916 |
# else |
| 917 |
# error "You can define HAVE_STACK_OVERFLOW_RECOVERY on this platform." |
| 918 |
# endif |
| 919 |
#endif |
| 920 |
|
| 921 |
/* ========================================================================== */ |
| 922 |
|
| 923 |
#if HAVE_STACK_OVERFLOW_RECOVERY |
| 924 |
|
| 925 |
/* ======= Leaving a signal handler executing on the alternate stack ======= */ |
| 926 |
|
| 927 |
/* Platform dependent: |
| 928 |
Leaving a signal handler executing on the alternate stack. */ |
| 929 |
static void sigsegv_reset_onstack_flag (void); |
| 930 |
|
| 931 |
/* -------------------------- leave-sigaltstack.c -------------------------- */ |
| 932 |
|
| 933 |
# if defined __gnu_hurd__ \ |
| 934 |
|| defined __FreeBSD_kernel__ || defined __FreeBSD__ || defined __DragonFly__ \ |
| 935 |
|| defined __NetBSD__ || defined __OpenBSD__ |
| 936 |
|
| 937 |
static void |
| 938 |
sigsegv_reset_onstack_flag (void) |
| 939 |
{ |
| 940 |
stack_t ss; |
| 941 |
|
| 942 |
if (sigaltstack (NULL, &ss) >= 0) |
| 943 |
{ |
| 944 |
ss.ss_flags &= ~SS_ONSTACK; |
| 945 |
sigaltstack (&ss, NULL); |
| 946 |
} |
| 947 |
} |
| 948 |
|
| 949 |
/* --------------------------- leave-setcontext.c --------------------------- */ |
| 950 |
|
| 951 |
# elif defined __sgi || defined __sun /* IRIX, Solaris */ |
| 952 |
|
| 953 |
# include <ucontext.h> |
| 954 |
|
| 955 |
static void |
| 956 |
sigsegv_reset_onstack_flag (void) |
| 957 |
{ |
| 958 |
ucontext_t uc; |
| 959 |
|
| 960 |
if (getcontext (&uc) >= 0) |
| 961 |
/* getcontext returns twice. We are interested in the returned context |
| 962 |
only the first time, i.e. when the SS_ONSTACK bit is set. */ |
| 963 |
if (uc.uc_stack.ss_flags & SS_ONSTACK) |
| 964 |
{ |
| 965 |
uc.uc_stack.ss_flags &= ~SS_ONSTACK; |
| 966 |
/* Note that setcontext() does not refill uc. Therefore if |
| 967 |
setcontext() keeps SS_ONSTACK set in the kernel, either |
| 968 |
setcontext() will return -1 or getcontext() will return a |
| 969 |
second time, with the SS_ONSTACK bit being cleared. */ |
| 970 |
setcontext (&uc); |
| 971 |
} |
| 972 |
} |
| 973 |
|
| 974 |
/* ------------------------------ leave-nop.c ------------------------------ */ |
| 975 |
|
| 976 |
# else |
| 977 |
|
| 978 |
static void |
| 979 |
sigsegv_reset_onstack_flag (void) |
| 980 |
{ |
| 981 |
/* Nothing to do. sigaltstack() simply looks at the stack pointer, |
| 982 |
therefore SS_ONSTACK is not sticky. */ |
| 983 |
} |
| 984 |
|
| 985 |
# endif |
| 986 |
|
| 987 |
/* ========================================================================== */ |
| 988 |
|
| 989 |
# if HAVE_STACKVMA |
| 990 |
|
| 991 |
/* Address of the last byte belonging to the stack vma. */ |
| 992 |
static uintptr_t stack_top = 0; |
| 993 |
|
| 994 |
/* Needs to be called once only. */ |
| 995 |
static void |
| 996 |
remember_stack_top (void *some_variable_on_stack) |
| 997 |
{ |
| 998 |
struct vma_struct vma; |
| 999 |
|
| 1000 |
if (sigsegv_get_vma ((uintptr_t) some_variable_on_stack, &vma) >= 0) |
| 1001 |
stack_top = vma.end - 1; |
| 1002 |
} |
| 1003 |
|
| 1004 |
# endif /* HAVE_STACKVMA */ |
| 1005 |
|
| 1006 |
static stackoverflow_handler_t stk_user_handler = (stackoverflow_handler_t)NULL; |
| 1007 |
static uintptr_t stk_extra_stack; |
| 1008 |
static size_t stk_extra_stack_size; |
| 1009 |
|
| 1010 |
#endif /* HAVE_STACK_OVERFLOW_RECOVERY */ |
| 1011 |
|
| 1012 |
#if HAVE_SIGSEGV_RECOVERY |
| 1013 |
|
| 1014 |
/* User's SIGSEGV handler. */ |
| 1015 |
static sigsegv_handler_t user_handler = (sigsegv_handler_t)NULL; |
| 1016 |
|
| 1017 |
#endif /* HAVE_SIGSEGV_RECOVERY */ |
| 1018 |
|
| 1019 |
|
| 1020 |
/* Our SIGSEGV handler, with OS dependent argument list. */ |
| 1021 |
|
| 1022 |
#if HAVE_SIGSEGV_RECOVERY |
| 1023 |
|
| 1024 |
static void |
| 1025 |
sigsegv_handler (SIGSEGV_FAULT_HANDLER_ARGLIST) |
| 1026 |
{ |
| 1027 |
void *address = (void *) (SIGSEGV_FAULT_ADDRESS); |
| 1028 |
|
| 1029 |
# if HAVE_STACK_OVERFLOW_RECOVERY |
| 1030 |
# if !(HAVE_STACKVMA || defined SIGSEGV_FAULT_STACKPOINTER) |
| 1031 |
#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!" |
| 1032 |
# endif |
| 1033 |
|
| 1034 |
/* Call user's handler. */ |
| 1035 |
if (user_handler && (*user_handler) (address, 0)) |
| 1036 |
{ |
| 1037 |
/* Handler successful. */ |
| 1038 |
} |
| 1039 |
else |
| 1040 |
{ |
| 1041 |
/* Handler declined responsibility. */ |
| 1042 |
|
| 1043 |
/* Did the user install a stack overflow handler? */ |
| 1044 |
if (stk_user_handler) |
| 1045 |
{ |
| 1046 |
/* See whether it was a stack overflow. If so, longjump away. */ |
| 1047 |
# ifdef SIGSEGV_FAULT_STACKPOINTER |
| 1048 |
uintptr_t old_sp = (uintptr_t) (SIGSEGV_FAULT_STACKPOINTER); |
| 1049 |
# ifdef __ia64 |
| 1050 |
uintptr_t old_bsp = (uintptr_t) (SIGSEGV_FAULT_BSP_POINTER); |
| 1051 |
# endif |
| 1052 |
# endif |
| 1053 |
|
| 1054 |
# if HAVE_STACKVMA |
| 1055 |
/* Were we able to determine the stack top? */ |
| 1056 |
if (stack_top) |
| 1057 |
{ |
| 1058 |
/* Determine stack bounds. */ |
| 1059 |
int saved_errno; |
| 1060 |
struct vma_struct vma; |
| 1061 |
int ret; |
| 1062 |
|
| 1063 |
saved_errno = errno; |
| 1064 |
ret = sigsegv_get_vma (stack_top, &vma); |
| 1065 |
errno = saved_errno; |
| 1066 |
if (ret >= 0) |
| 1067 |
{ |
| 1068 |
# ifndef BOGUS_FAULT_ADDRESS_UPON_STACK_OVERFLOW |
| 1069 |
/* Heuristic AC: If the fault_address is nearer to the stack |
| 1070 |
segment's [start,end] than to the previous segment, we |
| 1071 |
consider it a stack overflow. |
| 1072 |
In the case of IA-64, we know that the previous segment |
| 1073 |
is the up-growing bsp segment, and either of the two |
| 1074 |
stacks can overflow. */ |
| 1075 |
uintptr_t addr = (uintptr_t) address; |
| 1076 |
|
| 1077 |
# ifdef __ia64 |
| 1078 |
if (addr >= vma.prev_end && addr <= vma.end - 1) |
| 1079 |
# else |
| 1080 |
# if STACK_DIRECTION < 0 |
| 1081 |
if (addr >= vma.start |
| 1082 |
? (addr <= vma.end - 1) |
| 1083 |
: vma.is_near_this (addr, &vma)) |
| 1084 |
# else |
| 1085 |
if (addr <= vma.end - 1 |
| 1086 |
? (addr >= vma.start) |
| 1087 |
: vma.is_near_this (addr, &vma)) |
| 1088 |
# endif |
| 1089 |
# endif |
| 1090 |
{ |
| 1091 |
# else /* BOGUS_FAULT_ADDRESS_UPON_STACK_OVERFLOW */ |
| 1092 |
# if HAVE_GETRLIMIT && defined RLIMIT_STACK |
| 1093 |
/* Heuristic BC: If the stack size has reached its maximal size, |
| 1094 |
and old_sp is near the low end, we consider it a stack |
| 1095 |
overflow. */ |
| 1096 |
struct rlimit rl; |
| 1097 |
|
| 1098 |
saved_errno = errno; |
| 1099 |
ret = getrlimit (RLIMIT_STACK, &rl); |
| 1100 |
errno = saved_errno; |
| 1101 |
if (ret >= 0) |
| 1102 |
{ |
| 1103 |
uintptr_t current_stack_size = vma.end - vma.start; |
| 1104 |
uintptr_t max_stack_size = rl.rlim_cur; |
| 1105 |
if (current_stack_size <= max_stack_size + 4096 |
| 1106 |
&& max_stack_size <= current_stack_size + 4096 |
| 1107 |
# else |
| 1108 |
{ |
| 1109 |
if (1 |
| 1110 |
# endif |
| 1111 |
# ifdef SIGSEGV_FAULT_STACKPOINTER |
| 1112 |
/* Heuristic BC: If we know old_sp, and it is neither |
| 1113 |
near the low end, nor in the alternate stack, then |
| 1114 |
it's probably not a stack overflow. */ |
| 1115 |
&& ((old_sp >= stk_extra_stack |
| 1116 |
&& old_sp <= stk_extra_stack + stk_extra_stack_size) |
| 1117 |
# if STACK_DIRECTION < 0 |
| 1118 |
|| (old_sp <= vma.start + 4096 |
| 1119 |
&& vma.start <= old_sp + 4096)) |
| 1120 |
# else |
| 1121 |
|| (old_sp <= vma.end + 4096 |
| 1122 |
&& vma.end <= old_sp + 4096)) |
| 1123 |
# endif |
| 1124 |
# endif |
| 1125 |
) |
| 1126 |
# endif /* BOGUS_FAULT_ADDRESS_UPON_STACK_OVERFLOW */ |
| 1127 |
# else /* !HAVE_STACKVMA */ |
| 1128 |
/* Heuristic AB: If the fault address is near the stack pointer, |
| 1129 |
it's a stack overflow. */ |
| 1130 |
uintptr_t addr = (uintptr_t) address; |
| 1131 |
|
| 1132 |
if ((addr <= old_sp + 4096 && old_sp <= addr + 4096) |
| 1133 |
# ifdef __ia64 |
| 1134 |
|| (addr <= old_bsp + 4096 && old_bsp <= addr + 4096) |
| 1135 |
# endif |
| 1136 |
) |
| 1137 |
{ |
| 1138 |
{ |
| 1139 |
{ |
| 1140 |
# endif /* !HAVE_STACKVMA */ |
| 1141 |
{ |
| 1142 |
# ifdef SIGSEGV_FAULT_STACKPOINTER |
| 1143 |
int emergency = |
| 1144 |
(old_sp >= stk_extra_stack |
| 1145 |
&& old_sp <= stk_extra_stack + stk_extra_stack_size); |
| 1146 |
stackoverflow_context_t context = (SIGSEGV_FAULT_CONTEXT); |
| 1147 |
# else |
| 1148 |
int emergency = 0; |
| 1149 |
stackoverflow_context_t context = (void *) 0; |
| 1150 |
# endif |
| 1151 |
/* Call user's handler. */ |
| 1152 |
(*stk_user_handler) (emergency, context); |
| 1153 |
} |
| 1154 |
} |
| 1155 |
} |
| 1156 |
} |
| 1157 |
} |
| 1158 |
# endif /* HAVE_STACK_OVERFLOW_RECOVERY */ |
| 1159 |
|
| 1160 |
if (user_handler && (*user_handler) (address, 1)) |
| 1161 |
{ |
| 1162 |
/* Handler successful. */ |
| 1163 |
} |
| 1164 |
else |
| 1165 |
{ |
| 1166 |
/* Handler declined responsibility for real. */ |
| 1167 |
|
| 1168 |
/* Remove ourselves and dump core. */ |
| 1169 |
SIGSEGV_FOR_ALL_SIGNALS (signo, signal (signo, SIG_DFL);) |
| 1170 |
} |
| 1171 |
|
| 1172 |
# if HAVE_STACK_OVERFLOW_RECOVERY |
| 1173 |
} |
| 1174 |
# endif /* HAVE_STACK_OVERFLOW_RECOVERY */ |
| 1175 |
} |
| 1176 |
|
| 1177 |
#elif HAVE_STACK_OVERFLOW_RECOVERY |
| 1178 |
|
| 1179 |
static void |
| 1180 |
# ifdef SIGSEGV_FAULT_STACKPOINTER |
| 1181 |
sigsegv_handler (SIGSEGV_FAULT_HANDLER_ARGLIST) |
| 1182 |
# else |
| 1183 |
sigsegv_handler (int sig) |
| 1184 |
# endif |
| 1185 |
{ |
| 1186 |
# if !((HAVE_GETRLIMIT && defined RLIMIT_STACK) || defined SIGSEGV_FAULT_STACKPOINTER) |
| 1187 |
# error "Insufficient heuristics for detecting a stack overflow. Either define SIGSEGV_FAULT_STACKPOINTER correctly, or undefine HAVE_STACK_OVERFLOW_RECOVERY!" |
| 1188 |
# endif |
| 1189 |
|
| 1190 |
/* Did the user install a handler? */ |
| 1191 |
if (stk_user_handler) |
| 1192 |
{ |
| 1193 |
/* See whether it was a stack overflow. If so, longjump away. */ |
| 1194 |
# ifdef SIGSEGV_FAULT_STACKPOINTER |
| 1195 |
uintptr_t old_sp = (uintptr_t) (SIGSEGV_FAULT_STACKPOINTER); |
| 1196 |
# endif |
| 1197 |
|
| 1198 |
/* Were we able to determine the stack top? */ |
| 1199 |
if (stack_top) |
| 1200 |
{ |
| 1201 |
/* Determine stack bounds. */ |
| 1202 |
int saved_errno; |
| 1203 |
struct vma_struct vma; |
| 1204 |
int ret; |
| 1205 |
|
| 1206 |
saved_errno = errno; |
| 1207 |
ret = sigsegv_get_vma (stack_top, &vma); |
| 1208 |
errno = saved_errno; |
| 1209 |
if (ret >= 0) |
| 1210 |
{ |
| 1211 |
# if HAVE_GETRLIMIT && defined RLIMIT_STACK |
| 1212 |
/* Heuristic BC: If the stack size has reached its maximal size, |
| 1213 |
and old_sp is near the low end, we consider it a stack |
| 1214 |
overflow. */ |
| 1215 |
struct rlimit rl; |
| 1216 |
|
| 1217 |
saved_errno = errno; |
| 1218 |
ret = getrlimit (RLIMIT_STACK, &rl); |
| 1219 |
errno = saved_errno; |
| 1220 |
if (ret >= 0) |
| 1221 |
{ |
| 1222 |
uintptr_t current_stack_size = vma.end - vma.start; |
| 1223 |
uintptr_t max_stack_size = rl.rlim_cur; |
| 1224 |
if (current_stack_size <= max_stack_size + 4096 |
| 1225 |
&& max_stack_size <= current_stack_size + 4096 |
| 1226 |
# else |
| 1227 |
{ |
| 1228 |
if (1 |
| 1229 |
# endif |
| 1230 |
# ifdef SIGSEGV_FAULT_STACKPOINTER |
| 1231 |
/* Heuristic BC: If we know old_sp, and it is neither |
| 1232 |
near the low end, nor in the alternate stack, then |
| 1233 |
it's probably not a stack overflow. */ |
| 1234 |
&& ((old_sp >= stk_extra_stack |
| 1235 |
&& old_sp <= stk_extra_stack + stk_extra_stack_size) |
| 1236 |
# if STACK_DIRECTION < 0 |
| 1237 |
|| (old_sp <= vma.start + 4096 |
| 1238 |
&& vma.start <= old_sp + 4096)) |
| 1239 |
# else |
| 1240 |
|| (old_sp <= vma.end + 4096 |
| 1241 |
&& vma.end <= old_sp + 4096)) |
| 1242 |
# endif |
| 1243 |
# endif |
| 1244 |
) |
| 1245 |
{ |
| 1246 |
# ifdef SIGSEGV_FAULT_STACKPOINTER |
| 1247 |
int emergency = |
| 1248 |
(old_sp >= stk_extra_stack |
| 1249 |
&& old_sp <= stk_extra_stack + stk_extra_stack_size); |
| 1250 |
stackoverflow_context_t context = (SIGSEGV_FAULT_CONTEXT); |
| 1251 |
# else |
| 1252 |
int emergency = 0; |
| 1253 |
stackoverflow_context_t context = (void *) 0; |
| 1254 |
# endif |
| 1255 |
/* Call user's handler. */ |
| 1256 |
(*stk_user_handler)(emergency,context); |
| 1257 |
} |
| 1258 |
} |
| 1259 |
} |
| 1260 |
} |
| 1261 |
} |
| 1262 |
|
| 1263 |
/* Remove ourselves and dump core. */ |
| 1264 |
SIGSEGV_FOR_ALL_SIGNALS (signo, signal (signo, SIG_DFL);) |
| 1265 |
} |
| 1266 |
|
| 1267 |
#endif |
| 1268 |
|
| 1269 |
|
| 1270 |
#if HAVE_SIGSEGV_RECOVERY || HAVE_STACK_OVERFLOW_RECOVERY |
| 1271 |
|
| 1272 |
static void |
| 1273 |
install_for (int sig) |
| 1274 |
{ |
| 1275 |
struct sigaction action; |
| 1276 |
|
| 1277 |
# ifdef SIGSEGV_FAULT_ADDRESS_FROM_SIGINFO |
| 1278 |
action.sa_sigaction = (void (*) (int, siginfo_t *, void *)) &sigsegv_handler; |
| 1279 |
# else |
| 1280 |
action.sa_handler = (void (*) (int)) &sigsegv_handler; |
| 1281 |
# endif |
| 1282 |
/* Block most signals while SIGSEGV is being handled. */ |
| 1283 |
/* Signals SIGKILL, SIGSTOP cannot be blocked. */ |
| 1284 |
/* Signals SIGCONT, SIGTSTP, SIGTTIN, SIGTTOU are not blocked because |
| 1285 |
dealing with these signals seems dangerous. */ |
| 1286 |
/* Signals SIGILL, SIGABRT, SIGFPE, SIGSEGV, SIGTRAP, SIGIOT, SIGEMT, SIGBUS, |
| 1287 |
SIGSYS, SIGSTKFLT are not blocked because these are synchronous signals, |
| 1288 |
which may require immediate intervention, otherwise the process may |
| 1289 |
starve. */ |
| 1290 |
sigemptyset (&action.sa_mask); |
| 1291 |
# ifdef SIGHUP |
| 1292 |
sigaddset (&action.sa_mask,SIGHUP); |
| 1293 |
# endif |
| 1294 |
# ifdef SIGINT |
| 1295 |
sigaddset (&action.sa_mask,SIGINT); |
| 1296 |
# endif |
| 1297 |
# ifdef SIGQUIT |
| 1298 |
sigaddset (&action.sa_mask,SIGQUIT); |
| 1299 |
# endif |
| 1300 |
# ifdef SIGPIPE |
| 1301 |
sigaddset (&action.sa_mask,SIGPIPE); |
| 1302 |
# endif |
| 1303 |
# ifdef SIGALRM |
| 1304 |
sigaddset (&action.sa_mask,SIGALRM); |
| 1305 |
# endif |
| 1306 |
# ifdef SIGTERM |
| 1307 |
sigaddset (&action.sa_mask,SIGTERM); |
| 1308 |
# endif |
| 1309 |
# ifdef SIGUSR1 |
| 1310 |
sigaddset (&action.sa_mask,SIGUSR1); |
| 1311 |
# endif |
| 1312 |
# ifdef SIGUSR2 |
| 1313 |
sigaddset (&action.sa_mask,SIGUSR2); |
| 1314 |
# endif |
| 1315 |
# ifdef SIGCHLD |
| 1316 |
sigaddset (&action.sa_mask,SIGCHLD); |
| 1317 |
# endif |
| 1318 |
# ifdef SIGCLD |
| 1319 |
sigaddset (&action.sa_mask,SIGCLD); |
| 1320 |
# endif |
| 1321 |
# ifdef SIGURG |
| 1322 |
sigaddset (&action.sa_mask,SIGURG); |
| 1323 |
# endif |
| 1324 |
# ifdef SIGIO |
| 1325 |
sigaddset (&action.sa_mask,SIGIO); |
| 1326 |
# endif |
| 1327 |
# ifdef SIGPOLL |
| 1328 |
sigaddset (&action.sa_mask,SIGPOLL); |
| 1329 |
# endif |
| 1330 |
# ifdef SIGXCPU |
| 1331 |
sigaddset (&action.sa_mask,SIGXCPU); |
| 1332 |
# endif |
| 1333 |
# ifdef SIGXFSZ |
| 1334 |
sigaddset (&action.sa_mask,SIGXFSZ); |
| 1335 |
# endif |
| 1336 |
# ifdef SIGVTALRM |
| 1337 |
sigaddset (&action.sa_mask,SIGVTALRM); |
| 1338 |
# endif |
| 1339 |
# ifdef SIGPROF |
| 1340 |
sigaddset (&action.sa_mask,SIGPROF); |
| 1341 |
# endif |
| 1342 |
# ifdef SIGPWR |
| 1343 |
sigaddset (&action.sa_mask,SIGPWR); |
| 1344 |
# endif |
| 1345 |
# ifdef SIGLOST |
| 1346 |
sigaddset (&action.sa_mask,SIGLOST); |
| 1347 |
# endif |
| 1348 |
# ifdef SIGWINCH |
| 1349 |
sigaddset (&action.sa_mask,SIGWINCH); |
| 1350 |
# endif |
| 1351 |
/* Note that sigaction() implicitly adds sig itself to action.sa_mask. */ |
| 1352 |
/* Ask the OS to provide a structure siginfo_t to the handler. */ |
| 1353 |
# ifdef SIGSEGV_FAULT_ADDRESS_FROM_SIGINFO |
| 1354 |
action.sa_flags = SA_SIGINFO; |
| 1355 |
# else |
| 1356 |
action.sa_flags = 0; |
| 1357 |
# endif |
| 1358 |
# if HAVE_STACK_OVERFLOW_RECOVERY && HAVE_SIGALTSTACK /* not BeOS */ |
| 1359 |
/* Work around Linux 2.2.5 bug: If SA_ONSTACK is specified but sigaltstack() |
| 1360 |
has not been called, the kernel will busy loop, eating CPU time. So |
| 1361 |
avoid setting SA_ONSTACK until the user has requested stack overflow |
| 1362 |
handling. */ |
| 1363 |
if (stk_user_handler) |
| 1364 |
action.sa_flags |= SA_ONSTACK; |
| 1365 |
# endif |
| 1366 |
sigaction (sig, &action, (struct sigaction *) NULL); |
| 1367 |
} |
| 1368 |
|
| 1369 |
#endif /* HAVE_SIGSEGV_RECOVERY || HAVE_STACK_OVERFLOW_RECOVERY */ |
| 1370 |
|
| 1371 |
int |
| 1372 |
sigsegv_install_handler (sigsegv_handler_t handler) |
| 1373 |
{ |
| 1374 |
#if HAVE_SIGSEGV_RECOVERY |
| 1375 |
user_handler = handler; |
| 1376 |
|
| 1377 |
SIGSEGV_FOR_ALL_SIGNALS (sig, install_for (sig);) |
| 1378 |
|
| 1379 |
return 0; |
| 1380 |
#else |
| 1381 |
return -1; |
| 1382 |
#endif |
| 1383 |
} |
| 1384 |
|
| 1385 |
void |
| 1386 |
sigsegv_deinstall_handler (void) |
| 1387 |
{ |
| 1388 |
#if HAVE_SIGSEGV_RECOVERY |
| 1389 |
user_handler = (sigsegv_handler_t)NULL; |
| 1390 |
|
| 1391 |
# if HAVE_STACK_OVERFLOW_RECOVERY |
| 1392 |
if (!stk_user_handler) |
| 1393 |
# endif |
| 1394 |
{ |
| 1395 |
SIGSEGV_FOR_ALL_SIGNALS (sig, signal (sig, SIG_DFL);) |
| 1396 |
} |
| 1397 |
#endif |
| 1398 |
} |
| 1399 |
|
| 1400 |
int |
| 1401 |
sigsegv_leave_handler (void (*continuation) (void*, void*, void*), |
| 1402 |
void* cont_arg1, void* cont_arg2, void* cont_arg3) |
| 1403 |
{ |
| 1404 |
#if HAVE_STACK_OVERFLOW_RECOVERY |
| 1405 |
/* |
| 1406 |
* Reset the system's knowledge that we are executing on the alternate |
| 1407 |
* stack. If we didn't do that, siglongjmp would be needed instead of |
| 1408 |
* longjmp to leave the signal handler. |
| 1409 |
*/ |
| 1410 |
sigsegv_reset_onstack_flag (); |
| 1411 |
#endif |
| 1412 |
(*continuation) (cont_arg1, cont_arg2, cont_arg3); |
| 1413 |
return 1; |
| 1414 |
} |
| 1415 |
|
| 1416 |
int |
| 1417 |
stackoverflow_install_handler (stackoverflow_handler_t handler, |
| 1418 |
void *extra_stack, size_t extra_stack_size) |
| 1419 |
{ |
| 1420 |
#if HAVE_STACK_OVERFLOW_RECOVERY |
| 1421 |
# if HAVE_STACKVMA |
| 1422 |
if (!stack_top) |
| 1423 |
{ |
| 1424 |
int dummy; |
| 1425 |
remember_stack_top (&dummy); |
| 1426 |
if (!stack_top) |
| 1427 |
return -1; |
| 1428 |
} |
| 1429 |
# endif |
| 1430 |
|
| 1431 |
stk_user_handler = handler; |
| 1432 |
stk_extra_stack = (uintptr_t) extra_stack; |
| 1433 |
stk_extra_stack_size = extra_stack_size; |
| 1434 |
{ |
| 1435 |
stack_t ss; |
| 1436 |
# if SIGALTSTACK_SS_REVERSED |
| 1437 |
ss.ss_sp = (char *) extra_stack + extra_stack_size - sizeof (void *); |
| 1438 |
ss.ss_size = extra_stack_size - sizeof (void *); |
| 1439 |
# else |
| 1440 |
ss.ss_sp = extra_stack; |
| 1441 |
ss.ss_size = extra_stack_size; |
| 1442 |
# endif |
| 1443 |
ss.ss_flags = 0; /* no SS_DISABLE */ |
| 1444 |
if (sigaltstack (&ss, (stack_t*)0) < 0) |
| 1445 |
return -1; |
| 1446 |
} |
| 1447 |
|
| 1448 |
/* Install the signal handlers with SA_ONSTACK. */ |
| 1449 |
SIGSEGV_FOR_ALL_SIGNALS (sig, install_for (sig);) |
| 1450 |
return 0; |
| 1451 |
#else |
| 1452 |
return -1; |
| 1453 |
#endif |
| 1454 |
} |
| 1455 |
|
| 1456 |
void |
| 1457 |
stackoverflow_deinstall_handler (void) |
| 1458 |
{ |
| 1459 |
#if HAVE_STACK_OVERFLOW_RECOVERY |
| 1460 |
stk_user_handler = (stackoverflow_handler_t) NULL; |
| 1461 |
|
| 1462 |
# if HAVE_SIGSEGV_RECOVERY |
| 1463 |
if (user_handler) |
| 1464 |
{ |
| 1465 |
/* Reinstall the signal handlers without SA_ONSTACK, to avoid Linux |
| 1466 |
bug. */ |
| 1467 |
SIGSEGV_FOR_ALL_SIGNALS (sig, install_for (sig);) |
| 1468 |
} |
| 1469 |
else |
| 1470 |
# endif |
| 1471 |
{ |
| 1472 |
SIGSEGV_FOR_ALL_SIGNALS (sig, signal (sig, SIG_DFL);) |
| 1473 |
} |
| 1474 |
|
| 1475 |
{ |
| 1476 |
stack_t ss; |
| 1477 |
ss.ss_flags = SS_DISABLE; |
| 1478 |
if (sigaltstack (&ss, (stack_t *) 0) < 0) |
| 1479 |
perror ("gnulib sigsegv (stackoverflow_deinstall_handler)"); |
| 1480 |
} |
| 1481 |
#endif |
| 1482 |
} |