Skip to content

Commit

Permalink
Add support for reference counting using C11 atomics
Browse files Browse the repository at this point in the history
Reviewed-by: Andy Polyakov <[email protected]>
Reviewed-by: Rich Salz <[email protected]>

GH: openssl#1500
  • Loading branch information
kroeckx committed Nov 17, 2016
1 parent b6c6898 commit 2f545ae
Show file tree
Hide file tree
Showing 33 changed files with 145 additions and 61 deletions.
3 changes: 2 additions & 1 deletion crypto/bio/bio_lcl.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#define USE_SOCKETS
#include "e_os.h"
#include "internal/refcount.h"

/* BEGIN BIO_ADDRINFO/BIO_ADDR stuff. */

Expand Down Expand Up @@ -125,7 +126,7 @@ struct bio_st {
void *ptr;
struct bio_st *next_bio; /* used by filter BIOs */
struct bio_st *prev_bio; /* used by filter BIOs */
int references;
CRYPTO_REF_COUNT references;
uint64_t num_read;
uint64_t num_write;
CRYPTO_EX_DATA ex_data;
Expand Down
4 changes: 2 additions & 2 deletions crypto/bio/bio_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ int BIO_free(BIO *a)
if (a == NULL)
return 0;

if (CRYPTO_atomic_add(&a->references, -1, &ret, a->lock) <= 0)
if (CRYPTO_DOWN_REF(&a->references, &ret, a->lock) <= 0)
return 0;

REF_PRINT_COUNT("BIO", a);
Expand Down Expand Up @@ -178,7 +178,7 @@ int BIO_up_ref(BIO *a)
{
int i;

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

REF_PRINT_COUNT("BIO", a);
Expand Down
4 changes: 2 additions & 2 deletions crypto/bio/bio_meth.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ DEFINE_RUN_ONCE_STATIC(do_bio_type_init)

int BIO_get_new_index()
{
static int bio_count = BIO_TYPE_START;
static CRYPTO_REF_COUNT bio_count = BIO_TYPE_START;
int newval;

if (!RUN_ONCE(&bio_type_init, do_bio_type_init)) {
BIOerr(BIO_F_BIO_GET_NEW_INDEX, ERR_R_MALLOC_FAILURE);
return -1;
}
if (!CRYPTO_atomic_add(&bio_count, 1, &newval, bio_type_lock))
if (!CRYPTO_UP_REF(&bio_count, &newval, bio_type_lock))
return -1;
return newval;
}
Expand Down
4 changes: 2 additions & 2 deletions crypto/dh/dh_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ void DH_free(DH *r)
if (r == NULL)
return;

CRYPTO_atomic_add(&r->references, -1, &i, r->lock);
CRYPTO_DOWN_REF(&r->references, &i, r->lock);
REF_PRINT_COUNT("DH", r);
if (i > 0)
return;
Expand Down Expand Up @@ -142,7 +142,7 @@ int DH_up_ref(DH *r)
{
int i;

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

REF_PRINT_COUNT("DH", r);
Expand Down
3 changes: 2 additions & 1 deletion crypto/dh/dh_locl.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

#include <openssl/dh.h>
#include "internal/refcount.h"

struct dh_st {
/*
Expand All @@ -29,7 +30,7 @@ struct dh_st {
unsigned char *seed;
int seedlen;
BIGNUM *counter;
int references;
CRYPTO_REF_COUNT references;
CRYPTO_EX_DATA ex_data;
const DH_METHOD *meth;
ENGINE *engine;
Expand Down
4 changes: 2 additions & 2 deletions crypto/dsa/dsa_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ void DSA_free(DSA *r)
if (r == NULL)
return;

CRYPTO_atomic_add(&r->references, -1, &i, r->lock);
CRYPTO_DOWN_REF(&r->references, &i, r->lock);
REF_PRINT_COUNT("DSA", r);
if (i > 0)
return;
Expand Down Expand Up @@ -148,7 +148,7 @@ int DSA_up_ref(DSA *r)
{
int i;

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

REF_PRINT_COUNT("DSA", r);
Expand Down
3 changes: 2 additions & 1 deletion crypto/dsa/dsa_locl.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

#include <openssl/dsa.h>
#include "internal/refcount.h"

struct dsa_st {
/*
Expand All @@ -24,7 +25,7 @@ struct dsa_st {
int flags;
/* Normally used to cache montgomery values */
BN_MONT_CTX *method_mont_p;
int references;
CRYPTO_REF_COUNT references;
CRYPTO_EX_DATA ex_data;
const DSA_METHOD *meth;
/* functional reference if 'meth' is ENGINE-provided */
Expand Down
4 changes: 2 additions & 2 deletions crypto/dso/dso_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ int DSO_free(DSO *dso)
if (dso == NULL)
return (1);

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

REF_PRINT_COUNT("DSO", dso);
Expand Down Expand Up @@ -107,7 +107,7 @@ int DSO_up_ref(DSO *dso)
return 0;
}

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

REF_PRINT_COUNT("DSO", r);
Expand Down
3 changes: 2 additions & 1 deletion crypto/dso/dso_locl.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "internal/cryptlib.h"
#include "internal/dso.h"
#include "internal/dso_conf.h"
#include "internal/refcount.h"

/**********************************************************************/
/* The low-level handle type used to refer to a loaded shared library */
Expand All @@ -24,7 +25,7 @@ struct dso_st {
* "Handles" and such go in a STACK.
*/
STACK_OF(void) *meth_data;
int references;
CRYPTO_REF_COUNT references;
int flags;
/*
* For use by applications etc ... use this for your bits'n'pieces, don't
Expand Down
4 changes: 2 additions & 2 deletions crypto/ec/ec_key.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void EC_KEY_free(EC_KEY *r)
if (r == NULL)
return;

CRYPTO_atomic_add(&r->references, -1, &i, r->lock);
CRYPTO_DOWN_REF(&r->references, &i, r->lock);
REF_PRINT_COUNT("EC_KEY", r);
if (i > 0)
return;
Expand Down Expand Up @@ -169,7 +169,7 @@ int EC_KEY_up_ref(EC_KEY *r)
{
int i;

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

REF_PRINT_COUNT("EC_KEY", r);
Expand Down
3 changes: 2 additions & 1 deletion crypto/ec/ec_lcl.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <openssl/obj_mac.h>
#include <openssl/ec.h>
#include <openssl/bn.h>
#include "internal/refcount.h"

#include "e_os.h"

Expand Down Expand Up @@ -261,7 +262,7 @@ struct ec_key_st {
BIGNUM *priv_key;
unsigned int enc_flag;
point_conversion_form_t conv_form;
int references;
CRYPTO_REF_COUNT references;
int flags;
CRYPTO_EX_DATA ex_data;
CRYPTO_RWLOCK *lock;
Expand Down
6 changes: 3 additions & 3 deletions crypto/ec/ec_mult.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ struct ec_pre_comp_st {
* generator: 'num' pointers to EC_POINT
* objects followed by a NULL */
size_t num; /* numblocks * 2^(w-1) */
int references;
CRYPTO_REF_COUNT references;
CRYPTO_RWLOCK *lock;
};

Expand Down Expand Up @@ -73,7 +73,7 @@ EC_PRE_COMP *EC_ec_pre_comp_dup(EC_PRE_COMP *pre)
{
int i;
if (pre != NULL)
CRYPTO_atomic_add(&pre->references, 1, &i, pre->lock);
CRYPTO_UP_REF(&pre->references, &i, pre->lock);
return pre;
}

Expand All @@ -84,7 +84,7 @@ void EC_ec_pre_comp_free(EC_PRE_COMP *pre)
if (pre == NULL)
return;

CRYPTO_atomic_add(&pre->references, -1, &i, pre->lock);
CRYPTO_DOWN_REF(&pre->references, &i, pre->lock);
REF_PRINT_COUNT("EC_ec", pre);
if (i > 0)
return;
Expand Down
6 changes: 3 additions & 3 deletions crypto/ec/ecp_nistp224.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ static const felem gmul[2][16][3] = {
/* Precomputation for the group generator. */
struct nistp224_pre_comp_st {
felem g_pre_comp[2][16][3];
int references;
CRYPTO_REF_COUNT references;
CRYPTO_RWLOCK *lock;
};

Expand Down Expand Up @@ -1239,7 +1239,7 @@ NISTP224_PRE_COMP *EC_nistp224_pre_comp_dup(NISTP224_PRE_COMP *p)
{
int i;
if (p != NULL)
CRYPTO_atomic_add(&p->references, 1, &i, p->lock);
CRYPTO_UP_REF(&p->references, &i, p->lock);
return p;
}

Expand All @@ -1250,7 +1250,7 @@ void EC_nistp224_pre_comp_free(NISTP224_PRE_COMP *p)
if (p == NULL)
return;

CRYPTO_atomic_add(&p->references, -1, &i, p->lock);
CRYPTO_DOWN_REF(&p->references, &i, p->lock);
REF_PRINT_COUNT("EC_nistp224", x);
if (i > 0)
return;
Expand Down
6 changes: 3 additions & 3 deletions crypto/ec/ecp_nistp256.c
Original file line number Diff line number Diff line change
Expand Up @@ -1765,7 +1765,7 @@ static void batch_mul(felem x_out, felem y_out, felem z_out,
/* Precomputation for the group generator. */
struct nistp256_pre_comp_st {
smallfelem g_pre_comp[2][16][3];
int references;
CRYPTO_REF_COUNT references;
CRYPTO_RWLOCK *lock;
};

Expand Down Expand Up @@ -1855,7 +1855,7 @@ NISTP256_PRE_COMP *EC_nistp256_pre_comp_dup(NISTP256_PRE_COMP *p)
{
int i;
if (p != NULL)
CRYPTO_atomic_add(&p->references, 1, &i, p->lock);
CRYPTO_UP_REF(&p->references, &i, p->lock);
return p;
}

Expand All @@ -1866,7 +1866,7 @@ void EC_nistp256_pre_comp_free(NISTP256_PRE_COMP *pre)
if (pre == NULL)
return;

CRYPTO_atomic_add(&pre->references, -1, &i, pre->lock);
CRYPTO_DOWN_REF(&pre->references, &i, pre->lock);
REF_PRINT_COUNT("EC_nistp256", x);
if (i > 0)
return;
Expand Down
6 changes: 3 additions & 3 deletions crypto/ec/ecp_nistp521.c
Original file line number Diff line number Diff line change
Expand Up @@ -1594,7 +1594,7 @@ static void batch_mul(felem x_out, felem y_out, felem z_out,
/* Precomputation for the group generator. */
struct nistp521_pre_comp_st {
felem g_pre_comp[16][3];
int references;
CRYPTO_REF_COUNT references;
CRYPTO_RWLOCK *lock;
};

Expand Down Expand Up @@ -1684,7 +1684,7 @@ NISTP521_PRE_COMP *EC_nistp521_pre_comp_dup(NISTP521_PRE_COMP *p)
{
int i;
if (p != NULL)
CRYPTO_atomic_add(&p->references, 1, &i, p->lock);
CRYPTO_UP_REF(&p->references, &i, p->lock);
return p;
}

Expand All @@ -1695,7 +1695,7 @@ void EC_nistp521_pre_comp_free(NISTP521_PRE_COMP *p)
if (p == NULL)
return;

CRYPTO_atomic_add(&p->references, -1, &i, p->lock);
CRYPTO_DOWN_REF(&p->references, &i, p->lock);
REF_PRINT_COUNT("EC_nistp521", x);
if (i > 0)
return;
Expand Down
6 changes: 3 additions & 3 deletions crypto/ec/ecp_nistz256.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ struct nistz256_pre_comp_st {
*/
PRECOMP256_ROW *precomp;
void *precomp_storage;
int references;
CRYPTO_REF_COUNT references;
CRYPTO_RWLOCK *lock;
};

Expand Down Expand Up @@ -1477,7 +1477,7 @@ NISTZ256_PRE_COMP *EC_nistz256_pre_comp_dup(NISTZ256_PRE_COMP *p)
{
int i;
if (p != NULL)
CRYPTO_atomic_add(&p->references, 1, &i, p->lock);
CRYPTO_UP_REF(&p->references, &i, p->lock);
return p;
}

Expand All @@ -1488,7 +1488,7 @@ void EC_nistz256_pre_comp_free(NISTZ256_PRE_COMP *pre)
if (pre == NULL)
return;

CRYPTO_atomic_add(&pre->references, -1, &i, pre->lock);
CRYPTO_DOWN_REF(&pre->references, &i, pre->lock);
REF_PRINT_COUNT("EC_nistz256", x);
if (i > 0)
return;
Expand Down
3 changes: 2 additions & 1 deletion crypto/engine/eng_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
# include "internal/cryptlib.h"
# include <internal/engine.h>
# include <internal/thread_once.h>
# include "internal/refcount.h"

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -156,7 +157,7 @@ struct engine_st {
const ENGINE_CMD_DEFN *cmd_defns;
int flags;
/* reference count on the structure itself */
int struct_ref;
CRYPTO_REF_COUNT struct_ref;
/*
* reference count on usability of the engine type. NB: This controls the
* loading and initialisation of any functionality required by this
Expand Down
5 changes: 5 additions & 0 deletions crypto/engine/eng_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "eng_int.h"
#include <openssl/rand.h>
#include "internal/refcount.h"

CRYPTO_RWLOCK *global_engine_lock;

Expand Down Expand Up @@ -72,10 +73,14 @@ int engine_free_util(ENGINE *e, int locked)

if (e == NULL)
return 1;
#ifdef HAVE_ATOMICS
CRYPTO_DOWN_REF(&e->struct_ref, &i, global_engine_lock);
#else
if (locked)
CRYPTO_atomic_add(&e->struct_ref, -1, &i, global_engine_lock);
else
i = --e->struct_ref;
#endif
engine_ref_debug(e, 0, -1)
if (i > 0)
return 1;
Expand Down
2 changes: 1 addition & 1 deletion crypto/engine/eng_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,6 @@ int ENGINE_up_ref(ENGINE *e)
ENGINEerr(ENGINE_F_ENGINE_UP_REF, ERR_R_PASSED_NULL_PARAMETER);
return 0;
}
CRYPTO_atomic_add(&e->struct_ref, 1, &i, global_engine_lock);
CRYPTO_UP_REF(&e->struct_ref, &i, global_engine_lock);
return 1;
}
4 changes: 2 additions & 2 deletions crypto/evp/p_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ int EVP_PKEY_up_ref(EVP_PKEY *pkey)
{
int i;

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

REF_PRINT_COUNT("EVP_PKEY", pkey);
Expand Down Expand Up @@ -392,7 +392,7 @@ void EVP_PKEY_free(EVP_PKEY *x)
if (x == NULL)
return;

CRYPTO_atomic_add(&x->references, -1, &i, x->lock);
CRYPTO_DOWN_REF(&x->references, &i, x->lock);
REF_PRINT_COUNT("EVP_PKEY", x);
if (i > 0)
return;
Expand Down
Loading

0 comments on commit 2f545ae

Please sign in to comment.