From c74471d293c3fef2d3d1cc3eb20e092f167ccdf9 Mon Sep 17 00:00:00 2001 From: Alessandro Ghedini Date: Fri, 4 Mar 2016 16:04:37 +0000 Subject: [PATCH] Convert CRYPTO_LOCK_DSO to new multi-threading API Reviewed-by: Matt Caswell Reviewed-by: Rich Salz --- crypto/dso/dso_lib.c | 37 +++++++++++++++++++++++++++---------- include/openssl/crypto.h | 1 - include/openssl/dso.h | 1 + 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/crypto/dso/dso_lib.c b/crypto/dso/dso_lib.c index c410eac9ee..3082545e63 100644 --- a/crypto/dso/dso_lib.c +++ b/crypto/dso/dso_lib.c @@ -120,12 +120,20 @@ DSO *DSO_new_method(DSO_METHOD *meth) else ret->meth = meth; ret->references = 1; - if ((ret->meth->init != NULL) && !ret->meth->init(ret)) { + + ret->lock = CRYPTO_THREAD_lock_new(); + if (ret->lock == NULL) { sk_void_free(ret->meth_data); OPENSSL_free(ret); + return NULL; + } + + if ((ret->meth->init != NULL) && !ret->meth->init(ret)) { + DSO_free(ret); ret = NULL; } - return (ret); + + return ret; } int DSO_free(DSO *dso) @@ -135,27 +143,30 @@ int DSO_free(DSO *dso) if (dso == NULL) return (1); - i = CRYPTO_add(&dso->references, -1, CRYPTO_LOCK_DSO); + if (CRYPTO_atomic_add(&dso->references, -1, &i, dso->lock) <= 0) + return 0; + REF_PRINT_COUNT("DSO", dso); if (i > 0) - return (1); + return 1; REF_ASSERT_ISNT(i < 0); if ((dso->meth->dso_unload != NULL) && !dso->meth->dso_unload(dso)) { DSOerr(DSO_F_DSO_FREE, DSO_R_UNLOAD_FAILED); - return (0); + return 0; } if ((dso->meth->finish != NULL) && !dso->meth->finish(dso)) { DSOerr(DSO_F_DSO_FREE, DSO_R_FINISH_FAILED); - return (0); + return 0; } sk_void_free(dso->meth_data); OPENSSL_free(dso->filename); OPENSSL_free(dso->loaded_filename); + CRYPTO_THREAD_lock_free(dso->lock); OPENSSL_free(dso); - return (1); + return 1; } int DSO_flags(DSO *dso) @@ -165,13 +176,19 @@ int DSO_flags(DSO *dso) int DSO_up_ref(DSO *dso) { + int i; + if (dso == NULL) { DSOerr(DSO_F_DSO_UP_REF, ERR_R_PASSED_NULL_PARAMETER); - return (0); + return 0; } - CRYPTO_add(&dso->references, 1, CRYPTO_LOCK_DSO); - return (1); + if (CRYPTO_atomic_add(&dso->references, 1, &i, dso->lock) <= 0) + return 0; + + REF_PRINT_COUNT("DSO", r); + REF_ASSERT_ISNT(i < 2); + return ((i > 1) ? 1 : 0); } DSO *DSO_load(DSO *dso, const char *filename, DSO_METHOD *meth, int flags) diff --git a/include/openssl/crypto.h b/include/openssl/crypto.h index 12052e14ce..44611ca7fb 100644 --- a/include/openssl/crypto.h +++ b/include/openssl/crypto.h @@ -187,7 +187,6 @@ extern "C" { # define CRYPTO_LOCK_READDIR 24 # define CRYPTO_LOCK_RSA_BLINDING 25 # define CRYPTO_LOCK_MALLOC2 27 -# define CRYPTO_LOCK_DSO 28 # define CRYPTO_LOCK_DYNLOCK 29 # define CRYPTO_LOCK_ENGINE 30 # define CRYPTO_LOCK_UI 31 diff --git a/include/openssl/dso.h b/include/openssl/dso.h index c1229976a7..1eadbd96e1 100644 --- a/include/openssl/dso.h +++ b/include/openssl/dso.h @@ -228,6 +228,7 @@ struct dso_st { * loaded. */ char *loaded_filename; + CRYPTO_RWLOCK *lock; }; DSO *DSO_new(void); -- 2.25.1