Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 4 Feb 2019 21:16:16 +0000 (UTC)
From:      Konstantin Belousov <kib@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r343754 - head/lib/libthr/thread
Message-ID:  <201902042116.x14LGGCq084375@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: kib
Date: Mon Feb  4 21:16:15 2019
New Revision: 343754
URL: https://svnweb.freebsd.org/changeset/base/343754

Log:
  Fixes for very early use of the pthread_mutex_* and libthr malloc.
  
  When libthr is statically linked into the binary, order of the
  constructors execution is not deterministic.  It is possible for the
  application constructor to use pthread_mutex_* functions before the
  libthr initialization was done.
  
  Handle it by:
  - making thr_malloc.c locking functions operational when curthread is not
    yet set;
  - making __thr_malloc_init() idempotent, allowing more than one call to it;
  - unconditionally calling __thr_malloc_init() before initializing
    a process-private mutex.
  
  Reported and tested by:	mmel
  Sponsored by:	The FreeBSD Foundation
  MFC after:	1 week

Modified:
  head/lib/libthr/thread/thr_malloc.c
  head/lib/libthr/thread/thr_mutex.c

Modified: head/lib/libthr/thread/thr_malloc.c
==============================================================================
--- head/lib/libthr/thread/thr_malloc.c	Mon Feb  4 20:46:57 2019	(r343753)
+++ head/lib/libthr/thread/thr_malloc.c	Mon Feb  4 21:16:15 2019	(r343754)
@@ -46,6 +46,8 @@ void
 __thr_malloc_init(void)
 {
 
+	if (npagesizes != 0)
+		return;
 	npagesizes = getpagesizes(pagesizes_d, nitems(pagesizes_d));
 	if (npagesizes == -1) {
 		npagesizes = 1;
@@ -59,6 +61,8 @@ static void
 thr_malloc_lock(struct pthread *curthread)
 {
 
+	if (curthread == NULL)
+		return;
 	curthread->locklevel++;
 	_thr_umutex_lock(&thr_malloc_umtx, TID(curthread));
 }
@@ -67,6 +71,8 @@ static void
 thr_malloc_unlock(struct pthread *curthread)
 {
 
+	if (curthread == NULL)
+		return;
 	_thr_umutex_unlock(&thr_malloc_umtx, TID(curthread));
 	curthread->locklevel--;
 	_thr_ast(curthread);

Modified: head/lib/libthr/thread/thr_mutex.c
==============================================================================
--- head/lib/libthr/thread/thr_mutex.c	Mon Feb  4 20:46:57 2019	(r343753)
+++ head/lib/libthr/thread/thr_mutex.c	Mon Feb  4 21:16:15 2019	(r343754)
@@ -390,6 +390,7 @@ __pthread_mutex_init(pthread_mutex_t * __restrict mute
 	}
 	if (mutex_attr == NULL ||
 	    (*mutex_attr)->m_pshared == PTHREAD_PROCESS_PRIVATE) {
+		__thr_malloc_init();
 		return (mutex_init(mutex, mutex_attr ? *mutex_attr : NULL,
 		    __thr_calloc));
 	}



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201902042116.x14LGGCq084375>