Module Name:    src
Committed By:   joerg
Date:           Thu Jun  4 00:45:32 UTC 2020

Modified Files:
        src/external/bsd/jemalloc/dist/src: tsd.c
        src/external/bsd/jemalloc/include/jemalloc/internal:
            jemalloc_internal_defs.h
        src/lib/libpthread: pthread.c

Log Message:
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.


To generate a diff of this commit:
cvs rdiff -u -r1.2 -r1.3 src/external/bsd/jemalloc/dist/src/tsd.c
cvs rdiff -u -r1.12 -r1.13 \
    src/external/bsd/jemalloc/include/jemalloc/internal/jemalloc_internal_defs.h
cvs rdiff -u -r1.173 -r1.174 src/lib/libpthread/pthread.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/external/bsd/jemalloc/dist/src/tsd.c
diff -u src/external/bsd/jemalloc/dist/src/tsd.c:1.2 src/external/bsd/jemalloc/dist/src/tsd.c:1.3
--- src/external/bsd/jemalloc/dist/src/tsd.c:1.2	Wed Apr 24 14:34:21 2019
+++ src/external/bsd/jemalloc/dist/src/tsd.c	Thu Jun  4 00:45:32 2020
@@ -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

Index: src/external/bsd/jemalloc/include/jemalloc/internal/jemalloc_internal_defs.h
diff -u src/external/bsd/jemalloc/include/jemalloc/internal/jemalloc_internal_defs.h:1.12 src/external/bsd/jemalloc/include/jemalloc/internal/jemalloc_internal_defs.h:1.13
--- src/external/bsd/jemalloc/include/jemalloc/internal/jemalloc_internal_defs.h:1.12	Tue Apr 21 22:27:09 2020
+++ src/external/bsd/jemalloc/include/jemalloc/internal/jemalloc_internal_defs.h	Thu Jun  4 00:45:32 2020
@@ -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.

Index: src/lib/libpthread/pthread.c
diff -u src/lib/libpthread/pthread.c:1.173 src/lib/libpthread/pthread.c:1.174
--- src/lib/libpthread/pthread.c:1.173	Wed Jun  3 22:10:24 2020
+++ src/lib/libpthread/pthread.c	Thu Jun  4 00:45:32 2020
@@ -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
 #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.

Reply via email to