1 /* $NetBSD: t_siginfo.c,v 1.29 2015/02/17 09:47:08 isaki 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.
31 #include <sys/inttypes.h>
32 #include <sys/resource.h>
33 #include <sys/sysctl.h>
35 #include <sys/ucontext.h>
49 #include <ieeefp.h> /* only need for ARM Cortex/Neon hack */
50 #elif defined(_FLOAT_IEEE754)
65 sig_atomic_t fltdiv_signalled
= 0;
66 sig_atomic_t intdiv_signalled
= 0;
69 sig_debug(int signo
, siginfo_t
*info
, ucontext_t
*ctx
)
73 printf("%d %p %p\n", signo
, info
, ctx
);
75 printf("si_signo=%d\n", info
->si_signo
);
76 printf("si_errno=%d\n", info
->si_errno
);
77 printf("si_code=%d\n", info
->si_code
);
78 printf("si_value.sival_int=%d\n", info
->si_value
.sival_int
);
81 printf("uc_flags 0x%x\n", ctx
->uc_flags
);
82 printf("uc_link %p\n", ctx
->uc_link
);
83 for (i
= 0; i
< __arraycount(ctx
->uc_sigmask
.__bits
); i
++)
84 printf("uc_sigmask[%d] 0x%x\n", i
,
85 ctx
->uc_sigmask
.__bits
[i
]);
86 printf("uc_stack %p %lu 0x%x\n", ctx
->uc_stack
.ss_sp
,
87 (unsigned long)ctx
->uc_stack
.ss_size
,
88 ctx
->uc_stack
.ss_flags
);
89 for (i
= 0; i
< __arraycount(ctx
->uc_mcontext
.__gregs
); i
++)
90 printf("uc_mcontext.greg[%d] 0x%lx\n", i
,
91 (long)ctx
->uc_mcontext
.__gregs
[i
]);
96 sigalrm_action(int signo
, siginfo_t
*info
, void *ptr
)
99 sig_debug(signo
, info
, (ucontext_t
*)ptr
);
101 ATF_REQUIRE_EQ(info
->si_signo
, SIGALRM
);
102 ATF_REQUIRE_EQ(info
->si_code
, SI_TIMER
);
103 ATF_REQUIRE_EQ(info
->si_value
.sival_int
, ITIMER_REAL
);
111 ATF_TC_HEAD(sigalarm
, tc
)
114 atf_tc_set_md_var(tc
, "descr",
115 "Checks that signal trampoline correctly calls SIGALRM handler");
118 ATF_TC_BODY(sigalarm
, tc
)
121 sa
.sa_flags
= SA_SIGINFO
;
122 sa
.sa_sigaction
= sigalrm_action
;
123 sigemptyset(&sa
.sa_mask
);
124 sigaction(SIGALRM
, &sa
, NULL
);
129 atf_tc_fail("SIGALRM handler wasn't called");
133 sigchild_action(int signo
, siginfo_t
*info
, void *ptr
)
136 printf("info=%p\n", info
);
137 printf("ptr=%p\n", ptr
);
138 printf("si_signo=%d\n", info
->si_signo
);
139 printf("si_errno=%d\n", info
->si_errno
);
140 printf("si_code=%d\n", info
->si_code
);
141 printf("si_uid=%d\n", info
->si_uid
);
142 printf("si_pid=%d\n", info
->si_pid
);
143 printf("si_status=%d\n", info
->si_status
);
144 printf("si_utime=%lu\n", (unsigned long int)info
->si_utime
);
145 printf("si_stime=%lu\n", (unsigned long int)info
->si_stime
);
147 ATF_REQUIRE_EQ(info
->si_code
, code
);
148 ATF_REQUIRE_EQ(info
->si_signo
, SIGCHLD
);
149 ATF_REQUIRE_EQ(info
->si_uid
, getuid());
150 ATF_REQUIRE_EQ(info
->si_pid
, child
);
151 if (WIFEXITED(info
->si_status
))
152 ATF_REQUIRE_EQ(WEXITSTATUS(info
->si_status
), status
);
153 else if (WIFSTOPPED(info
->si_status
))
154 ATF_REQUIRE_EQ(WSTOPSIG(info
->si_status
), status
);
155 else if (WIFSIGNALED(info
->si_status
))
156 ATF_REQUIRE_EQ(WTERMSIG(info
->si_status
), status
);
160 setchildhandler(void (*action
)(int, siginfo_t
*, void *))
163 sa
.sa_flags
= SA_SIGINFO
;
164 sa
.sa_sigaction
= action
;
165 sigemptyset(&sa
.sa_mask
);
166 sigaction(SIGCHLD
, &sa
, NULL
);
175 (void)getrlimit(RLIMIT_CORE
, &rlim
);
176 rlim
.rlim_cur
= rlim
.rlim_max
;
177 (void)setrlimit(RLIMIT_CORE
, &rlim
);
179 setchildhandler(sigchild_action
);
181 sigaddset(&set
, SIGCHLD
);
182 sigprocmask(SIG_BLOCK
, &set
, NULL
);
185 ATF_TC(sigchild_normal
);
186 ATF_TC_HEAD(sigchild_normal
, tc
)
189 atf_tc_set_md_var(tc
, "descr",
190 "Checks that signal trampoline correctly calls SIGCHLD handler "
191 "when child exits normally");
194 ATF_TC_BODY(sigchild_normal
, tc
)
203 switch ((child
= fork())) {
208 atf_tc_fail("fork failed");
215 ATF_TC(sigchild_dump
);
216 ATF_TC_HEAD(sigchild_dump
, tc
)
219 atf_tc_set_md_var(tc
, "descr",
220 "Checks that signal trampoline correctly calls SIGCHLD handler "
221 "when child segfaults");
224 ATF_TC_BODY(sigchild_dump
, tc
)
233 switch ((child
= fork())) {
236 *(volatile long *)0 = 0;
237 atf_tc_fail("Child did not segfault");
240 atf_tc_fail("fork failed");
247 ATF_TC(sigchild_kill
);
248 ATF_TC_HEAD(sigchild_kill
, tc
)
251 atf_tc_set_md_var(tc
, "descr",
252 "Checks that signal trampoline correctly calls SIGCHLD handler "
253 "when child is killed");
256 ATF_TC_BODY(sigchild_kill
, tc
)
265 switch ((child
= fork())) {
271 atf_tc_fail("fork failed");
273 kill(child
, SIGPIPE
);
279 static sigjmp_buf sigfpe_flt_env
;
281 sigfpe_flt_action(int signo
, siginfo_t
*info
, void *ptr
)
284 sig_debug(signo
, info
, (ucontext_t
*)ptr
);
286 if (fltdiv_signalled
++ != 0)
287 atf_tc_fail("FPE handler called more than once");
289 ATF_REQUIRE_EQ(info
->si_signo
, SIGFPE
);
290 ATF_REQUIRE_EQ(info
->si_code
, FPE_FLTDIV
);
291 ATF_REQUIRE_EQ(info
->si_errno
, 0);
293 siglongjmp(sigfpe_flt_env
, 1);
297 ATF_TC_HEAD(sigfpe_flt
, tc
)
300 atf_tc_set_md_var(tc
, "descr",
301 "Checks that signal trampoline correctly calls SIGFPE handler "
302 "for floating div-by-zero");
305 ATF_TC_BODY(sigfpe_flt
, tc
)
308 double d
= strtod("0", NULL
);
311 atf_tc_skip("Test does not run correctly under QEMU");
312 #if defined(__powerpc__)
313 atf_tc_skip("Test not valid on powerpc");
314 #elif defined(__arm__) && !__SOFTFP__
316 * Some NEON fpus do not implement IEEE exception handling,
317 * skip these tests if running on them and compiled for
320 if (0 == fpsetmask(fpsetmask(FP_X_INV
)))
321 atf_tc_skip("FPU does not implement exception handling");
323 if (sigsetjmp(sigfpe_flt_env
, 0) == 0) {
324 sa
.sa_flags
= SA_SIGINFO
;
325 sa
.sa_sigaction
= sigfpe_flt_action
;
326 sigemptyset(&sa
.sa_mask
);
327 sigaction(SIGFPE
, &sa
, NULL
);
329 feenableexcept(FE_ALL_EXCEPT
);
330 #elif defined(_FLOAT_IEEE754)
331 fpsetmask(FP_X_INV
|FP_X_DZ
|FP_X_OFL
|FP_X_UFL
|FP_X_IMP
);
333 printf("%g\n", 1 / d
);
335 if (fltdiv_signalled
== 0)
336 atf_tc_fail("FPE signal handler was not invoked");
339 static sigjmp_buf sigfpe_int_env
;
341 sigfpe_int_action(int signo
, siginfo_t
*info
, void *ptr
)
344 sig_debug(signo
, info
, (ucontext_t
*)ptr
);
346 if (intdiv_signalled
++ != 0)
347 atf_tc_fail("INTDIV handler called more than once");
349 ATF_REQUIRE_EQ(info
->si_signo
, SIGFPE
);
350 ATF_REQUIRE_EQ(info
->si_code
, FPE_INTDIV
);
351 atf_tc_expect_pass();
352 ATF_REQUIRE_EQ(info
->si_errno
, 0);
354 siglongjmp(sigfpe_int_env
, 1);
358 ATF_TC_HEAD(sigfpe_int
, tc
)
361 atf_tc_set_md_var(tc
, "descr",
362 "Checks that signal trampoline correctly calls SIGFPE handler "
363 "for integer div-by-zero (PR port-i386/43655)");
366 ATF_TC_BODY(sigfpe_int
, tc
)
369 long l
= strtol("0", NULL
, 10);
371 #if defined(__powerpc__)
372 atf_tc_skip("Test not valid on powerpc");
374 if (sigsetjmp(sigfpe_int_env
, 0) == 0) {
375 sa
.sa_flags
= SA_SIGINFO
;
376 sa
.sa_sigaction
= sigfpe_int_action
;
377 sigemptyset(&sa
.sa_mask
);
378 sigaction(SIGFPE
, &sa
, NULL
);
380 feenableexcept(FE_ALL_EXCEPT
);
381 #elif defined(_FLOAT_IEEE754)
382 fpsetmask(FP_X_INV
|FP_X_DZ
|FP_X_OFL
|FP_X_UFL
|FP_X_IMP
);
384 printf("%ld\n", 1 / l
);
386 if (intdiv_signalled
== 0)
387 atf_tc_fail("FPE signal handler was not invoked");
391 sigsegv_action(int signo
, siginfo_t
*info
, void *ptr
)
394 sig_debug(signo
, info
, (ucontext_t
*)ptr
);
396 ATF_REQUIRE_EQ(info
->si_signo
, SIGSEGV
);
397 ATF_REQUIRE_EQ(info
->si_errno
, 0);
398 ATF_REQUIRE_EQ(info
->si_code
, SEGV_MAPERR
);
399 ATF_REQUIRE_EQ(info
->si_addr
, (void *)0);
406 ATF_TC_HEAD(sigsegv
, tc
)
409 atf_tc_set_md_var(tc
, "descr",
410 "Checks that signal trampoline correctly calls SIGSEGV handler");
413 ATF_TC_BODY(sigsegv
, tc
)
417 sa
.sa_flags
= SA_SIGINFO
;
418 sa
.sa_sigaction
= sigsegv_action
;
419 sigemptyset(&sa
.sa_mask
);
420 sigaction(SIGSEGV
, &sa
, NULL
);
422 *(volatile long *)0 = 0;
423 atf_tc_fail("Test did not fault as expected");
427 sigbus_action(int signo
, siginfo_t
*info
, void *ptr
)
430 printf("si_addr = %p\n", info
->si_addr
);
431 sig_debug(signo
, info
, (ucontext_t
*)ptr
);
433 ATF_REQUIRE_EQ(info
->si_signo
, SIGBUS
);
434 ATF_REQUIRE_EQ(info
->si_errno
, 0);
435 ATF_REQUIRE_EQ(info
->si_code
, BUS_ADRALN
);
437 #if defined(__i386__) || defined(__x86_64__)
438 atf_tc_expect_fail("x86 architecture does not correctly "
439 "report the address where the unaligned access occured");
441 ATF_REQUIRE_EQ(info
->si_addr
, (volatile void *)addr
);
447 ATF_TC(sigbus_adraln
);
448 ATF_TC_HEAD(sigbus_adraln
, tc
)
451 atf_tc_set_md_var(tc
, "descr",
452 "Checks that signal trampoline correctly calls SIGBUS handler "
453 "for invalid address alignment");
456 ATF_TC_BODY(sigbus_adraln
, tc
)
460 #if defined(__alpha__) || defined(__arm__)
462 size_t len
= sizeof(val
);
463 rv
= sysctlbyname("machdep.unaligned_sigbus", &val
, &len
, NULL
, 0);
464 ATF_REQUIRE(rv
== 0);
466 atf_tc_skip("No SIGBUS signal for unaligned accesses");
469 /* m68k (except sun2) never issue SIGBUS (PR lib/49653) */
470 if (strcmp(MACHINE_ARCH
, "m68k") == 0)
471 atf_tc_skip("No SIGBUS signal for unaligned accesses");
473 sa
.sa_flags
= SA_SIGINFO
;
474 sa
.sa_sigaction
= sigbus_action
;
475 sigemptyset(&sa
.sa_mask
);
476 sigaction(SIGBUS
, &sa
, NULL
);
478 /* Enable alignment checks for x86. 0x40000 is PSL_AC. */
479 #if defined(__i386__)
480 __asm__("pushf; orl $0x40000, (%esp); popf");
481 #elif defined(__amd64__)
482 __asm__("pushf; orl $0x40000, (%rsp); popf");
485 addr
= calloc(2, sizeof(int));
486 ATF_REQUIRE(addr
!= NULL
);
489 atf_tc_expect_fail("QEMU fails to trap unaligned accesses");
491 /* Force an unaligned access */
493 printf("now trying to access unaligned address %p\n", addr
);
494 ATF_REQUIRE_EQ(*(volatile int *)addr
, 0);
496 atf_tc_fail("Test did not fault as expected");
502 ATF_TP_ADD_TC(tp
, sigalarm
);
503 ATF_TP_ADD_TC(tp
, sigchild_normal
);
504 ATF_TP_ADD_TC(tp
, sigchild_dump
);
505 ATF_TP_ADD_TC(tp
, sigchild_kill
);
506 ATF_TP_ADD_TC(tp
, sigfpe_flt
);
507 ATF_TP_ADD_TC(tp
, sigfpe_int
);
508 ATF_TP_ADD_TC(tp
, sigsegv
);
509 ATF_TP_ADD_TC(tp
, sigbus_adraln
);
511 return atf_no_error();