]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/sigbus.c
test: fix the default timeout values described in README.testsuite
[thirdparty/systemd.git] / src / basic / sigbus.c
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 "missing_syscall.h"
11 #include "process-util.h"
12 #include "sigbus.h"
13
14 #define SIGBUS_QUEUE_MAX 64
15
16 static struct sigaction old_sigaction;
17 static unsigned n_installed = 0;
18
19 /* We maintain a fixed size list of page addresses that triggered a
20 SIGBUS. We access with list with atomic operations, so that we
21 don't have to deal with locks between signal handler and main
22 programs in possibly multiple threads. */
23
24 static void* volatile sigbus_queue[SIGBUS_QUEUE_MAX];
25 static volatile sig_atomic_t n_sigbus_queue = 0;
26
27 static void sigbus_push(void *addr) {
28 assert(addr);
29
30 /* Find a free place, increase the number of entries and leave, if we can */
31 for (size_t u = 0; u < SIGBUS_QUEUE_MAX; u++) {
32 /* OK to initialize this here since we haven't started the atomic ops yet */
33 void *tmp = NULL;
34 if (__atomic_compare_exchange_n(&sigbus_queue[u], &tmp, addr, false,
35 __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) {
36 __atomic_fetch_add(&n_sigbus_queue, 1, __ATOMIC_SEQ_CST);
37 return;
38 }
39 }
40
41 /* If we can't, make sure the queue size is out of bounds, to
42 * mark it as overflow */
43 for (;;) {
44 sig_atomic_t c;
45
46 __atomic_thread_fence(__ATOMIC_SEQ_CST);
47 c = n_sigbus_queue;
48
49 if (c > SIGBUS_QUEUE_MAX) /* already overflow */
50 return;
51
52 /* OK if we clobber c here, since we either immediately return
53 * or it will be immediately reinitialized on next loop */
54 if (__atomic_compare_exchange_n(&n_sigbus_queue, &c, c + SIGBUS_QUEUE_MAX, false,
55 __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST))
56 return;
57 }
58 }
59
60 int sigbus_pop(void **ret) {
61 assert(ret);
62
63 for (;;) {
64 unsigned u, c;
65
66 __atomic_thread_fence(__ATOMIC_SEQ_CST);
67 c = n_sigbus_queue;
68
69 if (_likely_(c == 0))
70 return 0;
71
72 if (_unlikely_(c >= SIGBUS_QUEUE_MAX))
73 return -EOVERFLOW;
74
75 for (u = 0; u < SIGBUS_QUEUE_MAX; u++) {
76 void *addr;
77
78 addr = sigbus_queue[u];
79 if (!addr)
80 continue;
81
82 /* OK if we clobber addr here, since we either immediately return
83 * or it will be immediately reinitialized on next loop */
84 if (__atomic_compare_exchange_n(&sigbus_queue[u], &addr, NULL, false,
85 __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) {
86 __atomic_fetch_sub(&n_sigbus_queue, 1, __ATOMIC_SEQ_CST);
87 /* If we successfully entered this if condition, addr won't
88 * have been modified since its assignment, so safe to use it */
89 *ret = addr;
90 return 1;
91 }
92 }
93 }
94 }
95
96 static void sigbus_handler(int sn, siginfo_t *si, void *data) {
97 unsigned long ul;
98 void *aligned;
99
100 assert(sn == SIGBUS);
101 assert(si);
102
103 if (si->si_code != BUS_ADRERR || !si->si_addr) {
104 assert_se(sigaction(SIGBUS, &old_sigaction, NULL) == 0);
105 rt_sigqueueinfo(getpid_cached(), SIGBUS, si);
106 return;
107 }
108
109 ul = (unsigned long) si->si_addr;
110 ul = ul / page_size();
111 ul = ul * page_size();
112 aligned = (void*) ul;
113
114 /* Let's remember which address failed */
115 sigbus_push(aligned);
116
117 /* Replace mapping with an anonymous page, so that the
118 * execution can continue, however with a zeroed out page */
119 assert_se(mmap(aligned, page_size(), PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED, -1, 0) == aligned);
120 }
121
122 void sigbus_install(void) {
123 struct sigaction sa = {
124 .sa_sigaction = sigbus_handler,
125 .sa_flags = SA_SIGINFO,
126 };
127
128 /* make sure that sysconf() is not called from a signal handler because
129 * it is not guaranteed to be async-signal-safe since POSIX.1-2008 */
130 (void) page_size();
131
132 n_installed++;
133
134 if (n_installed == 1)
135 assert_se(sigaction(SIGBUS, &sa, &old_sigaction) == 0);
136
137 return;
138 }
139
140 void sigbus_reset(void) {
141
142 if (n_installed <= 0)
143 return;
144
145 n_installed--;
146
147 if (n_installed == 0)
148 assert_se(sigaction(SIGBUS, &old_sigaction, NULL) == 0);
149
150 return;
151 }