Home | History | Annotate | Line # | Download | only in gen
t_siginfo.c revision 1.6
      1 /* $NetBSD: t_siginfo.c,v 1.6 2011/01/02 21:34:00 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 (sigsetjmp(sigfpe_flt_env, 0) == 0) {
    300 		sa.sa_flags = SA_SIGINFO;
    301 		sa.sa_sigaction = sigfpe_flt_action;
    302 		sigemptyset(&sa.sa_mask);
    303 		sigaction(SIGFPE, &sa, NULL);
    304 #ifndef __vax__
    305 		fpsetmask(FP_X_INV|FP_X_DZ|FP_X_OFL|FP_X_UFL|FP_X_IMP);
    306 #endif
    307 		printf("%g\n", 1 / d);
    308 	}
    309 	if (fltdiv_signalled == 0)
    310 		atf_tc_fail("FPE signal handler was not invoked");
    311 }
    312 
    313 static sigjmp_buf sigfpe_int_env;
    314 static void
    315 sigfpe_int_action(int signo, siginfo_t *info, void *ptr)
    316 {
    317 
    318 	sig_debug(signo, info, (ucontext_t *)ptr);
    319 
    320 	if (intdiv_signalled++ != 0)
    321 		atf_tc_fail("INTDIV handler called more than once");
    322 
    323 	ATF_REQUIRE_EQ(info->si_signo, SIGFPE);
    324 	if (info->si_code == FPE_FLTDIV)
    325 		atf_tc_expect_fail("PR port-i386/43655 : integer div-by-zero "
    326 		    "reports FPE_FLTDIV instead of FPE_INTDIV");
    327 	ATF_REQUIRE_EQ(info->si_code, FPE_INTDIV);
    328 	ATF_REQUIRE_EQ(info->si_errno, 0);
    329 
    330 	siglongjmp(sigfpe_int_env, 1);
    331 }
    332 
    333 ATF_TC(sigfpe_int);
    334 ATF_TC_HEAD(sigfpe_int, tc)
    335 {
    336 
    337 	atf_tc_set_md_var(tc, "descr",
    338 	    "Checks that signal trampoline correctly calls SIGFPE handler "
    339 	    "for integer div-by-zero");
    340 }
    341 
    342 ATF_TC_BODY(sigfpe_int, tc)
    343 {
    344 	struct sigaction sa;
    345 	long l = strtol("0", NULL, 10);
    346 
    347 	if (sigsetjmp(sigfpe_int_env, 0) == 0) {
    348 		sa.sa_flags = SA_SIGINFO;
    349 		sa.sa_sigaction = sigfpe_int_action;
    350 		sigemptyset(&sa.sa_mask);
    351 		sigaction(SIGFPE, &sa, NULL);
    352 #ifndef __vax__
    353 		fpsetmask(FP_X_INV|FP_X_DZ|FP_X_OFL|FP_X_UFL|FP_X_IMP);
    354 #endif
    355 		printf("%ld\n", 1 / l);
    356 	}
    357 	if (intdiv_signalled == 0)
    358 		atf_tc_fail("FPE signal handler was not invoked");
    359 }
    360 
    361 static void
    362 sigsegv_action(int signo, siginfo_t *info, void *ptr)
    363 {
    364 
    365 	sig_debug(signo, info, (ucontext_t *)ptr);
    366 
    367 	ATF_REQUIRE_EQ(info->si_signo, SIGSEGV);
    368 	ATF_REQUIRE_EQ(info->si_errno, 0);
    369 	ATF_REQUIRE_EQ(info->si_code, SEGV_MAPERR);
    370 	ATF_REQUIRE_EQ(info->si_addr, (void *)0);
    371 
    372 	atf_tc_pass();
    373 	/* NOTREACHED */
    374 }
    375 
    376 ATF_TC(sigsegv);
    377 ATF_TC_HEAD(sigsegv, tc)
    378 {
    379 
    380 	atf_tc_set_md_var(tc, "descr",
    381 	    "Checks that signal trampoline correctly calls SIGSEGV handler");
    382 }
    383 
    384 ATF_TC_BODY(sigsegv, tc)
    385 {
    386 	struct sigaction sa;
    387 
    388 	sa.sa_flags = SA_SIGINFO;
    389 	sa.sa_sigaction = sigsegv_action;
    390 	sigemptyset(&sa.sa_mask);
    391 	sigaction(SIGSEGV, &sa, NULL);
    392 
    393 	*(long *)0 = 0;
    394 	atf_tc_fail("Test did not fault as expected");
    395 }
    396 
    397 ATF_TP_ADD_TCS(tp)
    398 {
    399 
    400 	ATF_TP_ADD_TC(tp, sigalarm);
    401 	ATF_TP_ADD_TC(tp, sigchild_normal);
    402 	ATF_TP_ADD_TC(tp, sigchild_dump);
    403 	ATF_TP_ADD_TC(tp, sigchild_kill);
    404 	ATF_TP_ADD_TC(tp, sigfpe_flt);
    405 	ATF_TP_ADD_TC(tp, sigfpe_int);
    406 	ATF_TP_ADD_TC(tp, sigsegv);
    407 
    408 	return atf_no_error();
    409 }
    410