1 /* Stack overflow handling.
3 Copyright (C) 2002 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.
37 If ! HAVE_XSI_STACK_OVERFLOW_HEURISTIC, the current implementation
38 assumes that, if the RLIMIT_STACK limit changes during execution,
39 then c_stack_action is invoked immediately afterwards. */
46 # if __GNUC__ < 3 || __STRICT_ANSI__
47 # define __attribute__(x)
52 #define _(msgid) gettext (msgid)
56 # define ENOTSUP EINVAL
59 # define EOVERFLOW EINVAL
63 #if ! HAVE_STACK_T && ! defined stack_t
64 typedef struct sigaltstack stack_t
;
70 #if HAVE_SYS_RESOURCE_H
71 /* Include sys/time.h here, because...
72 SunOS-4.1.x <sys/resource.h> fails to include <sys/time.h>.
73 This gives "incomplete type" errors for ru_utime and tu_stime. */
75 # include <sys/time.h>
77 # include <sys/resource.h>
81 # include <ucontext.h>
88 # define STDERR_FILENO 2
98 extern char *program_name
;
100 /* The user-specified action to take when a SEGV-related program error
101 or stack overflow occurs. */
102 static void (* volatile segv_action
) (int);
104 /* Translated messages for program errors and stack overflow. Do not
105 translate them in the signal handler, since gettext is not
106 async-signal-safe. */
107 static char const * volatile program_error_message
;
108 static char const * volatile stack_overflow_message
;
110 /* Output an error message, then exit with status EXIT_FAILURE if it
111 appears to have been a stack overflow, or with a core dump
112 otherwise. This function is async-signal-safe. */
114 static void die (int) __attribute__ ((noreturn
));
118 char const *message
=
119 signo
? program_error_message
: stack_overflow_message
;
121 write (STDERR_FILENO
, program_name
, strlen (program_name
));
122 write (STDERR_FILENO
, ": ", 2);
123 write (STDERR_FILENO
, message
, strlen (message
));
124 write (STDERR_FILENO
, "\n", 1);
126 _exit (exit_failure
);
127 kill (getpid (), signo
);
131 #if HAVE_SIGALTSTACK && HAVE_DECL_SIGALTSTACK
133 /* Direction of the C runtime stack. This function is
134 async-signal-safe. */
137 # define find_stack_direction(ptr) STACK_DIRECTION
140 find_stack_direction (char const *addr
)
143 return ! addr
? find_stack_direction (&dummy
) : addr
< &dummy
? 1 : -1;
147 # if HAVE_XSI_STACK_OVERFLOW_HEURISTIC
148 # define get_stack_location(argv) 0
151 # if defined RLIMIT_STACK && defined _SC_PAGESIZE
153 /* Return the minimum machine address deducible from ARGV. This
154 includes the addresses of all the strings that ARGV points at, as
155 well as the address of ARGV itself. */
158 min_address_from_argv (char * const *argv
)
160 char const *min
= (char const *) argv
;
162 while ((p
= *argv
++))
168 /* Return the maximum machine address deducible from ARGV. */
171 max_address_from_argv (char * const *argv
)
173 char const *max
= *argv
;
176 while ((p
= *argv
++))
179 max1
= (char const *) (argv
+ 1);
180 return max
&& max1
< max
? max
+ strlen (max
) + 1 : max1
;
184 /* The base and size of the stack, determined at startup. */
185 static char const * volatile stack_base
;
186 static size_t volatile stack_size
;
188 /* Store the base and size of the stack into the static variables
189 STACK_BASE and STACK_SIZE. The base is the numerically lowest
190 address in the stack. Return -1 (setting errno) if this cannot be
194 get_stack_location (char * const *argv
)
196 # if ! (defined RLIMIT_STACK && defined _SC_PAGESIZE)
203 struct rlimit rlimit
;
204 int r
= getrlimit (RLIMIT_STACK
, &rlimit
);
208 size_t size
= rlimit
.rlim_cur
;
209 extern char **environ
;
210 size_t page_size
= sysconf (_SC_PAGESIZE
);
211 int stack_direction
= find_stack_direction (0);
213 # if HAVE_GETCONTEXT && HAVE_DECL_GETCONTEXT
215 if (getcontext (&context
) == 0)
217 base
= context
.uc_stack
.ss_sp
;
218 if (stack_direction
< 0)
219 base
-= size
- context
.uc_stack
.ss_size
;
224 if (stack_direction
< 0)
226 char const *a
= max_address_from_argv (argv
);
227 char const *b
= max_address_from_argv (environ
);
228 base
= (a
< b
? b
: a
) - size
;
229 base
+= - (size_t) base
% page_size
;
233 char const *a
= min_address_from_argv (argv
);
234 char const *b
= min_address_from_argv (environ
);
235 base
= a
< b
? a
: b
;
236 base
-= (size_t) base
% page_size
;
240 if (size
!= rlimit
.rlim_cur
241 || rlimit
.rlim_cur
< 0
242 || base
+ size
< base
243 # ifdef RLIM_SAVED_CUR
244 || rlimit
.rlim_cur
== RLIM_SAVED_CUR
246 # ifdef RLIM_SAVED_MAX
247 || rlimit
.rlim_cur
== RLIM_SAVED_MAX
249 # ifdef RLIM_INFINITY
250 || rlimit
.rlim_cur
== RLIM_INFINITY
262 fprintf (stderr
, "get_stack_location base=%p size=%lx\n",
263 base
, (unsigned long) size
);
273 /* Storage for the alternate signal stack. */
276 char buffer
[SIGSTKSZ
];
278 /* These other members are for proper alignment. There's no
279 standard way to guarantee stack alignment, but this seems enough
284 } alternate_signal_stack
;
286 # if defined SA_ONSTACK && defined SA_SIGINFO && defined _SC_PAGESIZE
288 /* Handle a segmentation violation and exit. This function is
289 async-signal-safe. */
291 static void segv_handler (int, siginfo_t
*, void *) __attribute__((noreturn
));
293 segv_handler (int signo
, siginfo_t
*info
,
294 void *context
__attribute__ ((unused
)))
296 /* Clear SIGNO if it seems to have been a stack overflow. */
297 if (0 < info
->si_code
)
299 /* If the faulting address is within the stack, or within one
300 page of the stack end, assume that it is a stack
302 # if HAVE_XSI_STACK_OVERFLOW_HEURISTIC
303 ucontext_t
const *user_context
= context
;
304 char const *stack_base
= user_context
->uc_stack
.ss_sp
;
305 size_t stack_size
= user_context
->uc_stack
.ss_size
;
307 char const *faulting_address
= info
->si_addr
;
308 size_t s
= faulting_address
- stack_base
;
309 size_t page_size
= sysconf (_SC_PAGESIZE
);
310 if (find_stack_direction (0) < 0)
312 if (s
< stack_size
+ page_size
)
319 "segv_handler fault=%p base=%p size=%lx page=%lx signo=%d\n",
320 faulting_address
, stack_base
, (unsigned long) stack_size
,
321 (unsigned long) page_size
, signo
);
322 write (STDERR_FILENO
, buf
, strlen (buf
));
332 null_action (int signo
__attribute__ ((unused
)))
336 /* Assuming ARGV is the argument vector of `main', set up ACTION so
337 that it is invoked on C stack overflow. Return -1 (setting errno)
338 if this cannot be done.
340 When ACTION is called, it is passed an argument equal to SIGSEGV
341 for a segmentation violation that does not appear related to stack
342 overflow, and is passed zero otherwise.
344 A null ACTION acts like an action that does nothing.
346 ACTION must be async-signal-safe. ACTION together with its callees
347 must not require more than SIGSTKSZ bytes of stack space. */
350 c_stack_action (char * const *argv
__attribute__ ((unused
)),
351 void (*action
) (int))
353 int r
= get_stack_location (argv
);
360 st
.ss_sp
= alternate_signal_stack
.buffer
;
361 st
.ss_size
= sizeof alternate_signal_stack
.buffer
;
362 r
= sigaltstack (&st
, 0);
367 segv_action
= action
? action
: null_action
;
368 program_error_message
= _("program error");
369 stack_overflow_message
= _("stack overflow");
372 # if ! (defined SA_ONSTACK && defined SA_SIGINFO && defined _SC_PAGESIZE)
373 return signal (SIGSEGV
, die
) == SIG_ERR
? -1 : 0;
375 struct sigaction act
;
376 sigemptyset (&act
.sa_mask
);
378 /* POSIX 1003.1-2001 says SA_RESETHAND implies SA_NODEFER, but
379 this is not true on Solaris 8 at least. It doesn't hurt to use
380 SA_NODEFER here, so leave it in. */
381 act
.sa_flags
= SA_NODEFER
| SA_ONSTACK
| SA_RESETHAND
| SA_SIGINFO
;
383 act
.sa_sigaction
= segv_handler
;
385 return sigaction (SIGSEGV
, &act
, 0);
390 #else /* ! (HAVE_SIGALTSTACK && HAVE_DECL_SIGALTSTACK) */
393 c_stack_action (char * const *argv
__attribute__ ((unused
)),
394 void (*action
) (int) __attribute__ ((unused
)))
406 int volatile exit_failure
;
413 return *p
+ recurse (array
);
419 main (int argc
__attribute__ ((unused
)), char **argv
)
421 program_name
= argv
[0];
422 fprintf (stderr
, "The last line of output should be \"stack overflow\".\n");
423 if (c_stack_action (argv
, 0) == 0)
424 return recurse ("\1");
425 perror ("c_stack_action");
433 compile-command: "gcc -DDEBUG -DHAVE_CONFIG_H -I.. -g -O -Wall -W c-stack.c"