FreeBSD _umtx_op: fix bad wrapper for robust lists
[valgrind.git] / none / tests / faultstatus.c
blob92a8350ab228509282154fb9f38044652441c735
1 /*
2 Check that a fault signal handler gets the expected info
3 */
4 #include <signal.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <fcntl.h>
8 #include <setjmp.h>
9 #include "tests/sys_mman.h"
10 #include <unistd.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
20 x86/amd64-linux.
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
28 #else
29 # define DIVISION_BY_ZERO_TRIGGERS_FPE 1
30 # define DIVISION_BY_ZERO_SI_CODE FPE_INTDIV
31 #endif
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
41 #else
42 # define BUS_ERROR_SI_CODE BUS_ADRERR
43 #endif
45 struct test {
46 void (*test)(void);
47 int sig;
48 int code;
49 volatile void *addr;
52 static const struct test *cur_test;
54 static int zero();
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)
67 if (sig != want) {
68 fprintf(stderr, " FAIL: expected signal %d, not %d\n", want, sig);
69 return 0;
71 return 1;
74 static int testcode(int code, int want)
76 if (code != want) {
77 fprintf(stderr, " FAIL: expected si_code==%d, not %d\n", want, code);
78 return 0;
80 return 1;
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 */
87 #if defined(__s390__)
88 if (addr != (void *) ((unsigned long) want & ~0xffful)) {
89 #else
90 if (addr != want) {
91 #endif
92 fprintf(stderr, " FAIL: expected si_addr==%p, not %p\n", want, addr);
93 return 0;
95 return 1;
99 static void handler(int sig, siginfo_t *si, void *uc)
101 int ok = 1;
103 ok = ok && testsig(sig, cur_test->sig);
104 ok = ok && testcode(si->si_code, cur_test->code);
105 if (cur_test->addr)
106 ok = ok && testaddr(si->si_addr, cur_test->addr);
108 if (ok)
109 fprintf(stderr, " PASS\n");
111 siglongjmp(escape, ok + 1);
115 static void test1(void)
117 *BADADDR = 'x';
120 static void test2()
122 mapping[0] = 'x';
125 static void test3()
127 mapping[FILESIZE+10];
130 static void test4()
132 volatile int v = 44/zero();
134 (void)v;
135 #if DIVISION_BY_ZERO_TRIGGERS_FPE == 0
136 raise(SIGFPE);
137 #endif
140 int main()
142 int fd, i;
143 static const int sigs[] = { SIGSEGV, SIGILL, SIGBUS, SIGFPE, SIGTRAP };
144 struct sigaction sa;
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);
155 if (fd == -1) {
156 perror("tmpfile");
157 exit(1);
159 unlink("faultstatus.tmp");
160 ftruncate(fd, FILESIZE);
162 mapping = mmap(0, MAPSIZE, PROT_READ, MAP_PRIVATE, fd, 0);
163 close(fd);
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]),
172 #else
173 T(3, SIGBUS, BUS_ERROR_SI_CODE, &mapping[FILESIZE+10]),
174 #endif
175 T(4, SIGFPE, DIVISION_BY_ZERO_SI_CODE, 0),
176 #undef T
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);
184 tests[i].test();
185 fprintf(stderr, " FAIL: no fault, or handler returned\n");
190 return 0;
193 static volatile int s_zero;
195 static int zero()
197 return s_zero;