t_signal_and_sp.c revision 1.10 1 1.10 riastrad /* $NetBSD: t_signal_and_sp.c,v 1.10 2025/04/21 12:06:08 riastradh Exp $ */
2 1.1 pho
3 1.1 pho /*
4 1.1 pho * Copyright (c) 2024 The NetBSD Foundation, Inc.
5 1.1 pho * All rights reserved.
6 1.1 pho *
7 1.1 pho * Redistribution and use in source and binary forms, with or without
8 1.1 pho * modification, are permitted provided that the following conditions
9 1.1 pho * are met:
10 1.1 pho * 1. Redistributions of source code must retain the above copyright
11 1.1 pho * notice, this list of conditions and the following disclaimer.
12 1.1 pho * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 pho * notice, this list of conditions and the following disclaimer in the
14 1.1 pho * documentation and/or other materials provided with the distribution.
15 1.1 pho *
16 1.1 pho * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 pho * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 pho * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 pho * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 pho * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 pho * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 pho * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 pho * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 pho * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 pho * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 pho * POSSIBILITY OF SUCH DAMAGE.
27 1.1 pho */
28 1.1 pho
29 1.2 riastrad #include <sys/cdefs.h>
30 1.10 riastrad __RCSID("$NetBSD: t_signal_and_sp.c,v 1.10 2025/04/21 12:06:08 riastradh Exp $");
31 1.2 riastrad
32 1.2 riastrad #include <sys/wait.h>
33 1.2 riastrad
34 1.2 riastrad #include <machine/param.h>
35 1.2 riastrad
36 1.1 pho #include <atf-c.h>
37 1.2 riastrad #include <limits.h>
38 1.2 riastrad #include <poll.h>
39 1.7 riastrad #include <pthread.h>
40 1.2 riastrad #include <signal.h>
41 1.2 riastrad #include <stdint.h>
42 1.2 riastrad #include <stdio.h>
43 1.10 riastrad #include <stdlib.h>
44 1.2 riastrad #include <string.h>
45 1.2 riastrad #include <time.h>
46 1.7 riastrad #include <ucontext.h>
47 1.2 riastrad #include <unistd.h>
48 1.1 pho
49 1.2 riastrad #include "h_execsp.h"
50 1.2 riastrad #include "h_macros.h"
51 1.2 riastrad
52 1.2 riastrad #ifdef HAVE_STACK_POINTER_H
53 1.1 pho # include "stack_pointer.h"
54 1.2 riastrad #endif
55 1.1 pho
56 1.2 riastrad #ifdef HAVE_SIGNALSPHANDLER
57 1.2 riastrad void signalsphandler(int); /* signalsphandler.S assembly routine */
58 1.2 riastrad #endif
59 1.2 riastrad
60 1.2 riastrad void *volatile signalsp;
61 1.2 riastrad
62 1.2 riastrad static void
63 1.2 riastrad test_execsp(const struct atf_tc *tc, const char *prog)
64 1.1 pho {
65 1.2 riastrad #ifdef STACK_ALIGNBYTES
66 1.2 riastrad char h_execsp[PATH_MAX];
67 1.2 riastrad struct execsp execsp;
68 1.2 riastrad int fd[2];
69 1.2 riastrad pid_t pid;
70 1.2 riastrad struct pollfd pollfd;
71 1.2 riastrad int nfds;
72 1.2 riastrad ssize_t nread;
73 1.2 riastrad int status;
74 1.2 riastrad
75 1.1 pho /*
76 1.2 riastrad * Determine the full path to the helper program.
77 1.1 pho */
78 1.2 riastrad RL(snprintf(h_execsp, sizeof(h_execsp), "%s/%s",
79 1.2 riastrad atf_tc_get_config_var(tc, "srcdir"), prog));
80 1.2 riastrad
81 1.2 riastrad /*
82 1.2 riastrad * Create a pipe to read a bundle of stack pointer samples from
83 1.2 riastrad * the child, and fork the child.
84 1.2 riastrad */
85 1.2 riastrad RL(pipe(fd));
86 1.2 riastrad RL(pid = vfork());
87 1.2 riastrad if (pid == 0) { /* child */
88 1.2 riastrad char *const argv[] = {h_execsp, NULL};
89 1.2 riastrad
90 1.2 riastrad if (dup2(fd[1], STDOUT_FILENO) == -1)
91 1.2 riastrad _exit(1);
92 1.2 riastrad if (closefrom(STDERR_FILENO + 1) == -1)
93 1.2 riastrad _exit(2);
94 1.2 riastrad if (execve(argv[0], argv, NULL) == -1)
95 1.2 riastrad _exit(3);
96 1.2 riastrad _exit(4);
97 1.2 riastrad }
98 1.2 riastrad
99 1.2 riastrad /*
100 1.2 riastrad * Close the writing end so, if something goes wrong in the
101 1.2 riastrad * child, we don't hang indefinitely waiting for output.
102 1.2 riastrad */
103 1.2 riastrad RL(close(fd[1]));
104 1.2 riastrad
105 1.2 riastrad /*
106 1.2 riastrad * Wait up to 5sec for the child to return an answer. Any more
107 1.2 riastrad * than that, and we kill it. The child is mostly hand-written
108 1.2 riastrad * assembly routines where lots can go wrong, so don't bother
109 1.2 riastrad * waiting if it gets stuck in a loop.
110 1.2 riastrad */
111 1.2 riastrad pollfd.fd = fd[0];
112 1.2 riastrad pollfd.events = POLLIN;
113 1.2 riastrad RL(nfds = poll(&pollfd, 1, 5*1000/*ms*/));
114 1.2 riastrad if (nfds == 0) {
115 1.2 riastrad fprintf(stderr, "child hung, killing\n");
116 1.2 riastrad RL(kill(pid, SIGKILL));
117 1.2 riastrad }
118 1.1 pho
119 1.1 pho /*
120 1.2 riastrad * Read a bundle of stack pointer samples from the child.
121 1.1 pho */
122 1.2 riastrad RL(nread = read(fd[0], &execsp, sizeof(execsp)));
123 1.2 riastrad ATF_CHECK_MSG((size_t)nread == sizeof(execsp),
124 1.2 riastrad "nread=%zu sizeof(execsp)=%zu",
125 1.2 riastrad (size_t)nread, sizeof(execsp));
126 1.2 riastrad
127 1.2 riastrad /*
128 1.2 riastrad * Wait for the child to terminate and report failure if it
129 1.2 riastrad * didn't exit cleanly.
130 1.2 riastrad */
131 1.2 riastrad RL(waitpid(pid, &status, 0));
132 1.2 riastrad if (WIFSIGNALED(status)) {
133 1.2 riastrad atf_tc_fail_nonfatal("child exited on signal %d (%s)",
134 1.2 riastrad WTERMSIG(status), strsignal(WTERMSIG(status)));
135 1.2 riastrad } else if (!WIFEXITED(status)) {
136 1.2 riastrad atf_tc_fail_nonfatal("child exited status=0x%x", status);
137 1.2 riastrad } else {
138 1.2 riastrad ATF_CHECK_MSG(WEXITSTATUS(status) == 0,
139 1.2 riastrad "child exited with code %d",
140 1.2 riastrad WEXITSTATUS(status));
141 1.2 riastrad }
142 1.2 riastrad
143 1.2 riastrad /*
144 1.2 riastrad * Now that we have reaped the child, stop here if the stack
145 1.2 riastrad * pointer samples are bogus; otherwise verify they are all
146 1.2 riastrad * aligned.
147 1.2 riastrad */
148 1.2 riastrad if ((size_t)nread != sizeof(execsp))
149 1.2 riastrad return; /* failed already */
150 1.2 riastrad
151 1.2 riastrad printf("start sp @ %p\n", execsp.startsp);
152 1.3 riastrad printf("ctor sp @ %p\n", execsp.ctorsp);
153 1.2 riastrad printf("main sp @ %p\n", execsp.mainsp);
154 1.3 riastrad printf("dtor sp @ %p\n", execsp.dtorsp);
155 1.2 riastrad
156 1.2 riastrad ATF_CHECK_MSG(((uintptr_t)execsp.startsp & STACK_ALIGNBYTES) == 0,
157 1.2 riastrad "elf entry point was called with misaligned sp: %p",
158 1.2 riastrad execsp.startsp);
159 1.2 riastrad
160 1.3 riastrad ATF_CHECK_MSG(((uintptr_t)execsp.ctorsp & STACK_ALIGNBYTES) == 0,
161 1.3 riastrad "elf constructor was called with misaligned sp: %p",
162 1.3 riastrad execsp.ctorsp);
163 1.3 riastrad
164 1.2 riastrad ATF_CHECK_MSG(((uintptr_t)execsp.mainsp & STACK_ALIGNBYTES) == 0,
165 1.2 riastrad "main function was called with misaligned sp: %p",
166 1.2 riastrad execsp.mainsp);
167 1.2 riastrad
168 1.3 riastrad ATF_CHECK_MSG(((uintptr_t)execsp.dtorsp & STACK_ALIGNBYTES) == 0,
169 1.3 riastrad "elf destructor was called with misaligned sp: %p",
170 1.3 riastrad execsp.dtorsp);
171 1.3 riastrad
172 1.2 riastrad /*
173 1.2 riastrad * Leave a reminder on architectures for which we haven't
174 1.2 riastrad * implemented execsp_start.S.
175 1.2 riastrad */
176 1.3 riastrad if (execsp.startsp == NULL ||
177 1.3 riastrad execsp.ctorsp == NULL ||
178 1.3 riastrad execsp.mainsp == NULL ||
179 1.3 riastrad execsp.dtorsp == NULL)
180 1.2 riastrad atf_tc_skip("Not fully supported on this architecture");
181 1.2 riastrad #else
182 1.2 riastrad atf_tc_skip("Unknown STACK_ALIGNBYTES on this architecture");
183 1.2 riastrad #endif
184 1.2 riastrad }
185 1.2 riastrad
186 1.2 riastrad ATF_TC(execsp_dynamic);
187 1.2 riastrad ATF_TC_HEAD(execsp_dynamic, tc)
188 1.2 riastrad {
189 1.2 riastrad atf_tc_set_md_var(tc, "descr",
190 1.2 riastrad "Verify stack pointer is aligned on dynamic program start");
191 1.2 riastrad }
192 1.2 riastrad ATF_TC_BODY(execsp_dynamic, tc)
193 1.2 riastrad {
194 1.2 riastrad test_execsp(tc, "h_execsp_dynamic");
195 1.1 pho }
196 1.2 riastrad
197 1.2 riastrad ATF_TC(execsp_static);
198 1.2 riastrad ATF_TC_HEAD(execsp_static, tc)
199 1.2 riastrad {
200 1.2 riastrad atf_tc_set_md_var(tc, "descr",
201 1.2 riastrad "Verify stack pointer is aligned on static program start");
202 1.2 riastrad }
203 1.2 riastrad ATF_TC_BODY(execsp_static, tc)
204 1.2 riastrad {
205 1.2 riastrad test_execsp(tc, "h_execsp_static");
206 1.2 riastrad }
207 1.2 riastrad
208 1.7 riastrad #if defined STACK_ALIGNBYTES && defined HAVE_CONTEXTSPFUNC
209 1.7 riastrad void *volatile contextsp; /* set by contextspfunc.S */
210 1.9 riastrad static ucontext_t return_context;
211 1.7 riastrad static volatile bool test_context_done;
212 1.7 riastrad
213 1.7 riastrad void contextspfunc(void); /* contextspfunc.S assembly routine */
214 1.7 riastrad
215 1.9 riastrad static void
216 1.9 riastrad contextnoop(void)
217 1.9 riastrad {
218 1.9 riastrad
219 1.9 riastrad fprintf(stderr, "contextnoop\n");
220 1.9 riastrad /* control will return to contextspfunc via uc_link */
221 1.9 riastrad }
222 1.9 riastrad
223 1.7 riastrad void contextdone(void); /* called by contextspfunc.S */
224 1.7 riastrad void
225 1.7 riastrad contextdone(void)
226 1.7 riastrad {
227 1.7 riastrad
228 1.7 riastrad fprintf(stderr, "contextdone\n");
229 1.7 riastrad ATF_REQUIRE(!test_context_done);
230 1.7 riastrad test_context_done = true;
231 1.7 riastrad RL(setcontext(&return_context));
232 1.7 riastrad atf_tc_fail("setcontext returned");
233 1.7 riastrad }
234 1.7 riastrad #endif
235 1.7 riastrad
236 1.7 riastrad ATF_TC(contextsp);
237 1.7 riastrad ATF_TC_HEAD(contextsp, tc)
238 1.7 riastrad {
239 1.7 riastrad atf_tc_set_md_var(tc, "descr",
240 1.7 riastrad "Verify stack pointer is aligned on makecontext entry");
241 1.7 riastrad }
242 1.7 riastrad ATF_TC_BODY(contextsp, tc)
243 1.7 riastrad {
244 1.7 riastrad #if defined STACK_ALIGNBYTES && defined HAVE_CONTEXTSPFUNC
245 1.9 riastrad ucontext_t uc;
246 1.7 riastrad char *stack;
247 1.7 riastrad unsigned i;
248 1.7 riastrad
249 1.7 riastrad REQUIRE_LIBC(stack = malloc(SIGSTKSZ + STACK_ALIGNBYTES), NULL);
250 1.9 riastrad fprintf(stderr, "stack @ [%p,%p)\n", stack,
251 1.9 riastrad stack + SIGSTKSZ + STACK_ALIGNBYTES);
252 1.9 riastrad
253 1.9 riastrad for (i = 0; i <= STACK_ALIGNBYTES; i++) {
254 1.9 riastrad contextsp = NULL;
255 1.9 riastrad test_context_done = false;
256 1.9 riastrad
257 1.9 riastrad RL(getcontext(&uc));
258 1.9 riastrad uc.uc_stack.ss_sp = stack;
259 1.9 riastrad uc.uc_stack.ss_size = SIGSTKSZ + i;
260 1.9 riastrad makecontext(&uc, &contextspfunc, 0);
261 1.9 riastrad
262 1.9 riastrad fprintf(stderr, "[%u] swapcontext\n", i);
263 1.9 riastrad RL(swapcontext(&return_context, &uc));
264 1.9 riastrad
265 1.9 riastrad ATF_CHECK(contextsp != NULL);
266 1.9 riastrad ATF_CHECK_MSG((uintptr_t)stack <= (uintptr_t)contextsp &&
267 1.9 riastrad (uintptr_t)contextsp <= (uintptr_t)stack + SIGSTKSZ + i,
268 1.9 riastrad "contextsp=%p", contextsp);
269 1.9 riastrad ATF_CHECK_MSG(((uintptr_t)contextsp & STACK_ALIGNBYTES) == 0,
270 1.9 riastrad "[%u] makecontext function called with misaligned sp %p",
271 1.9 riastrad i, contextsp);
272 1.9 riastrad }
273 1.9 riastrad
274 1.9 riastrad for (i = 0; i <= STACK_ALIGNBYTES; i++) {
275 1.9 riastrad contextsp = NULL;
276 1.9 riastrad test_context_done = false;
277 1.9 riastrad
278 1.9 riastrad RL(getcontext(&uc));
279 1.9 riastrad uc.uc_stack.ss_sp = stack + i;
280 1.9 riastrad uc.uc_stack.ss_size = SIGSTKSZ;
281 1.9 riastrad makecontext(&uc, &contextspfunc, 0);
282 1.9 riastrad
283 1.9 riastrad fprintf(stderr, "[%u] swapcontext\n", i);
284 1.9 riastrad RL(swapcontext(&return_context, &uc));
285 1.9 riastrad
286 1.9 riastrad ATF_CHECK(contextsp != NULL);
287 1.9 riastrad ATF_CHECK_MSG((uintptr_t)stack + i <= (uintptr_t)contextsp &&
288 1.9 riastrad (uintptr_t)contextsp <= (uintptr_t)stack + i + SIGSTKSZ,
289 1.9 riastrad "contextsp=%p", contextsp);
290 1.9 riastrad ATF_CHECK_MSG(((uintptr_t)contextsp & STACK_ALIGNBYTES) == 0,
291 1.9 riastrad "[%u] makecontext function called with misaligned sp %p",
292 1.9 riastrad i, contextsp);
293 1.9 riastrad }
294 1.9 riastrad #else
295 1.9 riastrad atf_tc_skip("Not implemented on this platform");
296 1.9 riastrad #endif
297 1.9 riastrad }
298 1.9 riastrad
299 1.9 riastrad ATF_TC(contextsplink);
300 1.9 riastrad ATF_TC_HEAD(contextsplink, tc)
301 1.9 riastrad {
302 1.9 riastrad atf_tc_set_md_var(tc, "descr",
303 1.9 riastrad "Verify stack pointer is aligned on makecontext link entry");
304 1.9 riastrad }
305 1.9 riastrad ATF_TC_BODY(contextsplink, tc)
306 1.9 riastrad {
307 1.9 riastrad #if defined STACK_ALIGNBYTES && defined HAVE_CONTEXTSPFUNC
308 1.9 riastrad ucontext_t uc1, uc2;
309 1.9 riastrad char *stack1, *stack2;
310 1.9 riastrad unsigned i;
311 1.9 riastrad
312 1.9 riastrad REQUIRE_LIBC(stack1 = malloc(SIGSTKSZ), NULL);
313 1.9 riastrad fprintf(stderr, "stack1 @ [%p,%p)\n", stack1, stack1 + SIGSTKSZ);
314 1.9 riastrad REQUIRE_LIBC(stack2 = malloc(SIGSTKSZ + STACK_ALIGNBYTES), NULL);
315 1.9 riastrad fprintf(stderr, "stack2 @ [%p,%p)\n",
316 1.9 riastrad stack2, stack2 + SIGSTKSZ + STACK_ALIGNBYTES);
317 1.9 riastrad
318 1.9 riastrad #ifdef __mips_n64
319 1.9 riastrad atf_tc_expect_fail("PR kern/59327:"
320 1.9 riastrad " user stack pointer is not aligned properly");
321 1.9 riastrad #endif
322 1.7 riastrad
323 1.7 riastrad for (i = 0; i <= STACK_ALIGNBYTES; i++) {
324 1.7 riastrad contextsp = NULL;
325 1.7 riastrad test_context_done = false;
326 1.9 riastrad
327 1.9 riastrad RL(getcontext(&uc1));
328 1.9 riastrad uc1.uc_stack.ss_sp = stack1;
329 1.9 riastrad uc1.uc_stack.ss_size = SIGSTKSZ;
330 1.9 riastrad uc1.uc_link = &uc2;
331 1.9 riastrad makecontext(&uc1, &contextnoop, 0);
332 1.9 riastrad
333 1.9 riastrad RL(getcontext(&uc2));
334 1.9 riastrad uc2.uc_stack.ss_sp = stack2;
335 1.9 riastrad uc2.uc_stack.ss_size = SIGSTKSZ + i;
336 1.9 riastrad makecontext(&uc2, &contextspfunc, 0);
337 1.9 riastrad
338 1.7 riastrad fprintf(stderr, "[%u] swapcontext\n", i);
339 1.9 riastrad RL(swapcontext(&return_context, &uc1));
340 1.9 riastrad
341 1.7 riastrad ATF_CHECK(contextsp != NULL);
342 1.9 riastrad ATF_CHECK_MSG((uintptr_t)stack2 <= (uintptr_t)contextsp &&
343 1.9 riastrad (uintptr_t)contextsp <= (uintptr_t)stack2 + SIGSTKSZ + i,
344 1.9 riastrad "contextsp=%p", contextsp);
345 1.7 riastrad ATF_CHECK_MSG(((uintptr_t)contextsp & STACK_ALIGNBYTES) == 0,
346 1.7 riastrad "[%u] makecontext function called with misaligned sp %p",
347 1.7 riastrad i, contextsp);
348 1.7 riastrad }
349 1.7 riastrad
350 1.7 riastrad for (i = 0; i <= STACK_ALIGNBYTES; i++) {
351 1.7 riastrad contextsp = NULL;
352 1.7 riastrad test_context_done = false;
353 1.9 riastrad
354 1.9 riastrad RL(getcontext(&uc1));
355 1.9 riastrad uc1.uc_stack.ss_sp = stack1;
356 1.9 riastrad uc1.uc_stack.ss_size = SIGSTKSZ;
357 1.9 riastrad uc1.uc_link = &uc2;
358 1.9 riastrad makecontext(&uc1, &contextnoop, 0);
359 1.9 riastrad
360 1.9 riastrad RL(getcontext(&uc2));
361 1.9 riastrad uc2.uc_stack.ss_sp = stack2 + i;
362 1.9 riastrad uc2.uc_stack.ss_size = SIGSTKSZ;
363 1.9 riastrad makecontext(&uc2, &contextspfunc, 0);
364 1.9 riastrad
365 1.7 riastrad fprintf(stderr, "[%u] swapcontext\n", i);
366 1.9 riastrad RL(swapcontext(&return_context, &uc1));
367 1.9 riastrad
368 1.7 riastrad ATF_CHECK(contextsp != NULL);
369 1.9 riastrad ATF_CHECK_MSG((uintptr_t)stack2 + i <= (uintptr_t)contextsp &&
370 1.9 riastrad (uintptr_t)contextsp <= (uintptr_t)stack2 + SIGSTKSZ + i,
371 1.9 riastrad "contextsp=%p", contextsp);
372 1.7 riastrad ATF_CHECK_MSG(((uintptr_t)contextsp & STACK_ALIGNBYTES) == 0,
373 1.7 riastrad "[%u] makecontext function called with misaligned sp %p",
374 1.7 riastrad i, contextsp);
375 1.7 riastrad }
376 1.7 riastrad #else
377 1.7 riastrad atf_tc_skip("Not implemented on this platform");
378 1.7 riastrad #endif
379 1.7 riastrad }
380 1.7 riastrad
381 1.2 riastrad ATF_TC(signalsp);
382 1.2 riastrad ATF_TC_HEAD(signalsp, tc)
383 1.2 riastrad {
384 1.2 riastrad atf_tc_set_md_var(tc, "descr",
385 1.2 riastrad "Verify stack pointer is aligned on entry to signal handler");
386 1.2 riastrad }
387 1.2 riastrad ATF_TC_BODY(signalsp, tc)
388 1.2 riastrad {
389 1.2 riastrad #if defined STACK_ALIGNBYTES && defined HAVE_SIGNALSPHANDLER
390 1.2 riastrad struct sigaction sa;
391 1.2 riastrad
392 1.2 riastrad memset(&sa, 0, sizeof(sa));
393 1.2 riastrad sa.sa_handler = &signalsphandler;
394 1.2 riastrad RL(sigaction(SIGUSR1, &sa, NULL));
395 1.2 riastrad RL(raise(SIGUSR1));
396 1.2 riastrad
397 1.2 riastrad ATF_CHECK_MSG(((uintptr_t)signalsp & STACK_ALIGNBYTES) == 0,
398 1.2 riastrad "signal handler was called with a misaligned sp: %p",
399 1.2 riastrad signalsp);
400 1.2 riastrad #else
401 1.2 riastrad atf_tc_skip("Not implemented on this platform");
402 1.2 riastrad #endif
403 1.2 riastrad }
404 1.2 riastrad
405 1.2 riastrad ATF_TC(signalsp_sigaltstack);
406 1.2 riastrad ATF_TC_HEAD(signalsp_sigaltstack, tc)
407 1.2 riastrad {
408 1.2 riastrad atf_tc_set_md_var(tc, "descr",
409 1.2 riastrad "Verify stack pointer is aligned on entry to signal handler"
410 1.2 riastrad " with maximally misaligned sigaltstack");
411 1.2 riastrad }
412 1.2 riastrad ATF_TC_BODY(signalsp_sigaltstack, tc)
413 1.2 riastrad {
414 1.2 riastrad #if defined STACK_ALIGNBYTES && HAVE_SIGNALSPHANDLER
415 1.2 riastrad char *stack;
416 1.2 riastrad struct sigaction sa;
417 1.2 riastrad struct sigaltstack ss;
418 1.2 riastrad unsigned i;
419 1.2 riastrad
420 1.2 riastrad memset(&sa, 0, sizeof(sa));
421 1.2 riastrad sa.sa_handler = &signalsphandler;
422 1.2 riastrad sa.sa_flags = SA_ONSTACK;
423 1.2 riastrad RL(sigaction(SIGUSR1, &sa, NULL));
424 1.2 riastrad
425 1.2 riastrad /*
426 1.2 riastrad * Allocate a signal stack with enough slop to try all possible
427 1.2 riastrad * misalignments of the stack pointer. Print it to stderr so
428 1.2 riastrad * it always appears in atf output before shenanigans happen.
429 1.2 riastrad */
430 1.2 riastrad REQUIRE_LIBC(stack = malloc(SIGSTKSZ + STACK_ALIGNBYTES), NULL);
431 1.7 riastrad fprintf(stderr, "stack @ [%p, %p)\n",
432 1.2 riastrad stack, stack + SIGSTKSZ + STACK_ALIGNBYTES);
433 1.2 riastrad
434 1.6 riastrad #if defined __alpha__ || defined __i386__ || defined __mips__
435 1.4 riastrad atf_tc_expect_fail("PR kern/59327:"
436 1.4 riastrad " user stack pointer is not aligned properly");
437 1.4 riastrad #endif
438 1.4 riastrad
439 1.2 riastrad /*
440 1.2 riastrad * Try with all alignments of high addresses.
441 1.2 riastrad */
442 1.2 riastrad for (i = 0; i <= STACK_ALIGNBYTES; i++) {
443 1.2 riastrad ss.ss_sp = stack;
444 1.2 riastrad ss.ss_size = SIGSTKSZ + i;
445 1.2 riastrad ss.ss_flags = 0;
446 1.2 riastrad RL(sigaltstack(&ss, NULL));
447 1.2 riastrad
448 1.2 riastrad signalsp = NULL;
449 1.2 riastrad RL(raise(SIGUSR1));
450 1.2 riastrad ATF_CHECK(signalsp != NULL);
451 1.9 riastrad ATF_CHECK_MSG((uintptr_t)stack <= (uintptr_t)signalsp &&
452 1.9 riastrad (uintptr_t)signalsp <= (uintptr_t)stack + SIGSTKSZ + i,
453 1.9 riastrad "signalsp=%p", signalsp);
454 1.2 riastrad ATF_CHECK_MSG(((uintptr_t)signalsp & STACK_ALIGNBYTES) == 0,
455 1.2 riastrad "[%u] signal handler was called with a misaligned sp: %p",
456 1.2 riastrad i, signalsp);
457 1.2 riastrad }
458 1.2 riastrad
459 1.2 riastrad /*
460 1.2 riastrad * Try with all alignments of low addresses.
461 1.2 riastrad */
462 1.2 riastrad for (i = 0; i <= STACK_ALIGNBYTES; i++) {
463 1.2 riastrad ss.ss_sp = stack + i;
464 1.2 riastrad ss.ss_size = SIGSTKSZ;
465 1.2 riastrad ss.ss_flags = 0;
466 1.2 riastrad RL(sigaltstack(&ss, NULL));
467 1.2 riastrad
468 1.2 riastrad signalsp = NULL;
469 1.2 riastrad RL(raise(SIGUSR1));
470 1.2 riastrad ATF_CHECK(signalsp != NULL);
471 1.9 riastrad ATF_CHECK_MSG((uintptr_t)stack + i <= (uintptr_t)signalsp &&
472 1.9 riastrad (uintptr_t)signalsp <= (uintptr_t)stack + i + SIGSTKSZ,
473 1.9 riastrad "signalsp=%p", signalsp);
474 1.2 riastrad ATF_CHECK_MSG(((uintptr_t)signalsp & STACK_ALIGNBYTES) == 0,
475 1.2 riastrad "[%u] signal handler was called with a misaligned sp: %p",
476 1.2 riastrad i, signalsp);
477 1.2 riastrad }
478 1.2 riastrad #else
479 1.2 riastrad atf_tc_skip("Not implemented on this platform");
480 1.1 pho #endif
481 1.2 riastrad }
482 1.1 pho
483 1.7 riastrad #if defined STACK_ALIGNBYTES && defined HAVE_THREADSPFUNC
484 1.7 riastrad void *threadspfunc(void *); /* threadspfunc.S assembly routine */
485 1.7 riastrad #endif
486 1.7 riastrad
487 1.7 riastrad ATF_TC(threadsp);
488 1.7 riastrad ATF_TC_HEAD(threadsp, tc)
489 1.7 riastrad {
490 1.7 riastrad atf_tc_set_md_var(tc, "descr",
491 1.7 riastrad "Verify stack pointer is aligned on thread start");
492 1.7 riastrad }
493 1.7 riastrad ATF_TC_BODY(threadsp, tc)
494 1.7 riastrad {
495 1.7 riastrad #if defined STACK_ALIGNBYTES && defined HAVE_THREADSPFUNC
496 1.7 riastrad char *stack;
497 1.7 riastrad unsigned i;
498 1.7 riastrad
499 1.7 riastrad REQUIRE_LIBC(stack = malloc(SIGSTKSZ + STACK_ALIGNBYTES), NULL);
500 1.9 riastrad fprintf(stderr, "stack @ [%p,%p)\n", stack,
501 1.9 riastrad stack + SIGSTKSZ + STACK_ALIGNBYTES);
502 1.9 riastrad
503 1.9 riastrad #ifdef __hppa__
504 1.9 riastrad atf_tc_expect_signal(SIGBUS, "PR kern/59327:"
505 1.9 riastrad " user stack pointer is not aligned properly");
506 1.9 riastrad #endif
507 1.10 riastrad #ifdef __mips__
508 1.10 riastrad atf_tc_expect_fail("PR kern/59327:"
509 1.10 riastrad " user stack pointer is not aligned properly");
510 1.9 riastrad #endif
511 1.7 riastrad
512 1.7 riastrad for (i = 0; i <= STACK_ALIGNBYTES; i++) {
513 1.7 riastrad pthread_t t;
514 1.7 riastrad pthread_attr_t attr;
515 1.7 riastrad void *sp;
516 1.7 riastrad
517 1.7 riastrad RZ(pthread_attr_init(&attr));
518 1.7 riastrad RZ(pthread_attr_setstack(&attr, stack, SIGSTKSZ + i));
519 1.7 riastrad RZ(pthread_create(&t, &attr, &threadspfunc, NULL));
520 1.7 riastrad RZ(pthread_attr_destroy(&attr));
521 1.7 riastrad
522 1.7 riastrad alarm(1);
523 1.7 riastrad RZ(pthread_join(t, &sp));
524 1.7 riastrad alarm(0);
525 1.7 riastrad
526 1.7 riastrad ATF_CHECK(sp != NULL);
527 1.9 riastrad ATF_CHECK_MSG((uintptr_t)stack <= (uintptr_t)sp &&
528 1.9 riastrad (uintptr_t)sp <= (uintptr_t)stack + SIGSTKSZ + i,
529 1.9 riastrad "sp=%p", sp);
530 1.10 riastrad ATF_CHECK_MSG(((uintptr_t)sp & STACK_ALIGNBYTES) == 0,
531 1.10 riastrad "[%u] thread called with misaligned sp: %p", i, sp);
532 1.7 riastrad }
533 1.7 riastrad
534 1.7 riastrad for (i = 0; i <= STACK_ALIGNBYTES; i++) {
535 1.7 riastrad pthread_t t;
536 1.7 riastrad pthread_attr_t attr;
537 1.7 riastrad void *sp;
538 1.7 riastrad
539 1.7 riastrad RZ(pthread_attr_init(&attr));
540 1.7 riastrad RZ(pthread_attr_setstack(&attr, stack + i, SIGSTKSZ));
541 1.7 riastrad RZ(pthread_create(&t, &attr, &threadspfunc, NULL));
542 1.7 riastrad RZ(pthread_attr_destroy(&attr));
543 1.7 riastrad
544 1.7 riastrad alarm(1);
545 1.7 riastrad RZ(pthread_join(t, &sp));
546 1.7 riastrad alarm(0);
547 1.7 riastrad
548 1.7 riastrad ATF_CHECK(sp != NULL);
549 1.9 riastrad ATF_CHECK_MSG((uintptr_t)stack + i <= (uintptr_t)sp &&
550 1.9 riastrad (uintptr_t)sp <= (uintptr_t)stack + i + SIGSTKSZ,
551 1.9 riastrad "sp=%p", sp);
552 1.10 riastrad ATF_CHECK_MSG(((uintptr_t)sp & STACK_ALIGNBYTES) == 0,
553 1.10 riastrad "[%u] thread called with misaligned sp: %p", i, sp);
554 1.7 riastrad }
555 1.7 riastrad #else
556 1.7 riastrad atf_tc_skip("Not implemented on this platform");
557 1.7 riastrad #endif
558 1.7 riastrad }
559 1.7 riastrad
560 1.1 pho ATF_TC(misaligned_sp_and_signal);
561 1.1 pho ATF_TC_HEAD(misaligned_sp_and_signal, tc)
562 1.1 pho {
563 1.1 pho atf_tc_set_md_var(tc, "descr", "process can return from a signal"
564 1.1 pho " handler even if the stack pointer is misaligned when a signal"
565 1.1 pho " arrives");
566 1.1 pho }
567 1.1 pho ATF_TC_BODY(misaligned_sp_and_signal, tc)
568 1.1 pho {
569 1.2 riastrad #if defined STACK_ALIGNBYTES && defined HAVE_STACK_POINTER_H
570 1.1 pho /*
571 1.1 pho * Set up a handler for SIGALRM.
572 1.1 pho */
573 1.1 pho struct sigaction sa;
574 1.1 pho memset(&sa, 0, sizeof(sa));
575 1.2 riastrad sa.sa_handler = &signalsphandler;
576 1.2 riastrad RL(sigaction(SIGALRM, &sa, NULL));
577 1.1 pho
578 1.6 riastrad #if defined __alpha__ || defined __i386__ || defined __mips__
579 1.4 riastrad atf_tc_expect_fail("PR kern/58149:"
580 1.4 riastrad " Cannot return from a signal handler"
581 1.4 riastrad " if SP was misaligned when the signal arrived");
582 1.4 riastrad #endif
583 1.4 riastrad
584 1.1 pho /*
585 1.1 pho * Set up an interval timer so that we receive SIGALRM after 50 ms.
586 1.1 pho */
587 1.1 pho struct itimerval itv;
588 1.1 pho memset(&itv, 0, sizeof(itv));
589 1.1 pho itv.it_value.tv_usec = 1000 * 50;
590 1.2 riastrad RL(setitimer(ITIMER_MONOTONIC, &itv, NULL));
591 1.1 pho
592 1.1 pho /*
593 1.1 pho * Now misalign the SP. Wait for the signal to arrive and see what
594 1.1 pho * happens. This should be fine as long as we don't use it to
595 1.1 pho * access memory.
596 1.1 pho */
597 1.1 pho MISALIGN_SP;
598 1.2 riastrad while (signalsp == NULL) {
599 1.1 pho /*
600 1.1 pho * Make sure the compiler does not optimize this busy loop
601 1.1 pho * away.
602 1.1 pho */
603 1.2 riastrad __asm__("" ::: "memory");
604 1.1 pho }
605 1.1 pho /*
606 1.1 pho * We could successfully return from a signal handler. Now we
607 1.1 pho * should fix the SP before calling any functions.
608 1.1 pho */
609 1.1 pho FIX_SP;
610 1.1 pho
611 1.1 pho /*
612 1.1 pho * But was the stack pointer aligned when we were on the signal
613 1.1 pho * handler?
614 1.1 pho */
615 1.2 riastrad ATF_CHECK_MSG(((uintptr_t)signalsp & STACK_ALIGNBYTES) == 0,
616 1.1 pho "signal handler was called with a misaligned sp: %p",
617 1.2 riastrad signalsp);
618 1.1 pho #else
619 1.1 pho atf_tc_skip("Not implemented for this platform");
620 1.1 pho #endif
621 1.1 pho }
622 1.1 pho
623 1.1 pho ATF_TP_ADD_TCS(tp)
624 1.1 pho {
625 1.2 riastrad
626 1.7 riastrad ATF_TP_ADD_TC(tp, contextsp);
627 1.9 riastrad ATF_TP_ADD_TC(tp, contextsplink);
628 1.2 riastrad ATF_TP_ADD_TC(tp, execsp_dynamic);
629 1.2 riastrad ATF_TP_ADD_TC(tp, execsp_static);
630 1.1 pho ATF_TP_ADD_TC(tp, misaligned_sp_and_signal);
631 1.2 riastrad ATF_TP_ADD_TC(tp, signalsp);
632 1.2 riastrad ATF_TP_ADD_TC(tp, signalsp_sigaltstack);
633 1.7 riastrad ATF_TP_ADD_TC(tp, threadsp);
634 1.1 pho return atf_no_error();
635 1.1 pho }
636