If _malloc_thread_cleanup is implement, call it from libpthread.

Provide the hook from modern jemalloc to avoid using TSD for the thread
destruction cleanup as it can result in reentrancy crashes if fork is
called from a thread that never called malloc as it will result in a
late malloc from the pre-fork synchronisation handler.
This commit is contained in:
joerg 2020-06-04 00:45:32 +00:00
parent 03cfc5665c
commit 558a0c7357
3 changed files with 14 additions and 3 deletions

View File

@ -169,6 +169,10 @@ malloc_tsd_dalloc(void *wrapper) {
a0dalloc(wrapper);
}
__BEGIN_DECLS
void _malloc_thread_cleanup(void);
__END_DECLS
#if defined(JEMALLOC_MALLOC_THREAD_CLEANUP) || defined(_WIN32)
#ifndef _WIN32
JEMALLOC_EXPORT

View File

@ -138,7 +138,7 @@
* _malloc_thread_cleanup() exists, use it as the basis for thread cleanup in
* malloc_tsd.
*/
/* #undef JEMALLOC_MALLOC_THREAD_CLEANUP */
#define JEMALLOC_MALLOC_THREAD_CLEANUP
/*
* Defined if threaded initialization is known to be safe on this platform.

View File

@ -1,4 +1,4 @@
/* $NetBSD: pthread.c,v 1.173 2020/06/03 22:10:24 ad Exp $ */
/* $NetBSD: pthread.c,v 1.174 2020/06/04 00:45:32 joerg Exp $ */
/*-
* Copyright (c) 2001, 2002, 2003, 2006, 2007, 2008, 2020
@ -31,7 +31,7 @@
*/
#include <sys/cdefs.h>
__RCSID("$NetBSD: pthread.c,v 1.173 2020/06/03 22:10:24 ad Exp $");
__RCSID("$NetBSD: pthread.c,v 1.174 2020/06/04 00:45:32 joerg Exp $");
#define __EXPOSE_STACK 1
@ -66,6 +66,10 @@ __RCSID("$NetBSD: pthread.c,v 1.173 2020/06/03 22:10:24 ad Exp $");
#include "pthread_makelwp.h"
#include "reentrant.h"
__BEGIN_DECLS
void _malloc_thread_cleanup(void) __weak;
__END_DECLS
pthread_rwlock_t pthread__alltree_lock = PTHREAD_RWLOCK_INITIALIZER;
static rb_tree_t pthread__alltree;
@ -677,6 +681,9 @@ pthread_exit(void *retval)
/* Perform cleanup of thread-specific data */
pthread__destroy_tsd(self);
if (_malloc_thread_cleanup)
_malloc_thread_cleanup();
/*
* Signal our exit. Our stack and pthread_t won't be reused until
* pthread_create() can see from kernel info that this LWP is gone.