This source file includes following definitions.
- die
- null_action
- segv_handler
- overflow_handler
- c_stack_action
- c_stack_action
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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 "getprogname.h"
56 #include "idx.h"
57 #include "ignore-value.h"
58
59 #include "gettext.h"
60 #define _(msgid) gettext (msgid)
61
62 #if HAVE_STACK_OVERFLOW_RECOVERY
63
64
65
66
67
68
69
70 static max_align_t alternate_signal_stack[(64 * 1024
71 + sizeof (max_align_t) - 1)
72 / sizeof (max_align_t)];
73
74
75
76 static _GL_ASYNC_SAFE void (* volatile segv_action) (int);
77
78
79
80
81 static char const * volatile program_error_message;
82 static char const * volatile stack_overflow_message;
83
84
85
86
87
88 static char const * volatile progname;
89
90 static _GL_ASYNC_SAFE _Noreturn void
91 die (int signo)
92 {
93 segv_action (signo);
94 char const *message = signo ? program_error_message : stack_overflow_message;
95
96
97
98
99 size_t prognamelen = strlen (progname);
100 size_t messagelen = strlen (message);
101 static char const separator[] = {':', ' '};
102 char buf[sizeof alternate_signal_stack / 16 + sizeof separator];
103 idx_t buflen;
104 if (prognamelen + messagelen < sizeof buf - sizeof separator)
105 {
106 char *p = mempcpy (buf, progname, prognamelen);
107 p = mempcpy (p, separator, sizeof separator);
108 p = mempcpy (p, message, messagelen);
109 *p++ = '\n';
110 buflen = p - buf;
111 }
112 else
113 {
114 ignore_value (write (STDERR_FILENO, progname, prognamelen));
115 ignore_value (write (STDERR_FILENO, separator, sizeof separator));
116 ignore_value (write (STDERR_FILENO, message, messagelen));
117 buf[0] = '\n';
118 buflen = 1;
119 }
120 ignore_value (write (STDERR_FILENO, buf, buflen));
121
122 if (! signo)
123 _exit (exit_failure);
124 raise (signo);
125 abort ();
126 }
127
128 static _GL_ASYNC_SAFE void
129 null_action (_GL_UNUSED int signo)
130 {
131 }
132
133
134 # if 4 < __GNUC__ + (6 <= __GNUC_MINOR__)
135 # pragma GCC diagnostic ignored "-Wsuggest-attribute=pure"
136 # endif
137
138
139 static volatile int segv_handler_missing;
140
141
142
143
144 static _GL_ASYNC_SAFE int
145 segv_handler (_GL_UNUSED void *address, int serious)
146 {
147 # if DEBUG
148 {
149 char buf[1024];
150 int saved_errno = errno;
151 ignore_value (write (STDERR_FILENO, buf,
152 sprintf (buf, "segv_handler serious=%d\n", serious)));
153 errno = saved_errno;
154 }
155 # endif
156
157
158
159 if (!serious)
160 return 0;
161 die (SIGSEGV);
162 }
163
164
165
166
167 static _GL_ASYNC_SAFE _Noreturn void
168 overflow_handler (int emergency, _GL_UNUSED stackoverflow_context_t context)
169 {
170 # if DEBUG
171 {
172 char buf[1024];
173 ignore_value (write (STDERR_FILENO, buf,
174 sprintf (buf, ("overflow_handler emergency=%d"
175 " segv_handler_missing=%d\n"),
176 emergency, segv_handler_missing)));
177 }
178 # endif
179
180 die ((!emergency || segv_handler_missing) ? 0 : SIGSEGV);
181 }
182
183 int
184 c_stack_action (_GL_ASYNC_SAFE void (*action) (int))
185 {
186 segv_action = action ? action : null_action;
187 program_error_message = _("program error");
188 stack_overflow_message = _("stack overflow");
189 progname = getprogname ();
190
191
192 if (stackoverflow_install_handler (overflow_handler,
193 alternate_signal_stack,
194 sizeof alternate_signal_stack))
195 {
196 errno = ENOTSUP;
197 return -1;
198 }
199
200
201 segv_handler_missing = sigsegv_install_handler (segv_handler);
202 return 0;
203 }
204
205 #else
206
207 int
208 c_stack_action (_GL_ASYNC_SAFE void (*action) (_GL_UNUSED int) )
209 {
210 errno = ENOTSUP;
211 return -1;
212 }
213
214 #endif