]> code.delx.au - pulseaudio/blob - src/pulsecore/memtrap.c
memtrap,aupdate: split atomic update from memtrap into seperate aupdate framework
[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/core-util.h>
32 #include <pulsecore/aupdate.h>
33 #include <pulsecore/atomic.h>
34 #include <pulsecore/once.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_aupdate *aupdate;
47
48 static void allocate_aupdate(void) {
49 PA_ONCE_BEGIN {
50 aupdate = pa_aupdate_new();
51 } PA_ONCE_END;
52 }
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 j;
66 pa_memtrap *m;
67 void *r;
68
69 j = pa_aupdate_read_begin(aupdate);
70
71 for (m = memtraps[j]; m; m = m->next[j])
72 if (si->si_addr >= m->start &&
73 (uint8_t*) si->si_addr < (uint8_t*) m->start + m->size)
74 break;
75
76 if (!m)
77 goto fail;
78
79 pa_atomic_store(&m->bad, 1);
80
81 /* Remap anonymous memory into the bad segment */
82 if ((r = mmap(m->start, m->size, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_FIXED|MAP_PRIVATE, -1, 0)) == MAP_FAILED) {
83 sigsafe_error("mmap() failed.\n");
84 goto fail;
85 }
86
87 pa_assert(r == m->start);
88
89 pa_aupdate_read_end(aupdate);
90 return;
91
92 fail:
93 pa_aupdate_read_end(aupdate);
94
95 sigsafe_error("Failed to handle SIGBUS.\n");
96 abort();
97 }
98
99 static void memtrap_link(pa_memtrap *m, unsigned j) {
100 pa_assert(m);
101
102 m->prev[j] = NULL;
103 m->next[j] = memtraps[j];
104 memtraps[j] = m;
105 }
106
107 static void memtrap_unlink(pa_memtrap *m, unsigned j) {
108 pa_assert(m);
109
110 if (m->next[j])
111 m->next[j]->prev[j] = m->prev[j];
112
113 if (m->prev[j])
114 m->prev[j]->next[j] = m->next[j];
115 else
116 memtraps[j] = m->next[j];
117 }
118
119 pa_memtrap* pa_memtrap_add(const void *start, size_t size) {
120 pa_memtrap *m = NULL;
121 unsigned j;
122
123 pa_assert(start);
124 pa_assert(size > 0);
125 pa_assert(PA_PAGE_ALIGN_PTR(start) == start);
126 pa_assert(PA_PAGE_ALIGN(size) == size);
127
128 m = pa_xnew(pa_memtrap, 1);
129 m->start = (void*) start;
130 m->size = size;
131 pa_atomic_store(&m->bad, 0);
132
133 allocate_aupdate();
134
135 j = pa_aupdate_write_begin(aupdate);
136 memtrap_link(m, j);
137 j = pa_aupdate_write_swap(aupdate);
138 memtrap_link(m, j);
139 pa_aupdate_write_end(aupdate);
140
141 return m;
142 }
143
144 void pa_memtrap_remove(pa_memtrap *m) {
145 unsigned j;
146
147 pa_assert(m);
148
149 allocate_aupdate();
150
151 j = pa_aupdate_write_begin(aupdate);
152 memtrap_unlink(m, j);
153 j = pa_aupdate_write_swap(aupdate);
154 memtrap_unlink(m, j);
155 pa_aupdate_write_end(aupdate);
156
157 pa_xfree(m);
158 }
159
160 pa_memtrap *pa_memtrap_update(pa_memtrap *m, const void *start, size_t size) {
161 unsigned j;
162
163 pa_assert(m);
164
165 pa_assert(start);
166 pa_assert(size > 0);
167 pa_assert(PA_PAGE_ALIGN_PTR(start) == start);
168 pa_assert(PA_PAGE_ALIGN(size) == size);
169
170 allocate_aupdate();
171
172 j = pa_aupdate_write_begin(aupdate);
173
174 if (m->start == start && m->size == size)
175 goto unlock;
176
177 memtrap_unlink(m, j);
178 j = pa_aupdate_write_swap(aupdate);
179
180 m->start = (void*) start;
181 m->size = size;
182 pa_atomic_store(&m->bad, 0);
183
184 j = pa_aupdate_write_swap(aupdate);
185 memtrap_link(m, j);
186
187 unlock:
188 pa_aupdate_write_end(aupdate);
189
190 return m;
191 }
192
193 void pa_memtrap_install(void) {
194 struct sigaction sa;
195
196 allocate_aupdate();
197
198 memset(&sa, 0, sizeof(sa));
199 sa.sa_sigaction = signal_handler;
200 sa.sa_flags = SA_RESTART|SA_SIGINFO;
201
202 pa_assert_se(sigaction(SIGBUS, &sa, NULL) == 0);
203 }