1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <signal.h>
5 #include <stddef.h>
6 #include <sys/mman.h>
7
8 #include "macro.h"
9 #include "memory-util.h"
10 #include "sigbus.h"
11
12 #define SIGBUS_QUEUE_MAX 64
13
14 static struct sigaction old_sigaction;
15 static unsigned n_installed = 0;
16
17 /* We maintain a fixed size list of page addresses that triggered a
18 SIGBUS. We access with list with atomic operations, so that we
19 don't have to deal with locks between signal handler and main
20 programs in possibly multiple threads. */
21
22 static void* volatile sigbus_queue[SIGBUS_QUEUE_MAX];
23 static volatile sig_atomic_t n_sigbus_queue = 0;
24
sigbus_push(void * addr)25 static void sigbus_push(void *addr) {
26 assert(addr);
27
28 /* Find a free place, increase the number of entries and leave, if we can */
29 for (size_t u = 0; u < SIGBUS_QUEUE_MAX; u++)
30 if (__sync_bool_compare_and_swap(&sigbus_queue[u], NULL, addr)) {
31 __sync_fetch_and_add(&n_sigbus_queue, 1);
32 return;
33 }
34
35 /* If we can't, make sure the queue size is out of bounds, to
36 * mark it as overflow */
37 for (;;) {
38 unsigned c;
39
40 __sync_synchronize();
41 c = n_sigbus_queue;
42
43 if (c > SIGBUS_QUEUE_MAX) /* already overflow */
44 return;
45
46 if (__sync_bool_compare_and_swap(&n_sigbus_queue, c, c + SIGBUS_QUEUE_MAX))
47 return;
48 }
49 }
50
sigbus_pop(void ** ret)51 int sigbus_pop(void **ret) {
52 assert(ret);
53
54 for (;;) {
55 unsigned u, c;
56
57 __sync_synchronize();
58 c = n_sigbus_queue;
59
60 if (_likely_(c == 0))
61 return 0;
62
63 if (_unlikely_(c >= SIGBUS_QUEUE_MAX))
64 return -EOVERFLOW;
65
66 for (u = 0; u < SIGBUS_QUEUE_MAX; u++) {
67 void *addr;
68
69 addr = sigbus_queue[u];
70 if (!addr)
71 continue;
72
73 if (__sync_bool_compare_and_swap(&sigbus_queue[u], addr, NULL)) {
74 __sync_fetch_and_sub(&n_sigbus_queue, 1);
75 *ret = addr;
76 return 1;
77 }
78 }
79 }
80 }
81
sigbus_handler(int sn,siginfo_t * si,void * data)82 static void sigbus_handler(int sn, siginfo_t *si, void *data) {
83 unsigned long ul;
84 void *aligned;
85
86 assert(sn == SIGBUS);
87 assert(si);
88
89 if (si->si_code != BUS_ADRERR || !si->si_addr) {
90 assert_se(sigaction(SIGBUS, &old_sigaction, NULL) == 0);
91 raise(SIGBUS);
92 return;
93 }
94
95 ul = (unsigned long) si->si_addr;
96 ul = ul / page_size();
97 ul = ul * page_size();
98 aligned = (void*) ul;
99
100 /* Let's remember which address failed */
101 sigbus_push(aligned);
102
103 /* Replace mapping with an anonymous page, so that the
104 * execution can continue, however with a zeroed out page */
105 assert_se(mmap(aligned, page_size(), PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED, -1, 0) == aligned);
106 }
107
sigbus_install(void)108 void sigbus_install(void) {
109 struct sigaction sa = {
110 .sa_sigaction = sigbus_handler,
111 .sa_flags = SA_SIGINFO,
112 };
113
114 /* make sure that sysconf() is not called from a signal handler because
115 * it is not guaranteed to be async-signal-safe since POSIX.1-2008 */
116 (void) page_size();
117
118 n_installed++;
119
120 if (n_installed == 1)
121 assert_se(sigaction(SIGBUS, &sa, &old_sigaction) == 0);
122
123 return;
124 }
125
sigbus_reset(void)126 void sigbus_reset(void) {
127
128 if (n_installed <= 0)
129 return;
130
131 n_installed--;
132
133 if (n_installed == 0)
134 assert_se(sigaction(SIGBUS, &old_sigaction, NULL) == 0);
135
136 return;
137 }
138