1 /* $NetBSD: t_siginfo.c,v 1.23 2014/02/09 21:26:07 jmmv 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 #include <atf-c/config.h> 31 32 #if defined(__NetBSD__) 33 #include <sys/inttypes.h> 34 #endif 35 #include <sys/resource.h> 36 #include <sys/sysctl.h> 37 #include <sys/time.h> 38 #include <sys/ucontext.h> 39 #include <sys/wait.h> 40 41 #include <assert.h> 42 #include <signal.h> 43 #include <stdio.h> 44 #include <stdlib.h> 45 #include <string.h> 46 #include <unistd.h> 47 #include <setjmp.h> 48 #include <float.h> 49 50 #ifdef HAVE_FENV 51 #include <fenv.h> 52 #elif defined(_FLOAT_IEEE754) 53 #include <ieeefp.h> 54 #endif 55 56 #include "isqemu.h" 57 58 /* for sigbus */ 59 volatile char *addr; 60 61 /* for sigchild */ 62 pid_t child; 63 int code; 64 int status; 65 66 /* for sigfpe */ 67 sig_atomic_t fltdiv_signalled = 0; 68 sig_atomic_t intdiv_signalled = 0; 69 70 static void 71 sig_debug(int signo, siginfo_t *info, ucontext_t *ctx) 72 { 73 unsigned int i; 74 75 printf("%d %p %p\n", signo, info, ctx); 76 if (info != NULL) { 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); 81 } 82 if (ctx != NULL) { 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); 91 #if defined(__NetBSD__) 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]); 95 #endif 96 } 97 } 98 99 static void 100 sigalrm_action(int signo, siginfo_t *info, void *ptr) 101 { 102 103 sig_debug(signo, info, (ucontext_t *)ptr); 104 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); 108 109 atf_tc_pass(); 110 /* NOTREACHED */ 111 } 112 113 ATF_TC(sigalarm); 114 115 ATF_TC_HEAD(sigalarm, tc) 116 { 117 118 atf_tc_set_md_var(tc, "descr", 119 "Checks that signal trampoline correctly calls SIGALRM handler"); 120 } 121 122 ATF_TC_BODY(sigalarm, tc) 123 { 124 struct sigaction sa; 125 sa.sa_flags = SA_SIGINFO; 126 sa.sa_sigaction = sigalrm_action; 127 sigemptyset(&sa.sa_mask); 128 sigaction(SIGALRM, &sa, NULL); 129 for (;;) { 130 alarm(1); 131 sleep(1); 132 } 133 atf_tc_fail("SIGALRM handler wasn't called"); 134 } 135 136 static void 137 sigchild_action(int signo, siginfo_t *info, void *ptr) 138 { 139 if (info != NULL) { 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); 148 #if defined(__NetBSD__) 149 printf("si_utime=%lu\n", (unsigned long int)info->si_utime); 150 printf("si_stime=%lu\n", (unsigned long int)info->si_stime); 151 #endif 152 } 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); 163 } 164 165 static void 166 setchildhandler(void (*action)(int, siginfo_t *, void *)) 167 { 168 struct sigaction sa; 169 sa.sa_flags = SA_SIGINFO; 170 sa.sa_sigaction = action; 171 sigemptyset(&sa.sa_mask); 172 sigaction(SIGCHLD, &sa, NULL); 173 } 174 175 static void 176 sigchild_setup(void) 177 { 178 sigset_t set; 179 struct rlimit rlim; 180 181 (void)getrlimit(RLIMIT_CORE, &rlim); 182 rlim.rlim_cur = rlim.rlim_max; 183 (void)setrlimit(RLIMIT_CORE, &rlim); 184 185 setchildhandler(sigchild_action); 186 sigemptyset(&set); 187 sigaddset(&set, SIGCHLD); 188 sigprocmask(SIG_BLOCK, &set, NULL); 189 } 190 191 ATF_TC(sigchild_normal); 192 ATF_TC_HEAD(sigchild_normal, tc) 193 { 194 195 atf_tc_set_md_var(tc, "descr", 196 "Checks that signal trampoline correctly calls SIGCHLD handler " 197 "when child exits normally"); 198 } 199 200 ATF_TC_BODY(sigchild_normal, tc) 201 { 202 sigset_t set; 203 204 sigchild_setup(); 205 206 status = 25; 207 code = CLD_EXITED; 208 209 switch ((child = fork())) { 210 case 0: 211 sleep(1); 212 exit(status); 213 case -1: 214 atf_tc_fail("fork failed"); 215 default: 216 sigemptyset(&set); 217 sigsuspend(&set); 218 } 219 } 220 221 ATF_TC(sigchild_dump); 222 ATF_TC_HEAD(sigchild_dump, tc) 223 { 224 225 atf_tc_set_md_var(tc, "descr", 226 "Checks that signal trampoline correctly calls SIGCHLD handler " 227 "when child segfaults"); 228 } 229 230 ATF_TC_BODY(sigchild_dump, tc) 231 { 232 sigset_t set; 233 234 sigchild_setup(); 235 236 status = SIGSEGV; 237 code = CLD_DUMPED; 238 239 switch ((child = fork())) { 240 case 0: 241 sleep(1); 242 *(volatile long *)0 = 0; 243 atf_tc_fail("Child did not segfault"); 244 /* NOTREACHED */ 245 case -1: 246 atf_tc_fail("fork failed"); 247 default: 248 sigemptyset(&set); 249 sigsuspend(&set); 250 } 251 } 252 253 ATF_TC(sigchild_kill); 254 ATF_TC_HEAD(sigchild_kill, tc) 255 { 256 257 atf_tc_set_md_var(tc, "descr", 258 "Checks that signal trampoline correctly calls SIGCHLD handler " 259 "when child is killed"); 260 } 261 262 ATF_TC_BODY(sigchild_kill, tc) 263 { 264 sigset_t set; 265 266 sigchild_setup(); 267 268 status = SIGPIPE; 269 code = CLD_KILLED; 270 271 switch ((child = fork())) { 272 case 0: 273 sigemptyset(&set); 274 sigsuspend(&set); 275 break; 276 case -1: 277 atf_tc_fail("fork failed"); 278 default: 279 kill(child, SIGPIPE); 280 sigemptyset(&set); 281 sigsuspend(&set); 282 } 283 } 284 285 static sigjmp_buf sigfpe_flt_env; 286 static void 287 sigfpe_flt_action(int signo, siginfo_t *info, void *ptr) 288 { 289 290 sig_debug(signo, info, (ucontext_t *)ptr); 291 292 if (fltdiv_signalled++ != 0) 293 atf_tc_fail("FPE handler called more than once"); 294 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); 298 299 siglongjmp(sigfpe_flt_env, 1); 300 } 301 302 ATF_TC(sigfpe_flt); 303 ATF_TC_HEAD(sigfpe_flt, tc) 304 { 305 306 atf_tc_set_md_var(tc, "descr", 307 "Checks that signal trampoline correctly calls SIGFPE handler " 308 "for floating div-by-zero"); 309 } 310 311 ATF_TC_BODY(sigfpe_flt, tc) 312 { 313 struct sigaction sa; 314 double d = strtod("0", NULL); 315 316 if (isQEMU()) 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 #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__) 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("SIGBUS signal not enabled 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