Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Atomics #21260

Closed
wants to merge 24 commits into from
Closed

Atomics #21260

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
1476292
refcnt: convert references counts to a structure
paulidale Jun 21, 2023
3b8afe2
engine: update to structure based atomics
paulidale Jun 21, 2023
e42d429
bio: update to structure based atomics
paulidale Jun 21, 2023
0a39d00
dh: update to structure based atomics
paulidale Jun 21, 2023
bad5f91
dsa: update to structure based atomics
paulidale Jun 21, 2023
921364a
dso: update to structure based atomics
paulidale Jun 21, 2023
638d8a0
asn1: update to structure based atomics
paulidale Jun 21, 2023
4b4912d
ec: update to structure based atomics
paulidale Jun 21, 2023
0d7d6bd
ecx: update to structure based atomics
paulidale Jun 21, 2023
73f43ff
provider: update to structure based atomics
paulidale Jun 21, 2023
e687193
rsa: update to structure based atomics
paulidale Jun 21, 2023
3d4b52e
store: update to structure based atomics
paulidale Jun 21, 2023
1268c75
test: update to structure based atomics
paulidale Jun 21, 2023
4d67c68
tls: update to structure based atomics
paulidale Jun 21, 2023
c1eb3e0
quic: update to structure based atomics
paulidale Jun 21, 2023
2d2ff87
encoder: update to structure based atomics
paulidale Jun 21, 2023
f0ce130
prov(legacy): update to structure based atomics
paulidale Jun 21, 2023
f03c85d
x509: update to structure based atomics
paulidale Jun 21, 2023
3e94623
evp: update to structure based atomics
paulidale Jun 21, 2023
c0acdf1
fixup! asn1: update to structure based atomics
paulidale Jun 25, 2023
62df7fb
fixup! bio: update to structure based atomics
paulidale Jun 26, 2023
cc9fa3b
fixup! evp: update to structure based atomics
paulidale Jun 28, 2023
224bd65
fixup! provider: update to structure based atomics
paulidale Jun 28, 2023
0d89e8f
fixup! provider: update to structure based atomics
paulidale Jun 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions crypto/asn1/tasn_utl.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ int ossl_asn1_set_choice_selector(ASN1_VALUE **pval, int value,
/*
* Do atomic reference counting. The value 'op' decides what to do.
* If it is +1 then the count is incremented.
* If |op| is 0, lock is initialised and count is set to 1.
* If |op| is 0, count is initialised and set to 1.
* If |op| is -1, count is decremented and the return value is the current
* reference count or 0 if no reference count is active.
* It returns -1 on initialisation error.
Expand All @@ -68,8 +68,8 @@ int ossl_asn1_set_choice_selector(ASN1_VALUE **pval, int value,
int ossl_asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it)
{
const ASN1_AUX *aux;
CRYPTO_REF_COUNT *lck;
CRYPTO_RWLOCK **lock;
kroeckx marked this conversation as resolved.
Show resolved Hide resolved
CRYPTO_REF_COUNT *refcnt;
int ret = -1;

if ((it->itype != ASN1_ITYPE_SEQUENCE)
Expand All @@ -78,30 +78,34 @@ int ossl_asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it)
aux = it->funcs;
if (aux == NULL || (aux->flags & ASN1_AFLG_REFCOUNT) == 0)
return 0;
lck = offset2ptr(*pval, aux->ref_offset);
lock = offset2ptr(*pval, aux->ref_lock);
refcnt = offset2ptr(*pval, aux->ref_offset);

switch (op) {
case 0:
*lck = ret = 1;
if (!CRYPTO_NEW_REF(refcnt, 1))
return -1;
*lock = CRYPTO_THREAD_lock_new();
mattcaswell marked this conversation as resolved.
Show resolved Hide resolved
if (*lock == NULL) {
CRYPTO_FREE_REF(refcnt);
ERR_raise(ERR_LIB_ASN1, ERR_R_CRYPTO_LIB);
return -1;
}
ret = 1;
break;
case 1:
if (!CRYPTO_UP_REF(lck, &ret, *lock))
if (!CRYPTO_UP_REF(refcnt, &ret))
return -1;
break;
case -1:
if (!CRYPTO_DOWN_REF(lck, &ret, *lock))
if (!CRYPTO_DOWN_REF(refcnt, &ret))
return -1; /* failed */
REF_PRINT_EX(it->sname, ret, (void *)it);
REF_ASSERT_ISNT(ret < 0);
if (ret == 0) {
CRYPTO_THREAD_lock_free(*lock);
mattcaswell marked this conversation as resolved.
Show resolved Hide resolved
*lock = NULL;
CRYPTO_FREE_REF(refcnt);
}
break;
}
Expand Down
23 changes: 9 additions & 14 deletions crypto/bio/bio_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data))
if (!CRYPTO_NEW_REF(&bio->references, 1))
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);
if (!CRYPTO_new_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);
paulidale marked this conversation as resolved.
Show resolved Hide resolved
goto err;
}
if (method->create == NULL)
Expand All @@ -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;
}
Expand All @@ -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);
Expand All @@ -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);

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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. */
Expand Down Expand Up @@ -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(...) */
Expand Down
3 changes: 1 addition & 2 deletions crypto/bio/bio_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);

