Skip to content

Commit e669b27

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 572663f commit e669b27

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
@@ -106,7 +106,7 @@ static Persistent<String> sessionid_sym;
106106

107107
static Persistent<FunctionTemplate> secure_context_constructor;
108108

109-
static uv_rwlock_t* locks;
109+
static uv_mutex_t* locks;
110110

111111

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

120120
n = CRYPTO_num_locks();
121-
locks = new uv_rwlock_t[n];
121+
locks = new uv_mutex_t[n];
122122

123123
for (i = 0; i < n; i++)
124-
if (uv_rwlock_init(locks + i))
124+
if (uv_mutex_init(locks + i))
125125
abort();
126126
}
127127

128128

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

133-
if (mode & CRYPTO_LOCK) {
134-
if (mode & CRYPTO_READ)
135-
uv_rwlock_rdlock(locks + n);
136-
else
137-
uv_rwlock_wrlock(locks + n);
138-
} else {
139-
if (mode & CRYPTO_READ)
140-
uv_rwlock_rdunlock(locks + n);
141-
else
142-
uv_rwlock_wrunlock(locks + n);
143-
}
133+
if (mode & CRYPTO_LOCK)
134+
uv_mutex_lock(locks + n);
135+
else
136+
uv_mutex_unlock(locks + n);
144137
}
145138

146139

0 commit comments

Comments
 (0)