1 /* $NetBSD: t_siginfo.c,v 1.26 2014/11/19 10:09:45 martin Exp $ */ 2 3 /*- 4 * Copyright (c) 2010 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 <atf-c.h> 30 31 #include <sys/inttypes.h> 32 #include <sys/resource.h> 33 #include <sys/sysctl.h> 34 #include <sys/time.h> 35 #include <sys/ucontext.h> 36 #include <sys/wait.h> 37 38 #include <assert.h> 39 #include <signal.h> 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <string.h> 43 #include <unistd.h> 44 #include <setjmp.h> 45 #include <float.h> 46 47 #ifdef HAVE_FENV 48 #include <fenv.h> 49 #elif defined(_FLOAT_IEEE754) 50 #include <ieeefp.h> 51 #endif 52 53 #include "isqemu.h" 54 55 /* for sigbus */ 56 volatile char *addr; 57 58 /* for sigchild */ 59 pid_t child; 60 int code; 61 int status; 62 63 /* for sigfpe */ 64 sig_atomic_t fltdiv_signalled = 0; 65 sig_atomic_t intdiv_signalled = 0; 66 67 static void 68 sig_debug(int signo, siginfo_t *info, ucontext_t *ctx) 69 { 70 unsigned int i; 71 72 printf("%d %p %p\n", signo, info, ctx); 73 if (info != NULL) { 74 printf("si_signo=%d\n", info->si_signo); 75 printf("si_errno=%d\n", info->si_errno); 76 printf("si_code=%d\n", info->si_code); 77 printf("si_value.sival_int=%d\n", info->si_value.sival_int); 78 } 79 if (ctx != NULL) { 80 printf("uc_flags 0x%x\n", ctx->uc_flags); 81 printf("uc_link %p\n", ctx->uc_link); 82 for (i = 0; i < __arraycount(ctx->uc_sigmask.__bits); i++) 83 printf("uc_sigmask[%d] 0x%x\n", i, 84 ctx->uc_sigmask.__bits[i]); 85 printf("uc_stack %p %lu 0x%x\n", ctx->uc_stack.ss_sp, 86 (unsigned long)ctx->uc_stack.ss_size, 87 ctx->uc_stack.ss_flags); 88 for (i = 0; i < __arraycount(ctx->uc_mcontext.__gregs); i++) 89 printf("uc_mcontext.greg[%d] 0x%lx\n", i, 90 (long)ctx->uc_mcontext.__gregs[i]); 91 } 92 } 93 94 static void 95 sigalrm_action(int signo, siginfo_t *info, void *ptr) 96 { 97 98 sig_debug(signo, info, (ucontext_t *)ptr); 99 100 ATF_REQUIRE_EQ(info->si_signo, SIGALRM); 101 ATF_REQUIRE_EQ(info->si_code, SI_TIMER); 102 ATF_REQUIRE_EQ(info->si_value.sival_int, ITIMER_REAL); 103 104 atf_tc_pass(); 105 /* NOTREACHED */ 106 } 107 108 ATF_TC(sigalarm); 109 110 ATF_TC_HEAD(sigalarm, tc) 111 { 112 113 atf_tc_set_md_var(tc, "descr", 114 "Checks that signal trampoline correctly calls SIGALRM handler"); 115 } 116 117 ATF_TC_BODY(sigalarm, tc) 118 { 119 struct sigaction sa; 120 sa.sa_flags = SA_SIGINFO; 121 sa.sa_sigaction = sigalrm_action; 122 sigemptyset(&sa.sa_mask); 123 sigaction(SIGALRM, &sa, NULL); 124 for (;;) { 125 alarm(1); 126 sleep(1); 127 } 128 atf_tc_fail("SIGALRM handler wasn't called"); 129 } 130 131 static void 132 sigchild_action(int signo, siginfo_t *info, void *ptr) 133 { 134 if (info != NULL) { 135 printf("info=%p\n", info); 136 printf("ptr=%p\n", ptr); 137 printf("si_signo=%d\n", info->si_signo); 138 printf("si_errno=%d\n", info->si_errno); 139 printf("si_code=%d\n", info->si_code); 140 printf("si_uid=%d\n", info->si_uid); 141 printf("si_pid=%d\n", info->si_pid); 142 printf("si_status=%d\n", info->si_status); 143 printf("si_utime=%lu\n", (unsigned long int)info->si_utime); 144 printf("si_stime=%lu\n", (unsigned long int)info->si_stime); 145 } 146 ATF_REQUIRE_EQ(info->si_code, code); 147 ATF_REQUIRE_EQ(info->si_signo, SIGCHLD); 148 ATF_REQUIRE_EQ(info->si_uid, getuid()); 149 ATF_REQUIRE_EQ(info->si_pid, child); 150 if (WIFEXITED(info->si_status)) 151 ATF_REQUIRE_EQ(WEXITSTATUS(info->si_status), status); 152 else if (WIFSTOPPED(info->si_status)) 153 ATF_REQUIRE_EQ(WSTOPSIG(info->si_status), status); 154 else if (WIFSIGNALED(info->si_status)) 155 ATF_REQUIRE_EQ(WTERMSIG(info->si_status), status); 156 } 157 158 static void 159 setchildhandler(void (*action)(int, siginfo_t *, void *)) 160 { 161 struct sigaction sa; 162 sa.sa_flags = SA_SIGINFO; 163 sa.sa_sigaction = action; 164 sigemptyset(&sa.sa_mask); 165 sigaction(SIGCHLD, &sa, NULL); 166 } 167 168 static void 169 sigchild_setup(void) 170 { 171 sigset_t set; 172 struct rlimit rlim; 173 174 (void)getrlimit(RLIMIT_CORE, &rlim); 175 rlim.rlim_cur = rlim.rlim_max; 176 (void)setrlimit(RLIMIT_CORE, &rlim); 177 178 setchildhandler(sigchild_action); 179 sigemptyset(&set); 180 sigaddset(&set, SIGCHLD); 181 sigprocmask(SIG_BLOCK, &set, NULL); 182 } 183 184 ATF_TC(sigchild_normal); 185 ATF_TC_HEAD(sigchild_normal, tc) 186 { 187 188 atf_tc_set_md_var(tc, "descr", 189 "Checks that signal trampoline correctly calls SIGCHLD handler " 190 "when child exits normally"); 191 } 192 193 ATF_TC_BODY(sigchild_normal, tc) 194 { 195 sigset_t set; 196 197 sigchild_setup(); 198 199 status = 25; 200 code = CLD_EXITED; 201 202 switch ((child = fork())) { 203 case 0: 204 sleep(1); 205 exit(status); 206 case -1: 207 atf_tc_fail("fork failed"); 208 default: 209 sigemptyset(&set); 210 sigsuspend(&set); 211 } 212 } 213 214 ATF_TC(sigchild_dump); 215 ATF_TC_HEAD(sigchild_dump, tc) 216 { 217 218 atf_tc_set_md_var(tc, "descr", 219 "Checks that signal trampoline correctly calls SIGCHLD handler " 220 "when child segfaults"); 221 } 222 223 ATF_TC_BODY(sigchild_dump, tc) 224 { 225 sigset_t set; 226 227 sigchild_setup(); 228 229 status = SIGSEGV; 230 code = CLD_DUMPED; 231 232 switch ((child = fork())) { 233 case 0: 234 sleep(1); 235 *(volatile long *)0 = 0; 236 atf_tc_fail("Child did not segfault"); 237 /* NOTREACHED */ 238 case -1: 239 atf_tc_fail("fork failed"); 240 default: 241 sigemptyset(&set); 242 sigsuspend(&set); 243 } 244 } 245 246 ATF_TC(sigchild_kill); 247 ATF_TC_HEAD(sigchild_kill, tc) 248 { 249 250 atf_tc_set_md_var(tc, "descr", 251 "Checks that signal trampoline correctly calls SIGCHLD handler " 252 "when child is killed"); 253 } 254 255 ATF_TC_BODY(sigchild_kill, tc) 256 { 257 sigset_t set; 258 259 sigchild_setup(); 260 261 status = SIGPIPE; 262 code = CLD_KILLED; 263 264 switch ((child = fork())) { 265 case 0: 266 sigemptyset(&set); 267 sigsuspend(&set); 268 break; 269 case -1: 270 atf_tc_fail("fork failed"); 271 default: 272 kill(child, SIGPIPE); 273 sigemptyset(&set); 274 sigsuspend(&set); 275 } 276 } 277 278 static sigjmp_buf sigfpe_flt_env; 279 static void 280 sigfpe_flt_action(int signo, siginfo_t *info, void *ptr) 281 { 282 283 sig_debug(signo, info, (ucontext_t *)ptr); 284 285 if (fltdiv_signalled++ != 0) 286 atf_tc_fail("FPE handler called more than once"); 287 288 ATF_REQUIRE_EQ(info->si_signo, SIGFPE); 289 ATF_REQUIRE_EQ(info->si_code, FPE_FLTDIV); 290 ATF_REQUIRE_EQ(info->si_errno, 0); 291 292 siglongjmp(sigfpe_flt_env, 1); 293 } 294 295 ATF_TC(sigfpe_flt); 296 ATF_TC_HEAD(sigfpe_flt, tc) 297 { 298 299 atf_tc_set_md_var(tc, "descr", 300 "Checks that signal trampoline correctly calls SIGFPE handler " 301 "for floating div-by-zero"); 302 } 303 304 ATF_TC_BODY(sigfpe_flt, tc) 305 { 306 struct sigaction sa; 307 double d = strtod("0", NULL); 308 309 if (isQEMU()) 310 atf_tc_skip("Test does not run correctly under QEMU"); 311 #if defined(__powerpc__) 312 atf_tc_skip("Test not valid on powerpc"); 313 #elif defined(__arm__) && !__SOFTFP__ /* 314 * Some NEON fpus do not implement IEEE exception handling, 315 * skip these tests if running on them and compiled for 316 * hard float. 317 */ 318 if (0 == fpsetmask(fpsetmask(FP_X_INV))) 319 atf_tc_skip("FPU does not implement exception handling"); 320 #endif 321 if (sigsetjmp(sigfpe_flt_env, 0) == 0) { 322 sa.sa_flags = SA_SIGINFO; 323 sa.sa_sigaction = sigfpe_flt_action; 324 sigemptyset(&sa.sa_mask); 325 sigaction(SIGFPE, &sa, NULL); 326 #ifdef HAVE_FENV 327 feenableexcept(FE_ALL_EXCEPT); 328 #elif defined(_FLOAT_IEEE754) 329 fpsetmask(FP_X_INV|FP_X_DZ|FP_X_OFL|FP_X_UFL|FP_X_IMP); 330 #endif 331 printf("%g\n", 1 / d); 332 } 333 if (fltdiv_signalled == 0) 334 atf_tc_fail("FPE signal handler was not invoked"); 335 } 336 337 static sigjmp_buf sigfpe_int_env; 338 static void 339 sigfpe_int_action(int signo, siginfo_t *info, void *ptr) 340 { 341 342 sig_debug(signo, info, (ucontext_t *)ptr); 343 344 if (intdiv_signalled++ != 0) 345 atf_tc_fail("INTDIV handler called more than once"); 346 347 ATF_REQUIRE_EQ(info->si_signo, SIGFPE); 348 ATF_REQUIRE_EQ(info->si_code, FPE_INTDIV); 349 atf_tc_expect_pass(); 350 ATF_REQUIRE_EQ(info->si_errno, 0); 351 352 siglongjmp(sigfpe_int_env, 1); 353 } 354 355 ATF_TC(sigfpe_int); 356 ATF_TC_HEAD(sigfpe_int, tc) 357 { 358 359 atf_tc_set_md_var(tc, "descr", 360 "Checks that signal trampoline correctly calls SIGFPE handler " 361 "for integer div-by-zero (PR port-i386/43655)"); 362 } 363 364 ATF_TC_BODY(sigfpe_int, tc) 365 { 366 struct sigaction sa; 367 long l = strtol("0", NULL, 10); 368 369 #if defined(__powerpc__) 370 atf_tc_skip("Test not valid on powerpc"); 371 #endif 372 if (sigsetjmp(sigfpe_int_env, 0) == 0) { 373 sa.sa_flags = SA_SIGINFO; 374 sa.sa_sigaction = sigfpe_int_action; 375 sigemptyset(&sa.sa_mask); 376 sigaction(SIGFPE, &sa, NULL); 377 #ifdef HAVE_FENV 378 feenableexcept(FE_ALL_EXCEPT); 379 #elif defined(_FLOAT_IEEE754) 380 fpsetmask(FP_X_INV|FP_X_DZ|FP_X_OFL|FP_X_UFL|FP_X_IMP); 381 #endif 382 printf("%ld\n", 1 / l); 383 } 384 if (intdiv_signalled == 0) 385 atf_tc_fail("FPE signal handler was not invoked"); 386 } 387 388 static void 389 sigsegv_action(int signo, siginfo_t *info, void *ptr) 390 { 391 392 sig_debug(signo, info, (ucontext_t *)ptr); 393 394 ATF_REQUIRE_EQ(info->si_signo, SIGSEGV); 395 ATF_REQUIRE_EQ(info->si_errno, 0); 396 ATF_REQUIRE_EQ(info->si_code, SEGV_MAPERR); 397 ATF_REQUIRE_EQ(info->si_addr, (void *)0); 398 399 atf_tc_pass(); 400 /* NOTREACHED */ 401 } 402 403 ATF_TC(sigsegv); 404 ATF_TC_HEAD(sigsegv, tc) 405 { 406 407 atf_tc_set_md_var(tc, "descr", 408 "Checks that signal trampoline correctly calls SIGSEGV handler"); 409 } 410 411 ATF_TC_BODY(sigsegv, tc) 412 { 413 struct sigaction sa; 414 415 sa.sa_flags = SA_SIGINFO; 416 sa.sa_sigaction = sigsegv_action; 417 sigemptyset(&sa.sa_mask); 418 sigaction(SIGSEGV, &sa, NULL); 419 420 *(volatile long *)0 = 0; 421 atf_tc_fail("Test did not fault as expected"); 422 } 423 424 static void 425 sigbus_action(int signo, siginfo_t *info, void *ptr) 426 { 427 428 printf("si_addr = %p\n", info->si_addr); 429 sig_debug(signo, info, (ucontext_t *)ptr); 430 431 ATF_REQUIRE_EQ(info->si_signo, SIGBUS); 432 ATF_REQUIRE_EQ(info->si_errno, 0); 433 ATF_REQUIRE_EQ(info->si_code, BUS_ADRALN); 434 435 #if defined(__i386__) || defined(__x86_64__) 436 atf_tc_expect_fail("x86 architecture does not correctly " 437 "report the address where the unaligned access occured"); 438 #endif 439 ATF_REQUIRE_EQ(info->si_addr, (volatile void *)addr); 440 441 atf_tc_pass(); 442 /* NOTREACHED */ 443 } 444 445 ATF_TC(sigbus_adraln); 446 ATF_TC_HEAD(sigbus_adraln, tc) 447 { 448 449 atf_tc_set_md_var(tc, "descr", 450 "Checks that signal trampoline correctly calls SIGBUS handler " 451 "for invalid address alignment"); 452 } 453 454 ATF_TC_BODY(sigbus_adraln, tc) 455 { 456 struct sigaction sa; 457 458 #if defined(__alpha__) || defined(__arm__) 459 int rv, val; 460 size_t len = sizeof(val); 461 rv = sysctlbyname("machdep.unaligned_sigbus", &val, &len, NULL, 0); 462 ATF_REQUIRE(rv == 0); 463 if (val == 0) 464 atf_tc_skip("No SIGBUS signal for unaligned accesses"); 465 #endif 466 467 sa.sa_flags = SA_SIGINFO; 468 sa.sa_sigaction = sigbus_action; 469 sigemptyset(&sa.sa_mask); 470 sigaction(SIGBUS, &sa, NULL); 471 472 /* Enable alignment checks for x86. 0x40000 is PSL_AC. */ 473 #if defined(__i386__) 474 __asm__("pushf; orl $0x40000, (%esp); popf"); 475 #elif defined(__amd64__) 476 __asm__("pushf; orl $0x40000, (%rsp); popf"); 477 #endif 478 479 addr = calloc(2, sizeof(int)); 480 ATF_REQUIRE(addr != NULL); 481 482 if (isQEMU()) 483 atf_tc_expect_fail("QEMU fails to trap unaligned accesses"); 484 485 /* Force an unaligned access */ 486 addr++; 487 printf("now trying to access unaligned address %p\n", addr); 488 ATF_REQUIRE_EQ(*(volatile int *)addr, 0); 489 490 atf_tc_fail("Test did not fault as expected"); 491 } 492 493 ATF_TP_ADD_TCS(tp) 494 { 495 496 ATF_TP_ADD_TC(tp, sigalarm); 497 ATF_TP_ADD_TC(tp, sigchild_normal); 498 ATF_TP_ADD_TC(tp, sigchild_dump); 499 ATF_TP_ADD_TC(tp, sigchild_kill); 500 ATF_TP_ADD_TC(tp, sigfpe_flt); 501 ATF_TP_ADD_TC(tp, sigfpe_int); 502 ATF_TP_ADD_TC(tp, sigsegv); 503 ATF_TP_ADD_TC(tp, sigbus_adraln); 504 505 return atf_no_error(); 506 } 507