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