From: Alessandro Ghedini Date: Fri, 26 Feb 2016 11:51:31 +0000 (+0000) Subject: Convert CRYPTO_LOCK_BIO to new multi-threading API X-Git-Tag: OpenSSL_1_1_0-pre4~237 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=fb46be034816e5fe9f04fd39da960d34dbf2f52d;p=oweals%2Fopenssl.git Convert CRYPTO_LOCK_BIO to new multi-threading API Reviewed-by: Matt Caswell Reviewed-by: Rich Salz --- diff --git a/crypto/bio/bio_lib.c b/crypto/bio/bio_lib.c index ee8d622b20..522525b129 100644 --- a/crypto/bio/bio_lib.c +++ b/crypto/bio/bio_lib.c @@ -94,12 +94,22 @@ int BIO_set(BIO *bio, BIO_METHOD *method) bio->num_read = 0L; bio->num_write = 0L; CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data); - if (method->create != NULL) + + bio->lock = CRYPTO_THREAD_lock_new(); + if (bio->lock == NULL) { + CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data); + return 0; + } + + if (method->create != NULL) { if (!method->create(bio)) { CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data); - return (0); + CRYPTO_THREAD_lock_free(bio->lock); + return 0; } - return (1); + } + + return 1; } int BIO_free(BIO *a) @@ -107,23 +117,29 @@ int BIO_free(BIO *a) int i; if (a == NULL) - return (0); + return 0; + + if (CRYPTO_atomic_add(&a->references, -1, &i, a->lock) <= 0) + return 0; - i = CRYPTO_add(&a->references, -1, CRYPTO_LOCK_BIO); REF_PRINT_COUNT("BIO", a); if (i > 0) - return (1); + return 1; REF_ASSERT_ISNT(i < 0); if ((a->callback != NULL) && ((i = (int)a->callback(a, BIO_CB_FREE, NULL, 0, 0L, 1L)) <= 0)) - return (i); + return i; CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, a, &a->ex_data); + CRYPTO_THREAD_lock_free(a->lock); + if ((a->method != NULL) && (a->method->destroy != NULL)) a->method->destroy(a); + OPENSSL_free(a); - return (1); + + return 1; } void BIO_vfree(BIO *a) @@ -131,6 +147,18 @@ void BIO_vfree(BIO *a) BIO_free(a); } +int BIO_up_ref(BIO *a) +{ + int i; + + if (CRYPTO_atomic_add(&a->references, 1, &i, a->lock) <= 0) + return 0; + + REF_PRINT_COUNT("BIO", a); + REF_ASSERT_ISNT(i < 2); + return ((i > 1) ? 1 : 0); +} + void BIO_clear_flags(BIO *b, int flags) { b->flags &= ~flags; diff --git a/doc/crypto/BIO_new.pod b/doc/crypto/BIO_new.pod index 76679f3da4..d6d87c3901 100644 --- a/doc/crypto/BIO_new.pod +++ b/doc/crypto/BIO_new.pod @@ -2,7 +2,7 @@ =head1 NAME -BIO_new, BIO_set, BIO_free, BIO_vfree, BIO_free_all - BIO allocation and freeing functions +BIO_new, BIO_set, BIO_up_ref, BIO_free, BIO_vfree, BIO_free_all - BIO allocation and freeing functions =head1 SYNOPSIS @@ -10,6 +10,7 @@ BIO_new, BIO_set, BIO_free, BIO_vfree, BIO_free_all - BIO allocation and freeing BIO * BIO_new(BIO_METHOD *type); int BIO_set(BIO *a,BIO_METHOD *type); + int BIO_up_ref(BIO *a); int BIO_free(BIO *a); void BIO_vfree(BIO *a); void BIO_free_all(BIO *a); @@ -20,6 +21,8 @@ The BIO_new() function returns a new BIO using method B. BIO_set() sets the method of an already existing BIO. +BIO_up_ref() increments the reference count associated with the BIO object. + BIO_free() frees up a single BIO, BIO_vfree() also frees up a single BIO but it does not return a value. If B is NULL nothing is done. @@ -36,7 +39,7 @@ If B is NULL nothing is done. BIO_new() returns a newly created BIO or NULL if the call fails. -BIO_set(), BIO_free() return 1 for success and 0 for failure. +BIO_set(), BIO_up_ref() and BIO_free() return 1 for success and 0 for failure. BIO_free_all() and BIO_vfree() do not return values. diff --git a/include/openssl/bio.h b/include/openssl/bio.h index a1b4ced571..ae59948389 100644 --- a/include/openssl/bio.h +++ b/include/openssl/bio.h @@ -326,6 +326,7 @@ struct bio_st { uint64_t num_read; uint64_t num_write; CRYPTO_EX_DATA ex_data; + CRYPTO_RWLOCK *lock; }; DEFINE_STACK_OF(BIO) @@ -635,6 +636,7 @@ BIO *BIO_new(BIO_METHOD *type); int BIO_set(BIO *a, BIO_METHOD *type); int BIO_free(BIO *a); void BIO_vfree(BIO *a); +int BIO_up_ref(BIO *a); int BIO_read(BIO *b, void *data, int len); int BIO_gets(BIO *bp, char *buf, int size); int BIO_write(BIO *b, const void *data, int len); diff --git a/include/openssl/crypto.h b/include/openssl/crypto.h index 53c26b9a7f..a36d7175e1 100644 --- a/include/openssl/crypto.h +++ b/include/openssl/crypto.h @@ -181,7 +181,6 @@ extern "C" { # define CRYPTO_LOCK_RAND 18 # define CRYPTO_LOCK_RAND2 19 # define CRYPTO_LOCK_MALLOC 20 -# define CRYPTO_LOCK_BIO 21 # define CRYPTO_LOCK_READDIR 24 # define CRYPTO_LOCK_RSA_BLINDING 25 # define CRYPTO_LOCK_MALLOC2 27 diff --git a/ssl/bio_ssl.c b/ssl/bio_ssl.c index 9eec0226af..c433cf5073 100644 --- a/ssl/bio_ssl.c +++ b/ssl/bio_ssl.c @@ -338,7 +338,7 @@ static long ssl_ctrl(BIO *b, int cmd, long num, void *ptr) if (b->next_bio != NULL) BIO_push(bio, b->next_bio); b->next_bio = bio; - CRYPTO_add(&bio->references, 1, CRYPTO_LOCK_BIO); + BIO_up_ref(bio); } b->init = 1; break; @@ -371,7 +371,7 @@ static long ssl_ctrl(BIO *b, int cmd, long num, void *ptr) case BIO_CTRL_PUSH: if ((b->next_bio != NULL) && (b->next_bio != ssl->rbio)) { SSL_set_bio(ssl, b->next_bio, b->next_bio); - CRYPTO_add(&b->next_bio->references, 1, CRYPTO_LOCK_BIO); + BIO_up_ref(b); } break; case BIO_CTRL_POP: @@ -384,7 +384,7 @@ static long ssl_ctrl(BIO *b, int cmd, long num, void *ptr) if (ssl->rbio != ssl->wbio) BIO_free_all(ssl->wbio); if (b->next_bio != NULL) - CRYPTO_add(&b->next_bio->references, -1, CRYPTO_LOCK_BIO); + BIO_free(b->next_bio); ssl->wbio = NULL; ssl->rbio = NULL; }