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. */
21 /* This module assumes that each stack frame is smaller than a page.
22 If you use alloca, dynamic arrays, or large local variables, your
23 program may extend the stack by more than a page at a time. If so,
24 the code below may incorrectly report a program error, or worse
25 yet, may not detect the overflow at all. To avoid this problem,
26 don't use large local arrays. */
33 #define _(msgid) gettext (msgid)
37 # define ENOTSUP EINVAL
41 # include <inttypes.h>
56 # define STDERR_FILENO 2
62 extern char *program_name
;
64 #if HAVE_XSI_STACK_OVERFLOW_HEURISTIC
66 # include <ucontext.h>
69 /* Storage for the alternate signal stack. */
72 char buffer
[SIGSTKSZ
];
74 /* These other members are for proper alignment. There's no
75 standard way to guarantee stack alignment, but this seems enough
80 } alternate_signal_stack
;
83 /* Direction of the C runtime stack. This function is
87 # define find_stack_direction(ptr) STACK_DIRECTION
90 find_stack_direction (char const *addr
)
93 return ! addr
? find_stack_direction (&dummy
) : addr
< &dummy
? 1 : -1;
97 /* The SIGSEGV handler. */
98 static void (* volatile segv_action
) (int, siginfo_t
*, void *);
100 /* Handle a segmentation violation and exit. This function is
101 async-signal-safe. */
104 segv_handler (int signo
, siginfo_t
*info
, void *context
)
106 /* Clear SIGNO if it seems to have been a stack overflow. */
107 if (0 < info
->si_code
)
109 /* If the faulting address is within the stack, or within one
110 page of the stack end, assume that it is a stack
112 ucontext_t
const *user_context
= context
;
113 char const *stack_min
= user_context
->uc_stack
.ss_sp
;
114 size_t stack_size
= user_context
->uc_stack
.ss_size
;
115 char const *faulting_address
= info
->si_addr
;
116 size_t s
= faulting_address
- stack_min
;
117 size_t page_size
= sysconf (_SC_PAGESIZE
);
118 if (find_stack_direction (0) < 0)
120 if (s
< stack_size
+ page_size
)
124 segv_action (signo
, info
, context
);
127 #endif /* HAVE_XSI_STACK_OVERFLOW_HEURISTIC */
130 /* Translated messages for program errors and stack overflow. Do not
131 translate them in the signal handler, since gettext is not
132 async-signal-safe. */
133 static char const * volatile program_error_message
;
134 static char const * volatile stack_overflow_message
;
136 /* Output an error message, then exit with status EXIT_FAILURE if it
137 appears to have been a stack overflow, or with a core dump
138 otherwise. This function is async-signal-safe. */
141 c_stack_die (int signo
, siginfo_t
*info
, void *context
)
143 char const *message
=
144 signo
? program_error_message
: stack_overflow_message
;
145 write (STDERR_FILENO
, program_name
, strlen (program_name
));
146 write (STDERR_FILENO
, ": ", 2);
147 write (STDERR_FILENO
, message
, strlen (message
));
148 write (STDERR_FILENO
, "\n", 1);
150 _exit (exit_failure
);
152 if (context
&& info
&& 0 <= info
->si_code
)
154 /* Re-raise the exception at the same address. */
155 char *addr
= info
->si_addr
;
159 kill (getpid (), signo
);
163 /* Set up ACTION so that it is invoked on C stack overflow. Return -1
164 (setting errno) if this cannot be done.
166 ACTION must invoke only async-signal-safe functions. ACTION
167 together with its callees must not require more than SIGSTKSZ bytes
171 c_stack_action (void (*action
) (int, siginfo_t
*, void *))
173 #if ! HAVE_XSI_STACK_OVERFLOW_HEURISTIC
177 struct sigaction act
;
182 st
.ss_sp
= alternate_signal_stack
.buffer
;
183 st
.ss_size
= sizeof alternate_signal_stack
.buffer
;
184 r
= sigaltstack (&st
, 0);
188 program_error_message
= _("program error");
189 stack_overflow_message
= _("stack overflow");
190 segv_action
= action
;
192 sigemptyset (&act
.sa_mask
);
194 /* POSIX 1003.1-2001 says SA_RESETHAND implies SA_NODEFER, but this
195 is not true on Solaris 8 at least. It doesn't hurt to use
196 SA_NODEFER here, so leave it in. */
197 act
.sa_flags
= SA_NODEFER
| SA_ONSTACK
| SA_RESETHAND
| SA_SIGINFO
;
199 act
.sa_sigaction
= segv_handler
;
200 return sigaction (SIGSEGV
, &act
, 0);
208 int volatile exit_failure
;
215 return *p
+ recurse (array
);
221 main (int argc
, char **argv
)
223 program_name
= argv
[0];
224 c_stack_action (c_stack_die
);
225 return recurse ("\1");
232 compile-command: "gcc -D_GNU_SOURCE -DDEBUG \
233 -DHAVE_INTTYPES_H -DHAVE_SIGINFO_T \
234 -DHAVE_XSI_STACK_OVERFLOW_HEURISTIC -DHAVE_UNISTD_H \
235 -Wall -W -g c-stack.c -o c-stack"