1 /* $NetBSD: t_siginfo.c,v 1.8 2011/01/03 20:51:26 pgoyette 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/time.h> 34 #include <sys/ucontext.h> 35 #include <sys/wait.h> 36 37 #include <assert.h> 38 #include <signal.h> 39 #include <stdio.h> 40 #include <stdlib.h> 41 #include <unistd.h> 42 #include <setjmp.h> 43 44 #ifndef __vax__ 45 #include <ieeefp.h> 46 #endif 47 48 /* for sigchild */ 49 pid_t child; 50 int code; 51 int status; 52 53 /* for sigfpe */ 54 sig_atomic_t fltdiv_signalled = 0; 55 sig_atomic_t intdiv_signalled = 0; 56 57 static void 58 sig_debug(int signo, siginfo_t *info, ucontext_t *ctx) 59 { 60 unsigned int i; 61 62 printf("%d %p %p\n", signo, info, ctx); 63 if (info != NULL) { 64 printf("si_signo=%d\n", info->si_signo); 65 printf("si_errno=%d\n", info->si_errno); 66 printf("si_code=%d\n", info->si_code); 67 printf("si_value.sival_int=%d\n", info->si_value.sival_int); 68 } 69 if (ctx != NULL) { 70 printf("uc_flags 0x%x\n", ctx->uc_flags); 71 printf("uc_link %p\n", ctx->uc_link); 72 for (i = 0; i < __arraycount(ctx->uc_sigmask.__bits); i++) 73 printf("uc_sigmask[%d] 0x%x\n", i, 74 ctx->uc_sigmask.__bits[i]); 75 printf("uc_stack %p %lu 0x%x\n", ctx->uc_stack.ss_sp, 76 (unsigned long)ctx->uc_stack.ss_size, 77 ctx->uc_stack.ss_flags); 78 for (i = 0; i < __arraycount(ctx->uc_mcontext.__gregs); i++) 79 printf("uc_mcontext.greg[%d] 0x%lx\n", i, 80 (long)ctx->uc_mcontext.__gregs[i]); 81 } 82 } 83 84 static void 85 sigalrm_action(int signo, siginfo_t *info, void *ptr) 86 { 87 88 sig_debug(signo, info, (ucontext_t *)ptr); 89 90 ATF_REQUIRE_EQ(info->si_signo, SIGALRM); 91 ATF_REQUIRE_EQ(info->si_code, SI_TIMER); 92 ATF_REQUIRE_EQ(info->si_value.sival_int, ITIMER_REAL); 93 94 atf_tc_pass(); 95 /* NOTREACHED */ 96 } 97 98 ATF_TC(sigalarm); 99 100 ATF_TC_HEAD(sigalarm, tc) 101 { 102 103 atf_tc_set_md_var(tc, "descr", 104 "Checks that signal trampoline correctly calls SIGALRM handler"); 105 } 106 107 ATF_TC_BODY(sigalarm, tc) 108 { 109 struct sigaction sa; 110 sa.sa_flags = SA_SIGINFO; 111 sa.sa_sigaction = sigalrm_action; 112 sigemptyset(&sa.sa_mask); 113 sigaction(SIGALRM, &sa, NULL); 114 for (;;) { 115 alarm(1); 116 sleep(1); 117 } 118 atf_tc_fail("SIGALRM handler wasn't called"); 119 } 120 121 static void 122 sigchild_action(int signo, siginfo_t *info, void *ptr) 123 { 124 if (info != NULL) { 125 printf("info=%p\n", info); 126 printf("ptr=%p\n", ptr); 127 printf("si_signo=%d\n", info->si_signo); 128 printf("si_errno=%d\n", info->si_errno); 129 printf("si_code=%d\n", info->si_code); 130 printf("si_uid=%d\n", info->si_uid); 131 printf("si_pid=%d\n", info->si_pid); 132 printf("si_status=%d\n", info->si_status); 133 printf("si_utime=%lu\n", (unsigned long int)info->si_utime); 134 printf("si_stime=%lu\n", (unsigned long int)info->si_stime); 135 } 136 ATF_REQUIRE_EQ(info->si_code, code); 137 ATF_REQUIRE_EQ(info->si_signo, SIGCHLD); 138 ATF_REQUIRE_EQ(info->si_uid, getuid()); 139 ATF_REQUIRE_EQ(info->si_pid, child); 140 if (WIFEXITED(info->si_status)) 141 ATF_REQUIRE_EQ(WEXITSTATUS(info->si_status), status); 142 else if (WIFSTOPPED(info->si_status)) 143 ATF_REQUIRE_EQ(WSTOPSIG(info->si_status), status); 144 else if (WIFSIGNALED(info->si_status)) 145 ATF_REQUIRE_EQ(WTERMSIG(info->si_status), status); 146 } 147 148 static void 149 setchildhandler(void (*action)(int, siginfo_t *, void *)) 150 { 151 struct sigaction sa; 152 sa.sa_flags = SA_SIGINFO; 153 sa.sa_sigaction = action; 154 sigemptyset(&sa.sa_mask); 155 sigaction(SIGCHLD, &sa, NULL); 156 } 157 158 static void 159 sigchild_setup(void) 160 { 161 sigset_t set; 162 struct rlimit rlim; 163 164 (void)getrlimit(RLIMIT_CORE, &rlim); 165 rlim.rlim_cur = rlim.rlim_max; 166 (void)setrlimit(RLIMIT_CORE, &rlim); 167 168 setchildhandler(sigchild_action); 169 sigemptyset(&set); 170 sigaddset(&set, SIGCHLD); 171 sigprocmask(SIG_BLOCK, &set, NULL); 172 } 173 174 ATF_TC(sigchild_normal); 175 ATF_TC_HEAD(sigchild_normal, tc) 176 { 177 178 atf_tc_set_md_var(tc, "descr", 179 "Checks that signal trampoline correctly calls SIGCHLD handler " 180 "when child exits normally"); 181 } 182 183 ATF_TC_BODY(sigchild_normal, tc) 184 { 185 sigset_t set; 186 187 sigchild_setup(); 188 189 status = 25; 190 code = CLD_EXITED; 191 192 switch ((child = fork())) { 193 case 0: 194 sleep(1); 195 exit(status); 196 case -1: 197 atf_tc_fail("fork failed"); 198 default: 199 sigemptyset(&set); 200 sigsuspend(&set); 201 } 202 } 203 204 ATF_TC(sigchild_dump); 205 ATF_TC_HEAD(sigchild_dump, tc) 206 { 207 208 atf_tc_set_md_var(tc, "descr", 209 "Checks that signal trampoline correctly calls SIGCHLD handler " 210 "when child segfaults"); 211 } 212 213 ATF_TC_BODY(sigchild_dump, tc) 214 { 215 sigset_t set; 216 217 sigchild_setup(); 218 219 status = SIGSEGV; 220 code = CLD_DUMPED; 221 222 switch ((child = fork())) { 223 case 0: 224 sleep(1); 225 *(long *)0 = 0; 226 atf_tc_fail("Child did not segfault"); 227 /* NOTREACHED */ 228 case -1: 229 atf_tc_fail("fork failed"); 230 default: 231 sigemptyset(&set); 232 sigsuspend(&set); 233 } 234 } 235 236 ATF_TC(sigchild_kill); 237 ATF_TC_HEAD(sigchild_kill, tc) 238 { 239 240 atf_tc_set_md_var(tc, "descr", 241 "Checks that signal trampoline correctly calls SIGCHLD handler " 242 "when child is killed"); 243 } 244 245 ATF_TC_BODY(sigchild_kill, tc) 246 { 247 sigset_t set; 248 249 sigchild_setup(); 250 251 status = SIGPIPE; 252 code = CLD_KILLED; 253 254 switch ((child = fork())) { 255 case 0: 256 sigemptyset(&set); 257 sigsuspend(&set); 258 break; 259 case -1: 260 atf_tc_fail("fork failed"); 261 default: 262 kill(child, SIGPIPE); 263 sigemptyset(&set); 264 sigsuspend(&set); 265 } 266 } 267 268 static sigjmp_buf sigfpe_flt_env; 269 static void 270 sigfpe_flt_action(int signo, siginfo_t *info, void *ptr) 271 { 272 273 sig_debug(signo, info, (ucontext_t *)ptr); 274 275 if (fltdiv_signalled++ != 0) 276 atf_tc_fail("FPE handler called more than once"); 277 278 ATF_REQUIRE_EQ(info->si_signo, SIGFPE); 279 ATF_REQUIRE_EQ(info->si_code, FPE_FLTDIV); 280 ATF_REQUIRE_EQ(info->si_errno, 0); 281 282 siglongjmp(sigfpe_flt_env, 1); 283 } 284 285 ATF_TC(sigfpe_flt); 286 ATF_TC_HEAD(sigfpe_flt, tc) 287 { 288 289 atf_tc_set_md_var(tc, "descr", 290 "Checks that signal trampoline correctly calls SIGFPE handler " 291 "for floating div-by-zero"); 292 } 293 294 ATF_TC_BODY(sigfpe_flt, tc) 295 { 296 struct sigaction sa; 297 double d = strtod("0", NULL); 298 299 if (system("cpuctl identify 0 | grep -q QEMU") == 0) 300 atf_tc_skip("Test does not run correctly under qemu"); 301 if (sigsetjmp(sigfpe_flt_env, 0) == 0) { 302 sa.sa_flags = SA_SIGINFO; 303 sa.sa_sigaction = sigfpe_flt_action; 304 sigemptyset(&sa.sa_mask); 305 sigaction(SIGFPE, &sa, NULL); 306 #ifndef __vax__ 307 fpsetmask(FP_X_INV|FP_X_DZ|FP_X_OFL|FP_X_UFL|FP_X_IMP); 308 #endif 309 printf("%g\n", 1 / d); 310 } 311 if (fltdiv_signalled == 0) 312 atf_tc_fail("FPE signal handler was not invoked"); 313 } 314 315 static sigjmp_buf sigfpe_int_env; 316 static void 317 sigfpe_int_action(int signo, siginfo_t *info, void *ptr) 318 { 319 320 sig_debug(signo, info, (ucontext_t *)ptr); 321 322 if (intdiv_signalled++ != 0) 323 atf_tc_fail("INTDIV handler called more than once"); 324 325 ATF_REQUIRE_EQ(info->si_signo, SIGFPE); 326 if (info->si_code == FPE_FLTDIV) 327 atf_tc_expect_fail("PR port-i386/43655 : integer div-by-zero " 328 "reports FPE_FLTDIV instead of FPE_INTDIV"); 329 ATF_REQUIRE_EQ(info->si_code, FPE_INTDIV); 330 atf_tc_expect_pass(); 331 ATF_REQUIRE_EQ(info->si_errno, 0); 332 333 siglongjmp(sigfpe_int_env, 1); 334 } 335 336 ATF_TC(sigfpe_int); 337 ATF_TC_HEAD(sigfpe_int, tc) 338 { 339 340 atf_tc_set_md_var(tc, "descr", 341 "Checks that signal trampoline correctly calls SIGFPE handler " 342 "for integer div-by-zero"); 343 } 344 345 ATF_TC_BODY(sigfpe_int, tc) 346 { 347 struct sigaction sa; 348 long l = strtol("0", NULL, 10); 349 350 if (sigsetjmp(sigfpe_int_env, 0) == 0) { 351 sa.sa_flags = SA_SIGINFO; 352 sa.sa_sigaction = sigfpe_int_action; 353 sigemptyset(&sa.sa_mask); 354 sigaction(SIGFPE, &sa, NULL); 355 #ifndef __vax__ 356 fpsetmask(FP_X_INV|FP_X_DZ|FP_X_OFL|FP_X_UFL|FP_X_IMP); 357 #endif 358 printf("%ld\n", 1 / l); 359 } 360 if (intdiv_signalled == 0) 361 atf_tc_fail("FPE signal handler was not invoked"); 362 } 363 364 static void 365 sigsegv_action(int signo, siginfo_t *info, void *ptr) 366 { 367 368 sig_debug(signo, info, (ucontext_t *)ptr); 369 370 ATF_REQUIRE_EQ(info->si_signo, SIGSEGV); 371 ATF_REQUIRE_EQ(info->si_errno, 0); 372 ATF_REQUIRE_EQ(info->si_code, SEGV_MAPERR); 373 ATF_REQUIRE_EQ(info->si_addr, (void *)0); 374 375 atf_tc_pass(); 376 /* NOTREACHED */ 377 } 378 379 ATF_TC(sigsegv); 380 ATF_TC_HEAD(sigsegv, tc) 381 { 382 383 atf_tc_set_md_var(tc, "descr", 384 "Checks that signal trampoline correctly calls SIGSEGV handler"); 385 } 386 387 ATF_TC_BODY(sigsegv, tc) 388 { 389 struct sigaction sa; 390 391 sa.sa_flags = SA_SIGINFO; 392 sa.sa_sigaction = sigsegv_action; 393 sigemptyset(&sa.sa_mask); 394 sigaction(SIGSEGV, &sa, NULL); 395 396 *(long *)0 = 0; 397 atf_tc_fail("Test did not fault as expected"); 398 } 399 400 ATF_TP_ADD_TCS(tp) 401 { 402 403 ATF_TP_ADD_TC(tp, sigalarm); 404 ATF_TP_ADD_TC(tp, sigchild_normal); 405 ATF_TP_ADD_TC(tp, sigchild_dump); 406 ATF_TP_ADD_TC(tp, sigchild_kill); 407 ATF_TP_ADD_TC(tp, sigfpe_flt); 408 ATF_TP_ADD_TC(tp, sigfpe_int); 409 ATF_TP_ADD_TC(tp, sigsegv); 410 411 return atf_no_error(); 412 } 413