t_signal_and_sp.c revision 1.2 1 /* $NetBSD: t_signal_and_sp.c,v 1.2 2025/04/20 22:31:00 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.2 2025/04/20 22:31:00 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("main sp @ %p\n", execsp.mainsp);
150
151 ATF_CHECK_MSG(((uintptr_t)execsp.startsp & STACK_ALIGNBYTES) == 0,
152 "elf entry point was called with misaligned sp: %p",
153 execsp.startsp);
154
155 ATF_CHECK_MSG(((uintptr_t)execsp.mainsp & STACK_ALIGNBYTES) == 0,
156 "main function was called with misaligned sp: %p",
157 execsp.mainsp);
158
159 /*
160 * Leave a reminder on architectures for which we haven't
161 * implemented execsp_start.S.
162 */
163 if (execsp.startsp == NULL || execsp.mainsp == NULL)
164 atf_tc_skip("Not fully supported on this architecture");
165 #else
166 atf_tc_skip("Unknown STACK_ALIGNBYTES on this architecture");
167 #endif
168 }
169
170 ATF_TC(execsp_dynamic);
171 ATF_TC_HEAD(execsp_dynamic, tc)
172 {
173 atf_tc_set_md_var(tc, "descr",
174 "Verify stack pointer is aligned on dynamic program start");
175 }
176 ATF_TC_BODY(execsp_dynamic, tc)
177 {
178 test_execsp(tc, "h_execsp_dynamic");
179 }
180
181 ATF_TC(execsp_static);
182 ATF_TC_HEAD(execsp_static, tc)
183 {
184 atf_tc_set_md_var(tc, "descr",
185 "Verify stack pointer is aligned on static program start");
186 }
187 ATF_TC_BODY(execsp_static, tc)
188 {
189 test_execsp(tc, "h_execsp_static");
190 }
191
192 ATF_TC(signalsp);
193 ATF_TC_HEAD(signalsp, tc)
194 {
195 atf_tc_set_md_var(tc, "descr",
196 "Verify stack pointer is aligned on entry to signal handler");
197 }
198 ATF_TC_BODY(signalsp, tc)
199 {
200 #if defined STACK_ALIGNBYTES && defined HAVE_SIGNALSPHANDLER
201 struct sigaction sa;
202
203 memset(&sa, 0, sizeof(sa));
204 sa.sa_handler = &signalsphandler;
205 RL(sigaction(SIGUSR1, &sa, NULL));
206 RL(raise(SIGUSR1));
207
208 ATF_CHECK_MSG(((uintptr_t)signalsp & STACK_ALIGNBYTES) == 0,
209 "signal handler was called with a misaligned sp: %p",
210 signalsp);
211 #else
212 atf_tc_skip("Not implemented on this platform");
213 #endif
214 }
215
216 ATF_TC(signalsp_sigaltstack);
217 ATF_TC_HEAD(signalsp_sigaltstack, tc)
218 {
219 atf_tc_set_md_var(tc, "descr",
220 "Verify stack pointer is aligned on entry to signal handler"
221 " with maximally misaligned sigaltstack");
222 }
223 ATF_TC_BODY(signalsp_sigaltstack, tc)
224 {
225 #if defined STACK_ALIGNBYTES && HAVE_SIGNALSPHANDLER
226 char *stack;
227 struct sigaction sa;
228 struct sigaltstack ss;
229 unsigned i;
230
231 memset(&sa, 0, sizeof(sa));
232 sa.sa_handler = &signalsphandler;
233 sa.sa_flags = SA_ONSTACK;
234 RL(sigaction(SIGUSR1, &sa, NULL));
235
236 /*
237 * Allocate a signal stack with enough slop to try all possible
238 * misalignments of the stack pointer. Print it to stderr so
239 * it always appears in atf output before shenanigans happen.
240 */
241 REQUIRE_LIBC(stack = malloc(SIGSTKSZ + STACK_ALIGNBYTES), NULL);
242 fprintf(stderr, "stack @ [%p, %p)",
243 stack, stack + SIGSTKSZ + STACK_ALIGNBYTES);
244
245 /*
246 * Try with all alignments of high addresses.
247 */
248 for (i = 0; i <= STACK_ALIGNBYTES; i++) {
249 ss.ss_sp = stack;
250 ss.ss_size = SIGSTKSZ + i;
251 ss.ss_flags = 0;
252 RL(sigaltstack(&ss, NULL));
253
254 signalsp = NULL;
255 RL(raise(SIGUSR1));
256 ATF_CHECK(signalsp != NULL);
257 ATF_CHECK_MSG(((uintptr_t)signalsp & STACK_ALIGNBYTES) == 0,
258 "[%u] signal handler was called with a misaligned sp: %p",
259 i, signalsp);
260 }
261
262 /*
263 * Try with all alignments of low addresses.
264 */
265 for (i = 0; i <= STACK_ALIGNBYTES; i++) {
266 ss.ss_sp = stack + i;
267 ss.ss_size = SIGSTKSZ;
268 ss.ss_flags = 0;
269 RL(sigaltstack(&ss, NULL));
270
271 signalsp = NULL;
272 RL(raise(SIGUSR1));
273 ATF_CHECK(signalsp != NULL);
274 ATF_CHECK_MSG(((uintptr_t)signalsp & STACK_ALIGNBYTES) == 0,
275 "[%u] signal handler was called with a misaligned sp: %p",
276 i, signalsp);
277 }
278 #else
279 atf_tc_skip("Not implemented on this platform");
280 #endif
281 }
282
283 ATF_TC(misaligned_sp_and_signal);
284 ATF_TC_HEAD(misaligned_sp_and_signal, tc)
285 {
286 atf_tc_set_md_var(tc, "descr", "process can return from a signal"
287 " handler even if the stack pointer is misaligned when a signal"
288 " arrives");
289 }
290 ATF_TC_BODY(misaligned_sp_and_signal, tc)
291 {
292 #if defined STACK_ALIGNBYTES && defined HAVE_STACK_POINTER_H
293 /*
294 * Set up a handler for SIGALRM.
295 */
296 struct sigaction sa;
297 memset(&sa, 0, sizeof(sa));
298 sa.sa_handler = &signalsphandler;
299 RL(sigaction(SIGALRM, &sa, NULL));
300
301 /*
302 * Set up an interval timer so that we receive SIGALRM after 50 ms.
303 */
304 struct itimerval itv;
305 memset(&itv, 0, sizeof(itv));
306 itv.it_value.tv_usec = 1000 * 50;
307 RL(setitimer(ITIMER_MONOTONIC, &itv, NULL));
308
309 /*
310 * Now misalign the SP. Wait for the signal to arrive and see what
311 * happens. This should be fine as long as we don't use it to
312 * access memory.
313 */
314 MISALIGN_SP;
315 while (signalsp == NULL) {
316 /*
317 * Make sure the compiler does not optimize this busy loop
318 * away.
319 */
320 __asm__("" ::: "memory");
321 }
322 /*
323 * We could successfully return from a signal handler. Now we
324 * should fix the SP before calling any functions.
325 */
326 FIX_SP;
327
328 /*
329 * But was the stack pointer aligned when we were on the signal
330 * handler?
331 */
332 ATF_CHECK_MSG(((uintptr_t)signalsp & STACK_ALIGNBYTES) == 0,
333 "signal handler was called with a misaligned sp: %p",
334 signalsp);
335 #else
336 atf_tc_skip("Not implemented for this platform");
337 #endif
338 }
339
340 ATF_TP_ADD_TCS(tp)
341 {
342
343 ATF_TP_ADD_TC(tp, execsp_dynamic);
344 ATF_TP_ADD_TC(tp, execsp_static);
345 ATF_TP_ADD_TC(tp, misaligned_sp_and_signal);
346 ATF_TP_ADD_TC(tp, signalsp);
347 ATF_TP_ADD_TC(tp, signalsp_sigaltstack);
348 return atf_no_error();
349 }
350