2 Check that a fault signal handler gets the expected info
9 #include "tests/sys_mman.h"
12 /* Division by zero triggers a SIGFPE on x86 and x86_64,
13 but not on the PowerPC architecture.
15 On ARM-Linux, we do get a SIGFPE, but not from the faulting of a
16 division instruction (there isn't any such thing) but rather
17 because the process exits via tgkill, sending itself a SIGFPE.
18 Hence we get a SIGFPE but the SI_CODE is different from that on
21 #if defined(__powerpc__) || defined(__aarch64__)
22 # define DIVISION_BY_ZERO_TRIGGERS_FPE 0
23 # define DIVISION_BY_ZERO_SI_CODE SI_TKILL
24 #elif defined(__arm__)
25 # define DIVISION_BY_ZERO_TRIGGERS_FPE 1
26 # define DIVISION_BY_ZERO_SI_CODE SI_TKILL
28 # define DIVISION_BY_ZERO_TRIGGERS_FPE 1
29 # define DIVISION_BY_ZERO_SI_CODE FPE_INTDIV
32 /* Accessing non-mapped virtual address results in SIGBUS
33 * with si_code equal to BUS_ADRERR on Linux, whereas in SIGBUS
34 * with si_code equal to BUS_OBJERR on Solaris. On Solaris,
35 * BUS_ADRERR is used for bus time out while BUS_OBJERR is translated
36 * from underlying codes FC_OBJERR (x86) or ASYNC_BERR (sparc).
38 #if defined(VGO_solaris)
39 # define BUS_ERROR_SI_CODE BUS_OBJERR
41 # define BUS_ERROR_SI_CODE BUS_ADRERR
51 static const struct test
*cur_test
;
55 static sigjmp_buf escape
;
57 #define BADADDR ((int *)0x1234)
59 #define FILESIZE (4*__pagesize)
60 #define MAPSIZE (2*FILESIZE)
61 static unsigned int __pagesize
;
62 static char volatile *volatile mapping
;
64 static int testsig(int sig
, int want
)
67 fprintf(stderr
, " FAIL: expected signal %d, not %d\n", want
, sig
);
73 static int testcode(int code
, int want
)
76 fprintf(stderr
, " FAIL: expected si_code==%d, not %d\n", want
, code
);
82 static int testaddr(void *addr
, volatile void *want
)
84 /* Some architectures (e.g. s390) just provide enough information to
85 resolve the page fault, but do not provide the offset within a page */
87 if (addr
!= (void *) ((unsigned long) want
& ~0xffful
)) {
91 fprintf(stderr
, " FAIL: expected si_addr==%p, not %p\n", want
, addr
);
98 static void handler(int sig
, siginfo_t
*si
, void *uc
)
102 ok
= ok
&& testsig(sig
, cur_test
->sig
);
103 ok
= ok
&& testcode(si
->si_code
, cur_test
->code
);
105 ok
= ok
&& testaddr(si
->si_addr
, cur_test
->addr
);
108 fprintf(stderr
, " PASS\n");
110 siglongjmp(escape
, ok
+ 1);
114 static void test1(void)
126 mapping
[FILESIZE
+10];
131 volatile int v
= 44/zero();
134 #if DIVISION_BY_ZERO_TRIGGERS_FPE == 0
142 static const int sigs
[] = { SIGSEGV
, SIGILL
, SIGBUS
, SIGFPE
, SIGTRAP
};
144 __pagesize
= (unsigned int)sysconf(_SC_PAGE_SIZE
);
145 sa
.sa_sigaction
= handler
;
146 sa
.sa_flags
= SA_SIGINFO
;
147 sigfillset(&sa
.sa_mask
);
149 for(i
= 0; i
< sizeof(sigs
)/sizeof(*sigs
); i
++)
150 sigaction(sigs
[i
], &sa
, NULL
);
152 /* we need O_RDWR for the truncate below */
153 fd
= open("faultstatus.tmp", O_CREAT
|O_TRUNC
|O_EXCL
|O_RDWR
, 0600);
158 unlink("faultstatus.tmp");
159 ftruncate(fd
, FILESIZE
);
161 mapping
= mmap(0, MAPSIZE
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
165 const struct test tests
[] = {
166 #define T(n, sig, code, addr) { test##n, sig, code, addr }
167 T(1, SIGSEGV
, SEGV_MAPERR
, BADADDR
),
168 T(2, SIGSEGV
, SEGV_ACCERR
, mapping
),
169 T(3, SIGBUS
, BUS_ERROR_SI_CODE
, &mapping
[FILESIZE
+10]),
170 T(4, SIGFPE
, DIVISION_BY_ZERO_SI_CODE
, 0),
174 for(i
= 0; i
< sizeof(tests
)/sizeof(*tests
); i
++) {
175 cur_test
= &tests
[i
];
177 if (sigsetjmp(escape
, 1) == 0) {
178 fprintf(stderr
, "Test %d: ", i
+1);
180 fprintf(stderr
, " FAIL: no fault, or handler returned\n");