]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/sigbus.c
2416929e369cd4905a6cc246812639cf50c1b566
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2014 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
30 #define SIGBUS_QUEUE_MAX 64
32 static struct sigaction old_sigaction
;
33 static unsigned n_installed
= 0;
35 /* We maintain a fixed size list of page addresses that triggered a
36 SIGBUS. We access with list with atomic operations, so that we
37 don't have to deal with locks between signal handler and main
38 programs in possibly multiple threads. */
40 static void* volatile sigbus_queue
[SIGBUS_QUEUE_MAX
];
41 static volatile sig_atomic_t n_sigbus_queue
= 0;
43 static void sigbus_push(void *addr
) {
48 /* Find a free place, increase the number of entries and leave, if we can */
49 for (u
= 0; u
< SIGBUS_QUEUE_MAX
; u
++)
50 if (__sync_bool_compare_and_swap(&sigbus_queue
[u
], NULL
, addr
)) {
51 __sync_fetch_and_add(&n_sigbus_queue
, 1);
55 /* If we can't, make sure the queue size is out of bounds, to
56 * mark it as overflow */
63 if (c
> SIGBUS_QUEUE_MAX
) /* already overflow */
66 if (__sync_bool_compare_and_swap(&n_sigbus_queue
, c
, c
+ SIGBUS_QUEUE_MAX
))
71 int sigbus_pop(void **ret
) {
83 if (_unlikely_(c
>= SIGBUS_QUEUE_MAX
))
86 for (u
= 0; u
< SIGBUS_QUEUE_MAX
; u
++) {
89 addr
= sigbus_queue
[u
];
93 if (__sync_bool_compare_and_swap(&sigbus_queue
[u
], addr
, NULL
)) {
94 __sync_fetch_and_sub(&n_sigbus_queue
, 1);
102 static void sigbus_handler(int sn
, siginfo_t
*si
, void *data
) {
106 assert(sn
== SIGBUS
);
109 if (si
->si_code
!= BUS_ADRERR
|| !si
->si_addr
) {
110 assert_se(sigaction(SIGBUS
, &old_sigaction
, NULL
) == 0);
115 ul
= (unsigned long) si
->si_addr
;
116 ul
= ul
/ page_size();
117 ul
= ul
* page_size();
118 aligned
= (void*) ul
;
120 /* Let's remember which address failed */
121 sigbus_push(aligned
);
123 /* Replace mapping with an anonymous page, so that the
124 * execution can continue, however with a zeroed out page */
125 assert_se(mmap(aligned
, page_size(), PROT_READ
|PROT_WRITE
, MAP_PRIVATE
|MAP_ANONYMOUS
|MAP_FIXED
, -1, 0) == aligned
);
128 void sigbus_install(void) {
129 struct sigaction sa
= {
130 .sa_sigaction
= sigbus_handler
,
131 .sa_flags
= SA_SIGINFO
,
136 if (n_installed
== 1)
137 assert_se(sigaction(SIGBUS
, &sa
, &old_sigaction
) == 0);
142 void sigbus_reset(void) {
144 if (n_installed
<= 0)
149 if (n_installed
== 0)
150 assert_se(sigaction(SIGBUS
, &old_sigaction
, NULL
) == 0);