2 Check that a fault signal handler gets the expected info
9 #include "tests/sys_mman.h"
11 #include "../../config.h"
13 /* Division by zero triggers a SIGFPE on x86 and x86_64,
14 but not on the PowerPC architecture.
16 On ARM-Linux, we do get a SIGFPE, but not from the faulting of a
17 division instruction (there isn't any such thing) but rather
18 because the process exits via tgkill, sending itself a SIGFPE.
19 Hence we get a SIGFPE but the SI_CODE is different from that on
22 #if defined(__powerpc__) || defined(__aarch64__)
23 # define DIVISION_BY_ZERO_TRIGGERS_FPE 0
24 # define DIVISION_BY_ZERO_SI_CODE SI_TKILL
25 #elif defined(__arm__)
26 # define DIVISION_BY_ZERO_TRIGGERS_FPE 1
27 # define DIVISION_BY_ZERO_SI_CODE SI_TKILL
29 # define DIVISION_BY_ZERO_TRIGGERS_FPE 1
30 # define DIVISION_BY_ZERO_SI_CODE FPE_INTDIV
33 /* Accessing non-mapped virtual address results in SIGBUS
34 * with si_code equal to BUS_ADRERR on Linux, whereas in SIGBUS
35 * with si_code equal to BUS_OBJERR on Solaris. On Solaris,
36 * BUS_ADRERR is used for bus time out while BUS_OBJERR is translated
37 * from underlying codes FC_OBJERR (x86) or ASYNC_BERR (sparc).
39 #if defined(VGO_solaris) || (defined(VGO_freebsd) && (FREEBSD_VERS >= FREEBSD_12_2))
40 # define BUS_ERROR_SI_CODE BUS_OBJERR
42 # define BUS_ERROR_SI_CODE BUS_ADRERR
52 static const struct test
*cur_test
;
56 static sigjmp_buf escape
;
58 #define BADADDR ((int *)0x1234)
60 #define FILESIZE (4*__pagesize)
61 #define MAPSIZE (2*FILESIZE)
62 static unsigned int __pagesize
;
63 static char volatile *volatile mapping
;
65 static int testsig(int sig
, int want
)
68 fprintf(stderr
, " FAIL: expected signal %d, not %d\n", want
, sig
);
74 static int testcode(int code
, int want
)
77 fprintf(stderr
, " FAIL: expected si_code==%d, not %d\n", want
, code
);
83 static int testaddr(void *addr
, volatile void *want
)
85 /* Some architectures (e.g. s390) just provide enough information to
86 resolve the page fault, but do not provide the offset within a page */
88 if (addr
!= (void *) ((unsigned long) want
& ~0xffful
)) {
92 fprintf(stderr
, " FAIL: expected si_addr==%p, not %p\n", want
, addr
);
99 static void handler(int sig
, siginfo_t
*si
, void *uc
)
103 ok
= ok
&& testsig(sig
, cur_test
->sig
);
104 ok
= ok
&& testcode(si
->si_code
, cur_test
->code
);
106 ok
= ok
&& testaddr(si
->si_addr
, cur_test
->addr
);
109 fprintf(stderr
, " PASS\n");
111 siglongjmp(escape
, ok
+ 1);
115 static void test1(void)
127 mapping
[FILESIZE
+10];
132 volatile int v
= 44/zero();
135 #if DIVISION_BY_ZERO_TRIGGERS_FPE == 0
143 static const int sigs
[] = { SIGSEGV
, SIGILL
, SIGBUS
, SIGFPE
, SIGTRAP
};
145 __pagesize
= (unsigned int)sysconf(_SC_PAGE_SIZE
);
146 sa
.sa_sigaction
= handler
;
147 sa
.sa_flags
= SA_SIGINFO
;
148 sigfillset(&sa
.sa_mask
);
150 for(i
= 0; i
< sizeof(sigs
)/sizeof(*sigs
); i
++)
151 sigaction(sigs
[i
], &sa
, NULL
);
153 /* we need O_RDWR for the truncate below */
154 fd
= open("faultstatus.tmp", O_CREAT
|O_TRUNC
|O_EXCL
|O_RDWR
, 0600);
159 unlink("faultstatus.tmp");
160 ftruncate(fd
, FILESIZE
);
162 mapping
= mmap(0, MAPSIZE
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
166 const struct test tests
[] = {
167 #define T(n, sig, code, addr) { test##n, sig, code, addr }
168 T(1, SIGSEGV
, SEGV_MAPERR
, BADADDR
),
169 T(2, SIGSEGV
, SEGV_ACCERR
, mapping
),
170 #if defined(VGO_freebsd) && (FREEBSD_VERS < FREEBSD_12_2)
171 T(3, SIGSEGV
, BUS_ERROR_SI_CODE
, &mapping
[FILESIZE
+10]),
173 T(3, SIGBUS
, BUS_ERROR_SI_CODE
, &mapping
[FILESIZE
+10]),
175 T(4, SIGFPE
, DIVISION_BY_ZERO_SI_CODE
, 0),
179 for(i
= 0; i
< sizeof(tests
)/sizeof(*tests
); i
++) {
180 cur_test
= &tests
[i
];
182 if (sigsetjmp(escape
, 1) == 0) {
183 fprintf(stderr
, "Test %d: ", i
+1);
185 fprintf(stderr
, " FAIL: no fault, or handler returned\n");
193 static volatile int s_zero
;