Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/libpthread If _malloc_thread_cleanup is implement, call ...



details:   https://anonhg.NetBSD.org/src/rev/d2d22f836d14
branches:  trunk
changeset: 934022:d2d22f836d14
user:      joerg <joerg%NetBSD.org@localhost>
date:      Thu Jun 04 00:45:32 2020 +0000

description:
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.

diffstat:

 external/bsd/jemalloc/dist/src/tsd.c                                     |   4 +++
 external/bsd/jemalloc/include/jemalloc/internal/jemalloc_internal_defs.h |   2 +-
 lib/libpthread/pthread.c                                                 |  11 ++++++++-
 3 files changed, 14 insertions(+), 3 deletions(-)

diffs (65 lines):

diff -r cb1659dcb090 -r d2d22f836d14 external/bsd/jemalloc/dist/src/tsd.c
--- a/external/bsd/jemalloc/dist/src/tsd.c      Wed Jun 03 22:25:49 2020 +0000
+++ b/external/bsd/jemalloc/dist/src/tsd.c      Thu Jun 04 00:45:32 2020 +0000
@@ -169,6 +169,10 @@
        a0dalloc(wrapper);
 }
 
+__BEGIN_DECLS
+void _malloc_thread_cleanup(void);
+__END_DECLS
+
 #if defined(JEMALLOC_MALLOC_THREAD_CLEANUP) || defined(_WIN32)
 #ifndef _WIN32
 JEMALLOC_EXPORT
diff -r cb1659dcb090 -r d2d22f836d14 external/bsd/jemalloc/include/jemalloc/internal/jemalloc_internal_defs.h
--- a/external/bsd/jemalloc/include/jemalloc/internal/jemalloc_internal_defs.h  Wed Jun 03 22:25:49 2020 +0000
+++ b/external/bsd/jemalloc/include/jemalloc/internal/jemalloc_internal_defs.h  Thu Jun 04 00:45:32 2020 +0000
@@ -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.
diff -r cb1659dcb090 -r d2d22f836d14 lib/libpthread/pthread.c
--- a/lib/libpthread/pthread.c  Wed Jun 03 22:25:49 2020 +0000
+++ b/lib/libpthread/pthread.c  Thu Jun 04 00:45:32 2020 +0000
@@ -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 @@
 #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 @@
        /* 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.



Home | Main Index | Thread Index | Old Index