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