Debian Codegraph @

lib/c-stack.c

raw
1 /* Stack overflow handling.
2
3 Copyright (C) 2002, 2004, 2006, 2008-2025 Free Software Foundation, Inc.
4
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.
9
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.
14
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/>. */
17
18 /* Written by Paul Eggert. */
19
20 /* NOTES:
21
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.
27
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.
33
34 No attempt has been made to deal with multithreaded applications. */
35
36 #include <config.h>
37
38 #include "c-stack.h"
39
40 #include <errno.h>
41 #include <inttypes.h>
42 #include <signal.h>
43 #include <stddef.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47
48 #if DEBUG
49 # include <stdio.h>
50 #endif
51
52 #include <sigsegv.h>
53
54 #include "exitfail.h"
55 #include "idx.h"
56 #include "ignore-value.h"
57
58 #include "gettext.h"
59 #define _(msgid) dgettext ("gnulib", msgid)
60
61 /* Here we need the original abort() function. (Printing a stack trace
62 from within a signal handler is not going to work in most cases anyway.) */
63 #undef abort
64
65 #if HAVE_STACK_OVERFLOW_RECOVERY
66
67 /* Storage for the alternate signal stack.
68 64 KiB is not too large for Gnulib-using apps, and is large enough
69 for all known platforms. Smaller sizes may run into trouble.
70 For example, libsigsegv 2.6 through 2.8 have a bug where some
71 architectures use more than the Linux default of an 8 KiB alternate
72 stack when deciding if a fault was caused by stack overflow. */
73 static max_align_t alternate_signal_stack[(64 * 1024
74 + sizeof (max_align_t) - 1)
75 / sizeof (max_align_t)];
76
77 /* The user-specified action to take when a SEGV-related program error
78 or stack overflow occurs. */
79 static _GL_ASYNC_SAFE void (* volatile segv_action) (int);
80
81 /* Translated messages for program errors and stack overflow. Do not
82 translate them in the signal handler, since gettext is not
83 async-signal-safe. */
84 static char const * volatile program_error_message;
85 static char const * volatile stack_overflow_message;
86
87 /* Output an error message, then exit with status EXIT_FAILURE if it
88 appears to have been a stack overflow, or with a core dump
89 otherwise. This function is async-signal-safe. */
90
91 static char const * volatile progname;
92
93 static _GL_ASYNC_SAFE _Noreturn void
94 die (int signo)
95 {
96 segv_action (signo);
97 char const *message = signo ? program_error_message : stack_overflow_message;
98
99 /* If the message is short, write it all at once to avoid
100 interleaving with other messages. Avoid writev as it is not
101 documented to be async-signal-safe. */
102 size_t prognamelen = strlen (progname);
103 size_t messagelen = strlen (message);
104 static char const separator[] = {':', ' '};
105 char buf[sizeof alternate_signal_stack / 16 + sizeof separator];
106 idx_t buflen;
107 if (prognamelen + messagelen < sizeof buf - sizeof separator)
108 {
109 char *p = mempcpy (buf, progname, prognamelen);
110 p = mempcpy (p, separator, sizeof separator);
111 p = mempcpy (p, message, messagelen);
112 *p++ = '\n';
113 buflen = p - buf;
114 }
115 else
116 {
117 ignore_value (write (STDERR_FILENO, progname, prognamelen));
118 ignore_value (write (STDERR_FILENO, separator, sizeof separator));
119 ignore_value (write (STDERR_FILENO, message, messagelen));
120 buf[0] = '\n';
121 buflen = 1;
122 }
123 ignore_value (write (STDERR_FILENO, buf, buflen));
124
125 if (! signo)
126 _exit (exit_failure);
127 raise (signo);
128 abort ();
129 }
130
131 static _GL_ASYNC_SAFE void
132 null_action (_GL_UNUSED int signo)
133 {
134 }
135
136 /* Pacify GCC 9.3.1, which otherwise would complain about segv_handler. */
137 # if _GL_GNUC_PREREQ (4, 6)
138 # pragma GCC diagnostic ignored "-Wsuggest-attribute=pure"
139 # endif
140
141 /* Nonzero if general segv handler could not be installed. */
142 static volatile int segv_handler_missing;
143
144 /* Handle a segmentation violation and exit if it cannot be stack
145 overflow. This function is async-signal-safe. */
146
147 static _GL_ASYNC_SAFE int
148 segv_handler (_GL_UNUSED void *address, int serious)
149 {
150 # if DEBUG
151 {
152 char buf[1024];
153 int saved_errno = errno;
154 ignore_value (write (STDERR_FILENO, buf,
155 sprintf (buf, "segv_handler serious=%d\n", serious)));
156 errno = saved_errno;
157 }
158 # endif
159
160 /* If this fault is not serious, return 0 to let the stack overflow
161 handler take a shot at it. */
162 if (!serious)
163 return 0;
164 die (SIGSEGV);
165 }
166
167 /* Handle a segmentation violation that is likely to be a stack
168 overflow and exit. This function is async-signal-safe. */
169
170 static _GL_ASYNC_SAFE _Noreturn void
171 overflow_handler (int emergency, _GL_UNUSED stackoverflow_context_t context)
172 {
173 # if DEBUG
174 {
175 char buf[1024];
176 ignore_value (write (STDERR_FILENO, buf,
177 sprintf (buf, ("overflow_handler emergency=%d"
178 " segv_handler_missing=%d\n"),
179 emergency, segv_handler_missing)));
180 }
181 # endif
182
183 die ((!emergency || segv_handler_missing) ? 0 : SIGSEGV);
184 }
185
186 int
187 c_stack_action (_GL_ASYNC_SAFE void (*action) (int))
188 {
189 segv_action = action ? action : null_action;
190 program_error_message = _("program error");
191 stack_overflow_message = _("stack overflow");
192 progname = getprogname ();
193
194 /* Always install the overflow handler. */
195 if (stackoverflow_install_handler (overflow_handler,
196 alternate_signal_stack,
197 sizeof alternate_signal_stack))
198 {
199 errno = ENOTSUP;
200 return -1;
201 }
202 /* Try installing a general handler; if it fails, then treat all
203 segv as stack overflow. */
204 segv_handler_missing = sigsegv_install_handler (segv_handler);
205 return 0;
206 }
207
208 #else /* !HAVE_STACK_OVERFLOW_RECOVERY */
209
210 int
211 c_stack_action (_GL_ASYNC_SAFE void (*action) (_GL_UNUSED int) )
212 {
213 errno = ENOTSUP;
214 return -1;
215 }
216
217 #endif