memtrap: make installation of SIGBUS handler explicit to ease integration into libraries
[profile/ivi/pulseaudio-panda.git] / 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     sigsafe_error("Failed to handle SIGBUS.\n");
106     pa_atomic_dec(&read_lock);
107     abort();
108 }
109
110 static void memtrap_swap(unsigned n) {
111
112     for (;;) {
113
114         /* If the read counter is > 0 wait; if it is 0 try to swap the lists */
115         if (COUNTER(n) > 0)
116             pa_semaphore_wait(pa_static_semaphore_get(&semaphore, 0));
117         else if (pa_atomic_cmpxchg(&read_lock, (int) n, (int) (n ^ MSB)))
118             break;
119
120         n = (unsigned) pa_atomic_load(&read_lock);
121     }
122 }
123
124 static void memtrap_link(pa_memtrap *m, unsigned j) {
125     pa_assert(m);
126
127     m->prev[j] = NULL;
128     m->next[j] = memtraps[j];
129     memtraps[j] = m;
130 }
131
132 static void memtrap_unlink(pa_memtrap *m, int j) {
133     pa_assert(m);
134
135     if (m->next[j])
136         m->next[j]->prev[j] = m->prev[j];
137
138     if (m->prev[j])
139         m->prev[j]->next[j] = m->next[j];
140     else
141         memtraps[j] = m->next[j];
142 }
143
144 pa_memtrap* pa_memtrap_add(const void *start, size_t size) {
145     pa_memtrap *m = NULL;
146     pa_mutex *lock;
147     unsigned n, j;
148
149     pa_assert(start);
150     pa_assert(size > 0);
151     pa_assert(PA_PAGE_ALIGN_PTR(start) == start);
152     pa_assert(PA_PAGE_ALIGN(size) == size);
153
154     lock = pa_static_mutex_get(&write_lock, FALSE, FALSE);
155     pa_mutex_lock(lock);
156
157     n = (unsigned) pa_atomic_load(&read_lock);
158     j = WHICH(n);
159
160     m = pa_xnew(pa_memtrap, 1);
161     m->start = (void*) start;
162     m->size = size;
163     pa_atomic_store(&m->bad, 0);
164
165     memtrap_link(m, !j);
166     memtrap_swap(n);
167     memtrap_link(m, j);
168
169     pa_mutex_unlock(lock);
170
171     return m;
172 }
173
174 void pa_memtrap_remove(pa_memtrap *m) {
175     unsigned n, j;
176     pa_mutex *lock;
177
178     pa_assert(m);
179
180     lock = pa_static_mutex_get(&write_lock, FALSE, FALSE);
181     pa_mutex_lock(lock);
182
183     n = (unsigned) pa_atomic_load(&read_lock);
184     j = WHICH(n);
185
186     memtrap_unlink(m, !j);
187     memtrap_swap(n);
188     memtrap_unlink(m, j);
189
190     pa_xfree(m);
191
192     pa_mutex_unlock(lock);
193 }
194
195 pa_memtrap *pa_memtrap_update(pa_memtrap *m, const void *start, size_t size) {
196     unsigned n, j;
197     pa_mutex *lock;
198
199     pa_assert(m);
200
201     pa_assert(start);
202     pa_assert(size > 0);
203     pa_assert(PA_PAGE_ALIGN_PTR(start) == start);
204     pa_assert(PA_PAGE_ALIGN(size) == size);
205
206     lock = pa_static_mutex_get(&write_lock, FALSE, FALSE);
207     pa_mutex_lock(lock);
208
209     if (m->start == start && m->size == size)
210         goto unlock;
211
212     n = (unsigned) pa_atomic_load(&read_lock);
213     j = WHICH(n);
214
215     memtrap_unlink(m, !j);
216     memtrap_swap(n);
217     memtrap_unlink(m, j);
218
219     m->start = (void*) start;
220     m->size = size;
221     pa_atomic_store(&m->bad, 0);
222
223     n = (unsigned) pa_atomic_load(&read_lock);
224     j = WHICH(n);
225
226     memtrap_link(m, !j);
227     memtrap_swap(n);
228     memtrap_link(m, j);
229
230 unlock:
231     pa_mutex_unlock(lock);
232
233     return m;
234 }
235
236 void pa_memtrap_install(void) {
237     struct sigaction sa;
238
239     /* Before we install the signal handler, make sure the semaphore
240      * is valid so that the initialization of the semaphore
241      * doesn't have to happen from the signal handler */
242     pa_static_semaphore_get(&semaphore, 0);
243
244     memset(&sa, 0, sizeof(sa));
245     sa.sa_sigaction = signal_handler;
246     sa.sa_flags = SA_RESTART|SA_SIGINFO;
247
248     pa_assert_se(sigaction(SIGBUS, &sa, NULL) == 0);
249 }