Skip to content

Loading…

openssl: some thread safety improvements #4105

Closed
wants to merge 1 commit into from

2 participants

@indutny
Node.js Foundation member
  • BIO: add BIO_TYPE_NO_EX_DATA option to allow constructing BIOs without ex data
  • CRYPTO: CRYPTO_add_lock should use atomic instructions where possible

/cc @bnoordhuis


I'm also going to send those patches to upstream... watch openssl-dev mailing list.

@indutny indutny openssl: some thread safety improvements
* BIO: add BIO_TYPE_NO_EX_DATA option to allow constructing BIOs without
  ex data
* CRYPTO: CRYPTO_add_lock should use atomic instructions where possible
b2bc377
@indutny
Node.js Foundation member

Also, it would be good to backport that to v0.8 ... /cc @isaacs

@bnoordhuis
Node.js Foundation member

Fedor, I think posting some benchmark numbers to openssl-dev will help your case.

@indutny
Node.js Foundation member

Not relevant anymore, guy on openssl-dev@ shown me a better way to do this stuff.

@indutny indutny closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 9, 2012
  1. @indutny

    openssl: some thread safety improvements

    indutny committed
    * BIO: add BIO_TYPE_NO_EX_DATA option to allow constructing BIOs without
      ex data
    * CRYPTO: CRYPTO_add_lock should use atomic instructions where possible
View
1 deps/openssl/openssl/crypto/bio/bio.h
@@ -112,6 +112,7 @@ extern "C" {
#define BIO_TYPE_DESCRIPTOR 0x0100 /* socket, fd, connect or accept */
#define BIO_TYPE_FILTER 0x0200
#define BIO_TYPE_SOURCE_SINK 0x0400
+#define BIO_TYPE_NO_EX_DATA 0x0800
/* BIO_FILENAME_READ|BIO_CLOSE to open or close on free.
* BIO_set_fp(in,stdin,BIO_NOCLOSE); */
View
22 deps/openssl/openssl/crypto/bio/bio_lib.c
@@ -97,11 +97,13 @@ int BIO_set(BIO *bio, BIO_METHOD *method)
bio->references=1;
bio->num_read=0L;
bio->num_write=0L;
- CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
+ if ((method->type & BIO_TYPE_NO_EX_DATA) == 0)
+ CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
if (method->create != NULL)
if (!method->create(bio))
{
- CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio,
+ if ((method->type & BIO_TYPE_NO_EX_DATA) == 0)
+ CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio,
&bio->ex_data);
return(0);
}
@@ -130,7 +132,8 @@ int BIO_free(BIO *a)
((i=(int)a->callback(a,BIO_CB_FREE,NULL,0,0L,1L)) <= 0))
return(i);
- CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, a, &a->ex_data);
+ if ((a->method->type & BIO_TYPE_NO_EX_DATA) == 0)
+ CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, a, &a->ex_data);
if ((a->method == NULL) || (a->method->destroy == NULL)) return(1);
a->method->destroy(a);
@@ -542,8 +545,9 @@ BIO *BIO_dup_chain(BIO *in)
}
/* copy app data */
- if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_BIO, &new_bio->ex_data,
- &bio->ex_data))
+ if ((bio->method->type & BIO_TYPE_NO_EX_DATA) == 0)
+ if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_BIO, &new_bio->ex_data,
+ &bio->ex_data))
goto err;
if (ret == NULL)
@@ -579,12 +583,16 @@ int BIO_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
int BIO_set_ex_data(BIO *bio, int idx, void *data)
{
- return(CRYPTO_set_ex_data(&(bio->ex_data),idx,data));
+ if ((bio->method->type & BIO_TYPE_NO_EX_DATA) == 0)
+ return(CRYPTO_set_ex_data(&(bio->ex_data),idx,data));
+ return -1;
}
void *BIO_get_ex_data(BIO *bio, int idx)
{
- return(CRYPTO_get_ex_data(&(bio->ex_data),idx));
+ if ((bio->method->type & BIO_TYPE_NO_EX_DATA) == 0)
+ return(CRYPTO_get_ex_data(&(bio->ex_data),idx));
+ return NULL;
}
unsigned long BIO_number_read(BIO *bio)
View
5 deps/openssl/openssl/crypto/cryptlib.c
@@ -629,6 +629,10 @@ int CRYPTO_add_lock(int *pointer, int amount, int type, const char *file,
}
else
{
+#if defined(__GNUC__) && \
+ ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1))
+ return __sync_add_and_fetch(pointer, amount);
+#else
CRYPTO_lock(CRYPTO_LOCK|CRYPTO_WRITE,type,file,line);
ret= *pointer+amount;
@@ -645,6 +649,7 @@ int CRYPTO_add_lock(int *pointer, int amount, int type, const char *file,
#endif
*pointer=ret;
CRYPTO_lock(CRYPTO_UNLOCK|CRYPTO_WRITE,type,file,line);
+#endif
}
return(ret);
}
Something went wrong with that request. Please try again.