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