thread_win.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /* Copyright (c) 2015, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include "internal.h"
  15. #if defined(OPENSSL_WINDOWS_THREADS)
  16. OPENSSL_MSVC_PRAGMA(warning(push, 3))
  17. #include <windows.h>
  18. OPENSSL_MSVC_PRAGMA(warning(pop))
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <openssl/mem.h>
  22. #include <openssl/type_check.h>
  23. OPENSSL_STATIC_ASSERT(sizeof(CRYPTO_MUTEX) >= sizeof(SRWLOCK),
  24. "CRYPTO_MUTEX is too small");
  25. static BOOL CALLBACK call_once_init(INIT_ONCE *once, void *arg, void **out) {
  26. void (**init)(void) = (void (**)(void))arg;
  27. (**init)();
  28. return TRUE;
  29. }
  30. void CRYPTO_once(CRYPTO_once_t *once, void (*init)(void)) {
  31. if (!InitOnceExecuteOnce(once, call_once_init, &init, NULL)) {
  32. abort();
  33. }
  34. }
  35. void CRYPTO_MUTEX_init(CRYPTO_MUTEX *lock) {
  36. InitializeSRWLock((SRWLOCK *) lock);
  37. }
  38. void CRYPTO_MUTEX_lock_read(CRYPTO_MUTEX *lock) {
  39. AcquireSRWLockShared((SRWLOCK *) lock);
  40. }
  41. void CRYPTO_MUTEX_lock_write(CRYPTO_MUTEX *lock) {
  42. AcquireSRWLockExclusive((SRWLOCK *) lock);
  43. }
  44. void CRYPTO_MUTEX_unlock_read(CRYPTO_MUTEX *lock) {
  45. ReleaseSRWLockShared((SRWLOCK *) lock);
  46. }
  47. void CRYPTO_MUTEX_unlock_write(CRYPTO_MUTEX *lock) {
  48. ReleaseSRWLockExclusive((SRWLOCK *) lock);
  49. }
  50. void CRYPTO_MUTEX_cleanup(CRYPTO_MUTEX *lock) {
  51. // SRWLOCKs require no cleanup.
  52. }
  53. void CRYPTO_STATIC_MUTEX_lock_read(struct CRYPTO_STATIC_MUTEX *lock) {
  54. AcquireSRWLockShared(&lock->lock);
  55. }
  56. void CRYPTO_STATIC_MUTEX_lock_write(struct CRYPTO_STATIC_MUTEX *lock) {
  57. AcquireSRWLockExclusive(&lock->lock);
  58. }
  59. void CRYPTO_STATIC_MUTEX_unlock_read(struct CRYPTO_STATIC_MUTEX *lock) {
  60. ReleaseSRWLockShared(&lock->lock);
  61. }
  62. void CRYPTO_STATIC_MUTEX_unlock_write(struct CRYPTO_STATIC_MUTEX *lock) {
  63. ReleaseSRWLockExclusive(&lock->lock);
  64. }
  65. static SRWLOCK g_destructors_lock = SRWLOCK_INIT;
  66. static thread_local_destructor_t g_destructors[NUM_OPENSSL_THREAD_LOCALS];
  67. static CRYPTO_once_t g_thread_local_init_once = CRYPTO_ONCE_INIT;
  68. static DWORD g_thread_local_key;
  69. static int g_thread_local_failed;
  70. static void thread_local_init(void) {
  71. g_thread_local_key = TlsAlloc();
  72. g_thread_local_failed = (g_thread_local_key == TLS_OUT_OF_INDEXES);
  73. }
  74. static void NTAPI thread_local_destructor(PVOID module, DWORD reason,
  75. PVOID reserved) {
  76. // Only free memory on |DLL_THREAD_DETACH|, not |DLL_PROCESS_DETACH|. In
  77. // VS2015's debug runtime, the C runtime has been unloaded by the time
  78. // |DLL_PROCESS_DETACH| runs. See https://crbug.com/575795. This is consistent
  79. // with |pthread_key_create| which does not call destructors on process exit,
  80. // only thread exit.
  81. if (reason != DLL_THREAD_DETACH) {
  82. return;
  83. }
  84. CRYPTO_once(&g_thread_local_init_once, thread_local_init);
  85. if (g_thread_local_failed) {
  86. return;
  87. }
  88. void **pointers = (void**) TlsGetValue(g_thread_local_key);
  89. if (pointers == NULL) {
  90. return;
  91. }
  92. thread_local_destructor_t destructors[NUM_OPENSSL_THREAD_LOCALS];
  93. AcquireSRWLockExclusive(&g_destructors_lock);
  94. OPENSSL_memcpy(destructors, g_destructors, sizeof(destructors));
  95. ReleaseSRWLockExclusive(&g_destructors_lock);
  96. for (unsigned i = 0; i < NUM_OPENSSL_THREAD_LOCALS; i++) {
  97. if (destructors[i] != NULL) {
  98. destructors[i](pointers[i]);
  99. }
  100. }
  101. OPENSSL_free(pointers);
  102. }
  103. // Thread Termination Callbacks.
  104. //
  105. // Windows doesn't support a per-thread destructor with its TLS primitives.
  106. // So, we build it manually by inserting a function to be called on each
  107. // thread's exit. This magic is from http://www.codeproject.com/threads/tls.asp
  108. // and it works for VC++ 7.0 and later.
  109. //
  110. // Force a reference to _tls_used to make the linker create the TLS directory
  111. // if it's not already there. (E.g. if __declspec(thread) is not used). Force
  112. // a reference to p_thread_callback_boringssl to prevent whole program
  113. // optimization from discarding the variable.
  114. //
  115. // Note, in the prefixed build, |p_thread_callback_boringssl| may be a macro.
  116. #define STRINGIFY(x) #x
  117. #define EXPAND_AND_STRINGIFY(x) STRINGIFY(x)
  118. #ifdef _WIN64
  119. __pragma(comment(linker, "/INCLUDE:_tls_used"))
  120. __pragma(comment(
  121. linker, "/INCLUDE:" EXPAND_AND_STRINGIFY(p_thread_callback_boringssl)))
  122. #else
  123. __pragma(comment(linker, "/INCLUDE:__tls_used"))
  124. __pragma(comment(
  125. linker, "/INCLUDE:_" EXPAND_AND_STRINGIFY(p_thread_callback_boringssl)))
  126. #endif
  127. // .CRT$XLA to .CRT$XLZ is an array of PIMAGE_TLS_CALLBACK pointers that are
  128. // called automatically by the OS loader code (not the CRT) when the module is
  129. // loaded and on thread creation. They are NOT called if the module has been
  130. // loaded by a LoadLibrary() call. It must have implicitly been loaded at
  131. // process startup.
  132. //
  133. // By implicitly loaded, I mean that it is directly referenced by the main EXE
  134. // or by one of its dependent DLLs. Delay-loaded DLL doesn't count as being
  135. // implicitly loaded.
  136. //
  137. // See VC\crt\src\tlssup.c for reference.
  138. // The linker must not discard p_thread_callback_boringssl. (We force a
  139. // reference to this variable with a linker /INCLUDE:symbol pragma to ensure
  140. // that.) If this variable is discarded, the OnThreadExit function will never
  141. // be called.
  142. #ifdef _WIN64
  143. // .CRT section is merged with .rdata on x64 so it must be constant data.
  144. #pragma const_seg(".CRT$XLC")
  145. // When defining a const variable, it must have external linkage to be sure the
  146. // linker doesn't discard it.
  147. extern const PIMAGE_TLS_CALLBACK p_thread_callback_boringssl;
  148. const PIMAGE_TLS_CALLBACK p_thread_callback_boringssl = thread_local_destructor;
  149. // Reset the default section.
  150. #pragma const_seg()
  151. #else
  152. #pragma data_seg(".CRT$XLC")
  153. PIMAGE_TLS_CALLBACK p_thread_callback_boringssl = thread_local_destructor;
  154. // Reset the default section.
  155. #pragma data_seg()
  156. #endif // _WIN64
  157. static void **get_thread_locals(void) {
  158. // |TlsGetValue| clears the last error even on success, so that callers may
  159. // distinguish it successfully returning NULL or failing. It is documented to
  160. // never fail if the argument is a valid index from |TlsAlloc|, so we do not
  161. // need to handle this.
  162. //
  163. // However, this error-mangling behavior interferes with the caller's use of
  164. // |GetLastError|. In particular |SSL_get_error| queries the error queue to
  165. // determine whether the caller should look at the OS's errors. To avoid
  166. // destroying state, save and restore the Windows error.
  167. //
  168. // https://msdn.microsoft.com/en-us/library/windows/desktop/ms686812(v=vs.85).aspx
  169. DWORD last_error = GetLastError();
  170. void **ret = TlsGetValue(g_thread_local_key);
  171. SetLastError(last_error);
  172. return ret;
  173. }
  174. void *CRYPTO_get_thread_local(thread_local_data_t index) {
  175. CRYPTO_once(&g_thread_local_init_once, thread_local_init);
  176. if (g_thread_local_failed) {
  177. return NULL;
  178. }
  179. void **pointers = get_thread_locals();
  180. if (pointers == NULL) {
  181. return NULL;
  182. }
  183. return pointers[index];
  184. }
  185. int CRYPTO_set_thread_local(thread_local_data_t index, void *value,
  186. thread_local_destructor_t destructor) {
  187. CRYPTO_once(&g_thread_local_init_once, thread_local_init);
  188. if (g_thread_local_failed) {
  189. destructor(value);
  190. return 0;
  191. }
  192. void **pointers = get_thread_locals();
  193. if (pointers == NULL) {
  194. pointers = OPENSSL_malloc(sizeof(void *) * NUM_OPENSSL_THREAD_LOCALS);
  195. if (pointers == NULL) {
  196. destructor(value);
  197. return 0;
  198. }
  199. OPENSSL_memset(pointers, 0, sizeof(void *) * NUM_OPENSSL_THREAD_LOCALS);
  200. if (TlsSetValue(g_thread_local_key, pointers) == 0) {
  201. OPENSSL_free(pointers);
  202. destructor(value);
  203. return 0;
  204. }
  205. }
  206. AcquireSRWLockExclusive(&g_destructors_lock);
  207. g_destructors[index] = destructor;
  208. ReleaseSRWLockExclusive(&g_destructors_lock);
  209. pointers[index] = value;
  210. return 1;
  211. }
  212. #endif // OPENSSL_WINDOWS_THREADS