]> code.delx.au - pulseaudio/blob - src/pulsecore/memtrap.c
Merge remote branch 'origin/master-tx'
[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 /* 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
111 if ((m->next[j] = memtraps[j]))
112 m->next[j]->prev[j] = m;
113
114 memtraps[j] = m;
115 }
116
117 static void memtrap_unlink(pa_memtrap *m, unsigned j) {
118 pa_assert(m);
119
120 if (m->next[j])
121 m->next[j]->prev[j] = m->prev[j];
122
123 if (m->prev[j])
124 m->prev[j]->next[j] = m->next[j];
125 else
126 memtraps[j] = m->next[j];
127 }
128
129 pa_memtrap* pa_memtrap_add(const void *start, size_t size) {
130 pa_memtrap *m = NULL;
131 unsigned j;
132 pa_mutex *mx;
133
134 pa_assert(start);
135 pa_assert(size > 0);
136
137 start = PA_PAGE_ALIGN_PTR(start);
138 size = PA_PAGE_ALIGN(size);
139
140 m = pa_xnew(pa_memtrap, 1);
141 m->start = (void*) start;
142 m->size = size;
143 pa_atomic_store(&m->bad, 0);
144
145 allocate_aupdate();
146
147 mx = pa_static_mutex_get(&mutex, FALSE, TRUE);
148 pa_mutex_lock(mx);
149
150 j = pa_aupdate_write_begin(aupdate);
151 memtrap_link(m, j);
152 j = pa_aupdate_write_swap(aupdate);
153 memtrap_link(m, j);
154 pa_aupdate_write_end(aupdate);
155
156 pa_mutex_unlock(mx);
157
158 return m;
159 }
160
161 void pa_memtrap_remove(pa_memtrap *m) {
162 unsigned j;
163 pa_mutex *mx;
164
165 pa_assert(m);
166
167 allocate_aupdate();
168
169 mx = pa_static_mutex_get(&mutex, FALSE, TRUE);
170 pa_mutex_lock(mx);
171
172 j = pa_aupdate_write_begin(aupdate);
173 memtrap_unlink(m, j);
174 j = pa_aupdate_write_swap(aupdate);
175 memtrap_unlink(m, j);
176 pa_aupdate_write_end(aupdate);
177
178 pa_mutex_unlock(mx);
179
180 pa_xfree(m);
181 }
182
183 pa_memtrap *pa_memtrap_update(pa_memtrap *m, const void *start, size_t size) {
184 unsigned j;
185 pa_mutex *mx;
186
187 pa_assert(m);
188
189 pa_assert(start);
190 pa_assert(size > 0);
191
192 start = PA_PAGE_ALIGN_PTR(start);
193 size = PA_PAGE_ALIGN(size);
194
195 allocate_aupdate();
196
197 mx = pa_static_mutex_get(&mutex, FALSE, TRUE);
198 pa_mutex_lock(mx);
199
200 j = pa_aupdate_write_begin(aupdate);
201
202 if (m->start == start && m->size == size)
203 goto unlock;
204
205 memtrap_unlink(m, j);
206 pa_aupdate_write_swap(aupdate);
207
208 m->start = (void*) start;
209 m->size = size;
210 pa_atomic_store(&m->bad, 0);
211
212 pa_assert_se(pa_aupdate_write_swap(aupdate) == j);
213 memtrap_link(m, j);
214
215 unlock:
216 pa_aupdate_write_end(aupdate);
217
218 pa_mutex_unlock(mx);
219
220 return m;
221 }
222
223 void pa_memtrap_install(void) {
224 struct sigaction sa;
225
226 allocate_aupdate();
227
228 memset(&sa, 0, sizeof(sa));
229 sa.sa_sigaction = signal_handler;
230 sa.sa_flags = SA_RESTART|SA_SIGINFO;
231
232 pa_assert_se(sigaction(SIGBUS, &sa, NULL) == 0);
233 }