]>
code.delx.au - pulseaudio/blob - src/pulsecore/memtrap.c
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
28 #ifdef HAVE_SYS_MMAN_H
32 /* This is deprecated on glibc but is still used by FreeBSD */
33 #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
34 # define MAP_ANONYMOUS MAP_ANON
37 #include <pulse/xmalloc.h>
39 #include <pulsecore/core-util.h>
40 #include <pulsecore/aupdate.h>
41 #include <pulsecore/atomic.h>
42 #include <pulsecore/once.h>
43 #include <pulsecore/mutex.h>
51 pa_memtrap
*next
[2], *prev
[2];
54 static pa_memtrap
*memtraps
[2] = { NULL
, NULL
};
55 static pa_aupdate
*aupdate
;
56 static pa_static_mutex mutex
= PA_STATIC_MUTEX_INIT
; /* only required to serialize access to the write side */
58 static void allocate_aupdate(void) {
60 aupdate
= pa_aupdate_new();
64 bool pa_memtrap_is_good(pa_memtrap
*m
) {
67 return !pa_atomic_load(&m
->bad
);
71 static void sigsafe_error(const char *s
) {
72 size_t ret PA_GCC_UNUSED
;
73 ret
= write(STDERR_FILENO
, s
, strlen(s
));
76 static void signal_handler(int sig
, siginfo_t
* si
, void *data
) {
81 j
= pa_aupdate_read_begin(aupdate
);
83 for (m
= memtraps
[j
]; m
; m
= m
->next
[j
])
84 if (si
->si_addr
>= m
->start
&&
85 (uint8_t*) si
->si_addr
< (uint8_t*) m
->start
+ m
->size
)
91 pa_atomic_store(&m
->bad
, 1);
93 /* Remap anonymous memory into the bad segment */
94 if ((r
= mmap(m
->start
, m
->size
, PROT_READ
|PROT_WRITE
, MAP_ANONYMOUS
|MAP_FIXED
|MAP_PRIVATE
, -1, 0)) == MAP_FAILED
) {
95 sigsafe_error("mmap() failed.\n");
99 pa_assert(r
== m
->start
);
101 pa_aupdate_read_end(aupdate
);
105 pa_aupdate_read_end(aupdate
);
107 sigsafe_error("Failed to handle SIGBUS.\n");
112 static void memtrap_link(pa_memtrap
*m
, unsigned j
) {
117 if ((m
->next
[j
] = memtraps
[j
]))
118 m
->next
[j
]->prev
[j
] = m
;
123 static void memtrap_unlink(pa_memtrap
*m
, unsigned j
) {
127 m
->next
[j
]->prev
[j
] = m
->prev
[j
];
130 m
->prev
[j
]->next
[j
] = m
->next
[j
];
132 memtraps
[j
] = m
->next
[j
];
135 pa_memtrap
* pa_memtrap_add(const void *start
, size_t size
) {
136 pa_memtrap
*m
= NULL
;
143 start
= PA_PAGE_ALIGN_PTR(start
);
144 size
= PA_PAGE_ALIGN(size
);
146 m
= pa_xnew(pa_memtrap
, 1);
147 m
->start
= (void*) start
;
149 pa_atomic_store(&m
->bad
, 0);
153 mx
= pa_static_mutex_get(&mutex
, false, true);
156 j
= pa_aupdate_write_begin(aupdate
);
158 j
= pa_aupdate_write_swap(aupdate
);
160 pa_aupdate_write_end(aupdate
);
167 void pa_memtrap_remove(pa_memtrap
*m
) {
175 mx
= pa_static_mutex_get(&mutex
, false, true);
178 j
= pa_aupdate_write_begin(aupdate
);
179 memtrap_unlink(m
, j
);
180 j
= pa_aupdate_write_swap(aupdate
);
181 memtrap_unlink(m
, j
);
182 pa_aupdate_write_end(aupdate
);
189 pa_memtrap
*pa_memtrap_update(pa_memtrap
*m
, const void *start
, size_t size
) {
198 start
= PA_PAGE_ALIGN_PTR(start
);
199 size
= PA_PAGE_ALIGN(size
);
203 mx
= pa_static_mutex_get(&mutex
, false, true);
206 j
= pa_aupdate_write_begin(aupdate
);
208 if (m
->start
== start
&& m
->size
== size
)
211 memtrap_unlink(m
, j
);
212 pa_aupdate_write_swap(aupdate
);
214 m
->start
= (void*) start
;
216 pa_atomic_store(&m
->bad
, 0);
218 pa_assert_se(pa_aupdate_write_swap(aupdate
) == j
);
222 pa_aupdate_write_end(aupdate
);
229 void pa_memtrap_install(void) {
230 #ifdef HAVE_SIGACTION
235 memset(&sa
, 0, sizeof(sa
));
236 sa
.sa_sigaction
= signal_handler
;
237 sa
.sa_flags
= SA_RESTART
|SA_SIGINFO
;
239 pa_assert_se(sigaction(SIGBUS
, &sa
, NULL
) == 0);