From: Ben Noordhuis Date: Mon, 7 Sep 2015 12:32:00 +0000 (+0200) Subject: crypto: replace rwlocks with simple mutexes X-Git-Tag: v4.0.0~9 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=03f900ab25b951800783a1a7a583280006fb1731;p=platform%2Fupstream%2Fnodejs.git crypto: replace rwlocks with simple mutexes 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: https://github.com/nodejs/node/pull/2723 Reviewed-By: Fedor Indutny --- diff --git a/src/node_crypto.cc b/src/node_crypto.cc index bf1f369..40f065e 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -123,7 +123,7 @@ struct ClearErrorOnReturn { ~ClearErrorOnReturn() { ERR_clear_error(); } }; -static uv_rwlock_t* locks; +static uv_mutex_t* locks; const char* const root_certs[] = { #include "node_root_certs.h" // NOLINT(build/include_order) @@ -178,29 +178,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) { - CHECK((mode & CRYPTO_LOCK) || (mode & CRYPTO_UNLOCK)); - CHECK((mode & CRYPTO_READ) || (mode & CRYPTO_WRITE)); + CHECK(!(mode & CRYPTO_LOCK) ^ !(mode & CRYPTO_UNLOCK)); + CHECK(!(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); }