bio: update to structure based atomics

Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/21260)
This commit is contained in:
Pauli 2023-06-22 09:26:38 +10:00
parent e3620700a7
commit a22d1966bb
4 changed files with 18 additions and 28 deletions

View File

@ -88,22 +88,17 @@ BIO *BIO_new_ex(OSSL_LIB_CTX *libctx, const BIO_METHOD *method)
bio->libctx = libctx;
bio->method = method;
bio->shutdown = 1;
bio->references = 1;
if (!CRYPTO_NEW_REF(&bio->references, 1))
goto err;
if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data))
goto err;
bio->lock = CRYPTO_THREAD_lock_new();
if (bio->lock == NULL) {
ERR_raise(ERR_LIB_BIO, ERR_R_CRYPTO_LIB);
CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
goto err;
}
if (method->create != NULL && !method->create(bio)) {
ERR_raise(ERR_LIB_BIO, ERR_R_INIT_FAIL);
CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
CRYPTO_THREAD_lock_free(bio->lock);
CRYPTO_FREE_REF(&bio->references);
goto err;
}
if (method->create == NULL)
@ -112,6 +107,7 @@ BIO *BIO_new_ex(OSSL_LIB_CTX *libctx, const BIO_METHOD *method)
return bio;
err:
CRYPTO_FREE_REF(&bio->references);
OPENSSL_free(bio);
return NULL;
}
@ -128,7 +124,7 @@ int BIO_free(BIO *a)
if (a == NULL)
return 0;
if (CRYPTO_DOWN_REF(&a->references, &ret, a->lock) <= 0)
if (CRYPTO_DOWN_REF(&a->references, &ret) <= 0)
return 0;
REF_PRINT_COUNT("BIO", a);
@ -147,7 +143,7 @@ int BIO_free(BIO *a)
CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, a, &a->ex_data);
CRYPTO_THREAD_lock_free(a->lock);
CRYPTO_FREE_REF(&a->references);
OPENSSL_free(a);
@ -193,7 +189,7 @@ int BIO_up_ref(BIO *a)
{
int i;
if (CRYPTO_UP_REF(&a->references, &i, a->lock) <= 0)
if (CRYPTO_UP_REF(&a->references, &i) <= 0)
return 0;
REF_PRINT_COUNT("BIO", a);
@ -858,7 +854,7 @@ void BIO_free_all(BIO *bio)
while (bio != NULL) {
b = bio;
ref = b->references;
CRYPTO_GET_REF(&b->references, &ref);
bio = bio->next_bio;
BIO_free(b);
/* Since ref count > 1, don't free anyone else. */
@ -955,8 +951,7 @@ void bio_cleanup(void)
CRYPTO_THREAD_lock_free(bio_lookup_lock);
bio_lookup_lock = NULL;
#endif
CRYPTO_THREAD_lock_free(bio_type_lock);
bio_type_lock = NULL;
CRYPTO_FREE_REF(&bio_type_count);
}
/* Internal variant of the below BIO_wait() not calling ERR_raise(...) */

View File

@ -116,7 +116,6 @@ struct bio_st {
uint64_t num_read;
uint64_t num_write;
CRYPTO_EX_DATA ex_data;
CRYPTO_RWLOCK *lock;
};
#ifndef OPENSSL_NO_SOCK
@ -140,7 +139,7 @@ extern LPFN_WSASENDMSG bio_WSASendMsg;
# endif
#endif
extern CRYPTO_RWLOCK *bio_type_lock;
extern CRYPTO_REF_COUNT bio_type_count;
void bio_sock_cleanup_int(void);

View File

@ -10,18 +10,16 @@
#include "bio_local.h"
#include "internal/thread_once.h"
CRYPTO_RWLOCK *bio_type_lock = NULL;
CRYPTO_REF_COUNT bio_type_count;
static CRYPTO_ONCE bio_type_init = CRYPTO_ONCE_STATIC_INIT;
DEFINE_RUN_ONCE_STATIC(do_bio_type_init)
{
bio_type_lock = CRYPTO_THREAD_lock_new();
return bio_type_lock != NULL;
return CRYPTO_NEW_REF(&bio_type_count, BIO_TYPE_START);
}
int BIO_get_new_index(void)
{
static CRYPTO_REF_COUNT bio_count = BIO_TYPE_START;
int newval;
if (!RUN_ONCE(&bio_type_init, do_bio_type_init)) {
@ -29,7 +27,7 @@ int BIO_get_new_index(void)
ERR_raise(ERR_LIB_BIO, ERR_R_CRYPTO_LIB);
return -1;
}
if (!CRYPTO_UP_REF(&bio_count, &newval, bio_type_lock))
if (!CRYPTO_UP_REF(&bio_type_count, &newval))
return -1;
return newval;
}

View File

@ -17,7 +17,6 @@
*/
struct ossl_core_bio_st {
CRYPTO_REF_COUNT ref_cnt;
CRYPTO_RWLOCK *ref_lock;
BIO *bio;
};
@ -25,11 +24,10 @@ static OSSL_CORE_BIO *core_bio_new(void)
{
OSSL_CORE_BIO *cb = OPENSSL_malloc(sizeof(*cb));
if (cb == NULL || (cb->ref_lock = CRYPTO_THREAD_lock_new()) == NULL) {
if (cb == NULL || !CRYPTO_NEW_REF(&cb->ref_cnt, 1)) {
OPENSSL_free(cb);
return NULL;
}
cb->ref_cnt = 1;
return cb;
}
@ -37,7 +35,7 @@ int ossl_core_bio_up_ref(OSSL_CORE_BIO *cb)
{
int ref = 0;
return CRYPTO_UP_REF(&cb->ref_cnt, &ref, cb->ref_lock);
return CRYPTO_UP_REF(&cb->ref_cnt, &ref);
}
int ossl_core_bio_free(OSSL_CORE_BIO *cb)
@ -45,10 +43,10 @@ int ossl_core_bio_free(OSSL_CORE_BIO *cb)
int ref = 0, res = 1;
if (cb != NULL) {
CRYPTO_DOWN_REF(&cb->ref_cnt, &ref, cb->ref_lock);
CRYPTO_DOWN_REF(&cb->ref_cnt, &ref);
if (ref <= 0) {
res = BIO_free(cb->bio);
CRYPTO_THREAD_lock_free(cb->ref_lock);
CRYPTO_FREE_REF(&cb->ref_cnt);
OPENSSL_free(cb);
}
}