xref: /freebsd-src/contrib/diff/lib/c-stack.c (revision 18fd37a72c3a7549d2d4f6c6ea00bdcd2bdaca01)
1*18fd37a7SXin LI /* Stack overflow handling.
2*18fd37a7SXin LI 
3*18fd37a7SXin LI    Copyright (C) 2002, 2004 Free Software Foundation, Inc.
4*18fd37a7SXin LI 
5*18fd37a7SXin LI    This program is free software; you can redistribute it and/or modify
6*18fd37a7SXin LI    it under the terms of the GNU General Public License as published by
7*18fd37a7SXin LI    the Free Software Foundation; either version 2, or (at your option)
8*18fd37a7SXin LI    any later version.
9*18fd37a7SXin LI 
10*18fd37a7SXin LI    This program is distributed in the hope that it will be useful,
11*18fd37a7SXin LI    but WITHOUT ANY WARRANTY; without even the implied warranty of
12*18fd37a7SXin LI    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13*18fd37a7SXin LI    GNU General Public License for more details.
14*18fd37a7SXin LI 
15*18fd37a7SXin LI    You should have received a copy of the GNU General Public License
16*18fd37a7SXin LI    along with this program; if not, write to the Free Software Foundation,
17*18fd37a7SXin LI    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
18*18fd37a7SXin LI 
19*18fd37a7SXin LI /* Written by Paul Eggert.  */
20*18fd37a7SXin LI 
21*18fd37a7SXin LI /* NOTES:
22*18fd37a7SXin LI 
23*18fd37a7SXin LI    A program that uses alloca, dynamic arrays, or large local
24*18fd37a7SXin LI    variables may extend the stack by more than a page at a time.  If
25*18fd37a7SXin LI    so, when the stack overflows the operating system may not detect
26*18fd37a7SXin LI    the overflow until the program uses the array, and this module may
27*18fd37a7SXin LI    incorrectly report a program error instead of a stack overflow.
28*18fd37a7SXin LI 
29*18fd37a7SXin LI    To avoid this problem, allocate only small objects on the stack; a
30*18fd37a7SXin LI    program should be OK if it limits single allocations to a page or
31*18fd37a7SXin LI    less.  Allocate larger arrays in static storage, or on the heap
32*18fd37a7SXin LI    (e.g., with malloc).  Yes, this is a pain, but we don't know of any
33*18fd37a7SXin LI    better solution that is portable.
34*18fd37a7SXin LI 
35*18fd37a7SXin LI    No attempt has been made to deal with multithreaded applications.  */
36*18fd37a7SXin LI 
37*18fd37a7SXin LI #if HAVE_CONFIG_H
38*18fd37a7SXin LI # include <config.h>
39*18fd37a7SXin LI #endif
40*18fd37a7SXin LI 
41*18fd37a7SXin LI #ifndef __attribute__
42*18fd37a7SXin LI # if __GNUC__ < 3 || __STRICT_ANSI__
43*18fd37a7SXin LI #  define __attribute__(x)
44*18fd37a7SXin LI # endif
45*18fd37a7SXin LI #endif
46*18fd37a7SXin LI 
47*18fd37a7SXin LI #include "gettext.h"
48*18fd37a7SXin LI #define _(msgid) gettext (msgid)
49*18fd37a7SXin LI 
50*18fd37a7SXin LI #include <errno.h>
51*18fd37a7SXin LI #ifndef ENOTSUP
52*18fd37a7SXin LI # define ENOTSUP EINVAL
53*18fd37a7SXin LI #endif
54*18fd37a7SXin LI #ifndef EOVERFLOW
55*18fd37a7SXin LI # define EOVERFLOW EINVAL
56*18fd37a7SXin LI #endif
57*18fd37a7SXin LI 
58*18fd37a7SXin LI #include <signal.h>
59*18fd37a7SXin LI #if ! HAVE_STACK_T && ! defined stack_t
60*18fd37a7SXin LI typedef struct sigaltstack stack_t;
61*18fd37a7SXin LI #endif
62*18fd37a7SXin LI 
63*18fd37a7SXin LI #include <stdlib.h>
64*18fd37a7SXin LI #include <string.h>
65*18fd37a7SXin LI 
66*18fd37a7SXin LI #if HAVE_SYS_RESOURCE_H
67*18fd37a7SXin LI /* Include sys/time.h here, because...
68*18fd37a7SXin LI    SunOS-4.1.x <sys/resource.h> fails to include <sys/time.h>.
69*18fd37a7SXin LI    This gives "incomplete type" errors for ru_utime and tu_stime.  */
70*18fd37a7SXin LI # if HAVE_SYS_TIME_H
71*18fd37a7SXin LI #  include <sys/time.h>
72*18fd37a7SXin LI # endif
73*18fd37a7SXin LI # include <sys/resource.h>
74*18fd37a7SXin LI #endif
75*18fd37a7SXin LI 
76*18fd37a7SXin LI #if HAVE_UCONTEXT_H
77*18fd37a7SXin LI # include <ucontext.h>
78*18fd37a7SXin LI #endif
79*18fd37a7SXin LI 
80*18fd37a7SXin LI #if HAVE_UNISTD_H
81*18fd37a7SXin LI # include <unistd.h>
82*18fd37a7SXin LI #endif
83*18fd37a7SXin LI #ifndef STDERR_FILENO
84*18fd37a7SXin LI # define STDERR_FILENO 2
85*18fd37a7SXin LI #endif
86*18fd37a7SXin LI 
87*18fd37a7SXin LI #if DEBUG
88*18fd37a7SXin LI # include <stdio.h>
89*18fd37a7SXin LI #endif
90*18fd37a7SXin LI 
91*18fd37a7SXin LI #include "c-stack.h"
92*18fd37a7SXin LI #include "exitfail.h"
93*18fd37a7SXin LI 
94*18fd37a7SXin LI #if (HAVE_STRUCT_SIGACTION_SA_SIGACTION && defined SA_NODEFER \
95*18fd37a7SXin LI      && defined SA_ONSTACK && defined SA_RESETHAND && defined SA_SIGINFO)
96*18fd37a7SXin LI # define SIGACTION_WORKS 1
97*18fd37a7SXin LI #else
98*18fd37a7SXin LI # define SIGACTION_WORKS 0
99*18fd37a7SXin LI #endif
100*18fd37a7SXin LI 
101*18fd37a7SXin LI extern char *program_name;
102*18fd37a7SXin LI 
103*18fd37a7SXin LI /* The user-specified action to take when a SEGV-related program error
104*18fd37a7SXin LI    or stack overflow occurs.  */
105*18fd37a7SXin LI static void (* volatile segv_action) (int);
106*18fd37a7SXin LI 
107*18fd37a7SXin LI /* Translated messages for program errors and stack overflow.  Do not
108*18fd37a7SXin LI    translate them in the signal handler, since gettext is not
109*18fd37a7SXin LI    async-signal-safe.  */
110*18fd37a7SXin LI static char const * volatile program_error_message;
111*18fd37a7SXin LI static char const * volatile stack_overflow_message;
112*18fd37a7SXin LI 
113*18fd37a7SXin LI /* Output an error message, then exit with status EXIT_FAILURE if it
114*18fd37a7SXin LI    appears to have been a stack overflow, or with a core dump
115*18fd37a7SXin LI    otherwise.  This function is async-signal-safe.  */
116*18fd37a7SXin LI 
117*18fd37a7SXin LI static void die (int) __attribute__ ((noreturn));
118*18fd37a7SXin LI static void
die(int signo)119*18fd37a7SXin LI die (int signo)
120*18fd37a7SXin LI {
121*18fd37a7SXin LI   char const *message;
122*18fd37a7SXin LI   segv_action (signo);
123*18fd37a7SXin LI   message = signo ? program_error_message : stack_overflow_message;
124*18fd37a7SXin LI   write (STDERR_FILENO, program_name, strlen (program_name));
125*18fd37a7SXin LI   write (STDERR_FILENO, ": ", 2);
126*18fd37a7SXin LI   write (STDERR_FILENO, message, strlen (message));
127*18fd37a7SXin LI   write (STDERR_FILENO, "\n", 1);
128*18fd37a7SXin LI   if (! signo)
129*18fd37a7SXin LI     _exit (exit_failure);
130*18fd37a7SXin LI   kill (getpid (), signo);
131*18fd37a7SXin LI   abort ();
132*18fd37a7SXin LI }
133*18fd37a7SXin LI 
134*18fd37a7SXin LI #if HAVE_SIGALTSTACK && HAVE_DECL_SIGALTSTACK
135*18fd37a7SXin LI 
136*18fd37a7SXin LI /* Direction of the C runtime stack.  This function is
137*18fd37a7SXin LI    async-signal-safe.  */
138*18fd37a7SXin LI 
139*18fd37a7SXin LI # if STACK_DIRECTION
140*18fd37a7SXin LI #  define find_stack_direction(ptr) STACK_DIRECTION
141*18fd37a7SXin LI # else
142*18fd37a7SXin LI static int
find_stack_direction(char const * addr)143*18fd37a7SXin LI find_stack_direction (char const *addr)
144*18fd37a7SXin LI {
145*18fd37a7SXin LI   char dummy;
146*18fd37a7SXin LI   return ! addr ? find_stack_direction (&dummy) : addr < &dummy ? 1 : -1;
147*18fd37a7SXin LI }
148*18fd37a7SXin LI # endif
149*18fd37a7SXin LI 
150*18fd37a7SXin LI /* Storage for the alternate signal stack.  */
151*18fd37a7SXin LI static union
152*18fd37a7SXin LI {
153*18fd37a7SXin LI   char buffer[SIGSTKSZ];
154*18fd37a7SXin LI 
155*18fd37a7SXin LI   /* These other members are for proper alignment.  There's no
156*18fd37a7SXin LI      standard way to guarantee stack alignment, but this seems enough
157*18fd37a7SXin LI      in practice.  */
158*18fd37a7SXin LI   long double ld;
159*18fd37a7SXin LI   long l;
160*18fd37a7SXin LI   void *p;
161*18fd37a7SXin LI } alternate_signal_stack;
162*18fd37a7SXin LI 
163*18fd37a7SXin LI # if SIGACTION_WORKS
164*18fd37a7SXin LI 
165*18fd37a7SXin LI /* Handle a segmentation violation and exit.  This function is
166*18fd37a7SXin LI    async-signal-safe.  */
167*18fd37a7SXin LI 
168*18fd37a7SXin LI static void segv_handler (int, siginfo_t *, void *) __attribute__((noreturn));
169*18fd37a7SXin LI static void
segv_handler(int signo,siginfo_t * info,void * context)170*18fd37a7SXin LI segv_handler (int signo, siginfo_t *info,
171*18fd37a7SXin LI 	      void *context __attribute__ ((unused)))
172*18fd37a7SXin LI {
173*18fd37a7SXin LI   /* Clear SIGNO if it seems to have been a stack overflow.  */
174*18fd37a7SXin LI   if (0 < info->si_code)
175*18fd37a7SXin LI     {
176*18fd37a7SXin LI #  if ! HAVE_XSI_STACK_OVERFLOW_HEURISTIC
177*18fd37a7SXin LI       /* We can't easily determine whether it is a stack overflow; so
178*18fd37a7SXin LI 	 assume that the rest of our program is perfect (!) and that
179*18fd37a7SXin LI 	 this segmentation violation is a stack overflow.  */
180*18fd37a7SXin LI       signo = 0;
181*18fd37a7SXin LI #  else
182*18fd37a7SXin LI       /* If the faulting address is within the stack, or within one
183*18fd37a7SXin LI 	 page of the stack end, assume that it is a stack
184*18fd37a7SXin LI 	 overflow.  */
185*18fd37a7SXin LI       ucontext_t const *user_context = context;
186*18fd37a7SXin LI       char const *stack_base = user_context->uc_stack.ss_sp;
187*18fd37a7SXin LI       size_t stack_size = user_context->uc_stack.ss_size;
188*18fd37a7SXin LI       char const *faulting_address = info->si_addr;
189*18fd37a7SXin LI       size_t s = faulting_address - stack_base;
190*18fd37a7SXin LI       size_t page_size = sysconf (_SC_PAGESIZE);
191*18fd37a7SXin LI       if (find_stack_direction (0) < 0)
192*18fd37a7SXin LI 	s += page_size;
193*18fd37a7SXin LI       if (s < stack_size + page_size)
194*18fd37a7SXin LI 	signo = 0;
195*18fd37a7SXin LI 
196*18fd37a7SXin LI #   if DEBUG
197*18fd37a7SXin LI       {
198*18fd37a7SXin LI 	char buf[1024];
199*18fd37a7SXin LI 	sprintf (buf,
200*18fd37a7SXin LI 		 "segv_handler fault=%p base=%p size=%lx page=%lx signo=%d\n",
201*18fd37a7SXin LI 		 faulting_address, stack_base, (unsigned long) stack_size,
202*18fd37a7SXin LI 		 (unsigned long) page_size, signo);
203*18fd37a7SXin LI 	write (STDERR_FILENO, buf, strlen (buf));
204*18fd37a7SXin LI       }
205*18fd37a7SXin LI #   endif
206*18fd37a7SXin LI #  endif
207*18fd37a7SXin LI     }
208*18fd37a7SXin LI 
209*18fd37a7SXin LI   die (signo);
210*18fd37a7SXin LI }
211*18fd37a7SXin LI # endif
212*18fd37a7SXin LI 
213*18fd37a7SXin LI static void
null_action(int signo)214*18fd37a7SXin LI null_action (int signo __attribute__ ((unused)))
215*18fd37a7SXin LI {
216*18fd37a7SXin LI }
217*18fd37a7SXin LI 
218*18fd37a7SXin LI /* Set up ACTION so that it is invoked on C stack overflow.  Return -1
219*18fd37a7SXin LI    (setting errno) if this cannot be done.
220*18fd37a7SXin LI 
221*18fd37a7SXin LI    When ACTION is called, it is passed an argument equal to SIGSEGV
222*18fd37a7SXin LI    for a segmentation violation that does not appear related to stack
223*18fd37a7SXin LI    overflow, and is passed zero otherwise.  On many platforms it is
224*18fd37a7SXin LI    hard to tell; when in doubt, zero is passed.
225*18fd37a7SXin LI 
226*18fd37a7SXin LI    A null ACTION acts like an action that does nothing.
227*18fd37a7SXin LI 
228*18fd37a7SXin LI    ACTION must be async-signal-safe.  ACTION together with its callees
229*18fd37a7SXin LI    must not require more than SIGSTKSZ bytes of stack space.  */
230*18fd37a7SXin LI 
231*18fd37a7SXin LI int
c_stack_action(void (* action)(int))232*18fd37a7SXin LI c_stack_action (void (*action) (int))
233*18fd37a7SXin LI {
234*18fd37a7SXin LI   int r;
235*18fd37a7SXin LI   stack_t st;
236*18fd37a7SXin LI   st.ss_flags = 0;
237*18fd37a7SXin LI   st.ss_sp = alternate_signal_stack.buffer;
238*18fd37a7SXin LI   st.ss_size = sizeof alternate_signal_stack.buffer;
239*18fd37a7SXin LI   r = sigaltstack (&st, 0);
240*18fd37a7SXin LI   if (r != 0)
241*18fd37a7SXin LI     return r;
242*18fd37a7SXin LI 
243*18fd37a7SXin LI   segv_action = action ? action : null_action;
244*18fd37a7SXin LI   program_error_message = _("program error");
245*18fd37a7SXin LI   stack_overflow_message = _("stack overflow");
246*18fd37a7SXin LI 
247*18fd37a7SXin LI   {
248*18fd37a7SXin LI # if SIGACTION_WORKS
249*18fd37a7SXin LI     struct sigaction act;
250*18fd37a7SXin LI     sigemptyset (&act.sa_mask);
251*18fd37a7SXin LI 
252*18fd37a7SXin LI     /* POSIX 1003.1-2001 says SA_RESETHAND implies SA_NODEFER, but
253*18fd37a7SXin LI        this is not true on Solaris 8 at least.  It doesn't hurt to use
254*18fd37a7SXin LI        SA_NODEFER here, so leave it in.  */
255*18fd37a7SXin LI     act.sa_flags = SA_NODEFER | SA_ONSTACK | SA_RESETHAND | SA_SIGINFO;
256*18fd37a7SXin LI 
257*18fd37a7SXin LI     act.sa_sigaction = segv_handler;
258*18fd37a7SXin LI 
259*18fd37a7SXin LI     return sigaction (SIGSEGV, &act, 0);
260*18fd37a7SXin LI # else
261*18fd37a7SXin LI     return signal (SIGSEGV, die) == SIG_ERR ? -1 : 0;
262*18fd37a7SXin LI # endif
263*18fd37a7SXin LI   }
264*18fd37a7SXin LI }
265*18fd37a7SXin LI 
266*18fd37a7SXin LI #else /* ! (HAVE_SIGALTSTACK && HAVE_DECL_SIGALTSTACK) */
267*18fd37a7SXin LI 
268*18fd37a7SXin LI int
c_stack_action(void (* action)(int))269*18fd37a7SXin LI c_stack_action (void (*action) (int)  __attribute__ ((unused)))
270*18fd37a7SXin LI {
271*18fd37a7SXin LI   errno = ENOTSUP;
272*18fd37a7SXin LI   return -1;
273*18fd37a7SXin LI }
274*18fd37a7SXin LI 
275*18fd37a7SXin LI #endif
276*18fd37a7SXin LI 
277*18fd37a7SXin LI 
278*18fd37a7SXin LI 
279*18fd37a7SXin LI #if DEBUG
280*18fd37a7SXin LI 
281*18fd37a7SXin LI int volatile exit_failure;
282*18fd37a7SXin LI 
283*18fd37a7SXin LI static long
recurse(char * p)284*18fd37a7SXin LI recurse (char *p)
285*18fd37a7SXin LI {
286*18fd37a7SXin LI   char array[500];
287*18fd37a7SXin LI   array[0] = 1;
288*18fd37a7SXin LI   return *p + recurse (array);
289*18fd37a7SXin LI }
290*18fd37a7SXin LI 
291*18fd37a7SXin LI char *program_name;
292*18fd37a7SXin LI 
293*18fd37a7SXin LI int
main(int argc,char ** argv)294*18fd37a7SXin LI main (int argc __attribute__ ((unused)), char **argv)
295*18fd37a7SXin LI {
296*18fd37a7SXin LI   program_name = argv[0];
297*18fd37a7SXin LI   fprintf (stderr,
298*18fd37a7SXin LI 	   "The last output line should contain \"stack overflow\".\n");
299*18fd37a7SXin LI   if (c_stack_action (0) == 0)
300*18fd37a7SXin LI     return recurse ("\1");
301*18fd37a7SXin LI   perror ("c_stack_action");
302*18fd37a7SXin LI   return 1;
303*18fd37a7SXin LI }
304*18fd37a7SXin LI 
305*18fd37a7SXin LI #endif /* DEBUG */
306*18fd37a7SXin LI 
307*18fd37a7SXin LI /*
308*18fd37a7SXin LI Local Variables:
309*18fd37a7SXin LI compile-command: "gcc -DDEBUG -DHAVE_CONFIG_H -I.. -g -O -Wall -W c-stack.c"
310*18fd37a7SXin LI End:
311*18fd37a7SXin LI */
312