xref: /netbsd-src/external/gpl2/diffutils/dist/lib/c-stack.c (revision 76c7fc5f6b13ed0b1508e6b313e88e59977ed78e)
1 /*	$NetBSD: c-stack.c,v 1.1.1.1 2016/01/13 03:15:30 christos Exp $	*/
2 
3 /* Stack overflow handling.
4 
5    Copyright (C) 2002 Free Software Foundation, Inc.
6 
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)
10    any later version.
11 
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.
16 
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.  */
20 
21 /* Written by Paul Eggert.  */
22 
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.  */
29 
30 #if HAVE_CONFIG_H
31 # include <config.h>
32 #endif
33 
34 #include "gettext.h"
35 #define _(msgid) gettext (msgid)
36 
37 #include <errno.h>
38 #ifndef ENOTSUP
39 # define ENOTSUP EINVAL
40 #endif
41 
42 #if HAVE_INTTYPES_H
43 # include <inttypes.h>
44 #else
45 # if HAVE_STDINT_H
46 #  include <stdint.h>
47 # endif
48 #endif
49 
50 #include <signal.h>
51 #include <stdlib.h>
52 #include <string.h>
53 
54 #if HAVE_UNISTD_H
55 # include <unistd.h>
56 #endif
57 #ifndef STDERR_FILENO
58 # define STDERR_FILENO 2
59 #endif
60 
61 #include "c-stack.h"
62 #include "exitfail.h"
63 
64 extern char *program_name;
65 
66 #if HAVE_XSI_STACK_OVERFLOW_HEURISTIC
67 
68 # include <ucontext.h>
69 
70 
71 /* Storage for the alternate signal stack.  */
72 static union
73 {
74   char buffer[SIGSTKSZ];
75 
76   /* These other members are for proper alignment.  There's no
77      standard way to guarantee stack alignment, but this seems enough
78      in practice.  */
79   long double ld;
80   uintmax_t u;
81   void *p;
82 } alternate_signal_stack;
83 
84 
85 /* Direction of the C runtime stack.  This function is
86    async-signal-safe.  */
87 
88 # if STACK_DIRECTION
89 #  define find_stack_direction(ptr) STACK_DIRECTION
90 # else
91 static int
92 find_stack_direction (char const *addr)
93 {
94   char dummy;
95   return ! addr ? find_stack_direction (&dummy) : addr < &dummy ? 1 : -1;
96 }
97 # endif
98 
99 /* The SIGSEGV handler.  */
100 static void (* volatile segv_action) (int, siginfo_t *, void *);
101 
102 /* Handle a segmentation violation and exit.  This function is
103    async-signal-safe.  */
104 
105 static void
106 segv_handler (int signo, siginfo_t *info, void *context)
107 {
108   /* Clear SIGNO if it seems to have been a stack overflow.  */
109   if (0 < info->si_code)
110     {
111       /* If the faulting address is within the stack, or within one
112 	 page of the stack end, assume that it is a stack
113 	 overflow.  */
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)
121 	s += page_size;
122       if (s < stack_size + page_size)
123 	signo = 0;
124     }
125 
126   segv_action (signo, info, context);
127 }
128 
129 #endif /* HAVE_XSI_STACK_OVERFLOW_HEURISTIC */
130 
131 
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;
137 
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.  */
141 
142 void
143 c_stack_die (int signo, siginfo_t *info, void *context)
144 {
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);
151   if (! signo)
152     _exit (exit_failure);
153 #if HAVE_SIGINFO_T
154   if (context && info && 0 <= info->si_code)
155     {
156       /* Re-raise the exception at the same address.  */
157       char *addr = info->si_addr;
158       *addr = 0;
159     }
160 #endif
161   kill (getpid (), signo);
162 }
163 
164 
165 /* Set up ACTION so that it is invoked on C stack overflow.  Return -1
166    (setting errno) if this cannot be done.
167 
168    ACTION must invoke only async-signal-safe functions.  ACTION
169    together with its callees must not require more than SIGSTKSZ bytes
170    of stack space.  */
171 
172 int
173 c_stack_action (void (*action) (int, siginfo_t *, void *))
174 {
175 #if ! HAVE_XSI_STACK_OVERFLOW_HEURISTIC
176   errno = ENOTSUP;
177   return -1;
178 #else
179   struct sigaction act;
180   stack_t st;
181   int r;
182 
183   st.ss_flags = 0;
184   st.ss_sp = alternate_signal_stack.buffer;
185   st.ss_size = sizeof alternate_signal_stack.buffer;
186   r = sigaltstack (&st, 0);
187   if (r != 0)
188     return r;
189 
190   program_error_message = _("program error");
191   stack_overflow_message = _("stack overflow");
192   segv_action = action;
193 
194   sigemptyset (&act.sa_mask);
195 
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;
200 
201   act.sa_sigaction = segv_handler;
202   return sigaction (SIGSEGV, &act, 0);
203 #endif
204 }
205 
206 #if DEBUG
207 
208 #include <stdio.h>
209 
210 int volatile exit_failure;
211 
212 static long
213 recurse (char *p)
214 {
215   char array[500];
216   array[0] = 1;
217   return *p + recurse (array);
218 }
219 
220 char *program_name;
221 
222 int
223 main (int argc, char **argv)
224 {
225   program_name = argv[0];
226   c_stack_action (c_stack_die);
227   return recurse ("\1");
228 }
229 
230 #endif /* DEBUG */
231 
232 /*
233 Local Variables:
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"
238 End:
239 */
240