Skip to content

Commit

Permalink
crypto: replace rwlocks with simple mutexes
Browse files Browse the repository at this point in the history
It was pointed out by Zhou Ran that the Windows XP implementation of
uv_rwlock_rdlock() and friends may unlock the inner write mutex on a
different thread than the one that locked it, resulting in undefined
behavior.

The only place that uses rwlocks is the crypto module.  Make that use
normal (simple) mutexes instead.

OpenSSL's critical sections are generally very short, with exclusive
access outnumbering shared access by a factor of three or more, so
it's not as if using rwlocks gives a decisive performance advantage.

PR-URL: nodejs#2723
Reviewed-By: Fedor Indutny <fedor@indutny.com>
  • Loading branch information
bnoordhuis authored and rvagg committed Sep 11, 2015
1 parent 572663f commit e669b27
Showing 1 changed file with 9 additions and 16 deletions.
25 changes: 9 additions & 16 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ static Persistent<String> sessionid_sym;

static Persistent<FunctionTemplate> secure_context_constructor;

static uv_rwlock_t* locks;
static uv_mutex_t* locks;


static void crypto_threadid_cb(CRYPTO_THREADID* tid) {
Expand All @@ -118,29 +118,22 @@ static void crypto_lock_init(void) {
int i, n;

n = CRYPTO_num_locks();
locks = new uv_rwlock_t[n];
locks = new uv_mutex_t[n];

for (i = 0; i < n; i++)
if (uv_rwlock_init(locks + i))
if (uv_mutex_init(locks + i))
abort();
}


static void crypto_lock_cb(int mode, int n, const char* file, int line) {
assert((mode & CRYPTO_LOCK) || (mode & CRYPTO_UNLOCK));
assert((mode & CRYPTO_READ) || (mode & CRYPTO_WRITE));
assert(!(mode & CRYPTO_LOCK) ^ !(mode & CRYPTO_UNLOCK));
assert(!(mode & CRYPTO_READ) ^ !(mode & CRYPTO_WRITE));

if (mode & CRYPTO_LOCK) {
if (mode & CRYPTO_READ)
uv_rwlock_rdlock(locks + n);
else
uv_rwlock_wrlock(locks + n);
} else {
if (mode & CRYPTO_READ)
uv_rwlock_rdunlock(locks + n);
else
uv_rwlock_wrunlock(locks + n);
}
if (mode & CRYPTO_LOCK)
uv_mutex_lock(locks + n);
else
uv_mutex_unlock(locks + n);
}


Expand Down

0 comments on commit e669b27

Please sign in to comment.