]> code.delx.au - pulseaudio/blob - src/pulsecore/memtrap.c
memtrap: add new logic to trap and handle SIGBUS
[pulseaudio] / src / pulsecore / memtrap.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2009 Lennart Poettering
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <signal.h>
27 #include <sys/mman.h>
28
29 #include <pulse/xmalloc.h>
30
31 #include <pulsecore/semaphore.h>
32 #include <pulsecore/macro.h>
33 #include <pulsecore/mutex.h>
34 #include <pulsecore/core-util.h>
35
36 #include "memtrap.h"
37
38 struct pa_memtrap {
39 void *start;
40 size_t size;
41 pa_atomic_t bad;
42 pa_memtrap *next[2], *prev[2];
43 };
44
45 static pa_memtrap *memtraps[2] = { NULL, NULL };
46 static pa_atomic_t read_lock = PA_ATOMIC_INIT(0);
47 static pa_static_semaphore semaphore = PA_STATIC_SEMAPHORE_INIT;
48 static pa_static_mutex write_lock = PA_STATIC_MUTEX_INIT;
49
50 #define MSB (1U << (sizeof(unsigned)*8U-1))
51 #define WHICH(n) (!!((n) & MSB))
52 #define COUNTER(n) ((n) & ~MSB)
53
54 pa_bool_t pa_memtrap_is_good(pa_memtrap *m) {
55 pa_assert(m);
56
57 return !pa_atomic_load(&m->bad);
58 }
59
60 static void sigsafe_error(const char *s) {
61 write(STDERR_FILENO, s, strlen(s));
62 }
63
64 static void signal_handler(int sig, siginfo_t* si, void *data) {
65 unsigned n, j;
66 pa_memtrap *m;
67 void *r;
68
69 /* Increase the lock counter */
70 n = (unsigned) pa_atomic_inc(&read_lock);
71
72 /* The uppermost bit tells us which list to look at */
73 j = WHICH(n);
74
75 /* When n is 0 we have about 2^31 threads running that
76 * all got a sigbus at the same time, oh my! */
77 pa_assert(COUNTER(n)+1 > 0);
78
79 for (m = memtraps[j]; m; m = m->next[j])
80 if (si->si_addr >= m->start &&
81 (uint8_t*) si->si_addr < (uint8_t*) m->start + m->size)
82 break;
83
84 if (!m)
85 goto fail;
86
87 pa_atomic_store(&m->bad, 1);
88
89 /* Remap anonymous memory into the bad segment */
90 if ((r = mmap(m->start, m->size, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_FIXED|MAP_PRIVATE, -1, 0)) == MAP_FAILED) {
91 sigsafe_error("mmap() failed.\n");
92 goto fail;
93 }
94
95 pa_assert(r == m->start);
96
97 pa_atomic_dec(&read_lock);
98
99 /* Post the semaphore */
100 pa_semaphore_post(pa_static_semaphore_get(&semaphore, 0));
101
102 return;
103
104 fail:
105 pa_atomic_dec(&read_lock);
106 abort();
107 }
108
109 static void memtrap_swap(unsigned n) {
110
111 for (;;) {
112
113 /* If the read counter is > 0 wait; if it is 0 try to swap the lists */
114 if (COUNTER(n) > 0)
115 pa_semaphore_wait(pa_static_semaphore_get(&semaphore, 0));
116 else if (pa_atomic_cmpxchg(&read_lock, (int) n, (int) (n ^ MSB)))
117 break;
118
119 n = (unsigned) pa_atomic_load(&read_lock);
120 }
121 }
122
123 static void memtrap_link(pa_memtrap *m, unsigned j) {
124 pa_assert(m);
125
126 m->prev[j] = NULL;
127 m->next[j] = memtraps[j];
128 memtraps[j] = m;
129 }
130
131 static void memtrap_unlink(pa_memtrap *m, int j) {
132 pa_assert(m);
133
134 if (m->next[j])
135 m->next[j]->prev[j] = m->prev[j];
136
137 if (m->prev[j])
138 m->prev[j]->next[j] = m->next[j];
139 else
140 memtraps[j] = m->next[j];
141 }
142
143 pa_memtrap* pa_memtrap_add(const void *start, size_t size) {
144 pa_memtrap *m = NULL;
145 pa_mutex *lock;
146 unsigned n, j;
147
148 pa_assert(start);
149 pa_assert(size > 0);
150 pa_assert(PA_PAGE_ALIGN_PTR(start) == start);
151 pa_assert(PA_PAGE_ALIGN(size) == size);
152
153 lock = pa_static_mutex_get(&write_lock, FALSE, FALSE);
154 pa_mutex_lock(lock);
155
156 if (!memtraps[0]) {
157 struct sigaction sa;
158
159 /* Before we install the signal handler, make sure the
160 * semaphore is valid so that the initialization of the
161 * semaphore doesn't have to happen from the signal handler */
162 pa_static_semaphore_get(&semaphore, 0);
163
164 memset(&sa, 0, sizeof(sa));
165 sa.sa_sigaction = signal_handler;
166 sa.sa_flags = SA_RESTART|SA_SIGINFO;
167
168 pa_assert_se(sigaction(SIGBUS, &sa, NULL) == 0);
169 }
170
171 n = (unsigned) pa_atomic_load(&read_lock);
172 j = WHICH(n);
173
174 m = pa_xnew(pa_memtrap, 1);
175 m->start = (void*) start;
176 m->size = size;
177 pa_atomic_store(&m->bad, 0);
178
179 memtrap_link(m, !j);
180 memtrap_swap(n);
181 memtrap_link(m, j);
182
183 pa_mutex_unlock(lock);
184
185 return m;
186 }
187
188 void pa_memtrap_remove(pa_memtrap *m) {
189 unsigned n, j;
190 pa_mutex *lock;
191
192 pa_assert(m);
193
194 lock = pa_static_mutex_get(&write_lock, FALSE, FALSE);
195 pa_mutex_lock(lock);
196
197 n = (unsigned) pa_atomic_load(&read_lock);
198 j = WHICH(n);
199
200 memtrap_unlink(m, !j);
201 memtrap_swap(n);
202 memtrap_unlink(m, j);
203
204 pa_xfree(m);
205
206 if (!memtraps[0]) {
207 struct sigaction sa;
208
209 memset(&sa, 0, sizeof(sa));
210 sa.sa_handler = SIG_DFL;
211 pa_assert_se(sigaction(SIGBUS, &sa, NULL) == 0);
212 }
213
214 pa_mutex_unlock(lock);
215 }
216
217 pa_memtrap *pa_memtrap_update(pa_memtrap *m, const void *start, size_t size) {
218 unsigned n, j;
219 pa_mutex *lock;
220
221 pa_assert(m);
222
223 pa_assert(start);
224 pa_assert(size > 0);
225 pa_assert(PA_PAGE_ALIGN_PTR(start) == start);
226 pa_assert(PA_PAGE_ALIGN(size) == size);
227
228 lock = pa_static_mutex_get(&write_lock, FALSE, FALSE);
229 pa_mutex_lock(lock);
230
231 if (m->start == start && m->size == size)
232 goto unlock;
233
234 n = (unsigned) pa_atomic_load(&read_lock);
235 j = WHICH(n);
236
237 memtrap_unlink(m, !j);
238 memtrap_swap(n);
239 memtrap_unlink(m, j);
240
241 m->start = (void*) start;
242 m->size = size;
243 pa_atomic_store(&m->bad, 0);
244
245 n = (unsigned) pa_atomic_load(&read_lock);
246 j = WHICH(n);
247
248 memtrap_link(m, !j);
249 memtrap_swap(n);
250 memtrap_link(m, j);
251
252 unlock:
253 pa_mutex_unlock(lock);
254
255 return m;
256 }