threads_win.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Copyright 2016-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #if defined(_WIN32)
  10. # include <windows.h>
  11. # if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x600
  12. # define USE_RWLOCK
  13. # endif
  14. #endif
  15. /*
  16. * VC++ 2008 or earlier x86 compilers do not have an inline implementation
  17. * of InterlockedOr64 for 32bit and will fail to run on Windows XP 32bit.
  18. * https://docs.microsoft.com/en-us/cpp/intrinsics/interlockedor-intrinsic-functions#requirements
  19. * To work around this problem, we implement a manual locking mechanism for
  20. * only VC++ 2008 or earlier x86 compilers.
  21. */
  22. #if (defined(_MSC_VER) && defined(_M_IX86) && _MSC_VER <= 1600)
  23. # define NO_INTERLOCKEDOR64
  24. #endif
  25. #include <openssl/crypto.h>
  26. #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG) && defined(OPENSSL_SYS_WINDOWS)
  27. # ifdef USE_RWLOCK
  28. typedef struct {
  29. SRWLOCK lock;
  30. int exclusive;
  31. } CRYPTO_win_rwlock;
  32. # endif
  33. CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
  34. {
  35. CRYPTO_RWLOCK *lock;
  36. # ifdef USE_RWLOCK
  37. CRYPTO_win_rwlock *rwlock;
  38. if ((lock = CRYPTO_zalloc(sizeof(CRYPTO_win_rwlock), NULL, 0)) == NULL)
  39. /* Don't set error, to avoid recursion blowup. */
  40. return NULL;
  41. rwlock = lock;
  42. InitializeSRWLock(&rwlock->lock);
  43. # else
  44. if ((lock = CRYPTO_zalloc(sizeof(CRITICAL_SECTION), NULL, 0)) == NULL)
  45. /* Don't set error, to avoid recursion blowup. */
  46. return NULL;
  47. # if !defined(_WIN32_WCE)
  48. /* 0x400 is the spin count value suggested in the documentation */
  49. if (!InitializeCriticalSectionAndSpinCount(lock, 0x400)) {
  50. OPENSSL_free(lock);
  51. return NULL;
  52. }
  53. # else
  54. InitializeCriticalSection(lock);
  55. # endif
  56. # endif
  57. return lock;
  58. }
  59. __owur int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
  60. {
  61. # ifdef USE_RWLOCK
  62. CRYPTO_win_rwlock *rwlock = lock;
  63. AcquireSRWLockShared(&rwlock->lock);
  64. # else
  65. EnterCriticalSection(lock);
  66. # endif
  67. return 1;
  68. }
  69. __owur int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
  70. {
  71. # ifdef USE_RWLOCK
  72. CRYPTO_win_rwlock *rwlock = lock;
  73. AcquireSRWLockExclusive(&rwlock->lock);
  74. rwlock->exclusive = 1;
  75. # else
  76. EnterCriticalSection(lock);
  77. # endif
  78. return 1;
  79. }
  80. int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
  81. {
  82. # ifdef USE_RWLOCK
  83. CRYPTO_win_rwlock *rwlock = lock;
  84. if (rwlock->exclusive) {
  85. rwlock->exclusive = 0;
  86. ReleaseSRWLockExclusive(&rwlock->lock);
  87. } else {
  88. ReleaseSRWLockShared(&rwlock->lock);
  89. }
  90. # else
  91. LeaveCriticalSection(lock);
  92. # endif
  93. return 1;
  94. }
  95. void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock)
  96. {
  97. if (lock == NULL)
  98. return;
  99. # ifndef USE_RWLOCK
  100. DeleteCriticalSection(lock);
  101. # endif
  102. OPENSSL_free(lock);
  103. return;
  104. }
  105. # define ONCE_UNINITED 0
  106. # define ONCE_ININIT 1
  107. # define ONCE_DONE 2
  108. /*
  109. * We don't use InitOnceExecuteOnce because that isn't available in WinXP which
  110. * we still have to support.
  111. */
  112. int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))
  113. {
  114. LONG volatile *lock = (LONG *)once;
  115. LONG result;
  116. if (*lock == ONCE_DONE)
  117. return 1;
  118. do {
  119. result = InterlockedCompareExchange(lock, ONCE_ININIT, ONCE_UNINITED);
  120. if (result == ONCE_UNINITED) {
  121. init();
  122. *lock = ONCE_DONE;
  123. return 1;
  124. }
  125. } while (result == ONCE_ININIT);
  126. return (*lock == ONCE_DONE);
  127. }
  128. int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
  129. {
  130. *key = TlsAlloc();
  131. if (*key == TLS_OUT_OF_INDEXES)
  132. return 0;
  133. return 1;
  134. }
  135. void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
  136. {
  137. DWORD last_error;
  138. void *ret;
  139. /*
  140. * TlsGetValue clears the last error even on success, so that callers may
  141. * distinguish it successfully returning NULL or failing. It is documented
  142. * to never fail if the argument is a valid index from TlsAlloc, so we do
  143. * not need to handle this.
  144. *
  145. * However, this error-mangling behavior interferes with the caller's use of
  146. * GetLastError. In particular SSL_get_error queries the error queue to
  147. * determine whether the caller should look at the OS's errors. To avoid
  148. * destroying state, save and restore the Windows error.
  149. *
  150. * https://msdn.microsoft.com/en-us/library/windows/desktop/ms686812(v=vs.85).aspx
  151. */
  152. last_error = GetLastError();
  153. ret = TlsGetValue(*key);
  154. SetLastError(last_error);
  155. return ret;
  156. }
  157. int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
  158. {
  159. if (TlsSetValue(*key, val) == 0)
  160. return 0;
  161. return 1;
  162. }
  163. int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
  164. {
  165. if (TlsFree(*key) == 0)
  166. return 0;
  167. return 1;
  168. }
  169. CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
  170. {
  171. return GetCurrentThreadId();
  172. }
  173. int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
  174. {
  175. return (a == b);
  176. }
  177. int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
  178. {
  179. *ret = (int)InterlockedExchangeAdd((long volatile *)val, (long)amount) + amount;
  180. return 1;
  181. }
  182. int CRYPTO_atomic_or(uint64_t *val, uint64_t op, uint64_t *ret,
  183. CRYPTO_RWLOCK *lock)
  184. {
  185. #if (defined(NO_INTERLOCKEDOR64))
  186. if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
  187. return 0;
  188. *val |= op;
  189. *ret = *val;
  190. if (!CRYPTO_THREAD_unlock(lock))
  191. return 0;
  192. return 1;
  193. #else
  194. *ret = (uint64_t)InterlockedOr64((LONG64 volatile *)val, (LONG64)op) | op;
  195. return 1;
  196. #endif
  197. }
  198. int CRYPTO_atomic_load(uint64_t *val, uint64_t *ret, CRYPTO_RWLOCK *lock)
  199. {
  200. #if (defined(NO_INTERLOCKEDOR64))
  201. if (lock == NULL || !CRYPTO_THREAD_read_lock(lock))
  202. return 0;
  203. *ret = *val;
  204. if (!CRYPTO_THREAD_unlock(lock))
  205. return 0;
  206. return 1;
  207. #else
  208. *ret = (uint64_t)InterlockedOr64((LONG64 volatile *)val, 0);
  209. return 1;
  210. #endif
  211. }
  212. int CRYPTO_atomic_load_int(int *val, int *ret, CRYPTO_RWLOCK *lock)
  213. {
  214. #if (defined(NO_INTERLOCKEDOR64))
  215. if (lock == NULL || !CRYPTO_THREAD_read_lock(lock))
  216. return 0;
  217. *ret = *val;
  218. if (!CRYPTO_THREAD_unlock(lock))
  219. return 0;
  220. return 1;
  221. #else
  222. /* On Windows, LONG is always the same size as int. */
  223. *ret = (int)InterlockedOr((LONG volatile *)val, 0);
  224. return 1;
  225. #endif
  226. }
  227. int openssl_init_fork_handlers(void)
  228. {
  229. return 0;
  230. }
  231. int openssl_get_fork_id(void)
  232. {
  233. return 0;
  234. }
  235. #endif