Debian Codegraph @

gnulib-tests/test-sigsegv-catch-stackoverflow2.c

raw
1 /* Test that stack overflow and SIGSEGV are correctly distinguished.
2 Copyright (C) 2002-2025 Free Software Foundation, Inc.
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
16
17 /* Written by Bruno Haible and Eric Blake. */
18
19 #include <config.h>
20
21 /* On GNU/Hurd, when compiling with -D_FORTIFY_SOURCE=2, avoid an error
22 "*** longjmp causes uninitialized stack frame ***: terminated".
23 Cf. <https://sourceware.org/bugzilla/show_bug.cgi?id=32522> */
24 #ifdef __gnu_hurd__
25 # undef _FORTIFY_SOURCE
26 # undef __USE_FORTIFY_LEVEL
27 #endif
28
29 /* Specification. */
30 #include "sigsegv.h"
31
32 #include <stdint.h>
33 #include <stdio.h>
34 #include <limits.h>
35
36 /* Skip this test when an address sanitizer is in use. */
37 #ifndef __has_feature
38 # define __has_feature(a) 0
39 #endif
40 #if defined __SANITIZE_ADDRESS__ || __has_feature (address_sanitizer)
41 # undef HAVE_STACK_OVERFLOW_RECOVERY
42 #endif
43
44 #if HAVE_STACK_OVERFLOW_RECOVERY && HAVE_SIGSEGV_RECOVERY
45
46 # if defined _WIN32 && !defined __CYGWIN__
47 /* Windows doesn't have sigset_t. */
48 typedef int sigset_t;
49 # define sigemptyset(set)
50 # define sigprocmask(how,set,oldset)
51 # endif
52
53 # include "mmap-anon-util.h"
54 # include <stddef.h> /* needed for NULL on SunOS4 */
55 # include <stdlib.h> /* for abort, exit */
56 # include <signal.h>
57 # include <setjmp.h>
58 # if HAVE_SETRLIMIT
59 # include <sys/types.h>
60 # include <sys/time.h>
61 # include <sys/resource.h>
62 # endif
63 # include "altstack-util.h"
64
65 static jmp_buf mainloop;
66 static sigset_t mainsigset;
67
68 static volatile int pass = 0;
69 static uintptr_t page;
70 static int *volatile null_pointer /* = NULL */;
71
72 static void
73 stackoverflow_handler_continuation (void *arg1, void *arg2, void *arg3)
74 {
75 int arg = (int) (long) arg1;
76 longjmp (mainloop, arg);
77 }
78
79 static void
80 stackoverflow_handler (int emergency, stackoverflow_context_t scp)
81 {
82 pass++;
83 if (pass <= 2)
84 printf ("Stack overflow %d caught.\n", pass);
85 else
86 {
87 printf ("Segmentation violation misdetected as stack overflow.\n");
88 exit (1);
89 }
90 sigprocmask (SIG_SETMASK, &mainsigset, NULL);
91 sigsegv_leave_handler (stackoverflow_handler_continuation,
92 (void *) (long) (emergency ? -1 : pass), NULL, NULL);
93 }
94
95 static int
96 sigsegv_handler (void *address, int emergency)
97 {
98 /* This test is necessary to distinguish stack overflow and SIGSEGV. */
99 if (!emergency)
100 return 0;
101
102 pass++;
103 if (pass <= 2)
104 {
105 printf ("Stack overflow %d missed.\n", pass);
106 exit (1);
107 }
108 else
109 printf ("Segmentation violation correctly detected.\n");
110 sigprocmask (SIG_SETMASK, &mainsigset, NULL);
111 return sigsegv_leave_handler (stackoverflow_handler_continuation,
112 (void *) (long) pass, NULL, NULL);
113 }
114
115 static volatile int *
116 recurse_1 (int n, volatile int *p)
117 {
118 if (n < INT_MAX)
119 *recurse_1 (n + 1, p) += n;
120 return p;
121 }
122
123 static int
124 recurse (volatile int n)
125 {
126 return *recurse_1 (n, &n);
127 }
128
129 int
130 main ()
131 {
132 int prot_unwritable;
133 void *p;
134 sigset_t emptyset;
135
136 # if HAVE_SETRLIMIT && defined RLIMIT_STACK
137 /* Before starting the endless recursion, try to be friendly to the user's
138 machine. On some Linux 2.2.x systems, there is no stack limit for user
139 processes at all. We don't want to kill such systems. */
140 struct rlimit rl;
141 rl.rlim_cur = rl.rlim_max = 0x100000; /* 1 MB */
142 setrlimit (RLIMIT_STACK, &rl);
143 # endif
144
145 /* Prepare the storage for the alternate stack. */
146 prepare_alternate_stack ();
147
148 /* Install the stack overflow handler. */
149 if (stackoverflow_install_handler (&stackoverflow_handler,
150 mystack, MYSTACK_SIZE)
151 < 0)
152 exit (2);
153
154 /* Preparations. */
155 # if !HAVE_MAP_ANONYMOUS
156 zero_fd = open ("/dev/zero", O_RDONLY, 0644);
157 # endif
158
159 # if defined __linux__ && defined __sparc__
160 /* On Linux 2.6.26/SPARC64, PROT_READ has the same effect as
161 PROT_READ | PROT_WRITE. */
162 prot_unwritable = PROT_NONE;
163 # else
164 prot_unwritable = PROT_READ;
165 # endif
166
167 /* Setup some mmapped memory. */
168 p = mmap_zeromap ((void *) 0x12340000, 0x4000);
169 if (p == (void *)(-1))
170 {
171 fprintf (stderr, "mmap_zeromap failed.\n");
172 exit (2);
173 }
174 page = (uintptr_t) p;
175
176 /* Make it read-only. */
177 if (mprotect ((void *) page, 0x4000, prot_unwritable) < 0)
178 {
179 fprintf (stderr, "mprotect failed.\n");
180 exit (2);
181 }
182
183 /* Install the SIGSEGV handler. */
184 if (sigsegv_install_handler (&sigsegv_handler) < 0)
185 exit (2);
186
187 /* Save the current signal mask. */
188 sigemptyset (&emptyset);
189 sigprocmask (SIG_BLOCK, &emptyset, &mainsigset);
190
191 /* Provoke two stack overflows in a row. */
192 switch (setjmp (mainloop))
193 {
194 case -1:
195 printf ("emergency exit\n"); exit (1);
196 case 0: case 1:
197 printf ("Starting recursion pass %d.\n", pass + 1);
198 recurse (0);
199 printf ("no endless recursion?!\n"); exit (1);
200 case 2:
201 *(volatile int *) (page + 0x678) = 42;
202 break;
203 case 3:
204 *null_pointer = 42;
205 break;
206 case 4:
207 break;
208 default:
209 abort ();
210 }
211
212 /* Validate that the alternate stack did not overflow. */
213 check_alternate_stack_no_overflow ();
214
215 printf ("Test passed.\n");
216 exit (0);
217 }
218
219 #else
220
221 int
222 main ()
223 {
224 return 77;
225 }
226
227 #endif