1 /* Stack overflow handling.
3 Copyright (C) 2002, 2004, 2006, 2008 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 <http://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)
49 # define ENOTSUP EINVAL
53 #if ! HAVE_STACK_T && ! defined stack_t
54 typedef struct sigaltstack stack_t;
60 /* Posix 2001 declares ucontext_t in <ucontext.h>, Posix 200x in
63 # include <ucontext.h>
68 # define STDERR_FILENO 2
74 #if defined SA_ONSTACK && defined SA_SIGINFO
75 # define SIGACTION_WORKS 1
77 # define SIGACTION_WORKS 0
80 extern char *program_name;
82 /* The user-specified action to take when a SEGV-related program error
83 or stack overflow occurs. */
84 static void (* volatile segv_action) (int);
86 /* Translated messages for program errors and stack overflow. Do not
87 translate them in the signal handler, since gettext is not
89 static char const * volatile program_error_message;
90 static char const * volatile stack_overflow_message;
92 /* Output an error message, then exit with status EXIT_FAILURE if it
93 appears to have been a stack overflow, or with a core dump
94 otherwise. This function is async-signal-safe. */
96 static void die (int) __attribute__ ((noreturn));
102 message = signo ? program_error_message : stack_overflow_message;
103 write (STDERR_FILENO, program_name, strlen (program_name));
104 write (STDERR_FILENO, ": ", 2);
105 write (STDERR_FILENO, message, strlen (message));
106 write (STDERR_FILENO, "\n", 1);
108 _exit (exit_failure);
113 #if HAVE_SIGALTSTACK && HAVE_DECL_SIGALTSTACK
115 /* Direction of the C runtime stack. This function is
116 async-signal-safe. */
119 # define find_stack_direction(ptr) STACK_DIRECTION
122 find_stack_direction (char const *addr)
125 return ! addr ? find_stack_direction (&dummy) : addr < &dummy ? 1 : -1;
129 /* Storage for the alternate signal stack. */
132 char buffer[SIGSTKSZ];
134 /* These other members are for proper alignment. There's no
135 standard way to guarantee stack alignment, but this seems enough
140 } alternate_signal_stack;
144 /* Handle a segmentation violation and exit. This function is
145 async-signal-safe. */
147 static void segv_handler (int, siginfo_t *, void *) __attribute__((noreturn));
149 segv_handler (int signo, siginfo_t *info,
150 void *context __attribute__ ((unused)))
152 /* Clear SIGNO if it seems to have been a stack overflow. */
153 if (0 < info->si_code)
155 # if ! HAVE_XSI_STACK_OVERFLOW_HEURISTIC
156 /* We can't easily determine whether it is a stack overflow; so
157 assume that the rest of our program is perfect (!) and that
158 this segmentation violation is a stack overflow. */
161 /* If the faulting address is within the stack, or within one
162 page of the stack end, assume that it is a stack
164 ucontext_t const *user_context = context;
165 char const *stack_base = user_context->uc_stack.ss_sp;
166 size_t stack_size = user_context->uc_stack.ss_size;
167 char const *faulting_address = info->si_addr;
168 size_t s = faulting_address - stack_base;
169 size_t page_size = sysconf (_SC_PAGESIZE);
170 if (find_stack_direction (NULL) < 0)
172 if (s < stack_size + page_size)
179 "segv_handler fault=%p base=%p size=%lx page=%lx signo=%d\n",
180 faulting_address, stack_base, (unsigned long) stack_size,
181 (unsigned long) page_size, signo);
182 write (STDERR_FILENO, buf, strlen (buf));
193 null_action (int signo __attribute__ ((unused)))
197 /* Set up ACTION so that it is invoked on C stack overflow. Return -1
198 (setting errno) if this cannot be done.
200 When ACTION is called, it is passed an argument equal to SIGSEGV
201 for a segmentation violation that does not appear related to stack
202 overflow, and is passed zero otherwise. On many platforms it is
203 hard to tell; when in doubt, zero is passed.
205 A null ACTION acts like an action that does nothing.
207 ACTION must be async-signal-safe. ACTION together with its callees
208 must not require more than SIGSTKSZ bytes of stack space. */
211 c_stack_action (void (*action) (int))
215 struct sigaction act;
217 st.ss_sp = alternate_signal_stack.buffer;
218 st.ss_size = sizeof alternate_signal_stack.buffer;
219 r = sigaltstack (&st, NULL);
223 segv_action = action ? action : null_action;
224 program_error_message = _("program error");
225 stack_overflow_message = _("stack overflow");
227 sigemptyset (&act.sa_mask);
230 /* POSIX 1003.1-2001 says SA_RESETHAND implies SA_NODEFER, but
231 this is not true on Solaris 8 at least. It doesn't hurt to use
232 SA_NODEFER here, so leave it in. */
233 act.sa_flags = SA_NODEFER | SA_ONSTACK | SA_RESETHAND | SA_SIGINFO;
234 act.sa_sigaction = segv_handler;
236 act.sa_flags = SA_NODEFER | SA_RESETHAND;
237 act.sa_handler = die;
240 return sigaction (SIGSEGV, &act, NULL);
243 #else /* ! (HAVE_SIGALTSTACK && HAVE_DECL_SIGALTSTACK) */
246 c_stack_action (void (*action) (int) __attribute__ ((unused)))