3 /* Stack overflow handling.
5 Copyright (C) 2002 Free Software Foundation, Inc.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* Written by Paul Eggert. */
23 /* This module assumes that each stack frame is smaller than a page.
24 If you use alloca, dynamic arrays, or large local variables, your
25 program may extend the stack by more than a page at a time. If so,
26 the code below may incorrectly report a program error, or worse
27 yet, may not detect the overflow at all. To avoid this problem,
28 don't use large local arrays. */
35 #define _(msgid) gettext (msgid)
39 # define ENOTSUP EINVAL
43 # include <inttypes.h>
58 # define STDERR_FILENO 2
64 extern char *program_name
;
66 #if HAVE_XSI_STACK_OVERFLOW_HEURISTIC
68 # include <ucontext.h>
71 /* Storage for the alternate signal stack. */
74 char buffer
[SIGSTKSZ
];
76 /* These other members are for proper alignment. There's no
77 standard way to guarantee stack alignment, but this seems enough
82 } alternate_signal_stack
;
85 /* Direction of the C runtime stack. This function is
89 # define find_stack_direction(ptr) STACK_DIRECTION
92 find_stack_direction (char const *addr
)
95 return ! addr
? find_stack_direction (&dummy
) : addr
< &dummy
? 1 : -1;
99 /* The SIGSEGV handler. */
100 static void (* volatile segv_action
) (int, siginfo_t
*, void *);
102 /* Handle a segmentation violation and exit. This function is
103 async-signal-safe. */
106 segv_handler (int signo
, siginfo_t
*info
, void *context
)
108 /* Clear SIGNO if it seems to have been a stack overflow. */
109 if (0 < info
->si_code
)
111 /* If the faulting address is within the stack, or within one
112 page of the stack end, assume that it is a stack
114 ucontext_t
const *user_context
= context
;
115 char const *stack_min
= user_context
->uc_stack
.ss_sp
;
116 size_t stack_size
= user_context
->uc_stack
.ss_size
;
117 char const *faulting_address
= info
->si_addr
;
118 size_t s
= faulting_address
- stack_min
;
119 size_t page_size
= sysconf (_SC_PAGESIZE
);
120 if (find_stack_direction (0) < 0)
122 if (s
< stack_size
+ page_size
)
126 segv_action (signo
, info
, context
);
129 #endif /* HAVE_XSI_STACK_OVERFLOW_HEURISTIC */
132 /* Translated messages for program errors and stack overflow. Do not
133 translate them in the signal handler, since gettext is not
134 async-signal-safe. */
135 static char const * volatile program_error_message
;
136 static char const * volatile stack_overflow_message
;
138 /* Output an error message, then exit with status EXIT_FAILURE if it
139 appears to have been a stack overflow, or with a core dump
140 otherwise. This function is async-signal-safe. */
143 c_stack_die (int signo
, siginfo_t
*info
, void *context
)
145 char const *message
=
146 signo
? program_error_message
: stack_overflow_message
;
147 write (STDERR_FILENO
, program_name
, strlen (program_name
));
148 write (STDERR_FILENO
, ": ", 2);
149 write (STDERR_FILENO
, message
, strlen (message
));
150 write (STDERR_FILENO
, "\n", 1);
152 _exit (exit_failure
);
154 if (context
&& info
&& 0 <= info
->si_code
)
156 /* Re-raise the exception at the same address. */
157 char *addr
= info
->si_addr
;
161 kill (getpid (), signo
);
165 /* Set up ACTION so that it is invoked on C stack overflow. Return -1
166 (setting errno) if this cannot be done.
168 ACTION must invoke only async-signal-safe functions. ACTION
169 together with its callees must not require more than SIGSTKSZ bytes
173 c_stack_action (void (*action
) (int, siginfo_t
*, void *))
175 #if ! HAVE_XSI_STACK_OVERFLOW_HEURISTIC
179 struct sigaction act
;
184 st
.ss_sp
= alternate_signal_stack
.buffer
;
185 st
.ss_size
= sizeof alternate_signal_stack
.buffer
;
186 r
= sigaltstack (&st
, 0);
190 program_error_message
= _("program error");
191 stack_overflow_message
= _("stack overflow");
192 segv_action
= action
;
194 sigemptyset (&act
.sa_mask
);
196 /* POSIX 1003.1-2001 says SA_RESETHAND implies SA_NODEFER, but this
197 is not true on Solaris 8 at least. It doesn't hurt to use
198 SA_NODEFER here, so leave it in. */
199 act
.sa_flags
= SA_NODEFER
| SA_ONSTACK
| SA_RESETHAND
| SA_SIGINFO
;
201 act
.sa_sigaction
= segv_handler
;
202 return sigaction (SIGSEGV
, &act
, 0);
210 int volatile exit_failure
;
217 return *p
+ recurse (array
);
223 main (int argc
, char **argv
)
225 program_name
= argv
[0];
226 c_stack_action (c_stack_die
);
227 return recurse ("\1");
234 compile-command: "gcc -D_GNU_SOURCE -DDEBUG \
235 -DHAVE_INTTYPES_H -DHAVE_SIGINFO_T \
236 -DHAVE_XSI_STACK_OVERFLOW_HEURISTIC -DHAVE_UNISTD_H \
237 -Wall -W -g c-stack.c -o c-stack"