Skip to content

Commit 071c860

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 <fedor@indutny.com>
1 parent 2860c53 commit 071c860

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
@@ -113,7 +113,7 @@ struct ClearErrorOnReturn {
113113
~ClearErrorOnReturn() { ERR_clear_error(); }
114114
};
115115

116-
static uv_rwlock_t* locks;
116+
static uv_mutex_t* locks;
117117

118118
const char* root_certs[] = {
119119
#include "node_root_certs.h" // NOLINT(build/include_order)
@@ -165,29 +165,22 @@ static void crypto_lock_init(void) {
165165
int i, n;
166166

167167
n = CRYPTO_num_locks();
168-
locks = new uv_rwlock_t[n];
168+
locks = new uv_mutex_t[n];
169169

170170
for (i = 0; i < n; i++)
171-
if (uv_rwlock_init(locks + i))
171+
if (uv_mutex_init(locks + i))
172172
abort();
173173
}
174174

175175

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

180-
if (mode & CRYPTO_LOCK) {
181-
if (mode & CRYPTO_READ)
182-
uv_rwlock_rdlock(locks + n);
183-
else
184-
uv_rwlock_wrlock(locks + n);
185-
} else {
186-
if (mode & CRYPTO_READ)
187-
uv_rwlock_rdunlock(locks + n);
188-
else
189-
uv_rwlock_wrunlock(locks + n);
190-
}
180+
if (mode & CRYPTO_LOCK)
181+
uv_mutex_lock(locks + n);
182+
else
183+
uv_mutex_unlock(locks + n);
191184
}
192185

193186

0 commit comments

Comments
 (0)