No empty .Rs/.Re
[netbsd-mini2440.git] / gnu / dist / diffutils / lib / c-stack.c
blobc4186e12de92625a5c136dd3d490ee1dd95aefe7
1 /* $NetBSD$ */
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)
10 any later version.
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. */
30 #if HAVE_CONFIG_H
31 # include <config.h>
32 #endif
34 #include "gettext.h"
35 #define _(msgid) gettext (msgid)
37 #include <errno.h>
38 #ifndef ENOTSUP
39 # define ENOTSUP EINVAL
40 #endif
42 #if HAVE_INTTYPES_H
43 # include <inttypes.h>
44 #else
45 # if HAVE_STDINT_H
46 # include <stdint.h>
47 # endif
48 #endif
50 #include <signal.h>
51 #include <stdlib.h>
52 #include <string.h>
54 #if HAVE_UNISTD_H
55 # include <unistd.h>
56 #endif
57 #ifndef STDERR_FILENO
58 # define STDERR_FILENO 2
59 #endif
61 #include "c-stack.h"
62 #include "exitfail.h"
64 extern char *program_name;
66 #if HAVE_XSI_STACK_OVERFLOW_HEURISTIC
68 # include <ucontext.h>
71 /* Storage for the alternate signal stack. */
72 static union
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
78 in practice. */
79 long double ld;
80 uintmax_t u;
81 void *p;
82 } alternate_signal_stack;
85 /* Direction of the C runtime stack. This function is
86 async-signal-safe. */
88 # if STACK_DIRECTION
89 # define find_stack_direction(ptr) STACK_DIRECTION
90 # else
91 static int
92 find_stack_direction (char const *addr)
94 char dummy;
95 return ! addr ? find_stack_direction (&dummy) : addr < &dummy ? 1 : -1;
97 # endif
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. */
105 static void
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
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;
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. */
142 void
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);
151 if (! signo)
152 _exit (exit_failure);
153 #if HAVE_SIGINFO_T
154 if (context && info && 0 <= info->si_code)
156 /* Re-raise the exception at the same address. */
157 char *addr = info->si_addr;
158 *addr = 0;
160 #endif
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
170 of stack space. */
173 c_stack_action (void (*action) (int, siginfo_t *, void *))
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;
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;
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);
203 #endif
206 #if DEBUG
208 #include <stdio.h>
210 int volatile exit_failure;
212 static long
213 recurse (char *p)
215 char array[500];
216 array[0] = 1;
217 return *p + recurse (array);
220 char *program_name;
223 main (int argc, char **argv)
225 program_name = argv[0];
226 c_stack_action (c_stack_die);
227 return recurse ("\1");
230 #endif /* DEBUG */
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: