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