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