1 /* Stack overflow handling.
3 Copyright (C) 2002, 2004, 2006, 2008-2017 Free Software Foundation, Inc.
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 3 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 Paul Eggert. */
22 A program that uses alloca, dynamic arrays, or large local
23 variables may extend the stack by more than a page at a time. If
24 so, when the stack overflows the operating system may not detect
25 the overflow until the program uses the array, and this module may
26 incorrectly report a program error instead of a stack overflow.
28 To avoid this problem, allocate only small objects on the stack; a
29 program should be OK if it limits single allocations to a page or
30 less. Allocate larger arrays in static storage, or on the heap
31 (e.g., with malloc). Yes, this is a pain, but we don't know of any
32 better solution that is portable.
34 No attempt has been made to deal with multithreaded applications. */
40 # define __attribute__(x)
45 #define _(msgid) gettext (msgid)
50 #if ! HAVE_STACK_T && ! defined stack_t
51 typedef struct sigaltstack stack_t
;
54 # define SIGSTKSZ 16384
55 #elif HAVE_LIBSIGSEGV && SIGSTKSZ < 16384
56 /* libsigsegv 2.6 through 2.8 have a bug where some architectures use
57 more than the Linux default of an 8k alternate stack when deciding
58 if a fault was caused by stack overflow. */
60 # define SIGSTKSZ 16384
66 /* Posix 2001 declares ucontext_t in <ucontext.h>, Posix 200x in
69 # include <ucontext.h>
80 #include "ignore-value.h"
81 #include "getprogname.h"
83 #if defined SA_ONSTACK && defined SA_SIGINFO
84 # define SIGINFO_WORKS 1
86 # define SIGINFO_WORKS 0
92 /* The user-specified action to take when a SEGV-related program error
93 or stack overflow occurs. */
94 static void (* volatile segv_action
) (int);
96 /* Translated messages for program errors and stack overflow. Do not
97 translate them in the signal handler, since gettext is not
99 static char const * volatile program_error_message
;
100 static char const * volatile stack_overflow_message
;
102 /* Output an error message, then exit with status EXIT_FAILURE if it
103 appears to have been a stack overflow, or with a core dump
104 otherwise. This function is async-signal-safe. */
106 static _Noreturn
void
110 #if !SIGINFO_WORKS && !HAVE_LIBSIGSEGV
111 /* We can't easily determine whether it is a stack overflow; so
112 assume that the rest of our program is perfect (!) and that
113 this segmentation violation is a stack overflow. */
115 #endif /* !SIGINFO_WORKS && !HAVE_LIBSIGSEGV */
117 message
= signo
? program_error_message
: stack_overflow_message
;
118 ignore_value (write (STDERR_FILENO
, getprogname (), strlen (getprogname ())));
119 ignore_value (write (STDERR_FILENO
, ": ", 2));
120 ignore_value (write (STDERR_FILENO
, message
, strlen (message
)));
121 ignore_value (write (STDERR_FILENO
, "\n", 1));
123 _exit (exit_failure
);
128 #if (HAVE_SIGALTSTACK && HAVE_DECL_SIGALTSTACK \
129 && HAVE_STACK_OVERFLOW_HANDLING) || HAVE_LIBSIGSEGV
131 /* Storage for the alternate signal stack. */
134 char buffer
[SIGSTKSZ
];
136 /* These other members are for proper alignment. There's no
137 standard way to guarantee stack alignment, but this seems enough
142 } alternate_signal_stack
;
145 null_action (int signo
__attribute__ ((unused
)))
149 #endif /* SIGALTSTACK || LIBSIGSEGV */
151 /* Only use libsigsegv if we need it; platforms like Solaris can
152 detect stack overflow without the overhead of an external
154 #if HAVE_LIBSIGSEGV && ! HAVE_XSI_STACK_OVERFLOW_HEURISTIC
156 /* Nonzero if general segv handler could not be installed. */
157 static volatile int segv_handler_missing
;
159 /* Handle a segmentation violation and exit if it cannot be stack
160 overflow. This function is async-signal-safe. */
162 static int segv_handler (void *address
__attribute__ ((unused
)),
168 sprintf (buf
, "segv_handler serious=%d\n", serious
);
169 write (STDERR_FILENO
, buf
, strlen (buf
));
173 /* If this fault is not serious, return 0 to let the stack overflow
174 handler take a shot at it. */
180 /* Handle a segmentation violation that is likely to be a stack
181 overflow and exit. This function is async-signal-safe. */
183 static _Noreturn
void
184 overflow_handler (int emergency
,
185 stackoverflow_context_t context
__attribute__ ((unused
)))
190 sprintf (buf
, "overflow_handler emergency=%d segv_handler_missing=%d\n",
191 emergency
, segv_handler_missing
);
192 write (STDERR_FILENO
, buf
, strlen (buf
));
196 die ((!emergency
|| segv_handler_missing
) ? 0 : SIGSEGV
);
200 c_stack_action (void (*action
) (int))
202 segv_action
= action
? action
: null_action
;
203 program_error_message
= _("program error");
204 stack_overflow_message
= _("stack overflow");
206 /* Always install the overflow handler. */
207 if (stackoverflow_install_handler (overflow_handler
,
208 alternate_signal_stack
.buffer
,
209 sizeof alternate_signal_stack
.buffer
))
214 /* Try installing a general handler; if it fails, then treat all
215 segv as stack overflow. */
216 segv_handler_missing
= sigsegv_install_handler (segv_handler
);
220 #elif HAVE_SIGALTSTACK && HAVE_DECL_SIGALTSTACK && HAVE_STACK_OVERFLOW_HANDLING
224 /* Handle a segmentation violation and exit. This function is
225 async-signal-safe. */
227 static _Noreturn
void
228 segv_handler (int signo
, siginfo_t
*info
,
229 void *context
__attribute__ ((unused
)))
231 /* Clear SIGNO if it seems to have been a stack overflow. */
232 # if ! HAVE_XSI_STACK_OVERFLOW_HEURISTIC
233 /* We can't easily determine whether it is a stack overflow; so
234 assume that the rest of our program is perfect (!) and that
235 this segmentation violation is a stack overflow.
237 Note that although both Linux and Solaris provide
238 sigaltstack, SA_ONSTACK, and SA_SIGINFO, currently only
239 Solaris satisfies the XSI heuristic. This is because
240 Solaris populates uc_stack with the details of the
241 interrupted stack, while Linux populates it with the details
242 of the current stack. */
245 if (0 < info
->si_code
)
247 /* If the faulting address is within the stack, or within one
248 page of the stack, assume that it is a stack overflow. */
249 ucontext_t
const *user_context
= context
;
250 char const *stack_base
= user_context
->uc_stack
.ss_sp
;
251 size_t stack_size
= user_context
->uc_stack
.ss_size
;
252 char const *faulting_address
= info
->si_addr
;
253 size_t page_size
= sysconf (_SC_PAGESIZE
);
254 size_t s
= faulting_address
- stack_base
+ page_size
;
255 if (s
< stack_size
+ 2 * page_size
)
262 "segv_handler fault=%p base=%p size=%lx page=%lx signo=%d\n",
263 faulting_address
, stack_base
, (unsigned long) stack_size
,
264 (unsigned long) page_size
, signo
);
265 write (STDERR_FILENO
, buf
, strlen (buf
));
276 c_stack_action (void (*action
) (int))
280 struct sigaction act
;
282 # if SIGALTSTACK_SS_REVERSED
283 /* Irix mistakenly treats ss_sp as the upper bound, rather than
284 lower bound, of the alternate stack. */
285 st
.ss_sp
= alternate_signal_stack
.buffer
+ SIGSTKSZ
- sizeof (void *);
286 st
.ss_size
= sizeof alternate_signal_stack
.buffer
- sizeof (void *);
288 st
.ss_sp
= alternate_signal_stack
.buffer
;
289 st
.ss_size
= sizeof alternate_signal_stack
.buffer
;
291 r
= sigaltstack (&st
, NULL
);
295 segv_action
= action
? action
: null_action
;
296 program_error_message
= _("program error");
297 stack_overflow_message
= _("stack overflow");
299 sigemptyset (&act
.sa_mask
);
302 /* POSIX 1003.1-2001 says SA_RESETHAND implies SA_NODEFER, but
303 this is not true on Solaris 8 at least. It doesn't hurt to use
304 SA_NODEFER here, so leave it in. */
305 act
.sa_flags
= SA_NODEFER
| SA_ONSTACK
| SA_RESETHAND
| SA_SIGINFO
;
306 act
.sa_sigaction
= segv_handler
;
308 act
.sa_flags
= SA_NODEFER
| SA_ONSTACK
| SA_RESETHAND
;
309 act
.sa_handler
= die
;
312 # if FAULT_YIELDS_SIGBUS
313 if (sigaction (SIGBUS
, &act
, NULL
) < 0)
316 return sigaction (SIGSEGV
, &act
, NULL
);
319 #else /* ! ((HAVE_SIGALTSTACK && HAVE_DECL_SIGALTSTACK
320 && HAVE_STACK_OVERFLOW_HANDLING) || HAVE_LIBSIGSEGV) */
323 c_stack_action (void (*action
) (int) __attribute__ ((unused
)))