memtrap: properly add items to linked list
[pulseaudio-mirror.git] / src / pulsecore / memtrap.c
blob4fc1821050482eeff2ea73c435d69470717a2649
1 /***
2 This file is part of PulseAudio.
4 Copyright 2009 Lennart Poettering
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.
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.
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 ***/
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <signal.h>
27 #include <sys/mman.h>
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
34 #include <pulse/xmalloc.h>
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>
42 #include "memtrap.h"
44 struct pa_memtrap {
45 void *start;
46 size_t size;
47 pa_atomic_t bad;
48 pa_memtrap *next[2], *prev[2];
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 */
55 static void allocate_aupdate(void) {
56 PA_ONCE_BEGIN {
57 aupdate = pa_aupdate_new();
58 } PA_ONCE_END;
61 pa_bool_t pa_memtrap_is_good(pa_memtrap *m) {
62 pa_assert(m);
64 return !pa_atomic_load(&m->bad);
67 static void sigsafe_error(const char *s) {
68 (void) write(STDERR_FILENO, s, strlen(s));
71 static void signal_handler(int sig, siginfo_t* si, void *data) {
72 unsigned j;
73 pa_memtrap *m;
74 void *r;
76 j = pa_aupdate_read_begin(aupdate);
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;
83 if (!m)
84 goto fail;
86 pa_atomic_store(&m->bad, 1);
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;
94 pa_assert(r == m->start);
96 pa_aupdate_read_end(aupdate);
97 return;
99 fail:
100 pa_aupdate_read_end(aupdate);
102 sigsafe_error("Failed to handle SIGBUS.\n");
103 abort();
106 static void memtrap_link(pa_memtrap *m, unsigned j) {
107 pa_assert(m);
109 m->prev[j] = NULL;
111 if ((m->next[j] = memtraps[j]))
112 m->next[j]->prev[j] = m;
114 memtraps[j] = m;
117 static void memtrap_unlink(pa_memtrap *m, unsigned j) {
118 pa_assert(m);
120 if (m->next[j])
121 m->next[j]->prev[j] = m->prev[j];
123 if (m->prev[j])
124 m->prev[j]->next[j] = m->next[j];
125 else
126 memtraps[j] = m->next[j];
129 pa_memtrap* pa_memtrap_add(const void *start, size_t size) {
130 pa_memtrap *m = NULL;
131 unsigned j;
132 pa_mutex *mx;
134 pa_assert(start);
135 pa_assert(size > 0);
137 start = PA_PAGE_ALIGN_PTR(start);
138 size = PA_PAGE_ALIGN(size);
140 m = pa_xnew(pa_memtrap, 1);
141 m->start = (void*) start;
142 m->size = size;
143 pa_atomic_store(&m->bad, 0);
145 allocate_aupdate();
147 mx = pa_static_mutex_get(&mutex, FALSE, TRUE);
148 pa_mutex_lock(mx);
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);
156 pa_mutex_unlock(mx);
158 return m;
161 void pa_memtrap_remove(pa_memtrap *m) {
162 unsigned j;
163 pa_mutex *mx;
165 pa_assert(m);
167 allocate_aupdate();
169 mx = pa_static_mutex_get(&mutex, FALSE, TRUE);
170 pa_mutex_lock(mx);
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);
178 pa_mutex_unlock(mx);
180 pa_xfree(m);
183 pa_memtrap *pa_memtrap_update(pa_memtrap *m, const void *start, size_t size) {
184 unsigned j;
185 pa_mutex *mx;
187 pa_assert(m);
189 pa_assert(start);
190 pa_assert(size > 0);
192 start = PA_PAGE_ALIGN_PTR(start);
193 size = PA_PAGE_ALIGN(size);
195 allocate_aupdate();
197 mx = pa_static_mutex_get(&mutex, FALSE, TRUE);
198 pa_mutex_lock(mx);
200 j = pa_aupdate_write_begin(aupdate);
202 if (m->start == start && m->size == size)
203 goto unlock;
205 memtrap_unlink(m, j);
206 pa_aupdate_write_swap(aupdate);
208 m->start = (void*) start;
209 m->size = size;
210 pa_atomic_store(&m->bad, 0);
212 pa_assert_se(pa_aupdate_write_swap(aupdate) == j);
213 memtrap_link(m, j);
215 unlock:
216 pa_aupdate_write_end(aupdate);
218 pa_mutex_unlock(mx);
220 return m;
223 void pa_memtrap_install(void) {
224 struct sigaction sa;
226 allocate_aupdate();
228 memset(&sa, 0, sizeof(sa));
229 sa.sa_sigaction = signal_handler;
230 sa.sa_flags = SA_RESTART|SA_SIGINFO;
232 pa_assert_se(sigaction(SIGBUS, &sa, NULL) == 0);