Expand Down
8 changes: 3 additions & 5 deletions crypto/bio/bio_meth.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,24 @@
#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)) {
/* Perhaps the error should be raised in do_bio_type_init()? */
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))
paulidale marked this conversation as resolved.
Show resolved Hide resolved
return -1;
return newval;
}
Expand Down
10 changes: 4 additions & 6 deletions crypto/bio/ossl_core_bio.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,36 @@
*/
struct ossl_core_bio_st {
CRYPTO_REF_COUNT ref_cnt;
CRYPTO_RWLOCK *ref_lock;
BIO *bio;
};

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;
}

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)
{
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);
}
}
Expand Down
9 changes: 6 additions & 3 deletions crypto/dh/dh_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,16 @@ static DH *dh_new_intern(ENGINE *engine, OSSL_LIB_CTX *libctx)
if (ret == NULL)
return NULL;

ret->references = 1;
ret->lock = CRYPTO_THREAD_lock_new();
if (ret->lock == NULL) {
ERR_raise(ERR_LIB_DH, ERR_R_CRYPTO_LIB);
OPENSSL_free(ret);
return NULL;
}

if (!CRYPTO_NEW_REF(&ret->references, 1))
goto err;
paulidale marked this conversation as resolved.
Show resolved Hide resolved

ret->libctx = libctx;
ret->meth = DH_get_default_method();
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
Expand Down Expand Up @@ -133,7 +135,7 @@ void DH_free(DH *r)
if (r == NULL)
return;

CRYPTO_DOWN_REF(&r->references, &i, r->lock);
CRYPTO_DOWN_REF(&r->references, &i);
REF_PRINT_COUNT("DH", r);
if (i > 0)
return;
Expand All @@ -149,6 +151,7 @@ void DH_free(DH *r)
#endif

CRYPTO_THREAD_lock_free(r->lock);
CRYPTO_FREE_REF(&r->references);

