t_siginfo.c revision 1.53 1 1.53 rin /* $NetBSD: t_siginfo.c,v 1.53 2024/06/29 06:58:23 rin Exp $ */
2 1.1 pgoyette
3 1.1 pgoyette /*-
4 1.1 pgoyette * Copyright (c) 2010 The NetBSD Foundation, Inc.
5 1.1 pgoyette * All rights reserved.
6 1.1 pgoyette *
7 1.1 pgoyette * Redistribution and use in source and binary forms, with or without
8 1.1 pgoyette * modification, are permitted provided that the following conditions
9 1.1 pgoyette * are met:
10 1.1 pgoyette * 1. Redistributions of source code must retain the above copyright
11 1.1 pgoyette * notice, this list of conditions and the following disclaimer.
12 1.1 pgoyette * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 pgoyette * notice, this list of conditions and the following disclaimer in the
14 1.1 pgoyette * documentation and/or other materials provided with the distribution.
15 1.1 pgoyette *
16 1.1 pgoyette * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 pgoyette * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 pgoyette * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 pgoyette * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 pgoyette * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 pgoyette * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 pgoyette * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 pgoyette * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 pgoyette * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 pgoyette * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 pgoyette * POSSIBILITY OF SUCH DAMAGE.
27 1.1 pgoyette */
28 1.1 pgoyette
29 1.1 pgoyette #include <atf-c.h>
30 1.1 pgoyette
31 1.1 pgoyette #include <sys/resource.h>
32 1.18 njoly #include <sys/sysctl.h>
33 1.1 pgoyette #include <sys/time.h>
34 1.1 pgoyette #include <sys/ucontext.h>
35 1.1 pgoyette #include <sys/wait.h>
36 1.1 pgoyette
37 1.1 pgoyette #include <assert.h>
38 1.32 maya #include <float.h>
39 1.32 maya #include <inttypes.h>
40 1.32 maya #include <setjmp.h>
41 1.1 pgoyette #include <signal.h>
42 1.1 pgoyette #include <stdio.h>
43 1.1 pgoyette #include <stdlib.h>
44 1.10 riz #include <string.h>
45 1.1 pgoyette #include <unistd.h>
46 1.1 pgoyette
47 1.22 matt #include <fenv.h>
48 1.30 christos #ifdef __HAVE_FENV
49 1.27 martin #include <ieeefp.h> /* only need for ARM Cortex/Neon hack */
50 1.22 matt #elif defined(_FLOAT_IEEE754)
51 1.1 pgoyette #include <ieeefp.h>
52 1.1 pgoyette #endif
53 1.1 pgoyette
54 1.19 christos #include "isqemu.h"
55 1.19 christos
56 1.39 kamil #ifdef ENABLE_TESTS
57 1.15 jym /* for sigbus */
58 1.17 martin volatile char *addr;
59 1.15 jym
60 1.1 pgoyette /* for sigchild */
61 1.1 pgoyette pid_t child;
62 1.1 pgoyette int code;
63 1.1 pgoyette int status;
64 1.1 pgoyette
65 1.1 pgoyette /* for sigfpe */
66 1.1 pgoyette sig_atomic_t fltdiv_signalled = 0;
67 1.1 pgoyette sig_atomic_t intdiv_signalled = 0;
68 1.1 pgoyette
69 1.1 pgoyette static void
70 1.1 pgoyette sig_debug(int signo, siginfo_t *info, ucontext_t *ctx)
71 1.1 pgoyette {
72 1.1 pgoyette unsigned int i;
73 1.1 pgoyette
74 1.1 pgoyette printf("%d %p %p\n", signo, info, ctx);
75 1.1 pgoyette if (info != NULL) {
76 1.1 pgoyette printf("si_signo=%d\n", info->si_signo);
77 1.1 pgoyette printf("si_errno=%d\n", info->si_errno);
78 1.1 pgoyette printf("si_code=%d\n", info->si_code);
79 1.1 pgoyette printf("si_value.sival_int=%d\n", info->si_value.sival_int);
80 1.1 pgoyette }
81 1.1 pgoyette if (ctx != NULL) {
82 1.1 pgoyette printf("uc_flags 0x%x\n", ctx->uc_flags);
83 1.1 pgoyette printf("uc_link %p\n", ctx->uc_link);
84 1.1 pgoyette for (i = 0; i < __arraycount(ctx->uc_sigmask.__bits); i++)
85 1.1 pgoyette printf("uc_sigmask[%d] 0x%x\n", i,
86 1.1 pgoyette ctx->uc_sigmask.__bits[i]);
87 1.14 jruoho printf("uc_stack %p %lu 0x%x\n", ctx->uc_stack.ss_sp,
88 1.1 pgoyette (unsigned long)ctx->uc_stack.ss_size,
89 1.1 pgoyette ctx->uc_stack.ss_flags);
90 1.3 pgoyette for (i = 0; i < __arraycount(ctx->uc_mcontext.__gregs); i++)
91 1.3 pgoyette printf("uc_mcontext.greg[%d] 0x%lx\n", i,
92 1.3 pgoyette (long)ctx->uc_mcontext.__gregs[i]);
93 1.1 pgoyette }
94 1.1 pgoyette }
95 1.1 pgoyette
96 1.1 pgoyette static void
97 1.1 pgoyette sigalrm_action(int signo, siginfo_t *info, void *ptr)
98 1.1 pgoyette {
99 1.1 pgoyette
100 1.1 pgoyette sig_debug(signo, info, (ucontext_t *)ptr);
101 1.1 pgoyette
102 1.1 pgoyette ATF_REQUIRE_EQ(info->si_signo, SIGALRM);
103 1.1 pgoyette ATF_REQUIRE_EQ(info->si_code, SI_TIMER);
104 1.1 pgoyette ATF_REQUIRE_EQ(info->si_value.sival_int, ITIMER_REAL);
105 1.1 pgoyette
106 1.1 pgoyette atf_tc_pass();
107 1.1 pgoyette /* NOTREACHED */
108 1.1 pgoyette }
109 1.1 pgoyette
110 1.1 pgoyette ATF_TC(sigalarm);
111 1.1 pgoyette
112 1.1 pgoyette ATF_TC_HEAD(sigalarm, tc)
113 1.14 jruoho {
114 1.1 pgoyette
115 1.1 pgoyette atf_tc_set_md_var(tc, "descr",
116 1.14 jruoho "Checks that signal trampoline correctly calls SIGALRM handler");
117 1.1 pgoyette }
118 1.1 pgoyette
119 1.1 pgoyette ATF_TC_BODY(sigalarm, tc)
120 1.1 pgoyette {
121 1.1 pgoyette struct sigaction sa;
122 1.1 pgoyette sa.sa_flags = SA_SIGINFO;
123 1.1 pgoyette sa.sa_sigaction = sigalrm_action;
124 1.1 pgoyette sigemptyset(&sa.sa_mask);
125 1.1 pgoyette sigaction(SIGALRM, &sa, NULL);
126 1.1 pgoyette for (;;) {
127 1.1 pgoyette alarm(1);
128 1.1 pgoyette sleep(1);
129 1.1 pgoyette }
130 1.14 jruoho atf_tc_fail("SIGALRM handler wasn't called");
131 1.1 pgoyette }
132 1.1 pgoyette
133 1.1 pgoyette static void
134 1.1 pgoyette sigchild_action(int signo, siginfo_t *info, void *ptr)
135 1.1 pgoyette {
136 1.1 pgoyette if (info != NULL) {
137 1.1 pgoyette printf("info=%p\n", info);
138 1.1 pgoyette printf("ptr=%p\n", ptr);
139 1.1 pgoyette printf("si_signo=%d\n", info->si_signo);
140 1.1 pgoyette printf("si_errno=%d\n", info->si_errno);
141 1.1 pgoyette printf("si_code=%d\n", info->si_code);
142 1.1 pgoyette printf("si_uid=%d\n", info->si_uid);
143 1.1 pgoyette printf("si_pid=%d\n", info->si_pid);
144 1.1 pgoyette printf("si_status=%d\n", info->si_status);
145 1.3 pgoyette printf("si_utime=%lu\n", (unsigned long int)info->si_utime);
146 1.3 pgoyette printf("si_stime=%lu\n", (unsigned long int)info->si_stime);
147 1.1 pgoyette }
148 1.1 pgoyette ATF_REQUIRE_EQ(info->si_code, code);
149 1.1 pgoyette ATF_REQUIRE_EQ(info->si_signo, SIGCHLD);
150 1.1 pgoyette ATF_REQUIRE_EQ(info->si_uid, getuid());
151 1.1 pgoyette ATF_REQUIRE_EQ(info->si_pid, child);
152 1.1 pgoyette if (WIFEXITED(info->si_status))
153 1.1 pgoyette ATF_REQUIRE_EQ(WEXITSTATUS(info->si_status), status);
154 1.1 pgoyette else if (WIFSTOPPED(info->si_status))
155 1.1 pgoyette ATF_REQUIRE_EQ(WSTOPSIG(info->si_status), status);
156 1.1 pgoyette else if (WIFSIGNALED(info->si_status))
157 1.1 pgoyette ATF_REQUIRE_EQ(WTERMSIG(info->si_status), status);
158 1.1 pgoyette }
159 1.1 pgoyette
160 1.1 pgoyette static void
161 1.1 pgoyette setchildhandler(void (*action)(int, siginfo_t *, void *))
162 1.1 pgoyette {
163 1.1 pgoyette struct sigaction sa;
164 1.1 pgoyette sa.sa_flags = SA_SIGINFO;
165 1.1 pgoyette sa.sa_sigaction = action;
166 1.1 pgoyette sigemptyset(&sa.sa_mask);
167 1.1 pgoyette sigaction(SIGCHLD, &sa, NULL);
168 1.1 pgoyette }
169 1.1 pgoyette
170 1.1 pgoyette static void
171 1.1 pgoyette sigchild_setup(void)
172 1.1 pgoyette {
173 1.1 pgoyette sigset_t set;
174 1.1 pgoyette struct rlimit rlim;
175 1.1 pgoyette
176 1.1 pgoyette (void)getrlimit(RLIMIT_CORE, &rlim);
177 1.1 pgoyette rlim.rlim_cur = rlim.rlim_max;
178 1.1 pgoyette (void)setrlimit(RLIMIT_CORE, &rlim);
179 1.1 pgoyette
180 1.1 pgoyette setchildhandler(sigchild_action);
181 1.1 pgoyette sigemptyset(&set);
182 1.1 pgoyette sigaddset(&set, SIGCHLD);
183 1.1 pgoyette sigprocmask(SIG_BLOCK, &set, NULL);
184 1.1 pgoyette }
185 1.1 pgoyette
186 1.1 pgoyette ATF_TC(sigchild_normal);
187 1.1 pgoyette ATF_TC_HEAD(sigchild_normal, tc)
188 1.1 pgoyette {
189 1.1 pgoyette
190 1.1 pgoyette atf_tc_set_md_var(tc, "descr",
191 1.1 pgoyette "Checks that signal trampoline correctly calls SIGCHLD handler "
192 1.1 pgoyette "when child exits normally");
193 1.1 pgoyette }
194 1.1 pgoyette
195 1.1 pgoyette ATF_TC_BODY(sigchild_normal, tc)
196 1.1 pgoyette {
197 1.1 pgoyette sigset_t set;
198 1.1 pgoyette
199 1.14 jruoho sigchild_setup();
200 1.1 pgoyette
201 1.1 pgoyette status = 25;
202 1.1 pgoyette code = CLD_EXITED;
203 1.1 pgoyette
204 1.1 pgoyette switch ((child = fork())) {
205 1.1 pgoyette case 0:
206 1.1 pgoyette sleep(1);
207 1.1 pgoyette exit(status);
208 1.1 pgoyette case -1:
209 1.1 pgoyette atf_tc_fail("fork failed");
210 1.1 pgoyette default:
211 1.1 pgoyette sigemptyset(&set);
212 1.1 pgoyette sigsuspend(&set);
213 1.1 pgoyette }
214 1.1 pgoyette }
215 1.1 pgoyette
216 1.1 pgoyette ATF_TC(sigchild_dump);
217 1.1 pgoyette ATF_TC_HEAD(sigchild_dump, tc)
218 1.1 pgoyette {
219 1.1 pgoyette
220 1.1 pgoyette atf_tc_set_md_var(tc, "descr",
221 1.1 pgoyette "Checks that signal trampoline correctly calls SIGCHLD handler "
222 1.1 pgoyette "when child segfaults");
223 1.1 pgoyette }
224 1.1 pgoyette
225 1.1 pgoyette ATF_TC_BODY(sigchild_dump, tc)
226 1.14 jruoho {
227 1.1 pgoyette sigset_t set;
228 1.1 pgoyette
229 1.14 jruoho sigchild_setup();
230 1.1 pgoyette
231 1.1 pgoyette status = SIGSEGV;
232 1.1 pgoyette code = CLD_DUMPED;
233 1.1 pgoyette
234 1.1 pgoyette switch ((child = fork())) {
235 1.1 pgoyette case 0:
236 1.1 pgoyette sleep(1);
237 1.11 joerg *(volatile long *)0 = 0;
238 1.1 pgoyette atf_tc_fail("Child did not segfault");
239 1.1 pgoyette /* NOTREACHED */
240 1.1 pgoyette case -1:
241 1.1 pgoyette atf_tc_fail("fork failed");
242 1.1 pgoyette default:
243 1.1 pgoyette sigemptyset(&set);
244 1.1 pgoyette sigsuspend(&set);
245 1.1 pgoyette }
246 1.1 pgoyette }
247 1.1 pgoyette
248 1.1 pgoyette ATF_TC(sigchild_kill);
249 1.1 pgoyette ATF_TC_HEAD(sigchild_kill, tc)
250 1.1 pgoyette {
251 1.1 pgoyette
252 1.1 pgoyette atf_tc_set_md_var(tc, "descr",
253 1.1 pgoyette "Checks that signal trampoline correctly calls SIGCHLD handler "
254 1.1 pgoyette "when child is killed");
255 1.1 pgoyette }
256 1.1 pgoyette
257 1.1 pgoyette ATF_TC_BODY(sigchild_kill, tc)
258 1.14 jruoho {
259 1.1 pgoyette sigset_t set;
260 1.1 pgoyette
261 1.14 jruoho sigchild_setup();
262 1.1 pgoyette
263 1.1 pgoyette status = SIGPIPE;
264 1.1 pgoyette code = CLD_KILLED;
265 1.1 pgoyette
266 1.1 pgoyette switch ((child = fork())) {
267 1.1 pgoyette case 0:
268 1.1 pgoyette sigemptyset(&set);
269 1.1 pgoyette sigsuspend(&set);
270 1.1 pgoyette break;
271 1.1 pgoyette case -1:
272 1.1 pgoyette atf_tc_fail("fork failed");
273 1.1 pgoyette default:
274 1.1 pgoyette kill(child, SIGPIPE);
275 1.1 pgoyette sigemptyset(&set);
276 1.1 pgoyette sigsuspend(&set);
277 1.1 pgoyette }
278 1.1 pgoyette }
279 1.1 pgoyette
280 1.5 mlelstv static sigjmp_buf sigfpe_flt_env;
281 1.1 pgoyette static void
282 1.1 pgoyette sigfpe_flt_action(int signo, siginfo_t *info, void *ptr)
283 1.1 pgoyette {
284 1.1 pgoyette
285 1.1 pgoyette sig_debug(signo, info, (ucontext_t *)ptr);
286 1.1 pgoyette
287 1.1 pgoyette if (fltdiv_signalled++ != 0)
288 1.1 pgoyette atf_tc_fail("FPE handler called more than once");
289 1.1 pgoyette
290 1.1 pgoyette ATF_REQUIRE_EQ(info->si_signo, SIGFPE);
291 1.1 pgoyette ATF_REQUIRE_EQ(info->si_code, FPE_FLTDIV);
292 1.1 pgoyette ATF_REQUIRE_EQ(info->si_errno, 0);
293 1.5 mlelstv
294 1.5 mlelstv siglongjmp(sigfpe_flt_env, 1);
295 1.1 pgoyette }
296 1.1 pgoyette
297 1.1 pgoyette ATF_TC(sigfpe_flt);
298 1.1 pgoyette ATF_TC_HEAD(sigfpe_flt, tc)
299 1.1 pgoyette {
300 1.1 pgoyette
301 1.1 pgoyette atf_tc_set_md_var(tc, "descr",
302 1.1 pgoyette "Checks that signal trampoline correctly calls SIGFPE handler "
303 1.1 pgoyette "for floating div-by-zero");
304 1.1 pgoyette }
305 1.1 pgoyette
306 1.1 pgoyette ATF_TC_BODY(sigfpe_flt, tc)
307 1.14 jruoho {
308 1.1 pgoyette struct sigaction sa;
309 1.52 riastrad volatile double d = strtod("0", NULL);
310 1.1 pgoyette
311 1.19 christos if (isQEMU())
312 1.20 christos atf_tc_skip("Test does not run correctly under QEMU");
313 1.33 martin #if (__arm__ && !__SOFTFP__) || __aarch64__
314 1.28 martin /*
315 1.36 kamil * Some NEON fpus do not trap on IEEE 754 FP exceptions.
316 1.26 martin * skip these tests if running on them and compiled for
317 1.26 martin * hard float.
318 1.26 martin */
319 1.26 martin if (0 == fpsetmask(fpsetmask(FP_X_INV)))
320 1.35 martin atf_tc_skip("FPU does not implement traps on FP exceptions");
321 1.50 riastrad #elif defined __riscv__
322 1.50 riastrad atf_tc_skip("RISC-V does not support floating-point exception traps");
323 1.23 jmmv #endif
324 1.5 mlelstv if (sigsetjmp(sigfpe_flt_env, 0) == 0) {
325 1.5 mlelstv sa.sa_flags = SA_SIGINFO;
326 1.5 mlelstv sa.sa_sigaction = sigfpe_flt_action;
327 1.5 mlelstv sigemptyset(&sa.sa_mask);
328 1.5 mlelstv sigaction(SIGFPE, &sa, NULL);
329 1.30 christos #ifdef __HAVE_FENV
330 1.22 matt feenableexcept(FE_ALL_EXCEPT);
331 1.22 matt #elif defined(_FLOAT_IEEE754)
332 1.5 mlelstv fpsetmask(FP_X_INV|FP_X_DZ|FP_X_OFL|FP_X_UFL|FP_X_IMP);
333 1.1 pgoyette #endif
334 1.5 mlelstv printf("%g\n", 1 / d);
335 1.5 mlelstv }
336 1.1 pgoyette if (fltdiv_signalled == 0)
337 1.1 pgoyette atf_tc_fail("FPE signal handler was not invoked");
338 1.1 pgoyette }
339 1.1 pgoyette
340 1.5 mlelstv static sigjmp_buf sigfpe_int_env;
341 1.1 pgoyette static void
342 1.1 pgoyette sigfpe_int_action(int signo, siginfo_t *info, void *ptr)
343 1.1 pgoyette {
344 1.1 pgoyette
345 1.1 pgoyette sig_debug(signo, info, (ucontext_t *)ptr);
346 1.1 pgoyette
347 1.1 pgoyette if (intdiv_signalled++ != 0)
348 1.1 pgoyette atf_tc_fail("INTDIV handler called more than once");
349 1.1 pgoyette
350 1.1 pgoyette ATF_REQUIRE_EQ(info->si_signo, SIGFPE);
351 1.1 pgoyette ATF_REQUIRE_EQ(info->si_code, FPE_INTDIV);
352 1.7 pgoyette atf_tc_expect_pass();
353 1.1 pgoyette ATF_REQUIRE_EQ(info->si_errno, 0);
354 1.5 mlelstv
355 1.5 mlelstv siglongjmp(sigfpe_int_env, 1);
356 1.1 pgoyette }
357 1.1 pgoyette
358 1.1 pgoyette ATF_TC(sigfpe_int);
359 1.1 pgoyette ATF_TC_HEAD(sigfpe_int, tc)
360 1.1 pgoyette {
361 1.1 pgoyette
362 1.1 pgoyette atf_tc_set_md_var(tc, "descr",
363 1.1 pgoyette "Checks that signal trampoline correctly calls SIGFPE handler "
364 1.14 jruoho "for integer div-by-zero (PR port-i386/43655)");
365 1.1 pgoyette }
366 1.1 pgoyette
367 1.1 pgoyette ATF_TC_BODY(sigfpe_int, tc)
368 1.14 jruoho {
369 1.1 pgoyette struct sigaction sa;
370 1.1 pgoyette
371 1.50 riastrad #if defined(__aarch64__) || defined(__powerpc__) || defined(__sh3__) || \
372 1.50 riastrad defined(__riscv__)
373 1.37 riastrad atf_tc_skip("Integer division by zero doesn't trap");
374 1.23 jmmv #endif
375 1.5 mlelstv if (sigsetjmp(sigfpe_int_env, 0) == 0) {
376 1.5 mlelstv sa.sa_flags = SA_SIGINFO;
377 1.5 mlelstv sa.sa_sigaction = sigfpe_int_action;
378 1.5 mlelstv sigemptyset(&sa.sa_mask);
379 1.5 mlelstv sigaction(SIGFPE, &sa, NULL);
380 1.30 christos #ifdef __HAVE_FENV
381 1.22 matt feenableexcept(FE_ALL_EXCEPT);
382 1.22 matt #elif defined(_FLOAT_IEEE754)
383 1.5 mlelstv fpsetmask(FP_X_INV|FP_X_DZ|FP_X_OFL|FP_X_UFL|FP_X_IMP);
384 1.1 pgoyette #endif
385 1.49 rin /*
386 1.49 rin * Do not use constant 1 here. GCC >= 12 optimizes
387 1.49 rin * (1 / i) to (abs(i) == 1 ? i : 0), even for -O0.
388 1.49 rin */
389 1.51 riastrad volatile long unity = strtol("1", NULL, 10),
390 1.49 rin zero = strtol("0", NULL, 10);
391 1.49 rin printf("%ld\n", unity / zero);
392 1.5 mlelstv }
393 1.1 pgoyette if (intdiv_signalled == 0)
394 1.1 pgoyette atf_tc_fail("FPE signal handler was not invoked");
395 1.1 pgoyette }
396 1.1 pgoyette
397 1.1 pgoyette static void
398 1.1 pgoyette sigsegv_action(int signo, siginfo_t *info, void *ptr)
399 1.1 pgoyette {
400 1.1 pgoyette
401 1.1 pgoyette sig_debug(signo, info, (ucontext_t *)ptr);
402 1.1 pgoyette
403 1.1 pgoyette ATF_REQUIRE_EQ(info->si_signo, SIGSEGV);
404 1.1 pgoyette ATF_REQUIRE_EQ(info->si_errno, 0);
405 1.1 pgoyette ATF_REQUIRE_EQ(info->si_code, SEGV_MAPERR);
406 1.1 pgoyette ATF_REQUIRE_EQ(info->si_addr, (void *)0);
407 1.1 pgoyette
408 1.1 pgoyette atf_tc_pass();
409 1.1 pgoyette /* NOTREACHED */
410 1.1 pgoyette }
411 1.1 pgoyette
412 1.1 pgoyette ATF_TC(sigsegv);
413 1.1 pgoyette ATF_TC_HEAD(sigsegv, tc)
414 1.1 pgoyette {
415 1.1 pgoyette
416 1.1 pgoyette atf_tc_set_md_var(tc, "descr",
417 1.1 pgoyette "Checks that signal trampoline correctly calls SIGSEGV handler");
418 1.1 pgoyette }
419 1.1 pgoyette
420 1.1 pgoyette ATF_TC_BODY(sigsegv, tc)
421 1.1 pgoyette {
422 1.1 pgoyette struct sigaction sa;
423 1.1 pgoyette
424 1.1 pgoyette sa.sa_flags = SA_SIGINFO;
425 1.1 pgoyette sa.sa_sigaction = sigsegv_action;
426 1.1 pgoyette sigemptyset(&sa.sa_mask);
427 1.1 pgoyette sigaction(SIGSEGV, &sa, NULL);
428 1.1 pgoyette
429 1.11 joerg *(volatile long *)0 = 0;
430 1.1 pgoyette atf_tc_fail("Test did not fault as expected");
431 1.1 pgoyette }
432 1.1 pgoyette
433 1.15 jym static void
434 1.15 jym sigbus_action(int signo, siginfo_t *info, void *ptr)
435 1.15 jym {
436 1.15 jym
437 1.17 martin printf("si_addr = %p\n", info->si_addr);
438 1.15 jym sig_debug(signo, info, (ucontext_t *)ptr);
439 1.15 jym
440 1.15 jym ATF_REQUIRE_EQ(info->si_signo, SIGBUS);
441 1.15 jym ATF_REQUIRE_EQ(info->si_errno, 0);
442 1.15 jym ATF_REQUIRE_EQ(info->si_code, BUS_ADRALN);
443 1.15 jym
444 1.23 jmmv #if defined(__i386__) || defined(__x86_64__)
445 1.23 jmmv atf_tc_expect_fail("x86 architecture does not correctly "
446 1.46 andvar "report the address where the unaligned access occurred");
447 1.23 jmmv #endif
448 1.17 martin ATF_REQUIRE_EQ(info->si_addr, (volatile void *)addr);
449 1.16 martin
450 1.15 jym atf_tc_pass();
451 1.15 jym /* NOTREACHED */
452 1.15 jym }
453 1.15 jym
454 1.15 jym ATF_TC(sigbus_adraln);
455 1.15 jym ATF_TC_HEAD(sigbus_adraln, tc)
456 1.15 jym {
457 1.15 jym
458 1.15 jym atf_tc_set_md_var(tc, "descr",
459 1.15 jym "Checks that signal trampoline correctly calls SIGBUS handler "
460 1.15 jym "for invalid address alignment");
461 1.15 jym }
462 1.15 jym
463 1.15 jym ATF_TC_BODY(sigbus_adraln, tc)
464 1.15 jym {
465 1.15 jym struct sigaction sa;
466 1.15 jym
467 1.25 martin #if defined(__alpha__) || defined(__arm__)
468 1.23 jmmv int rv, val;
469 1.23 jmmv size_t len = sizeof(val);
470 1.23 jmmv rv = sysctlbyname("machdep.unaligned_sigbus", &val, &len, NULL, 0);
471 1.23 jmmv ATF_REQUIRE(rv == 0);
472 1.23 jmmv if (val == 0)
473 1.25 martin atf_tc_skip("No SIGBUS signal for unaligned accesses");
474 1.23 jmmv #endif
475 1.18 njoly
476 1.40 rin #ifdef __powerpc__
477 1.40 rin /*
478 1.40 rin * SIGBUS is not mandatory for powerpc; most processors (not all)
479 1.40 rin * can deal with unaligned accesses.
480 1.40 rin */
481 1.40 rin atf_tc_skip("SIGBUS signal for unaligned accesses is "
482 1.40 rin "not mandatory for this architecture");
483 1.40 rin #endif
484 1.40 rin
485 1.34 martin /* m68k (except sun2) never issue SIGBUS (PR lib/49653),
486 1.34 martin * same for armv8 or newer */
487 1.42 rin #if (defined(__m68k__) && !defined(__mc68010__)) || \
488 1.48 skrll defined(__aarch64__) || \
489 1.53 rin defined(__riscv__) || \
490 1.53 rin defined(__vax__)
491 1.42 rin atf_tc_skip("No SIGBUS signal for unaligned accesses");
492 1.42 rin #endif
493 1.29 isaki
494 1.43 skrll #if defined(__mips__)
495 1.44 skrll /* no way of detecting if on GXemul, so disable everywhere for now */
496 1.45 skrll atf_tc_skip("GXemul fails to trap unaligned accesses with "
497 1.44 skrll "correct ENTRYHI");
498 1.43 skrll #endif
499 1.43 skrll
500 1.15 jym sa.sa_flags = SA_SIGINFO;
501 1.15 jym sa.sa_sigaction = sigbus_action;
502 1.15 jym sigemptyset(&sa.sa_mask);
503 1.15 jym sigaction(SIGBUS, &sa, NULL);
504 1.15 jym
505 1.21 skrll /* Enable alignment checks for x86. 0x40000 is PSL_AC. */
506 1.15 jym #if defined(__i386__)
507 1.15 jym __asm__("pushf; orl $0x40000, (%esp); popf");
508 1.15 jym #elif defined(__amd64__)
509 1.15 jym __asm__("pushf; orl $0x40000, (%rsp); popf");
510 1.15 jym #endif
511 1.15 jym
512 1.15 jym addr = calloc(2, sizeof(int));
513 1.15 jym ATF_REQUIRE(addr != NULL);
514 1.15 jym
515 1.41 gson if (isQEMU_TCG())
516 1.19 christos atf_tc_expect_fail("QEMU fails to trap unaligned accesses");
517 1.15 jym
518 1.15 jym /* Force an unaligned access */
519 1.15 jym addr++;
520 1.17 martin printf("now trying to access unaligned address %p\n", addr);
521 1.15 jym ATF_REQUIRE_EQ(*(volatile int *)addr, 0);
522 1.15 jym
523 1.15 jym atf_tc_fail("Test did not fault as expected");
524 1.15 jym }
525 1.15 jym
526 1.39 kamil #else
527 1.39 kamil ATF_TC(dummy);
528 1.39 kamil ATF_TC_HEAD(dummy, tc)
529 1.39 kamil {
530 1.39 kamil atf_tc_set_md_var(tc, "descr", "A dummy test");
531 1.39 kamil }
532 1.39 kamil
533 1.39 kamil ATF_TC_BODY(dummy, tc)
534 1.39 kamil {
535 1.39 kamil
536 1.39 kamil // Dummy, skipped
537 1.39 kamil // The ATF framework requires at least a single defined test.
538 1.39 kamil }
539 1.39 kamil #endif
540 1.39 kamil
541 1.1 pgoyette ATF_TP_ADD_TCS(tp)
542 1.1 pgoyette {
543 1.1 pgoyette
544 1.39 kamil #ifdef ENABLE_TESTS
545 1.1 pgoyette ATF_TP_ADD_TC(tp, sigalarm);
546 1.1 pgoyette ATF_TP_ADD_TC(tp, sigchild_normal);
547 1.1 pgoyette ATF_TP_ADD_TC(tp, sigchild_dump);
548 1.1 pgoyette ATF_TP_ADD_TC(tp, sigchild_kill);
549 1.1 pgoyette ATF_TP_ADD_TC(tp, sigfpe_flt);
550 1.1 pgoyette ATF_TP_ADD_TC(tp, sigfpe_int);
551 1.1 pgoyette ATF_TP_ADD_TC(tp, sigsegv);
552 1.15 jym ATF_TP_ADD_TC(tp, sigbus_adraln);
553 1.39 kamil #else
554 1.39 kamil ATF_TP_ADD_TC(tp, dummy);
555 1.39 kamil #endif
556 1.1 pgoyette
557 1.1 pgoyette return atf_no_error();
558 1.1 pgoyette }
559