Merge commit 'origin/master-tx'
[platform/upstream/pulseaudio.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 /* This is deprecated on glibc but is still used by FreeBSD */
30 #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
31 # define MAP_ANONYMOUS MAP_ANON
32 #endif
33
34 #include <pulse/xmalloc.h>
35
36 #include <pulsecore/core-util.h>
37 #include <pulsecore/aupdate.h>
38 #include <pulsecore/atomic.h>
39 #include <pulsecore/once.h>
40 #include <pulsecore/mutex.h>
41
42 #include "memtrap.h"
43
44 struct pa_memtrap {
45     void *start;
46     size_t size;
47     pa_atomic_t bad;
48     pa_memtrap *next[2], *prev[2];
49 };
50
51 static pa_memtrap *memtraps[2] = { NULL, NULL };
52 static pa_aupdate *aupdate;
53 static pa_static_mutex mutex = PA_STATIC_MUTEX_INIT; /* only required to serialize access to the write side */
54
55 static void allocate_aupdate(void) {
56     PA_ONCE_BEGIN {
57         aupdate = pa_aupdate_new();
58     } PA_ONCE_END;
59 }
60
61 pa_bool_t pa_memtrap_is_good(pa_memtrap *m) {
62     pa_assert(m);
63
64     return !pa_atomic_load(&m->bad);
65 }
66
67 static void sigsafe_error(const char *s) {
68     (void) write(STDERR_FILENO, s, strlen(s));
69 }
70
71 static void signal_handler(int sig, siginfo_t* si, void *data) {
72     unsigned j;
73     pa_memtrap *m;
74     void *r;
75
76     j = pa_aupdate_read_begin(aupdate);
77
78     for (m = memtraps[j]; m; m = m->next[j])
79         if (si->si_addr >= m->start &&
80             (uint8_t*) si->si_addr < (uint8_t*) m->start + m->size)
81             break;
82
83     if (!m)
84         goto fail;
85
86     pa_atomic_store(&m->bad, 1);
87
88     /* Remap anonymous memory into the bad segment */
89     if ((r = mmap(m->start, m->size, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_FIXED|MAP_PRIVATE, -1, 0)) == MAP_FAILED) {
90         sigsafe_error("mmap() failed.\n");
91         goto fail;
92     }
93
94     pa_assert(r == m->start);
95
96     pa_aupdate_read_end(aupdate);
97     return;
98
99 fail:
100     pa_aupdate_read_end(aupdate);
101
102     sigsafe_error("Failed to handle SIGBUS.\n");
103     abort();
104 }
105
106 static void memtrap_link(pa_memtrap *m, unsigned j) {
107     pa_assert(m);
108
109     m->prev[j] = NULL;
110     m->next[j] = memtraps[j];
111     memtraps[j] = m;
112 }
113
114 static void memtrap_unlink(pa_memtrap *m, unsigned j) {
115     pa_assert(m);
116
117     if (m->next[j])
118         m->next[j]->prev[j] = m->prev[j];
119
120     if (m->prev[j])
121         m->prev[j]->next[j] = m->next[j];
122     else
123         memtraps[j] = m->next[j];
124 }
125
126 pa_memtrap* pa_memtrap_add(const void *start, size_t size) {
127     pa_memtrap *m = NULL;
128     unsigned j;
129     pa_mutex *mx;
130
131     pa_assert(start);
132     pa_assert(size > 0);
133
134     start = PA_PAGE_ALIGN_PTR(start);
135     size = PA_PAGE_ALIGN(size);
136
137     m = pa_xnew(pa_memtrap, 1);
138     m->start = (void*) start;
139     m->size = size;
140     pa_atomic_store(&m->bad, 0);
141
142     allocate_aupdate();
143
144     mx = pa_static_mutex_get(&mutex, FALSE, TRUE);
145     pa_mutex_lock(mx);
146
147     j = pa_aupdate_write_begin(aupdate);
148     memtrap_link(m, j);
149     j = pa_aupdate_write_swap(aupdate);
150     memtrap_link(m, j);
151     pa_aupdate_write_end(aupdate);
152
153     pa_mutex_unlock(mx);
154
155     return m;
156 }
157
158 void pa_memtrap_remove(pa_memtrap *m) {
159     unsigned j;
160     pa_mutex *mx;
161
162     pa_assert(m);
163
164     allocate_aupdate();
165
166     mx = pa_static_mutex_get(&mutex, FALSE, TRUE);
167     pa_mutex_lock(mx);
168
169     j = pa_aupdate_write_begin(aupdate);
170     memtrap_unlink(m, j);
171     j = pa_aupdate_write_swap(aupdate);
172     memtrap_unlink(m, j);
173     pa_aupdate_write_end(aupdate);
174
175     pa_mutex_unlock(mx);
176
177     pa_xfree(m);
178 }
179
180 pa_memtrap *pa_memtrap_update(pa_memtrap *m, const void *start, size_t size) {
181     unsigned j;
182     pa_mutex *mx;
183
184     pa_assert(m);
185
186     pa_assert(start);
187     pa_assert(size > 0);
188
189     start = PA_PAGE_ALIGN_PTR(start);
190     size = PA_PAGE_ALIGN(size);
191
192     allocate_aupdate();
193
194     mx = pa_static_mutex_get(&mutex, FALSE, TRUE);
195     pa_mutex_lock(mx);
196
197     j = pa_aupdate_write_begin(aupdate);
198
199     if (m->start == start && m->size == size)
200         goto unlock;
201
202     memtrap_unlink(m, j);
203     j = pa_aupdate_write_swap(aupdate);
204
205     m->start = (void*) start;
206     m->size = size;
207     pa_atomic_store(&m->bad, 0);
208
209     j = pa_aupdate_write_swap(aupdate);
210     memtrap_link(m, j);
211
212 unlock:
213     pa_aupdate_write_end(aupdate);
214
215     pa_mutex_unlock(mx);
216
217     return m;
218 }
219
220 void pa_memtrap_install(void) {
221     struct sigaction sa;
222
223     allocate_aupdate();
224
225     memset(&sa, 0, sizeof(sa));
226     sa.sa_sigaction = signal_handler;
227     sa.sa_flags = SA_RESTART|SA_SIGINFO;
228
229     pa_assert_se(sigaction(SIGBUS, &sa, NULL) == 0);
230 }