1 /* Stack overflow handling.
3 Copyright (C) 2002, 2004, 2006, 2008, 2009 Free Software
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 /* Written by Paul Eggert. */
23 A program that uses alloca, dynamic arrays, or large local
24 variables may extend the stack by more than a page at a time. If
25 so, when the stack overflows the operating system may not detect
26 the overflow until the program uses the array, and this module may
27 incorrectly report a program error instead of a stack overflow.
29 To avoid this problem, allocate only small objects on the stack; a
30 program should be OK if it limits single allocations to a page or
31 less. Allocate larger arrays in static storage, or on the heap
32 (e.g., with malloc). Yes, this is a pain, but we don't know of any
33 better solution that is portable.
35 No attempt has been made to deal with multithreaded applications. */
41 # define __attribute__(x)
46 #define _(msgid) gettext (msgid)
51 #if ! HAVE_STACK_T && ! defined stack_t
52 typedef struct sigaltstack stack_t;
55 # define SIGSTKSZ 16384
61 /* Posix 2001 declares ucontext_t in <ucontext.h>, Posix 200x in
64 # include <ucontext.h>
76 #if defined SA_ONSTACK && defined SA_SIGINFO
77 # define SIGACTION_WORKS 1
79 # define SIGACTION_WORKS 0
85 extern char *program_name;
87 /* The user-specified action to take when a SEGV-related program error
88 or stack overflow occurs. */
89 static void (* volatile segv_action) (int);
91 /* Translated messages for program errors and stack overflow. Do not
92 translate them in the signal handler, since gettext is not
94 static char const * volatile program_error_message;
95 static char const * volatile stack_overflow_message;
97 /* Output an error message, then exit with status EXIT_FAILURE if it
98 appears to have been a stack overflow, or with a core dump
99 otherwise. This function is async-signal-safe. */
101 static void die (int) __attribute__ ((noreturn));
107 message = signo ? program_error_message : stack_overflow_message;
108 write (STDERR_FILENO, program_name, strlen (program_name));
109 write (STDERR_FILENO, ": ", 2);
110 write (STDERR_FILENO, message, strlen (message));
111 write (STDERR_FILENO, "\n", 1);
113 _exit (exit_failure);
118 #if (HAVE_SIGALTSTACK && HAVE_DECL_SIGALTSTACK \
119 && HAVE_STACK_OVERFLOW_HANDLING) || HAVE_LIBSIGSEGV
121 /* Storage for the alternate signal stack. */
124 char buffer[SIGSTKSZ];
126 /* These other members are for proper alignment. There's no
127 standard way to guarantee stack alignment, but this seems enough
132 } alternate_signal_stack;
135 null_action (int signo __attribute__ ((unused)))
139 #endif /* SIGALTSTACK || LIBSIGSEGV */
141 /* Only use libsigsegv if we need it; platforms like Solaris can
142 detect stack overflow without the overhead of an external
144 #if HAVE_LIBSIGSEGV && ! HAVE_XSI_STACK_OVERFLOW_HEURISTIC
146 /* Nonzero if general segv handler could not be installed. */
147 static volatile int segv_handler_missing;
149 /* Handle a segmentation violation and exit if it cannot be stack
150 overflow. This function is async-signal-safe. */
152 static int segv_handler (void *address __attribute__ ((unused)),
158 sprintf (buf, "segv_handler serious=%d\n", serious);
159 write (STDERR_FILENO, buf, strlen (buf));
163 /* If this fault is not serious, return 0 to let the stack overflow
164 handler take a shot at it. */
170 /* Handle a segmentation violation that is likely to be a stack
171 overflow and exit. This function is async-signal-safe. */
173 static void overflow_handler (int, stackoverflow_context_t)
174 __attribute__ ((noreturn));
176 overflow_handler (int emergency,
177 stackoverflow_context_t context __attribute__ ((unused)))
182 sprintf (buf, "overflow_handler emergency=%d segv_handler_missing=%d\n",
183 emergency, segv_handler_missing);
184 write (STDERR_FILENO, buf, strlen (buf));
188 die ((!emergency || segv_handler_missing) ? 0 : SIGSEGV);
192 c_stack_action (void (*action) (int))
194 segv_action = action ? action : null_action;
195 program_error_message = _("program error");
196 stack_overflow_message = _("stack overflow");
198 /* Always install the overflow handler. */
199 if (stackoverflow_install_handler (overflow_handler,
200 alternate_signal_stack.buffer,
201 sizeof alternate_signal_stack.buffer))
206 /* Try installing a general handler; if it fails, then treat all
207 segv as stack overflow. */
208 segv_handler_missing = sigsegv_install_handler (segv_handler);
212 #elif HAVE_SIGALTSTACK && HAVE_DECL_SIGALTSTACK && HAVE_STACK_OVERFLOW_HANDLING
214 /* Direction of the C runtime stack. This function is
215 async-signal-safe. */
218 # define find_stack_direction(ptr) STACK_DIRECTION
221 find_stack_direction (char const *addr)
224 return ! addr ? find_stack_direction (&dummy) : addr < &dummy ? 1 : -1;
230 /* Handle a segmentation violation and exit. This function is
231 async-signal-safe. */
233 static void segv_handler (int, siginfo_t *, void *) __attribute__((noreturn));
235 segv_handler (int signo, siginfo_t *info,
236 void *context __attribute__ ((unused)))
238 /* Clear SIGNO if it seems to have been a stack overflow. */
239 # if ! HAVE_XSI_STACK_OVERFLOW_HEURISTIC
240 /* We can't easily determine whether it is a stack overflow; so
241 assume that the rest of our program is perfect (!) and that
242 this segmentation violation is a stack overflow.
244 Note that although both Linux and Solaris provide
245 sigaltstack, SA_ONSTACK, and SA_SIGINFO, currently only
246 Solaris satisfies the XSI heueristic. This is because
247 Solaris populates uc_stack with the details of the
248 interrupted stack, while Linux populates it with the details
249 of the current stack. */
252 if (0 < info->si_code)
254 /* If the faulting address is within the stack, or within one
255 page of the stack end, assume that it is a stack
257 ucontext_t const *user_context = context;
258 char const *stack_base = user_context->uc_stack.ss_sp;
259 size_t stack_size = user_context->uc_stack.ss_size;
260 char const *faulting_address = info->si_addr;
261 size_t s = faulting_address - stack_base;
262 size_t page_size = sysconf (_SC_PAGESIZE);
263 if (find_stack_direction (NULL) < 0)
265 if (s < stack_size + page_size)
272 "segv_handler fault=%p base=%p size=%lx page=%lx signo=%d\n",
273 faulting_address, stack_base, (unsigned long) stack_size,
274 (unsigned long) page_size, signo);
275 write (STDERR_FILENO, buf, strlen (buf));
286 c_stack_action (void (*action) (int))
290 struct sigaction act;
292 # if SIGALTSTACK_SS_REVERSED
293 /* Irix mistakenly treats ss_sp as the upper bound, rather than
294 lower bound, of the alternate stack. */
295 st.ss_sp = alternate_signal_stack.buffer + SIGSTKSZ - sizeof (void *);
296 st.ss_size = sizeof alternate_signal_stack.buffer - sizeof (void *);
298 st.ss_sp = alternate_signal_stack.buffer;
299 st.ss_size = sizeof alternate_signal_stack.buffer;
301 r = sigaltstack (&st, NULL);
305 segv_action = action ? action : null_action;
306 program_error_message = _("program error");
307 stack_overflow_message = _("stack overflow");
309 sigemptyset (&act.sa_mask);
312 /* POSIX 1003.1-2001 says SA_RESETHAND implies SA_NODEFER, but
313 this is not true on Solaris 8 at least. It doesn't hurt to use
314 SA_NODEFER here, so leave it in. */
315 act.sa_flags = SA_NODEFER | SA_ONSTACK | SA_RESETHAND | SA_SIGINFO;
316 act.sa_sigaction = segv_handler;
318 act.sa_flags = SA_NODEFER | SA_ONSTACK | SA_RESETHAND;
319 act.sa_handler = die;
322 # if FAULT_YIELDS_SIGBUS
323 if (sigaction (SIGBUS, &act, NULL) < 0)
326 return sigaction (SIGSEGV, &act, NULL);
329 #else /* ! ((HAVE_SIGALTSTACK && HAVE_DECL_SIGALTSTACK
330 && HAVE_STACK_OVERFLOW_HANDLING) || HAVE_LIBSIGSEGV) */
333 c_stack_action (void (*action) (int) __attribute__ ((unused)))