ossl_ffc_params_cleanup(&r->params);
BN_clear_free(r->pub_key);
Expand All @@ -160,7 +163,7 @@ int DH_up_ref(DH *r)
{
int i;

if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
if (CRYPTO_UP_REF(&r->references, &i) <= 0)
return 0;

REF_PRINT_COUNT("DH", r);
Expand Down
9 changes: 6 additions & 3 deletions crypto/dsa/dsa_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,16 @@ static DSA *dsa_new_intern(ENGINE *engine, OSSL_LIB_CTX *libctx)
if (ret == NULL)
return NULL;

ret->references = 1;
ret->lock = CRYPTO_THREAD_lock_new();
if (ret->lock == NULL) {
ERR_raise(ERR_LIB_DSA, ERR_R_CRYPTO_LIB);
OPENSSL_free(ret);
return NULL;
}

if (!CRYPTO_NEW_REF(&ret->references, 1))
goto err;
paulidale marked this conversation as resolved.
Show resolved Hide resolved

ret->libctx = libctx;
ret->meth = DSA_get_default_method();
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
Expand Down Expand Up @@ -210,7 +212,7 @@ void DSA_free(DSA *r)
if (r == NULL)
return;

CRYPTO_DOWN_REF(&r->references, &i, r->lock);
CRYPTO_DOWN_REF(&r->references, &i);
REF_PRINT_COUNT("DSA", r);
if (i > 0)
return;
Expand All @@ -227,6 +229,7 @@ void DSA_free(DSA *r)
#endif

CRYPTO_THREAD_lock_free(r->lock);
CRYPTO_FREE_REF(&r->references);

ossl_ffc_params_cleanup(&r->params);
BN_clear_free(r->pub_key);
Expand All @@ -238,7 +241,7 @@ int DSA_up_ref(DSA *r)
{
int i;

if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
if (CRYPTO_UP_REF(&r->references, &i) <= 0)
return 0;

REF_PRINT_COUNT("DSA", r);
Expand Down
11 changes: 4 additions & 7 deletions crypto/dso/dso_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ static DSO *DSO_new_method(DSO_METHOD *meth)
return NULL;
}
ret->meth = DSO_METHOD_openssl();
ret->references = 1;
ret->lock = CRYPTO_THREAD_lock_new();
if (ret->lock == NULL) {
ERR_raise(ERR_LIB_DSO, ERR_R_CRYPTO_LIB);
if (!CRYPTO_NEW_REF(&ret->references, 1)) {
sk_void_free(ret->meth_data);
OPENSSL_free(ret);
return NULL;
Expand All @@ -54,7 +51,7 @@ int DSO_free(DSO *dso)
if (dso == NULL)
return 1;

if (CRYPTO_DOWN_REF(&dso->references, &i, dso->lock) <= 0)
if (CRYPTO_DOWN_REF(&dso->references, &i) <= 0)
return 0;

REF_PRINT_COUNT("DSO", dso);
Expand All @@ -77,7 +74,7 @@ int DSO_free(DSO *dso)
sk_void_free(dso->meth_data);
OPENSSL_free(dso->filename);
OPENSSL_free(dso->loaded_filename);
CRYPTO_THREAD_lock_free(dso->lock);
CRYPTO_FREE_REF(&dso->references);
OPENSSL_free(dso);
return 1;
}
Expand All @@ -96,7 +93,7 @@ int DSO_up_ref(DSO *dso)
return 0;
}

if (CRYPTO_UP_REF(&dso->references, &i, dso->lock) <= 0)
if (CRYPTO_UP_REF(&dso->references, &i) <= 0)
return 0;

REF_PRINT_COUNT("DSO", dso);
Expand Down
1 change: 0 additions & 1 deletion crypto/dso/dso_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ struct dso_st {
* loaded.
*/
char *loaded_filename;
CRYPTO_RWLOCK *lock;
};

struct dso_meth_st {
Expand Down
6 changes: 3 additions & 3 deletions crypto/ec/ec_key.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void EC_KEY_free(EC_KEY *r)
if (r == NULL)
return;

CRYPTO_DOWN_REF(&r->references, &i, r->lock);
CRYPTO_DOWN_REF(&r->references, &i);
REF_PRINT_COUNT("EC_KEY", r);
if (i > 0)
return;
Expand All @@ -94,7 +94,7 @@ void EC_KEY_free(EC_KEY *r)
#ifndef FIPS_MODULE
CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EC_KEY, r, &r->ex_data);
#endif
CRYPTO_THREAD_lock_free(r->lock);
CRYPTO_FREE_REF(&r->references);
EC_GROUP_free(r->group);
EC_POINT_free(r->pub_key);
BN_clear_free(r->priv_key);
Expand Down Expand Up @@ -194,7 +194,7 @@ int EC_KEY_up_ref(EC_KEY *r)
{
int i;

if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
if (CRYPTO_UP_REF(&r->references, &i) <= 0)
return 0;

REF_PRINT_COUNT("EC_KEY", r);
Expand Down
Loading
Loading