1*09d4459fSDaniel Fojt /* Stack overflow handling.
2*09d4459fSDaniel Fojt
3*09d4459fSDaniel Fojt Copyright (C) 2002, 2004, 2006, 2008-2020 Free Software Foundation, Inc.
4*09d4459fSDaniel Fojt
5*09d4459fSDaniel Fojt This program is free software: you can redistribute it and/or modify
6*09d4459fSDaniel Fojt it under the terms of the GNU General Public License as published by
7*09d4459fSDaniel Fojt the Free Software Foundation; either version 3 of the License, or
8*09d4459fSDaniel Fojt (at your option) any later version.
9*09d4459fSDaniel Fojt
10*09d4459fSDaniel Fojt This program is distributed in the hope that it will be useful,
11*09d4459fSDaniel Fojt but WITHOUT ANY WARRANTY; without even the implied warranty of
12*09d4459fSDaniel Fojt MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*09d4459fSDaniel Fojt GNU General Public License for more details.
14*09d4459fSDaniel Fojt
15*09d4459fSDaniel Fojt You should have received a copy of the GNU General Public License
16*09d4459fSDaniel Fojt along with this program. If not, see <https://www.gnu.org/licenses/>. */
17*09d4459fSDaniel Fojt
18*09d4459fSDaniel Fojt /* Written by Paul Eggert. */
19*09d4459fSDaniel Fojt
20*09d4459fSDaniel Fojt /* NOTES:
21*09d4459fSDaniel Fojt
22*09d4459fSDaniel Fojt A program that uses alloca, dynamic arrays, or large local
23*09d4459fSDaniel Fojt variables may extend the stack by more than a page at a time. If
24*09d4459fSDaniel Fojt so, when the stack overflows the operating system may not detect
25*09d4459fSDaniel Fojt the overflow until the program uses the array, and this module may
26*09d4459fSDaniel Fojt incorrectly report a program error instead of a stack overflow.
27*09d4459fSDaniel Fojt
28*09d4459fSDaniel Fojt To avoid this problem, allocate only small objects on the stack; a
29*09d4459fSDaniel Fojt program should be OK if it limits single allocations to a page or
30*09d4459fSDaniel Fojt less. Allocate larger arrays in static storage, or on the heap
31*09d4459fSDaniel Fojt (e.g., with malloc). Yes, this is a pain, but we don't know of any
32*09d4459fSDaniel Fojt better solution that is portable.
33*09d4459fSDaniel Fojt
34*09d4459fSDaniel Fojt No attempt has been made to deal with multithreaded applications. */
35*09d4459fSDaniel Fojt
36*09d4459fSDaniel Fojt #include <config.h>
37*09d4459fSDaniel Fojt
38*09d4459fSDaniel Fojt #ifndef __attribute__
39*09d4459fSDaniel Fojt # if __GNUC__ < 3
40*09d4459fSDaniel Fojt # define __attribute__(x)
41*09d4459fSDaniel Fojt # endif
42*09d4459fSDaniel Fojt #endif
43*09d4459fSDaniel Fojt
44*09d4459fSDaniel Fojt #include "gettext.h"
45*09d4459fSDaniel Fojt #define _(msgid) gettext (msgid)
46*09d4459fSDaniel Fojt
47*09d4459fSDaniel Fojt #include <errno.h>
48*09d4459fSDaniel Fojt
49*09d4459fSDaniel Fojt #include <signal.h>
50*09d4459fSDaniel Fojt #if ! HAVE_STACK_T && ! defined stack_t
51*09d4459fSDaniel Fojt typedef struct sigaltstack stack_t;
52*09d4459fSDaniel Fojt #endif
53*09d4459fSDaniel Fojt #ifndef SIGSTKSZ
54*09d4459fSDaniel Fojt # define SIGSTKSZ 16384
55*09d4459fSDaniel Fojt #elif HAVE_LIBSIGSEGV && SIGSTKSZ < 16384
56*09d4459fSDaniel Fojt /* libsigsegv 2.6 through 2.8 have a bug where some architectures use
57*09d4459fSDaniel Fojt more than the Linux default of an 8k alternate stack when deciding
58*09d4459fSDaniel Fojt if a fault was caused by stack overflow. */
59*09d4459fSDaniel Fojt # undef SIGSTKSZ
60*09d4459fSDaniel Fojt # define SIGSTKSZ 16384
61*09d4459fSDaniel Fojt #endif
62*09d4459fSDaniel Fojt
63*09d4459fSDaniel Fojt #include <stdlib.h>
64*09d4459fSDaniel Fojt #include <string.h>
65*09d4459fSDaniel Fojt
66*09d4459fSDaniel Fojt /* Posix 2001 declares ucontext_t in <ucontext.h>, Posix 200x in
67*09d4459fSDaniel Fojt <signal.h>. */
68*09d4459fSDaniel Fojt #if HAVE_UCONTEXT_H
69*09d4459fSDaniel Fojt # include <ucontext.h>
70*09d4459fSDaniel Fojt #endif
71*09d4459fSDaniel Fojt
72*09d4459fSDaniel Fojt #include <unistd.h>
73*09d4459fSDaniel Fojt
74*09d4459fSDaniel Fojt #if HAVE_LIBSIGSEGV
75*09d4459fSDaniel Fojt # include <sigsegv.h>
76*09d4459fSDaniel Fojt #endif
77*09d4459fSDaniel Fojt
78*09d4459fSDaniel Fojt #include "c-stack.h"
79*09d4459fSDaniel Fojt #include "exitfail.h"
80*09d4459fSDaniel Fojt #include "ignore-value.h"
81*09d4459fSDaniel Fojt #include "getprogname.h"
82*09d4459fSDaniel Fojt
83*09d4459fSDaniel Fojt #if defined SA_ONSTACK && defined SA_SIGINFO
84*09d4459fSDaniel Fojt # define SIGINFO_WORKS 1
85*09d4459fSDaniel Fojt #else
86*09d4459fSDaniel Fojt # define SIGINFO_WORKS 0
87*09d4459fSDaniel Fojt # ifndef SA_ONSTACK
88*09d4459fSDaniel Fojt # define SA_ONSTACK 0
89*09d4459fSDaniel Fojt # endif
90*09d4459fSDaniel Fojt #endif
91*09d4459fSDaniel Fojt
92*09d4459fSDaniel Fojt /* The user-specified action to take when a SEGV-related program error
93*09d4459fSDaniel Fojt or stack overflow occurs. */
94*09d4459fSDaniel Fojt static _GL_ASYNC_SAFE void (* volatile segv_action) (int);
95*09d4459fSDaniel Fojt
96*09d4459fSDaniel Fojt /* Translated messages for program errors and stack overflow. Do not
97*09d4459fSDaniel Fojt translate them in the signal handler, since gettext is not
98*09d4459fSDaniel Fojt async-signal-safe. */
99*09d4459fSDaniel Fojt static char const * volatile program_error_message;
100*09d4459fSDaniel Fojt static char const * volatile stack_overflow_message;
101*09d4459fSDaniel Fojt
102*09d4459fSDaniel Fojt #if ((HAVE_LIBSIGSEGV && ! HAVE_XSI_STACK_OVERFLOW_HEURISTIC) \
103*09d4459fSDaniel Fojt || (HAVE_SIGALTSTACK && HAVE_DECL_SIGALTSTACK \
104*09d4459fSDaniel Fojt && HAVE_STACK_OVERFLOW_HANDLING))
105*09d4459fSDaniel Fojt
106*09d4459fSDaniel Fojt /* Output an error message, then exit with status EXIT_FAILURE if it
107*09d4459fSDaniel Fojt appears to have been a stack overflow, or with a core dump
108*09d4459fSDaniel Fojt otherwise. This function is async-signal-safe. */
109*09d4459fSDaniel Fojt
110*09d4459fSDaniel Fojt static char const * volatile progname;
111*09d4459fSDaniel Fojt
112*09d4459fSDaniel Fojt static _GL_ASYNC_SAFE _Noreturn void
die(int signo)113*09d4459fSDaniel Fojt die (int signo)
114*09d4459fSDaniel Fojt {
115*09d4459fSDaniel Fojt char const *message;
116*09d4459fSDaniel Fojt #if !SIGINFO_WORKS && !HAVE_LIBSIGSEGV
117*09d4459fSDaniel Fojt /* We can't easily determine whether it is a stack overflow; so
118*09d4459fSDaniel Fojt assume that the rest of our program is perfect (!) and that
119*09d4459fSDaniel Fojt this segmentation violation is a stack overflow. */
120*09d4459fSDaniel Fojt signo = 0;
121*09d4459fSDaniel Fojt #endif /* !SIGINFO_WORKS && !HAVE_LIBSIGSEGV */
122*09d4459fSDaniel Fojt segv_action (signo);
123*09d4459fSDaniel Fojt message = signo ? program_error_message : stack_overflow_message;
124*09d4459fSDaniel Fojt ignore_value (write (STDERR_FILENO, progname, strlen (progname)));
125*09d4459fSDaniel Fojt ignore_value (write (STDERR_FILENO, ": ", 2));
126*09d4459fSDaniel Fojt ignore_value (write (STDERR_FILENO, message, strlen (message)));
127*09d4459fSDaniel Fojt ignore_value (write (STDERR_FILENO, "\n", 1));
128*09d4459fSDaniel Fojt if (! signo)
129*09d4459fSDaniel Fojt _exit (exit_failure);
130*09d4459fSDaniel Fojt raise (signo);
131*09d4459fSDaniel Fojt abort ();
132*09d4459fSDaniel Fojt }
133*09d4459fSDaniel Fojt #endif
134*09d4459fSDaniel Fojt
135*09d4459fSDaniel Fojt #if (HAVE_SIGALTSTACK && HAVE_DECL_SIGALTSTACK \
136*09d4459fSDaniel Fojt && HAVE_STACK_OVERFLOW_HANDLING) || HAVE_LIBSIGSEGV
137*09d4459fSDaniel Fojt
138*09d4459fSDaniel Fojt /* Storage for the alternate signal stack. */
139*09d4459fSDaniel Fojt static union
140*09d4459fSDaniel Fojt {
141*09d4459fSDaniel Fojt char buffer[SIGSTKSZ];
142*09d4459fSDaniel Fojt
143*09d4459fSDaniel Fojt /* These other members are for proper alignment. There's no
144*09d4459fSDaniel Fojt standard way to guarantee stack alignment, but this seems enough
145*09d4459fSDaniel Fojt in practice. */
146*09d4459fSDaniel Fojt long double ld;
147*09d4459fSDaniel Fojt long l;
148*09d4459fSDaniel Fojt void *p;
149*09d4459fSDaniel Fojt } alternate_signal_stack;
150*09d4459fSDaniel Fojt
151*09d4459fSDaniel Fojt static _GL_ASYNC_SAFE void
null_action(int signo _GL_UNUSED)152*09d4459fSDaniel Fojt null_action (int signo _GL_UNUSED)
153*09d4459fSDaniel Fojt {
154*09d4459fSDaniel Fojt }
155*09d4459fSDaniel Fojt
156*09d4459fSDaniel Fojt #endif /* SIGALTSTACK || LIBSIGSEGV */
157*09d4459fSDaniel Fojt
158*09d4459fSDaniel Fojt /* Only use libsigsegv if we need it; platforms like Solaris can
159*09d4459fSDaniel Fojt detect stack overflow without the overhead of an external
160*09d4459fSDaniel Fojt library. */
161*09d4459fSDaniel Fojt #if HAVE_LIBSIGSEGV && ! HAVE_XSI_STACK_OVERFLOW_HEURISTIC
162*09d4459fSDaniel Fojt
163*09d4459fSDaniel Fojt /* Nonzero if general segv handler could not be installed. */
164*09d4459fSDaniel Fojt static volatile int segv_handler_missing;
165*09d4459fSDaniel Fojt
166*09d4459fSDaniel Fojt /* Handle a segmentation violation and exit if it cannot be stack
167*09d4459fSDaniel Fojt overflow. This function is async-signal-safe. */
168*09d4459fSDaniel Fojt
169*09d4459fSDaniel Fojt static _GL_ASYNC_SAFE int
segv_handler(void * address _GL_UNUSED,int serious)170*09d4459fSDaniel Fojt segv_handler (void *address _GL_UNUSED, int serious)
171*09d4459fSDaniel Fojt {
172*09d4459fSDaniel Fojt # if DEBUG
173*09d4459fSDaniel Fojt {
174*09d4459fSDaniel Fojt char buf[1024];
175*09d4459fSDaniel Fojt int saved_errno = errno;
176*09d4459fSDaniel Fojt sprintf (buf, "segv_handler serious=%d\n", serious);
177*09d4459fSDaniel Fojt write (STDERR_FILENO, buf, strlen (buf));
178*09d4459fSDaniel Fojt errno = saved_errno;
179*09d4459fSDaniel Fojt }
180*09d4459fSDaniel Fojt # endif
181*09d4459fSDaniel Fojt
182*09d4459fSDaniel Fojt /* If this fault is not serious, return 0 to let the stack overflow
183*09d4459fSDaniel Fojt handler take a shot at it. */
184*09d4459fSDaniel Fojt if (!serious)
185*09d4459fSDaniel Fojt return 0;
186*09d4459fSDaniel Fojt die (SIGSEGV);
187*09d4459fSDaniel Fojt }
188*09d4459fSDaniel Fojt
189*09d4459fSDaniel Fojt /* Handle a segmentation violation that is likely to be a stack
190*09d4459fSDaniel Fojt overflow and exit. This function is async-signal-safe. */
191*09d4459fSDaniel Fojt
192*09d4459fSDaniel Fojt static _GL_ASYNC_SAFE _Noreturn void
overflow_handler(int emergency,stackoverflow_context_t context _GL_UNUSED)193*09d4459fSDaniel Fojt overflow_handler (int emergency, stackoverflow_context_t context _GL_UNUSED)
194*09d4459fSDaniel Fojt {
195*09d4459fSDaniel Fojt # if DEBUG
196*09d4459fSDaniel Fojt {
197*09d4459fSDaniel Fojt char buf[1024];
198*09d4459fSDaniel Fojt sprintf (buf, "overflow_handler emergency=%d segv_handler_missing=%d\n",
199*09d4459fSDaniel Fojt emergency, segv_handler_missing);
200*09d4459fSDaniel Fojt write (STDERR_FILENO, buf, strlen (buf));
201*09d4459fSDaniel Fojt }
202*09d4459fSDaniel Fojt # endif
203*09d4459fSDaniel Fojt
204*09d4459fSDaniel Fojt die ((!emergency || segv_handler_missing) ? 0 : SIGSEGV);
205*09d4459fSDaniel Fojt }
206*09d4459fSDaniel Fojt
207*09d4459fSDaniel Fojt int
c_stack_action(_GL_ASYNC_SAFE void (* action)(int))208*09d4459fSDaniel Fojt c_stack_action (_GL_ASYNC_SAFE void (*action) (int))
209*09d4459fSDaniel Fojt {
210*09d4459fSDaniel Fojt segv_action = action ? action : null_action;
211*09d4459fSDaniel Fojt program_error_message = _("program error");
212*09d4459fSDaniel Fojt stack_overflow_message = _("stack overflow");
213*09d4459fSDaniel Fojt progname = getprogname ();
214*09d4459fSDaniel Fojt
215*09d4459fSDaniel Fojt /* Always install the overflow handler. */
216*09d4459fSDaniel Fojt if (stackoverflow_install_handler (overflow_handler,
217*09d4459fSDaniel Fojt alternate_signal_stack.buffer,
218*09d4459fSDaniel Fojt sizeof alternate_signal_stack.buffer))
219*09d4459fSDaniel Fojt {
220*09d4459fSDaniel Fojt errno = ENOTSUP;
221*09d4459fSDaniel Fojt return -1;
222*09d4459fSDaniel Fojt }
223*09d4459fSDaniel Fojt /* Try installing a general handler; if it fails, then treat all
224*09d4459fSDaniel Fojt segv as stack overflow. */
225*09d4459fSDaniel Fojt segv_handler_missing = sigsegv_install_handler (segv_handler);
226*09d4459fSDaniel Fojt return 0;
227*09d4459fSDaniel Fojt }
228*09d4459fSDaniel Fojt
229*09d4459fSDaniel Fojt #elif HAVE_SIGALTSTACK && HAVE_DECL_SIGALTSTACK && HAVE_STACK_OVERFLOW_HANDLING
230*09d4459fSDaniel Fojt
231*09d4459fSDaniel Fojt # if SIGINFO_WORKS
232*09d4459fSDaniel Fojt
233*09d4459fSDaniel Fojt /* Handle a segmentation violation and exit. This function is
234*09d4459fSDaniel Fojt async-signal-safe. */
235*09d4459fSDaniel Fojt
236*09d4459fSDaniel Fojt static _GL_ASYNC_SAFE _Noreturn void
segv_handler(int signo,siginfo_t * info,void * context _GL_UNUSED)237*09d4459fSDaniel Fojt segv_handler (int signo, siginfo_t *info, void *context _GL_UNUSED)
238*09d4459fSDaniel Fojt {
239*09d4459fSDaniel Fojt /* Clear SIGNO if it seems to have been a stack overflow. */
240*09d4459fSDaniel Fojt # if ! HAVE_XSI_STACK_OVERFLOW_HEURISTIC
241*09d4459fSDaniel Fojt /* We can't easily determine whether it is a stack overflow; so
242*09d4459fSDaniel Fojt assume that the rest of our program is perfect (!) and that
243*09d4459fSDaniel Fojt this segmentation violation is a stack overflow.
244*09d4459fSDaniel Fojt
245*09d4459fSDaniel Fojt Note that although both Linux and Solaris provide
246*09d4459fSDaniel Fojt sigaltstack, SA_ONSTACK, and SA_SIGINFO, currently only
247*09d4459fSDaniel Fojt Solaris satisfies the XSI heuristic. This is because
248*09d4459fSDaniel Fojt Solaris populates uc_stack with the details of the
249*09d4459fSDaniel Fojt interrupted stack, while Linux populates it with the details
250*09d4459fSDaniel Fojt of the current stack. */
251*09d4459fSDaniel Fojt signo = 0;
252*09d4459fSDaniel Fojt # else
253*09d4459fSDaniel Fojt if (0 < info->si_code)
254*09d4459fSDaniel Fojt {
255*09d4459fSDaniel Fojt /* If the faulting address is within the stack, or within one
256*09d4459fSDaniel Fojt page of the stack, assume that it is a stack overflow. */
257*09d4459fSDaniel Fojt ucontext_t const *user_context = context;
258*09d4459fSDaniel Fojt char const *stack_base = user_context->uc_stack.ss_sp;
259*09d4459fSDaniel Fojt size_t stack_size = user_context->uc_stack.ss_size;
260*09d4459fSDaniel Fojt char const *faulting_address = info->si_addr;
261*09d4459fSDaniel Fojt size_t page_size = sysconf (_SC_PAGESIZE);
262*09d4459fSDaniel Fojt size_t s = faulting_address - stack_base + page_size;
263*09d4459fSDaniel Fojt if (s < stack_size + 2 * page_size)
264*09d4459fSDaniel Fojt signo = 0;
265*09d4459fSDaniel Fojt
266*09d4459fSDaniel Fojt # if DEBUG
267*09d4459fSDaniel Fojt {
268*09d4459fSDaniel Fojt char buf[1024];
269*09d4459fSDaniel Fojt sprintf (buf,
270*09d4459fSDaniel Fojt "segv_handler fault=%p base=%p size=%lx page=%lx signo=%d\n",
271*09d4459fSDaniel Fojt faulting_address, stack_base, (unsigned long) stack_size,
272*09d4459fSDaniel Fojt (unsigned long) page_size, signo);
273*09d4459fSDaniel Fojt write (STDERR_FILENO, buf, strlen (buf));
274*09d4459fSDaniel Fojt }
275*09d4459fSDaniel Fojt # endif
276*09d4459fSDaniel Fojt }
277*09d4459fSDaniel Fojt # endif
278*09d4459fSDaniel Fojt
279*09d4459fSDaniel Fojt die (signo);
280*09d4459fSDaniel Fojt }
281*09d4459fSDaniel Fojt # endif
282*09d4459fSDaniel Fojt
283*09d4459fSDaniel Fojt int
c_stack_action(_GL_ASYNC_SAFE void (* action)(int))284*09d4459fSDaniel Fojt c_stack_action (_GL_ASYNC_SAFE void (*action) (int))
285*09d4459fSDaniel Fojt {
286*09d4459fSDaniel Fojt int r;
287*09d4459fSDaniel Fojt stack_t st;
288*09d4459fSDaniel Fojt struct sigaction act;
289*09d4459fSDaniel Fojt st.ss_flags = 0;
290*09d4459fSDaniel Fojt # if SIGALTSTACK_SS_REVERSED
291*09d4459fSDaniel Fojt /* Irix mistakenly treats ss_sp as the upper bound, rather than
292*09d4459fSDaniel Fojt lower bound, of the alternate stack. */
293*09d4459fSDaniel Fojt st.ss_sp = alternate_signal_stack.buffer + SIGSTKSZ - sizeof (void *);
294*09d4459fSDaniel Fojt st.ss_size = sizeof alternate_signal_stack.buffer - sizeof (void *);
295*09d4459fSDaniel Fojt # else
296*09d4459fSDaniel Fojt st.ss_sp = alternate_signal_stack.buffer;
297*09d4459fSDaniel Fojt st.ss_size = sizeof alternate_signal_stack.buffer;
298*09d4459fSDaniel Fojt # endif
299*09d4459fSDaniel Fojt r = sigaltstack (&st, NULL);
300*09d4459fSDaniel Fojt if (r != 0)
301*09d4459fSDaniel Fojt return r;
302*09d4459fSDaniel Fojt
303*09d4459fSDaniel Fojt segv_action = action ? action : null_action;
304*09d4459fSDaniel Fojt program_error_message = _("program error");
305*09d4459fSDaniel Fojt stack_overflow_message = _("stack overflow");
306*09d4459fSDaniel Fojt progname = getprogname ();
307*09d4459fSDaniel Fojt
308*09d4459fSDaniel Fojt sigemptyset (&act.sa_mask);
309*09d4459fSDaniel Fojt
310*09d4459fSDaniel Fojt # if SIGINFO_WORKS
311*09d4459fSDaniel Fojt /* POSIX 1003.1-2001 says SA_RESETHAND implies SA_NODEFER, but
312*09d4459fSDaniel Fojt this is not true on Solaris 8 at least. It doesn't hurt to use
313*09d4459fSDaniel Fojt SA_NODEFER here, so leave it in. */
314*09d4459fSDaniel Fojt act.sa_flags = SA_NODEFER | SA_ONSTACK | SA_RESETHAND | SA_SIGINFO;
315*09d4459fSDaniel Fojt act.sa_sigaction = segv_handler;
316*09d4459fSDaniel Fojt # else
317*09d4459fSDaniel Fojt act.sa_flags = SA_NODEFER | SA_ONSTACK | SA_RESETHAND;
318*09d4459fSDaniel Fojt act.sa_handler = die;
319*09d4459fSDaniel Fojt # endif
320*09d4459fSDaniel Fojt
321*09d4459fSDaniel Fojt # if FAULT_YIELDS_SIGBUS
322*09d4459fSDaniel Fojt if (sigaction (SIGBUS, &act, NULL) < 0)
323*09d4459fSDaniel Fojt return -1;
324*09d4459fSDaniel Fojt # endif
325*09d4459fSDaniel Fojt return sigaction (SIGSEGV, &act, NULL);
326*09d4459fSDaniel Fojt }
327*09d4459fSDaniel Fojt
328*09d4459fSDaniel Fojt #else /* ! ((HAVE_SIGALTSTACK && HAVE_DECL_SIGALTSTACK
329*09d4459fSDaniel Fojt && HAVE_STACK_OVERFLOW_HANDLING) || HAVE_LIBSIGSEGV) */
330*09d4459fSDaniel Fojt
331*09d4459fSDaniel Fojt int
c_stack_action(_GL_ASYNC_SAFE void (* action)(int)_GL_UNUSED)332*09d4459fSDaniel Fojt c_stack_action (_GL_ASYNC_SAFE void (*action) (int) _GL_UNUSED)
333*09d4459fSDaniel Fojt {
334*09d4459fSDaniel Fojt errno = ENOTSUP;
335*09d4459fSDaniel Fojt return -1;
336*09d4459fSDaniel Fojt }
337*09d4459fSDaniel Fojt
338*09d4459fSDaniel Fojt #endif
339