Skip to content

Commit 4237373

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 28c2d31 commit 4237373

File tree

1 file changed

+9
-16
lines changed

1 file changed

+9
-16
lines changed

src/node_crypto.cc

+9-16
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ struct ClearErrorOnReturn {
124124
~ClearErrorOnReturn() { ERR_clear_error(); }
125125
};
126126

127-
static uv_rwlock_t* locks;
127+
static uv_mutex_t* locks;
128128

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

181181
n = CRYPTO_num_locks();
182-
locks = new uv_rwlock_t[n];
182+
locks = new uv_mutex_t[n];
183183

184184
for (i = 0; i < n; i++)
185-
if (uv_rwlock_init(locks + i))
185+
if (uv_mutex_init(locks + i))
186186
abort();
187187
}
188188

189189

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

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

207200

0 commit comments

Comments
 (0)