t_signal_and_sp.c revision 1.6 1 /* $NetBSD: t_signal_and_sp.c,v 1.6 2025/04/20 22:33:13 riastradh Exp $ */
2
3 /*
4 * Copyright (c) 2024 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __RCSID("$NetBSD: t_signal_and_sp.c,v 1.6 2025/04/20 22:33:13 riastradh Exp $");
31
32 #include <sys/wait.h>
33
34 #include <machine/param.h>
35
36 #include <atf-c.h>
37 #include <limits.h>
38 #include <poll.h>
39 #include <signal.h>
40 #include <stdint.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <time.h>
44 #include <unistd.h>
45
46 #include "h_execsp.h"
47 #include "h_macros.h"
48
49 #ifdef HAVE_STACK_POINTER_H
50 # include "stack_pointer.h"
51 #endif
52
53 #ifdef HAVE_SIGNALSPHANDLER
54 void signalsphandler(int); /* signalsphandler.S assembly routine */
55 #endif
56
57 void *volatile signalsp;
58
59 static void
60 test_execsp(const struct atf_tc *tc, const char *prog)
61 {
62 #ifdef STACK_ALIGNBYTES
63 char h_execsp[PATH_MAX];
64 struct execsp execsp;
65 int fd[2];
66 pid_t pid;
67 struct pollfd pollfd;
68 int nfds;
69 ssize_t nread;
70 int status;
71
72 /*
73 * Determine the full path to the helper program.
74 */
75 RL(snprintf(h_execsp, sizeof(h_execsp), "%s/%s",
76 atf_tc_get_config_var(tc, "srcdir"), prog));
77
78 /*
79 * Create a pipe to read a bundle of stack pointer samples from
80 * the child, and fork the child.
81 */
82 RL(pipe(fd));
83 RL(pid = vfork());
84 if (pid == 0) { /* child */
85 char *const argv[] = {h_execsp, NULL};
86
87 if (dup2(fd[1], STDOUT_FILENO) == -1)
88 _exit(1);
89 if (closefrom(STDERR_FILENO + 1) == -1)
90 _exit(2);
91 if (execve(argv[0], argv, NULL) == -1)
92 _exit(3);
93 _exit(4);
94 }
95
96 /*
97 * Close the writing end so, if something goes wrong in the
98 * child, we don't hang indefinitely waiting for output.
99 */
100 RL(close(fd[1]));
101
102 /*
103 * Wait up to 5sec for the child to return an answer. Any more
104 * than that, and we kill it. The child is mostly hand-written
105 * assembly routines where lots can go wrong, so don't bother
106 * waiting if it gets stuck in a loop.
107 */
108 pollfd.fd = fd[0];
109 pollfd.events = POLLIN;
110 RL(nfds = poll(&pollfd, 1, 5*1000/*ms*/));
111 if (nfds == 0) {
112 fprintf(stderr, "child hung, killing\n");
113 RL(kill(pid, SIGKILL));
114 }
115
116 /*
117 * Read a bundle of stack pointer samples from the child.
118 */
119 RL(nread = read(fd[0], &execsp, sizeof(execsp)));
120 ATF_CHECK_MSG((size_t)nread == sizeof(execsp),
121 "nread=%zu sizeof(execsp)=%zu",
122 (size_t)nread, sizeof(execsp));
123
124 /*
125 * Wait for the child to terminate and report failure if it
126 * didn't exit cleanly.
127 */
128 RL(waitpid(pid, &status, 0));
129 if (WIFSIGNALED(status)) {
130 atf_tc_fail_nonfatal("child exited on signal %d (%s)",
131 WTERMSIG(status), strsignal(WTERMSIG(status)));
132 } else if (!WIFEXITED(status)) {
133 atf_tc_fail_nonfatal("child exited status=0x%x", status);
134 } else {
135 ATF_CHECK_MSG(WEXITSTATUS(status) == 0,
136 "child exited with code %d",
137 WEXITSTATUS(status));
138 }
139
140 /*
141 * Now that we have reaped the child, stop here if the stack
142 * pointer samples are bogus; otherwise verify they are all
143 * aligned.
144 */
145 if ((size_t)nread != sizeof(execsp))
146 return; /* failed already */
147
148 printf("start sp @ %p\n", execsp.startsp);
149 printf("ctor sp @ %p\n", execsp.ctorsp);
150 printf("main sp @ %p\n", execsp.mainsp);
151 printf("dtor sp @ %p\n", execsp.dtorsp);
152
153 ATF_CHECK_MSG(((uintptr_t)execsp.startsp & STACK_ALIGNBYTES) == 0,
154 "elf entry point was called with misaligned sp: %p",
155 execsp.startsp);
156
157 ATF_CHECK_MSG(((uintptr_t)execsp.ctorsp & STACK_ALIGNBYTES) == 0,
158 "elf constructor was called with misaligned sp: %p",
159 execsp.ctorsp);
160
161 ATF_CHECK_MSG(((uintptr_t)execsp.mainsp & STACK_ALIGNBYTES) == 0,
162 "main function was called with misaligned sp: %p",
163 execsp.mainsp);
164
165 ATF_CHECK_MSG(((uintptr_t)execsp.dtorsp & STACK_ALIGNBYTES) == 0,
166 "elf destructor was called with misaligned sp: %p",
167 execsp.dtorsp);
168
169 /*
170 * Leave a reminder on architectures for which we haven't
171 * implemented execsp_start.S.
172 */
173 if (execsp.startsp == NULL ||
174 execsp.ctorsp == NULL ||
175 execsp.mainsp == NULL ||
176 execsp.dtorsp == NULL)
177 atf_tc_skip("Not fully supported on this architecture");
178 #else
179 atf_tc_skip("Unknown STACK_ALIGNBYTES on this architecture");
180 #endif
181 }
182
183 ATF_TC(execsp_dynamic);
184 ATF_TC_HEAD(execsp_dynamic, tc)
185 {
186 atf_tc_set_md_var(tc, "descr",
187 "Verify stack pointer is aligned on dynamic program start");
188 }
189 ATF_TC_BODY(execsp_dynamic, tc)
190 {
191 test_execsp(tc, "h_execsp_dynamic");
192 }
193
194 ATF_TC(execsp_static);
195 ATF_TC_HEAD(execsp_static, tc)
196 {
197 atf_tc_set_md_var(tc, "descr",
198 "Verify stack pointer is aligned on static program start");
199 }
200 ATF_TC_BODY(execsp_static, tc)
201 {
202 test_execsp(tc, "h_execsp_static");
203 }
204
205 ATF_TC(signalsp);
206 ATF_TC_HEAD(signalsp, tc)
207 {
208 atf_tc_set_md_var(tc, "descr",
209 "Verify stack pointer is aligned on entry to signal handler");
210 }
211 ATF_TC_BODY(signalsp, tc)
212 {
213 #if defined STACK_ALIGNBYTES && defined HAVE_SIGNALSPHANDLER
214 struct sigaction sa;
215
216 memset(&sa, 0, sizeof(sa));
217 sa.sa_handler = &signalsphandler;
218 RL(sigaction(SIGUSR1, &sa, NULL));
219 RL(raise(SIGUSR1));
220
221 ATF_CHECK_MSG(((uintptr_t)signalsp & STACK_ALIGNBYTES) == 0,
222 "signal handler was called with a misaligned sp: %p",
223 signalsp);
224 #else
225 atf_tc_skip("Not implemented on this platform");
226 #endif
227 }
228
229 ATF_TC(signalsp_sigaltstack);
230 ATF_TC_HEAD(signalsp_sigaltstack, tc)
231 {
232 atf_tc_set_md_var(tc, "descr",
233 "Verify stack pointer is aligned on entry to signal handler"
234 " with maximally misaligned sigaltstack");
235 }
236 ATF_TC_BODY(signalsp_sigaltstack, tc)
237 {
238 #if defined STACK_ALIGNBYTES && HAVE_SIGNALSPHANDLER
239 char *stack;
240 struct sigaction sa;
241 struct sigaltstack ss;
242 unsigned i;
243
244 memset(&sa, 0, sizeof(sa));
245 sa.sa_handler = &signalsphandler;
246 sa.sa_flags = SA_ONSTACK;
247 RL(sigaction(SIGUSR1, &sa, NULL));
248
249 /*
250 * Allocate a signal stack with enough slop to try all possible
251 * misalignments of the stack pointer. Print it to stderr so
252 * it always appears in atf output before shenanigans happen.
253 */
254 REQUIRE_LIBC(stack = malloc(SIGSTKSZ + STACK_ALIGNBYTES), NULL);
255 fprintf(stderr, "stack @ [%p, %p)",
256 stack, stack + SIGSTKSZ + STACK_ALIGNBYTES);
257
258 #if defined __alpha__ || defined __i386__ || defined __mips__
259 atf_tc_expect_fail("PR kern/59327:"
260 " user stack pointer is not aligned properly");
261 #endif
262
263 /*
264 * Try with all alignments of high addresses.
265 */
266 for (i = 0; i <= STACK_ALIGNBYTES; i++) {
267 ss.ss_sp = stack;
268 ss.ss_size = SIGSTKSZ + i;
269 ss.ss_flags = 0;
270 RL(sigaltstack(&ss, NULL));
271
272 signalsp = NULL;
273 RL(raise(SIGUSR1));
274 ATF_CHECK(signalsp != NULL);
275 ATF_CHECK_MSG(((uintptr_t)signalsp & STACK_ALIGNBYTES) == 0,
276 "[%u] signal handler was called with a misaligned sp: %p",
277 i, signalsp);
278 }
279
280 /*
281 * Try with all alignments of low addresses.
282 */
283 for (i = 0; i <= STACK_ALIGNBYTES; i++) {
284 ss.ss_sp = stack + i;
285 ss.ss_size = SIGSTKSZ;
286 ss.ss_flags = 0;
287 RL(sigaltstack(&ss, NULL));
288
289 signalsp = NULL;
290 RL(raise(SIGUSR1));
291 ATF_CHECK(signalsp != NULL);
292 ATF_CHECK_MSG(((uintptr_t)signalsp & STACK_ALIGNBYTES) == 0,
293 "[%u] signal handler was called with a misaligned sp: %p",
294 i, signalsp);
295 }
296 #else
297 atf_tc_skip("Not implemented on this platform");
298 #endif
299 }
300
301 ATF_TC(misaligned_sp_and_signal);
302 ATF_TC_HEAD(misaligned_sp_and_signal, tc)
303 {
304 atf_tc_set_md_var(tc, "descr", "process can return from a signal"
305 " handler even if the stack pointer is misaligned when a signal"
306 " arrives");
307 }
308 ATF_TC_BODY(misaligned_sp_and_signal, tc)
309 {
310 #if defined STACK_ALIGNBYTES && defined HAVE_STACK_POINTER_H
311 /*
312 * Set up a handler for SIGALRM.
313 */
314 struct sigaction sa;
315 memset(&sa, 0, sizeof(sa));
316 sa.sa_handler = &signalsphandler;
317 RL(sigaction(SIGALRM, &sa, NULL));
318
319 #if defined __alpha__ || defined __i386__ || defined __mips__
320 atf_tc_expect_fail("PR kern/58149:"
321 " Cannot return from a signal handler"
322 " if SP was misaligned when the signal arrived");
323 #endif
324
325 /*
326 * Set up an interval timer so that we receive SIGALRM after 50 ms.
327 */
328 struct itimerval itv;
329 memset(&itv, 0, sizeof(itv));
330 itv.it_value.tv_usec = 1000 * 50;
331 RL(setitimer(ITIMER_MONOTONIC, &itv, NULL));
332
333 /*
334 * Now misalign the SP. Wait for the signal to arrive and see what
335 * happens. This should be fine as long as we don't use it to
336 * access memory.
337 */
338 MISALIGN_SP;
339 while (signalsp == NULL) {
340 /*
341 * Make sure the compiler does not optimize this busy loop
342 * away.
343 */
344 __asm__("" ::: "memory");
345 }
346 /*
347 * We could successfully return from a signal handler. Now we
348 * should fix the SP before calling any functions.
349 */
350 FIX_SP;
351
352 /*
353 * But was the stack pointer aligned when we were on the signal
354 * handler?
355 */
356 ATF_CHECK_MSG(((uintptr_t)signalsp & STACK_ALIGNBYTES) == 0,
357 "signal handler was called with a misaligned sp: %p",
358 signalsp);
359 #else
360 atf_tc_skip("Not implemented for this platform");
361 #endif
362 }
363
364 ATF_TP_ADD_TCS(tp)
365 {
366
367 ATF_TP_ADD_TC(tp, execsp_dynamic);
368 ATF_TP_ADD_TC(tp, execsp_static);
369 ATF_TP_ADD_TC(tp, misaligned_sp_and_signal);
370 ATF_TP_ADD_TC(tp, signalsp);
371 ATF_TP_ADD_TC(tp, signalsp_sigaltstack);
372 return atf_no_error();
373 }
374