Skip to content

Commit 337e1e0

Browse files
bnoordhuisrvagg
authored andcommitted
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: #2723 Reviewed-By: Fedor Indutny <[email protected]>
1 parent 6efb697 commit 337e1e0

File tree

1 file changed

+9
-16
lines changed

1 file changed

+9
-16
lines changed

src/node_crypto.cc

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ struct ClearErrorOnReturn {
123123
~ClearErrorOnReturn() { ERR_clear_error(); }
124124
};
125125

126-
static uv_rwlock_t* locks;
126+
static uv_mutex_t* locks;
127127

128128
const char* const root_certs[] = {
129129
#include "node_root_certs.h" // NOLINT(build/include_order)
@@ -178,29 +178,22 @@ static void crypto_lock_init(void) {
178178
int i, n;
179179

180180
n = CRYPTO_num_locks();
181-
locks = new uv_rwlock_t[n];
181+
locks = new uv_mutex_t[n];
182182

183183
for (i = 0; i < n; i++)
184-
if (uv_rwlock_init(locks + i))
184+
if (uv_mutex_init(locks + i))
185185
abort();
186186
}
187187

188188

189189
static void crypto_lock_cb(int mode, int n, const char* file, int line) {
190-
CHECK((mode & CRYPTO_LOCK) || (mode & CRYPTO_UNLOCK));
191-
CHECK((mode & CRYPTO_READ) || (mode & CRYPTO_WRITE));
190+
CHECK(!(mode & CRYPTO_LOCK) ^ !(mode & CRYPTO_UNLOCK));
191+
CHECK(!(mode & CRYPTO_READ) ^ !(mode & CRYPTO_WRITE));
192192

193-
if (mode & CRYPTO_LOCK) {
194-
if (mode & CRYPTO_READ)
195-
uv_rwlock_rdlock(locks + n);
196-
else
197-
uv_rwlock_wrlock(locks + n);
198-
} else {
199-
if (mode & CRYPTO_READ)
200-
uv_rwlock_rdunlock(locks + n);
201-
else
202-
uv_rwlock_wrunlock(locks + n);
203-
}
193+
if (mode & CRYPTO_LOCK)
194+
uv_mutex_lock(locks + n);
195+
else
196+
uv_mutex_unlock(locks + n);
204197
}
205198

206199

0 commit comments

Comments
 (0)