t_signal_and_sp.c revision 1.7 1 /* $NetBSD: t_signal_and_sp.c,v 1.7 2025/04/21 02:33:44 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.7 2025/04/21 02:33:44 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 <pthread.h>
40 #include <signal.h>
41 #include <stdint.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <time.h>
45 #include <ucontext.h>
46 #include <unistd.h>
47
48 #include "h_execsp.h"
49 #include "h_macros.h"
50
51 #ifdef HAVE_STACK_POINTER_H
52 # include "stack_pointer.h"
53 #endif
54
55 #ifdef HAVE_SIGNALSPHANDLER
56 void signalsphandler(int); /* signalsphandler.S assembly routine */
57 #endif
58
59 void *volatile signalsp;
60
61 static void
62 test_execsp(const struct atf_tc *tc, const char *prog)
63 {
64 #ifdef STACK_ALIGNBYTES
65 char h_execsp[PATH_MAX];
66 struct execsp execsp;
67 int fd[2];
68 pid_t pid;
69 struct pollfd pollfd;
70 int nfds;
71 ssize_t nread;
72 int status;
73
74 /*
75 * Determine the full path to the helper program.
76 */
77 RL(snprintf(h_execsp, sizeof(h_execsp), "%s/%s",
78 atf_tc_get_config_var(tc, "srcdir"), prog));
79
80 /*
81 * Create a pipe to read a bundle of stack pointer samples from
82 * the child, and fork the child.
83 */
84 RL(pipe(fd));
85 RL(pid = vfork());
86 if (pid == 0) { /* child */
87 char *const argv[] = {h_execsp, NULL};
88
89 if (dup2(fd[1], STDOUT_FILENO) == -1)
90 _exit(1);
91 if (closefrom(STDERR_FILENO + 1) == -1)
92 _exit(2);
93 if (execve(argv[0], argv, NULL) == -1)
94 _exit(3);
95 _exit(4);
96 }
97
98 /*
99 * Close the writing end so, if something goes wrong in the
100 * child, we don't hang indefinitely waiting for output.
101 */
102 RL(close(fd[1]));
103
104 /*
105 * Wait up to 5sec for the child to return an answer. Any more
106 * than that, and we kill it. The child is mostly hand-written
107 * assembly routines where lots can go wrong, so don't bother
108 * waiting if it gets stuck in a loop.
109 */
110 pollfd.fd = fd[0];
111 pollfd.events = POLLIN;
112 RL(nfds = poll(&pollfd, 1, 5*1000/*ms*/));
113 if (nfds == 0) {
114 fprintf(stderr, "child hung, killing\n");
115 RL(kill(pid, SIGKILL));
116 }
117
118 /*
119 * Read a bundle of stack pointer samples from the child.
120 */
121 RL(nread = read(fd[0], &execsp, sizeof(execsp)));
122 ATF_CHECK_MSG((size_t)nread == sizeof(execsp),
123 "nread=%zu sizeof(execsp)=%zu",
124 (size_t)nread, sizeof(execsp));
125
126 /*
127 * Wait for the child to terminate and report failure if it
128 * didn't exit cleanly.
129 */
130 RL(waitpid(pid, &status, 0));
131 if (WIFSIGNALED(status)) {
132 atf_tc_fail_nonfatal("child exited on signal %d (%s)",
133 WTERMSIG(status), strsignal(WTERMSIG(status)));
134 } else if (!WIFEXITED(status)) {
135 atf_tc_fail_nonfatal("child exited status=0x%x", status);
136 } else {
137 ATF_CHECK_MSG(WEXITSTATUS(status) == 0,
138 "child exited with code %d",
139 WEXITSTATUS(status));
140 }
141
142 /*
143 * Now that we have reaped the child, stop here if the stack
144 * pointer samples are bogus; otherwise verify they are all
145 * aligned.
146 */
147 if ((size_t)nread != sizeof(execsp))
148 return; /* failed already */
149
150 printf("start sp @ %p\n", execsp.startsp);
151 printf("ctor sp @ %p\n", execsp.ctorsp);
152 printf("main sp @ %p\n", execsp.mainsp);
153 printf("dtor sp @ %p\n", execsp.dtorsp);
154
155 ATF_CHECK_MSG(((uintptr_t)execsp.startsp & STACK_ALIGNBYTES) == 0,
156 "elf entry point was called with misaligned sp: %p",
157 execsp.startsp);
158
159 ATF_CHECK_MSG(((uintptr_t)execsp.ctorsp & STACK_ALIGNBYTES) == 0,
160 "elf constructor was called with misaligned sp: %p",
161 execsp.ctorsp);
162
163 ATF_CHECK_MSG(((uintptr_t)execsp.mainsp & STACK_ALIGNBYTES) == 0,
164 "main function was called with misaligned sp: %p",
165 execsp.mainsp);
166
167 ATF_CHECK_MSG(((uintptr_t)execsp.dtorsp & STACK_ALIGNBYTES) == 0,
168 "elf destructor was called with misaligned sp: %p",
169 execsp.dtorsp);
170
171 /*
172 * Leave a reminder on architectures for which we haven't
173 * implemented execsp_start.S.
174 */
175 if (execsp.startsp == NULL ||
176 execsp.ctorsp == NULL ||
177 execsp.mainsp == NULL ||
178 execsp.dtorsp == NULL)
179 atf_tc_skip("Not fully supported on this architecture");
180 #else
181 atf_tc_skip("Unknown STACK_ALIGNBYTES on this architecture");
182 #endif
183 }
184
185 ATF_TC(execsp_dynamic);
186 ATF_TC_HEAD(execsp_dynamic, tc)
187 {
188 atf_tc_set_md_var(tc, "descr",
189 "Verify stack pointer is aligned on dynamic program start");
190 }
191 ATF_TC_BODY(execsp_dynamic, tc)
192 {
193 test_execsp(tc, "h_execsp_dynamic");
194 }
195
196 ATF_TC(execsp_static);
197 ATF_TC_HEAD(execsp_static, tc)
198 {
199 atf_tc_set_md_var(tc, "descr",
200 "Verify stack pointer is aligned on static program start");
201 }
202 ATF_TC_BODY(execsp_static, tc)
203 {
204 test_execsp(tc, "h_execsp_static");
205 }
206
207 #if defined STACK_ALIGNBYTES && defined HAVE_CONTEXTSPFUNC
208 void *volatile contextsp; /* set by contextspfunc.S */
209 static ucontext_t test_context, return_context;
210 static volatile bool test_context_done;
211
212 void contextspfunc(void); /* contextspfunc.S assembly routine */
213
214 void contextdone(void); /* called by contextspfunc.S */
215 void
216 contextdone(void)
217 {
218
219 fprintf(stderr, "contextdone\n");
220 ATF_REQUIRE(!test_context_done);
221 test_context_done = true;
222 RL(setcontext(&return_context));
223 atf_tc_fail("setcontext returned");
224 }
225 #endif
226
227 ATF_TC(contextsp);
228 ATF_TC_HEAD(contextsp, tc)
229 {
230 atf_tc_set_md_var(tc, "descr",
231 "Verify stack pointer is aligned on makecontext entry");
232 }
233 ATF_TC_BODY(contextsp, tc)
234 {
235 #if defined STACK_ALIGNBYTES && defined HAVE_CONTEXTSPFUNC
236 char *stack;
237 unsigned i;
238
239 REQUIRE_LIBC(stack = malloc(SIGSTKSZ + STACK_ALIGNBYTES), NULL);
240 fprintf(stderr, "stack @ [%p,%p)\n", stack, stack + STACK_ALIGNBYTES);
241
242 for (i = 0; i <= STACK_ALIGNBYTES; i++) {
243 contextsp = NULL;
244 test_context_done = false;
245 RL(getcontext(&test_context));
246 test_context.uc_stack.ss_sp = stack;
247 test_context.uc_stack.ss_size = SIGSTKSZ + i;
248 makecontext(&test_context, &contextspfunc, 0);
249 fprintf(stderr, "[%u] swapcontext\n", i);
250 RL(swapcontext(&return_context, &test_context));
251 ATF_CHECK(contextsp != NULL);
252 ATF_CHECK_MSG(((uintptr_t)contextsp & STACK_ALIGNBYTES) == 0,
253 "[%u] makecontext function called with misaligned sp %p",
254 i, contextsp);
255 }
256
257 for (i = 0; i <= STACK_ALIGNBYTES; i++) {
258 contextsp = NULL;
259 test_context_done = false;
260 RL(getcontext(&test_context));
261 test_context.uc_stack.ss_sp = stack + i;
262 test_context.uc_stack.ss_size = SIGSTKSZ;
263 makecontext(&test_context, &contextspfunc, 0);
264 fprintf(stderr, "[%u] swapcontext\n", i);
265 RL(swapcontext(&return_context, &test_context));
266 ATF_CHECK(contextsp != NULL);
267 ATF_CHECK_MSG(((uintptr_t)contextsp & STACK_ALIGNBYTES) == 0,
268 "[%u] makecontext function called with misaligned sp %p",
269 i, contextsp);
270 }
271 #else
272 atf_tc_skip("Not implemented on this platform");
273 #endif
274 }
275
276 ATF_TC(signalsp);
277 ATF_TC_HEAD(signalsp, tc)
278 {
279 atf_tc_set_md_var(tc, "descr",
280 "Verify stack pointer is aligned on entry to signal handler");
281 }
282 ATF_TC_BODY(signalsp, tc)
283 {
284 #if defined STACK_ALIGNBYTES && defined HAVE_SIGNALSPHANDLER
285 struct sigaction sa;
286
287 memset(&sa, 0, sizeof(sa));
288 sa.sa_handler = &signalsphandler;
289 RL(sigaction(SIGUSR1, &sa, NULL));
290 RL(raise(SIGUSR1));
291
292 ATF_CHECK_MSG(((uintptr_t)signalsp & STACK_ALIGNBYTES) == 0,
293 "signal handler was called with a misaligned sp: %p",
294 signalsp);
295 #else
296 atf_tc_skip("Not implemented on this platform");
297 #endif
298 }
299
300 ATF_TC(signalsp_sigaltstack);
301 ATF_TC_HEAD(signalsp_sigaltstack, tc)
302 {
303 atf_tc_set_md_var(tc, "descr",
304 "Verify stack pointer is aligned on entry to signal handler"
305 " with maximally misaligned sigaltstack");
306 }
307 ATF_TC_BODY(signalsp_sigaltstack, tc)
308 {
309 #if defined STACK_ALIGNBYTES && HAVE_SIGNALSPHANDLER
310 char *stack;
311 struct sigaction sa;
312 struct sigaltstack ss;
313 unsigned i;
314
315 memset(&sa, 0, sizeof(sa));
316 sa.sa_handler = &signalsphandler;
317 sa.sa_flags = SA_ONSTACK;
318 RL(sigaction(SIGUSR1, &sa, NULL));
319
320 /*
321 * Allocate a signal stack with enough slop to try all possible
322 * misalignments of the stack pointer. Print it to stderr so
323 * it always appears in atf output before shenanigans happen.
324 */
325 REQUIRE_LIBC(stack = malloc(SIGSTKSZ + STACK_ALIGNBYTES), NULL);
326 fprintf(stderr, "stack @ [%p, %p)\n",
327 stack, stack + SIGSTKSZ + STACK_ALIGNBYTES);
328
329 #if defined __alpha__ || defined __i386__ || defined __mips__
330 atf_tc_expect_fail("PR kern/59327:"
331 " user stack pointer is not aligned properly");
332 #endif
333
334 /*
335 * Try with all alignments of high addresses.
336 */
337 for (i = 0; i <= STACK_ALIGNBYTES; i++) {
338 ss.ss_sp = stack;
339 ss.ss_size = SIGSTKSZ + i;
340 ss.ss_flags = 0;
341 RL(sigaltstack(&ss, NULL));
342
343 signalsp = NULL;
344 RL(raise(SIGUSR1));
345 ATF_CHECK(signalsp != NULL);
346 ATF_CHECK_MSG(((uintptr_t)signalsp & STACK_ALIGNBYTES) == 0,
347 "[%u] signal handler was called with a misaligned sp: %p",
348 i, signalsp);
349 }
350
351 /*
352 * Try with all alignments of low addresses.
353 */
354 for (i = 0; i <= STACK_ALIGNBYTES; i++) {
355 ss.ss_sp = stack + i;
356 ss.ss_size = SIGSTKSZ;
357 ss.ss_flags = 0;
358 RL(sigaltstack(&ss, NULL));
359
360 signalsp = NULL;
361 RL(raise(SIGUSR1));
362 ATF_CHECK(signalsp != NULL);
363 ATF_CHECK_MSG(((uintptr_t)signalsp & STACK_ALIGNBYTES) == 0,
364 "[%u] signal handler was called with a misaligned sp: %p",
365 i, signalsp);
366 }
367 #else
368 atf_tc_skip("Not implemented on this platform");
369 #endif
370 }
371
372 #if defined STACK_ALIGNBYTES && defined HAVE_THREADSPFUNC
373 void *threadspfunc(void *); /* threadspfunc.S assembly routine */
374 #endif
375
376 ATF_TC(threadsp);
377 ATF_TC_HEAD(threadsp, tc)
378 {
379 atf_tc_set_md_var(tc, "descr",
380 "Verify stack pointer is aligned on thread start");
381 }
382 ATF_TC_BODY(threadsp, tc)
383 {
384 #if defined STACK_ALIGNBYTES && defined HAVE_THREADSPFUNC
385 char *stack;
386 unsigned i;
387
388 REQUIRE_LIBC(stack = malloc(SIGSTKSZ + STACK_ALIGNBYTES), NULL);
389 fprintf(stderr, "stack @ [%p,%p)\n", stack, stack + STACK_ALIGNBYTES);
390
391 #ifdef __mips__
392 atf_tc_expect_fail("PR kern/59327:"
393 " user stack pointer is not aligned properly");
394 #endif
395
396 for (i = 0; i <= STACK_ALIGNBYTES; i++) {
397 pthread_t t;
398 pthread_attr_t attr;
399 void *sp;
400
401 RZ(pthread_attr_init(&attr));
402 RZ(pthread_attr_setstack(&attr, stack, SIGSTKSZ + i));
403 RZ(pthread_create(&t, &attr, &threadspfunc, NULL));
404 RZ(pthread_attr_destroy(&attr));
405
406 alarm(1);
407 RZ(pthread_join(t, &sp));
408 alarm(0);
409
410 ATF_CHECK(sp != NULL);
411 ATF_CHECK_MSG(((uintptr_t)signalsp & STACK_ALIGNBYTES) == 0,
412 "[%u] thread called with misaligned sp: %p", i, signalsp);
413 }
414
415 for (i = 0; i <= STACK_ALIGNBYTES; i++) {
416 pthread_t t;
417 pthread_attr_t attr;
418 void *sp;
419
420 RZ(pthread_attr_init(&attr));
421 RZ(pthread_attr_setstack(&attr, stack + i, SIGSTKSZ));
422 RZ(pthread_create(&t, &attr, &threadspfunc, NULL));
423 RZ(pthread_attr_destroy(&attr));
424
425 alarm(1);
426 RZ(pthread_join(t, &sp));
427 alarm(0);
428
429 ATF_CHECK(sp != NULL);
430 ATF_CHECK_MSG(((uintptr_t)signalsp & STACK_ALIGNBYTES) == 0,
431 "[%u] thread called with misaligned sp: %p", i, signalsp);
432 }
433 #else
434 atf_tc_skip("Not implemented on this platform");
435 #endif
436 }
437
438 ATF_TC(misaligned_sp_and_signal);
439 ATF_TC_HEAD(misaligned_sp_and_signal, tc)
440 {
441 atf_tc_set_md_var(tc, "descr", "process can return from a signal"
442 " handler even if the stack pointer is misaligned when a signal"
443 " arrives");
444 }
445 ATF_TC_BODY(misaligned_sp_and_signal, tc)
446 {
447 #if defined STACK_ALIGNBYTES && defined HAVE_STACK_POINTER_H
448 /*
449 * Set up a handler for SIGALRM.
450 */
451 struct sigaction sa;
452 memset(&sa, 0, sizeof(sa));
453 sa.sa_handler = &signalsphandler;
454 RL(sigaction(SIGALRM, &sa, NULL));
455
456 #if defined __alpha__ || defined __i386__ || defined __mips__
457 atf_tc_expect_fail("PR kern/58149:"
458 " Cannot return from a signal handler"
459 " if SP was misaligned when the signal arrived");
460 #endif
461
462 /*
463 * Set up an interval timer so that we receive SIGALRM after 50 ms.
464 */
465 struct itimerval itv;
466 memset(&itv, 0, sizeof(itv));
467 itv.it_value.tv_usec = 1000 * 50;
468 RL(setitimer(ITIMER_MONOTONIC, &itv, NULL));
469
470 /*
471 * Now misalign the SP. Wait for the signal to arrive and see what
472 * happens. This should be fine as long as we don't use it to
473 * access memory.
474 */
475 MISALIGN_SP;
476 while (signalsp == NULL) {
477 /*
478 * Make sure the compiler does not optimize this busy loop
479 * away.
480 */
481 __asm__("" ::: "memory");
482 }
483 /*
484 * We could successfully return from a signal handler. Now we
485 * should fix the SP before calling any functions.
486 */
487 FIX_SP;
488
489 /*
490 * But was the stack pointer aligned when we were on the signal
491 * handler?
492 */
493 ATF_CHECK_MSG(((uintptr_t)signalsp & STACK_ALIGNBYTES) == 0,
494 "signal handler was called with a misaligned sp: %p",
495 signalsp);
496 #else
497 atf_tc_skip("Not implemented for this platform");
498 #endif
499 }
500
501 ATF_TP_ADD_TCS(tp)
502 {
503
504 ATF_TP_ADD_TC(tp, contextsp);
505 ATF_TP_ADD_TC(tp, execsp_dynamic);
506 ATF_TP_ADD_TC(tp, execsp_static);
507 ATF_TP_ADD_TC(tp, misaligned_sp_and_signal);
508 ATF_TP_ADD_TC(tp, signalsp);
509 ATF_TP_ADD_TC(tp, signalsp_sigaltstack);
510 ATF_TP_ADD_TC(tp, threadsp);
511 return atf_no_error();
512 }
513