1 /* Stack overflow handling.
3 Copyright (C) 2002, 2004 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 2, or (at your option)
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, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
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. */
42 # if __GNUC__ < 3 || __STRICT_ANSI__
43 # define __attribute__(x)
48 #define _(msgid) gettext (msgid)
52 # define ENOTSUP EINVAL
55 # define EOVERFLOW EINVAL
59 #if ! HAVE_STACK_T && ! defined stack_t
60 typedef struct sigaltstack stack_t;
66 #if HAVE_SYS_RESOURCE_H
67 /* Include sys/time.h here, because...
68 SunOS-4.1.x <sys/resource.h> fails to include <sys/time.h>.
69 This gives "incomplete type" errors for ru_utime and tu_stime. */
71 # include <sys/time.h>
73 # include <sys/resource.h>
77 # include <ucontext.h>
84 # define STDERR_FILENO 2
94 #if (HAVE_STRUCT_SIGACTION_SA_SIGACTION && defined SA_NODEFER \
95 && defined SA_ONSTACK && defined SA_RESETHAND && defined SA_SIGINFO)
96 # define SIGACTION_WORKS 1
98 # define SIGACTION_WORKS 0
101 extern char *program_name;
103 /* The user-specified action to take when a SEGV-related program error
104 or stack overflow occurs. */
105 static void (* volatile segv_action) (int);
107 /* Translated messages for program errors and stack overflow. Do not
108 translate them in the signal handler, since gettext is not
109 async-signal-safe. */
110 static char const * volatile program_error_message;
111 static char const * volatile stack_overflow_message;
113 /* Output an error message, then exit with status EXIT_FAILURE if it
114 appears to have been a stack overflow, or with a core dump
115 otherwise. This function is async-signal-safe. */
117 static void die (int) __attribute__ ((noreturn));
123 message = signo ? program_error_message : stack_overflow_message;
124 write (STDERR_FILENO, program_name, strlen (program_name));
125 write (STDERR_FILENO, ": ", 2);
126 write (STDERR_FILENO, message, strlen (message));
127 write (STDERR_FILENO, "\n", 1);
129 _exit (exit_failure);
130 kill (getpid (), signo);
134 #if HAVE_SIGALTSTACK && HAVE_DECL_SIGALTSTACK
136 /* Direction of the C runtime stack. This function is
137 async-signal-safe. */
140 # define find_stack_direction(ptr) STACK_DIRECTION
143 find_stack_direction (char const *addr)
146 return ! addr ? find_stack_direction (&dummy) : addr < &dummy ? 1 : -1;
150 /* Storage for the alternate signal stack. */
153 char buffer[SIGSTKSZ];
155 /* These other members are for proper alignment. There's no
156 standard way to guarantee stack alignment, but this seems enough
161 } alternate_signal_stack;
165 /* Handle a segmentation violation and exit. This function is
166 async-signal-safe. */
168 static void segv_handler (int, siginfo_t *, void *) __attribute__((noreturn));
170 segv_handler (int signo, siginfo_t *info,
171 void *context __attribute__ ((unused)))
173 /* Clear SIGNO if it seems to have been a stack overflow. */
174 if (0 < info->si_code)
176 # if ! HAVE_XSI_STACK_OVERFLOW_HEURISTIC
177 /* We can't easily determine whether it is a stack overflow; so
178 assume that the rest of our program is perfect (!) and that
179 this segmentation violation is a stack overflow. */
182 /* If the faulting address is within the stack, or within one
183 page of the stack end, assume that it is a stack
185 ucontext_t const *user_context = context;
186 char const *stack_base = user_context->uc_stack.ss_sp;
187 size_t stack_size = user_context->uc_stack.ss_size;
188 char const *faulting_address = info->si_addr;
189 size_t s = faulting_address - stack_base;
190 size_t page_size = sysconf (_SC_PAGESIZE);
191 if (find_stack_direction (0) < 0)
193 if (s < stack_size + page_size)
200 "segv_handler fault=%p base=%p size=%lx page=%lx signo=%d\n",
201 faulting_address, stack_base, (unsigned long) stack_size,
202 (unsigned long) page_size, signo);
203 write (STDERR_FILENO, buf, strlen (buf));
214 null_action (int signo __attribute__ ((unused)))
218 /* Set up ACTION so that it is invoked on C stack overflow. Return -1
219 (setting errno) if this cannot be done.
221 When ACTION is called, it is passed an argument equal to SIGSEGV
222 for a segmentation violation that does not appear related to stack
223 overflow, and is passed zero otherwise. On many platforms it is
224 hard to tell; when in doubt, zero is passed.
226 A null ACTION acts like an action that does nothing.
228 ACTION must be async-signal-safe. ACTION together with its callees
229 must not require more than SIGSTKSZ bytes of stack space. */
232 c_stack_action (void (*action) (int))
237 st.ss_sp = alternate_signal_stack.buffer;
238 st.ss_size = sizeof alternate_signal_stack.buffer;
239 r = sigaltstack (&st, 0);
243 segv_action = action ? action : null_action;
244 program_error_message = _("program error");
245 stack_overflow_message = _("stack overflow");
249 struct sigaction act;
250 sigemptyset (&act.sa_mask);
252 /* POSIX 1003.1-2001 says SA_RESETHAND implies SA_NODEFER, but
253 this is not true on Solaris 8 at least. It doesn't hurt to use
254 SA_NODEFER here, so leave it in. */
255 act.sa_flags = SA_NODEFER | SA_ONSTACK | SA_RESETHAND | SA_SIGINFO;
257 act.sa_sigaction = segv_handler;
259 return sigaction (SIGSEGV, &act, 0);
261 return signal (SIGSEGV, die) == SIG_ERR ? -1 : 0;
266 #else /* ! (HAVE_SIGALTSTACK && HAVE_DECL_SIGALTSTACK) */
269 c_stack_action (void (*action) (int) __attribute__ ((unused)))
281 int volatile exit_failure;
288 return *p + recurse (array);
294 main (int argc __attribute__ ((unused)), char **argv)
296 program_name = argv[0];
298 "The last output line should contain \"stack overflow\".\n");
299 if (c_stack_action (0) == 0)
300 return recurse ("\1");
301 perror ("c_stack_action");
309 compile-command: "gcc -DDEBUG -DHAVE_CONFIG_H -I.. -g -O -Wall -W c-stack.c"