]>
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
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
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>
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) {
57 aupdate
= pa_aupdate_new();
61 pa_bool_t
pa_memtrap_is_good(pa_memtrap
*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
) {
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
)
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");
94 pa_assert(r
== m
->start
);
96 pa_aupdate_read_end(aupdate
);
100 pa_aupdate_read_end(aupdate
);
102 sigsafe_error("Failed to handle SIGBUS.\n");
106 static void memtrap_link(pa_memtrap
*m
, unsigned j
) {
111 if ((m
->next
[j
] = memtraps
[j
]))
112 m
->next
[j
]->prev
[j
] = m
;
117 static void memtrap_unlink(pa_memtrap
*m
, unsigned j
) {
121 m
->next
[j
]->prev
[j
] = m
->prev
[j
];
124 m
->prev
[j
]->next
[j
] = m
->next
[j
];
126 memtraps
[j
] = m
->next
[j
];
129 pa_memtrap
* pa_memtrap_add(const void *start
, size_t size
) {
130 pa_memtrap
*m
= NULL
;
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
;
143 pa_atomic_store(&m
->bad
, 0);
147 mx
= pa_static_mutex_get(&mutex
, FALSE
, TRUE
);
150 j
= pa_aupdate_write_begin(aupdate
);
152 j
= pa_aupdate_write_swap(aupdate
);
154 pa_aupdate_write_end(aupdate
);
161 void pa_memtrap_remove(pa_memtrap
*m
) {
169 mx
= pa_static_mutex_get(&mutex
, FALSE
, TRUE
);
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
);
183 pa_memtrap
*pa_memtrap_update(pa_memtrap
*m
, const void *start
, size_t size
) {
192 start
= PA_PAGE_ALIGN_PTR(start
);
193 size
= PA_PAGE_ALIGN(size
);
197 mx
= pa_static_mutex_get(&mutex
, FALSE
, TRUE
);
200 j
= pa_aupdate_write_begin(aupdate
);
202 if (m
->start
== start
&& m
->size
== size
)
205 memtrap_unlink(m
, j
);
206 pa_aupdate_write_swap(aupdate
);
208 m
->start
= (void*) start
;
210 pa_atomic_store(&m
->bad
, 0);
212 pa_assert_se(pa_aupdate_write_swap(aupdate
) == j
);
216 pa_aupdate_write_end(aupdate
);
223 void pa_memtrap_install(void) {
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);