From 39a4e527933a6b46234ff6f229469a68d9d455ac Mon Sep 17 00:00:00 2001 From: daurnimator Date: Tue, 4 Apr 2017 00:27:57 +1000 Subject: Use a single lock across multiple init functions --- src/openssl.c | 39 +++++++++++++-------------------------- 1 file changed, 13 insertions(+), 26 deletions(-) (limited to 'src/openssl.c') diff --git a/src/openssl.c b/src/openssl.c index a01fde5..df559d7 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -1781,14 +1781,11 @@ static int compat_X509_up_ref(X509 *crt) { */ #endif +/* compat_init must not be called from multiple threads at once */ static int compat_init(void) { - static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; static int store_index = -1, ssl_ctx_index = -1, done; int error = 0; - if ((error = pthread_mutex_lock(&mutex))) - return error; - if (done) goto epilog; @@ -1859,8 +1856,6 @@ epilog: compat.tmp.store = NULL; } - (void)pthread_mutex_unlock(&mutex); - return error; sslerr: error = auxL_EOPENSSL; @@ -1984,15 +1979,12 @@ static void ex_onfree(void *parent NOTUSED, void *_data, CRYPTO_EX_DATA *ad NOTU free(data); } /* ex_onfree() */ +/* ex_init must not be called from multiple threads at once */ static int ex_init(void) { - static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; static int done; struct ex_type *type; int error = 0; - if ((error = pthread_mutex_lock(&mutex))) - return error; - if (done) goto epilog; @@ -2013,8 +2005,6 @@ static int ex_init(void) { done = 1; epilog: - (void)pthread_mutex_unlock(&mutex); - return error; sslerr: error = auxL_EOPENSSL; @@ -10058,14 +10048,11 @@ static unsigned long mt_gettid(void) { #endif } /* mt_gettid() */ +/* mt_init must not be called from multiple threads at once */ static int mt_init(void) { - static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; static int done, bound; int error = 0; - if ((error = pthread_mutex_lock(&mutex))) - return error; - if (done) goto epilog; @@ -10108,8 +10095,6 @@ static int mt_init(void) { done = 1; epilog: - pthread_mutex_unlock(&mutex); - return error; } /* mt_init() */ @@ -10119,12 +10104,11 @@ static void initall(lua_State *L) { static int initssl; int error; - if ((error = mt_init())) - auxL_error(L, error, "openssl.init"); - pthread_mutex_lock(&mutex); - if (!initssl) { + error = mt_init(); + + if (!error && !initssl) { initssl = 1; SSL_load_error_strings(); @@ -10138,12 +10122,15 @@ static void initall(lua_State *L) { OPENSSL_config(NULL); } - pthread_mutex_unlock(&mutex); + if (!error) + error = compat_init(); - if ((error = compat_init())) - auxL_error(L, error, "openssl.init"); + if (!error) + error = ex_init(); + + pthread_mutex_unlock(&mutex); - if ((error = ex_init())) + if (error) auxL_error(L, error, "openssl.init"); ex_newstate(L); -- cgit v1.2.3-59-g8ed1b From 13b82f5706ee9f430203c2559250056e5e12de72 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Thu, 10 Aug 2017 11:54:15 +1000 Subject: Remove mutex from dl_anchor All call sites already hold a single mutex --- src/openssl.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) (limited to 'src/openssl.c') diff --git a/src/openssl.c b/src/openssl.c index df559d7..9fc5b97 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -1227,17 +1227,14 @@ static const EVP_MD *auxL_optdigest(lua_State *L, int index, EVP_PKEY *key, cons * Prevent loader from unlinking us if we've registered a callback with * OpenSSL by taking another reference to ourselves. */ +/* dl_anchor must not be called from multiple threads at once */ static int dl_anchor(void) { #if HAVE_DLADDR extern int luaopen__openssl(lua_State *); - static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; static void *anchor; Dl_info info; int error = 0; - if ((error = pthread_mutex_lock(&mutex))) - return error; - if (anchor) goto epilog; @@ -1247,8 +1244,6 @@ static int dl_anchor(void) { if (!(anchor = dlopen(info.dli_fname, RTLD_NOW|RTLD_LOCAL))) goto dlerr; epilog: - (void)pthread_mutex_unlock(&mutex); - return error; dlerr: error = auxL_EDYLD; -- cgit v1.2.3-59-g8ed1b