1 /* $NetBSD: t_siginfo.c,v 1.30 2015/12/22 14:25:58 christos Exp $ */
4 * Copyright (c) 2010 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
32 #include <sys/inttypes.h>
34 #include <sys/resource.h>
35 #include <sys/sysctl.h>
37 #include <sys/ucontext.h>
51 #include <ieeefp.h> /* only need for ARM Cortex/Neon hack */
52 #elif defined(_FLOAT_IEEE754)
67 sig_atomic_t fltdiv_signalled = 0;
68 sig_atomic_t intdiv_signalled = 0;
71 sig_debug(int signo, siginfo_t *info, ucontext_t *ctx)
75 printf("%d %p %p\n", signo, info, ctx);
77 printf("si_signo=%d\n", info->si_signo);
78 printf("si_errno=%d\n", info->si_errno);
79 printf("si_code=%d\n", info->si_code);
80 printf("si_value.sival_int=%d\n", info->si_value.sival_int);
83 printf("uc_flags 0x%x\n", ctx->uc_flags);
84 printf("uc_link %p\n", ctx->uc_link);
85 for (i = 0; i < __arraycount(ctx->uc_sigmask.__bits); i++)
86 printf("uc_sigmask[%d] 0x%x\n", i,
87 ctx->uc_sigmask.__bits[i]);
88 printf("uc_stack %p %lu 0x%x\n", ctx->uc_stack.ss_sp,
89 (unsigned long)ctx->uc_stack.ss_size,
90 ctx->uc_stack.ss_flags);
92 for (i = 0; i < __arraycount(ctx->uc_mcontext.__gregs); i++)
93 printf("uc_mcontext.greg[%d] 0x%lx\n", i,
94 (long)ctx->uc_mcontext.__gregs[i]);
100 sigalrm_action(int signo, siginfo_t *info, void *ptr)
103 sig_debug(signo, info, (ucontext_t *)ptr);
105 ATF_REQUIRE_EQ(info->si_signo, SIGALRM);
106 ATF_REQUIRE_EQ(info->si_code, SI_TIMER);
107 ATF_REQUIRE_EQ(info->si_value.sival_int, ITIMER_REAL);
115 ATF_TC_HEAD(sigalarm, tc)
118 atf_tc_set_md_var(tc, "descr",
119 "Checks that signal trampoline correctly calls SIGALRM handler");
122 ATF_TC_BODY(sigalarm, tc)
125 sa.sa_flags = SA_SIGINFO;
126 sa.sa_sigaction = sigalrm_action;
127 sigemptyset(&sa.sa_mask);
128 sigaction(SIGALRM, &sa, NULL);
133 atf_tc_fail("SIGALRM handler wasn't called");
137 sigchild_action(int signo, siginfo_t *info, void *ptr)
140 printf("info=%p\n", info);
141 printf("ptr=%p\n", ptr);
142 printf("si_signo=%d\n", info->si_signo);
143 printf("si_errno=%d\n", info->si_errno);
144 printf("si_code=%d\n", info->si_code);
145 printf("si_uid=%d\n", info->si_uid);
146 printf("si_pid=%d\n", info->si_pid);
147 printf("si_status=%d\n", info->si_status);
149 printf("si_utime=%lu\n", (unsigned long int)info->si_utime);
150 printf("si_stime=%lu\n", (unsigned long int)info->si_stime);
153 ATF_REQUIRE_EQ(info->si_code, code);
154 ATF_REQUIRE_EQ(info->si_signo, SIGCHLD);
155 ATF_REQUIRE_EQ(info->si_uid, getuid());
156 ATF_REQUIRE_EQ(info->si_pid, child);
157 if (WIFEXITED(info->si_status))
158 ATF_REQUIRE_EQ(WEXITSTATUS(info->si_status), status);
159 else if (WIFSTOPPED(info->si_status))
160 ATF_REQUIRE_EQ(WSTOPSIG(info->si_status), status);
161 else if (WIFSIGNALED(info->si_status))
162 ATF_REQUIRE_EQ(WTERMSIG(info->si_status), status);
166 setchildhandler(void (*action)(int, siginfo_t *, void *))
169 sa.sa_flags = SA_SIGINFO;
170 sa.sa_sigaction = action;
171 sigemptyset(&sa.sa_mask);
172 sigaction(SIGCHLD, &sa, NULL);
181 (void)getrlimit(RLIMIT_CORE, &rlim);
182 rlim.rlim_cur = rlim.rlim_max;
183 (void)setrlimit(RLIMIT_CORE, &rlim);
185 setchildhandler(sigchild_action);
187 sigaddset(&set, SIGCHLD);
188 sigprocmask(SIG_BLOCK, &set, NULL);
191 ATF_TC(sigchild_normal);
192 ATF_TC_HEAD(sigchild_normal, tc)
195 atf_tc_set_md_var(tc, "descr",
196 "Checks that signal trampoline correctly calls SIGCHLD handler "
197 "when child exits normally");
200 ATF_TC_BODY(sigchild_normal, tc)
209 switch ((child = fork())) {
214 atf_tc_fail("fork failed");
221 ATF_TC(sigchild_dump);
222 ATF_TC_HEAD(sigchild_dump, tc)
225 atf_tc_set_md_var(tc, "descr",
226 "Checks that signal trampoline correctly calls SIGCHLD handler "
227 "when child segfaults");
230 ATF_TC_BODY(sigchild_dump, tc)
239 switch ((child = fork())) {
242 *(volatile long *)0 = 0;
243 atf_tc_fail("Child did not segfault");
246 atf_tc_fail("fork failed");
253 ATF_TC(sigchild_kill);
254 ATF_TC_HEAD(sigchild_kill, tc)
257 atf_tc_set_md_var(tc, "descr",
258 "Checks that signal trampoline correctly calls SIGCHLD handler "
259 "when child is killed");
262 ATF_TC_BODY(sigchild_kill, tc)
271 switch ((child = fork())) {
277 atf_tc_fail("fork failed");
279 kill(child, SIGPIPE);
285 static sigjmp_buf sigfpe_flt_env;
287 sigfpe_flt_action(int signo, siginfo_t *info, void *ptr)
290 sig_debug(signo, info, (ucontext_t *)ptr);
292 if (fltdiv_signalled++ != 0)
293 atf_tc_fail("FPE handler called more than once");
295 ATF_REQUIRE_EQ(info->si_signo, SIGFPE);
296 ATF_REQUIRE_EQ(info->si_code, FPE_FLTDIV);
297 ATF_REQUIRE_EQ(info->si_errno, 0);
299 siglongjmp(sigfpe_flt_env, 1);
303 ATF_TC_HEAD(sigfpe_flt, tc)
306 atf_tc_set_md_var(tc, "descr",
307 "Checks that signal trampoline correctly calls SIGFPE handler "
308 "for floating div-by-zero");
311 ATF_TC_BODY(sigfpe_flt, tc)
314 double d = strtod("0", NULL);
317 atf_tc_skip("Test does not run correctly under QEMU");
318 #if defined(__powerpc__)
319 atf_tc_skip("Test not valid on powerpc");
320 #elif defined(__arm__) && !__SOFTFP__
322 * Some NEON fpus do not implement IEEE exception handling,
323 * skip these tests if running on them and compiled for
326 if (0 == fpsetmask(fpsetmask(FP_X_INV)))
327 atf_tc_skip("FPU does not implement exception handling");
329 if (sigsetjmp(sigfpe_flt_env, 0) == 0) {
330 sa.sa_flags = SA_SIGINFO;
331 sa.sa_sigaction = sigfpe_flt_action;
332 sigemptyset(&sa.sa_mask);
333 sigaction(SIGFPE, &sa, NULL);
335 feenableexcept(FE_ALL_EXCEPT);
336 #elif defined(_FLOAT_IEEE754)
337 fpsetmask(FP_X_INV|FP_X_DZ|FP_X_OFL|FP_X_UFL|FP_X_IMP);
339 printf("%g\n", 1 / d);
341 if (fltdiv_signalled == 0)
342 atf_tc_fail("FPE signal handler was not invoked");
345 static sigjmp_buf sigfpe_int_env;
347 sigfpe_int_action(int signo, siginfo_t *info, void *ptr)
350 sig_debug(signo, info, (ucontext_t *)ptr);
352 if (intdiv_signalled++ != 0)
353 atf_tc_fail("INTDIV handler called more than once");
355 ATF_REQUIRE_EQ(info->si_signo, SIGFPE);
356 ATF_REQUIRE_EQ(info->si_code, FPE_INTDIV);
357 atf_tc_expect_pass();
358 ATF_REQUIRE_EQ(info->si_errno, 0);
360 siglongjmp(sigfpe_int_env, 1);
364 ATF_TC_HEAD(sigfpe_int, tc)
367 atf_tc_set_md_var(tc, "descr",
368 "Checks that signal trampoline correctly calls SIGFPE handler "
369 "for integer div-by-zero (PR port-i386/43655)");
372 ATF_TC_BODY(sigfpe_int, tc)
375 long l = strtol("0", NULL, 10);
377 #if defined(__powerpc__)
378 atf_tc_skip("Test not valid on powerpc");
380 if (sigsetjmp(sigfpe_int_env, 0) == 0) {
381 sa.sa_flags = SA_SIGINFO;
382 sa.sa_sigaction = sigfpe_int_action;
383 sigemptyset(&sa.sa_mask);
384 sigaction(SIGFPE, &sa, NULL);
386 feenableexcept(FE_ALL_EXCEPT);
387 #elif defined(_FLOAT_IEEE754)
388 fpsetmask(FP_X_INV|FP_X_DZ|FP_X_OFL|FP_X_UFL|FP_X_IMP);
390 printf("%ld\n", 1 / l);
392 if (intdiv_signalled == 0)
393 atf_tc_fail("FPE signal handler was not invoked");
397 sigsegv_action(int signo, siginfo_t *info, void *ptr)
400 sig_debug(signo, info, (ucontext_t *)ptr);
402 ATF_REQUIRE_EQ(info->si_signo, SIGSEGV);
403 ATF_REQUIRE_EQ(info->si_errno, 0);
404 ATF_REQUIRE_EQ(info->si_code, SEGV_MAPERR);
405 ATF_REQUIRE_EQ(info->si_addr, (void *)0);
412 ATF_TC_HEAD(sigsegv, tc)
415 atf_tc_set_md_var(tc, "descr",
416 "Checks that signal trampoline correctly calls SIGSEGV handler");
419 ATF_TC_BODY(sigsegv, tc)
423 sa.sa_flags = SA_SIGINFO;
424 sa.sa_sigaction = sigsegv_action;
425 sigemptyset(&sa.sa_mask);
426 sigaction(SIGSEGV, &sa, NULL);
428 *(volatile long *)0 = 0;
429 atf_tc_fail("Test did not fault as expected");
433 sigbus_action(int signo, siginfo_t *info, void *ptr)
436 printf("si_addr = %p\n", info->si_addr);
437 sig_debug(signo, info, (ucontext_t *)ptr);
439 ATF_REQUIRE_EQ(info->si_signo, SIGBUS);
440 ATF_REQUIRE_EQ(info->si_errno, 0);
441 ATF_REQUIRE_EQ(info->si_code, BUS_ADRALN);
443 #if defined(__i386__) || defined(__x86_64__)
444 atf_tc_expect_fail("x86 architecture does not correctly "
445 "report the address where the unaligned access occured");
447 ATF_REQUIRE_EQ(info->si_addr, (volatile void *)addr);
453 ATF_TC(sigbus_adraln);
454 ATF_TC_HEAD(sigbus_adraln, tc)
457 atf_tc_set_md_var(tc, "descr",
458 "Checks that signal trampoline correctly calls SIGBUS handler "
459 "for invalid address alignment");
462 ATF_TC_BODY(sigbus_adraln, tc)
466 #if defined(__alpha__) || defined(__arm__)
468 size_t len = sizeof(val);
469 rv = sysctlbyname("machdep.unaligned_sigbus", &val, &len, NULL, 0);
470 ATF_REQUIRE(rv == 0);
472 atf_tc_skip("No SIGBUS signal for unaligned accesses");
475 /* m68k (except sun2) never issue SIGBUS (PR lib/49653) */
476 if (strcmp(MACHINE_ARCH, "m68k") == 0)
477 atf_tc_skip("No SIGBUS signal for unaligned accesses");
479 sa.sa_flags = SA_SIGINFO;
480 sa.sa_sigaction = sigbus_action;
481 sigemptyset(&sa.sa_mask);
482 sigaction(SIGBUS, &sa, NULL);
484 /* Enable alignment checks for x86. 0x40000 is PSL_AC. */
485 #if defined(__i386__)
486 __asm__("pushf; orl $0x40000, (%esp); popf");
487 #elif defined(__amd64__)
488 __asm__("pushf; orl $0x40000, (%rsp); popf");
491 addr = calloc(2, sizeof(int));
492 ATF_REQUIRE(addr != NULL);
495 atf_tc_expect_fail("QEMU fails to trap unaligned accesses");
497 /* Force an unaligned access */
499 printf("now trying to access unaligned address %p\n", addr);
500 ATF_REQUIRE_EQ(*(volatile int *)addr, 0);
502 atf_tc_fail("Test did not fault as expected");
508 ATF_TP_ADD_TC(tp, sigalarm);
509 ATF_TP_ADD_TC(tp, sigchild_normal);
510 ATF_TP_ADD_TC(tp, sigchild_dump);
511 ATF_TP_ADD_TC(tp, sigchild_kill);
512 ATF_TP_ADD_TC(tp, sigfpe_flt);
513 ATF_TP_ADD_TC(tp, sigfpe_int);
514 ATF_TP_ADD_TC(tp, sigsegv);
515 ATF_TP_ADD_TC(tp, sigbus_adraln);
517 return atf_no_error();