mirror of
git://git.musl-libc.org/musl
synced 2025-03-06 20:48:29 +01:00
defer free of thread-local dlerror buffers from inconsistent context
__dl_thread_cleanup is called from the context of an exiting thread
that is not in a consistent state valid for calling application code.
since commit c9f415d7ea
, it's possible
(and supported usage) for the allocator to have been replaced by the
application, so __dl_thread_cleanup can no longer call free. instead,
reuse the message buffer as a linked-list pointer, and queue it to be
freed the next time any dynamic linker error message is generated.
This commit is contained in:
parent
b2020571f0
commit
aa5a9d15e0
1 changed files with 20 additions and 2 deletions
|
@ -3,6 +3,7 @@
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include "pthread_impl.h"
|
#include "pthread_impl.h"
|
||||||
#include "dynlink.h"
|
#include "dynlink.h"
|
||||||
|
#include "lock.h"
|
||||||
|
|
||||||
char *dlerror()
|
char *dlerror()
|
||||||
{
|
{
|
||||||
|
@ -16,21 +17,38 @@ char *dlerror()
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static volatile int freebuf_queue_lock[1];
|
||||||
|
static void **freebuf_queue;
|
||||||
|
|
||||||
void __dl_thread_cleanup(void)
|
void __dl_thread_cleanup(void)
|
||||||
{
|
{
|
||||||
pthread_t self = __pthread_self();
|
pthread_t self = __pthread_self();
|
||||||
if (self->dlerror_buf != (void *)-1)
|
if (self->dlerror_buf && self->dlerror_buf != (void *)-1) {
|
||||||
free(self->dlerror_buf);
|
LOCK(freebuf_queue_lock);
|
||||||
|
void **p = (void **)self->dlerror_buf;
|
||||||
|
*p = freebuf_queue;
|
||||||
|
freebuf_queue = p;
|
||||||
|
UNLOCK(freebuf_queue_lock);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
hidden void __dl_vseterr(const char *fmt, va_list ap)
|
hidden void __dl_vseterr(const char *fmt, va_list ap)
|
||||||
{
|
{
|
||||||
|
LOCK(freebuf_queue_lock);
|
||||||
|
while (freebuf_queue) {
|
||||||
|
void **p = freebuf_queue;
|
||||||
|
freebuf_queue = *p;
|
||||||
|
free(p);
|
||||||
|
}
|
||||||
|
UNLOCK(freebuf_queue_lock);
|
||||||
|
|
||||||
va_list ap2;
|
va_list ap2;
|
||||||
va_copy(ap2, ap);
|
va_copy(ap2, ap);
|
||||||
pthread_t self = __pthread_self();
|
pthread_t self = __pthread_self();
|
||||||
if (self->dlerror_buf != (void *)-1)
|
if (self->dlerror_buf != (void *)-1)
|
||||||
free(self->dlerror_buf);
|
free(self->dlerror_buf);
|
||||||
size_t len = vsnprintf(0, 0, fmt, ap2);
|
size_t len = vsnprintf(0, 0, fmt, ap2);
|
||||||
|
if (len < sizeof(void *)) len = sizeof(void *);
|
||||||
va_end(ap2);
|
va_end(ap2);
|
||||||
char *buf = malloc(len+1);
|
char *buf = malloc(len+1);
|
||||||
if (buf) {
|
if (buf) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue