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