From e4314da2e45770e4b9b3be74f4b552045a041d3a Mon Sep 17 00:00:00 2001 From: Konstantin Belousov Date: Mon, 4 Feb 2019 21:16:15 +0000 Subject: [PATCH] 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 --- lib/libthr/thread/thr_malloc.c | 6 ++++++ lib/libthr/thread/thr_mutex.c | 1 + 2 files changed, 7 insertions(+) diff --git a/lib/libthr/thread/thr_malloc.c b/lib/libthr/thread/thr_malloc.c index 157c72f10d6c..2f1c81142942 100644 --- a/lib/libthr/thread/thr_malloc.c +++ b/lib/libthr/thread/thr_malloc.c @@ -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); diff --git a/lib/libthr/thread/thr_mutex.c b/lib/libthr/thread/thr_mutex.c index f6f37c1264e2..4db653843311 100644 --- a/lib/libthr/thread/thr_mutex.c +++ b/lib/libthr/thread/thr_mutex.c @@ -390,6 +390,7 @@ __pthread_mutex_init(pthread_mutex_t * __restrict mutex, } 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)); }