You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

290 lines
9.0 KiB

  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) && !defined(OPENSSL_NO_THREADS)
  16. #pragma warning(push, 3)
  17. #include <windows.h>
  18. #pragma warning(pop)
  19. #include <assert.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <openssl/mem.h>
  23. #include <openssl/type_check.h>
  24. OPENSSL_COMPILE_ASSERT(sizeof(CRYPTO_MUTEX) >= sizeof(CRITICAL_SECTION),
  25. CRYPTO_MUTEX_too_small);
  26. union run_once_arg_t {
  27. void (*func)(void);
  28. void *data;
  29. };
  30. static void run_once(CRYPTO_once_t *once, void (*init)(union run_once_arg_t),
  31. union run_once_arg_t arg) {
  32. /* Values must be aligned. */
  33. assert((((uintptr_t) once) & 3) == 0);
  34. /* This assumes that reading *once has acquire semantics. This should be true
  35. * on x86 and x86-64, where we expect Windows to run. */
  36. #if !defined(OPENSSL_X86) && !defined(OPENSSL_X86_64)
  37. #error "Windows once code may not work on other platforms." \
  38. "You can use InitOnceBeginInitialize on >=Vista"
  39. #endif
  40. if (*once == 1) {
  41. return;
  42. }
  43. for (;;) {
  44. switch (InterlockedCompareExchange(once, 2, 0)) {
  45. case 0:
  46. /* The value was zero so we are the first thread to call |CRYPTO_once|
  47. * on it. */
  48. init(arg);
  49. /* Write one to indicate that initialisation is complete. */
  50. InterlockedExchange(once, 1);
  51. return;
  52. case 1:
  53. /* Another thread completed initialisation between our fast-path check
  54. * and |InterlockedCompareExchange|. */
  55. return;
  56. case 2:
  57. /* Another thread is running the initialisation. Switch to it then try
  58. * again. */
  59. SwitchToThread();
  60. break;
  61. default:
  62. abort();
  63. }
  64. }
  65. }
  66. static void call_once_init(union run_once_arg_t arg) {
  67. arg.func();
  68. }
  69. void CRYPTO_once(CRYPTO_once_t *in_once, void (*init)(void)) {
  70. union run_once_arg_t arg;
  71. arg.func = init;
  72. run_once(in_once, call_once_init, arg);
  73. }
  74. void CRYPTO_MUTEX_init(CRYPTO_MUTEX *lock) {
  75. if (!InitializeCriticalSectionAndSpinCount((CRITICAL_SECTION *) lock, 0x400)) {
  76. abort();
  77. }
  78. }
  79. void CRYPTO_MUTEX_lock_read(CRYPTO_MUTEX *lock) {
  80. /* Since we have to support Windows XP, read locks are actually exclusive. */
  81. EnterCriticalSection((CRITICAL_SECTION *) lock);
  82. }
  83. void CRYPTO_MUTEX_lock_write(CRYPTO_MUTEX *lock) {
  84. EnterCriticalSection((CRITICAL_SECTION *) lock);
  85. }
  86. void CRYPTO_MUTEX_unlock(CRYPTO_MUTEX *lock) {
  87. LeaveCriticalSection((CRITICAL_SECTION *) lock);
  88. }
  89. void CRYPTO_MUTEX_cleanup(CRYPTO_MUTEX *lock) {
  90. DeleteCriticalSection((CRITICAL_SECTION *) lock);
  91. }
  92. static void static_lock_init(union run_once_arg_t arg) {
  93. struct CRYPTO_STATIC_MUTEX *lock = arg.data;
  94. if (!InitializeCriticalSectionAndSpinCount(&lock->lock, 0x400)) {
  95. abort();
  96. }
  97. }
  98. void CRYPTO_STATIC_MUTEX_lock_read(struct CRYPTO_STATIC_MUTEX *lock) {
  99. union run_once_arg_t arg;
  100. arg.data = lock;
  101. /* Since we have to support Windows XP, read locks are actually exclusive. */
  102. run_once(&lock->once, static_lock_init, arg);
  103. EnterCriticalSection(&lock->lock);
  104. }
  105. void CRYPTO_STATIC_MUTEX_lock_write(struct CRYPTO_STATIC_MUTEX *lock) {
  106. CRYPTO_STATIC_MUTEX_lock_read(lock);
  107. }
  108. void CRYPTO_STATIC_MUTEX_unlock(struct CRYPTO_STATIC_MUTEX *lock) {
  109. LeaveCriticalSection(&lock->lock);
  110. }
  111. static CRITICAL_SECTION g_destructors_lock;
  112. static thread_local_destructor_t g_destructors[NUM_OPENSSL_THREAD_LOCALS];
  113. static CRYPTO_once_t g_thread_local_init_once = CRYPTO_ONCE_INIT;
  114. static DWORD g_thread_local_key;
  115. static int g_thread_local_failed;
  116. static void thread_local_init(void) {
  117. if (!InitializeCriticalSectionAndSpinCount(&g_destructors_lock, 0x400)) {
  118. g_thread_local_failed = 1;
  119. return;
  120. }
  121. g_thread_local_key = TlsAlloc();
  122. g_thread_local_failed = (g_thread_local_key == TLS_OUT_OF_INDEXES);
  123. }
  124. static void NTAPI thread_local_destructor(PVOID module, DWORD reason,
  125. PVOID reserved) {
  126. /* Only free memory on |DLL_THREAD_DETACH|, not |DLL_PROCESS_DETACH|. In
  127. * VS2015's debug runtime, the C runtime has been unloaded by the time
  128. * |DLL_PROCESS_DETACH| runs. See https://crbug.com/575795. This is consistent
  129. * with |pthread_key_create| which does not call destructors on process exit,
  130. * only thread exit. */
  131. if (reason != DLL_THREAD_DETACH) {
  132. return;
  133. }
  134. CRYPTO_once(&g_thread_local_init_once, thread_local_init);
  135. if (g_thread_local_failed) {
  136. return;
  137. }
  138. void **pointers = (void**) TlsGetValue(g_thread_local_key);
  139. if (pointers == NULL) {
  140. return;
  141. }
  142. thread_local_destructor_t destructors[NUM_OPENSSL_THREAD_LOCALS];
  143. EnterCriticalSection(&g_destructors_lock);
  144. memcpy(destructors, g_destructors, sizeof(destructors));
  145. LeaveCriticalSection(&g_destructors_lock);
  146. unsigned i;
  147. for (i = 0; i < NUM_OPENSSL_THREAD_LOCALS; i++) {
  148. if (destructors[i] != NULL) {
  149. destructors[i](pointers[i]);
  150. }
  151. }
  152. OPENSSL_free(pointers);
  153. }
  154. /* Thread Termination Callbacks.
  155. *
  156. * Windows doesn't support a per-thread destructor with its TLS primitives.
  157. * So, we build it manually by inserting a function to be called on each
  158. * thread's exit. This magic is from http://www.codeproject.com/threads/tls.asp
  159. * and it works for VC++ 7.0 and later.
  160. *
  161. * Force a reference to _tls_used to make the linker create the TLS directory
  162. * if it's not already there. (E.g. if __declspec(thread) is not used). Force
  163. * a reference to p_thread_callback_boringssl to prevent whole program
  164. * optimization from discarding the variable. */
  165. #ifdef _WIN64
  166. #pragma comment(linker, "/INCLUDE:_tls_used")
  167. #pragma comment(linker, "/INCLUDE:p_thread_callback_boringssl")
  168. #else
  169. #pragma comment(linker, "/INCLUDE:__tls_used")
  170. #pragma comment(linker, "/INCLUDE:_p_thread_callback_boringssl")
  171. #endif
  172. /* .CRT$XLA to .CRT$XLZ is an array of PIMAGE_TLS_CALLBACK pointers that are
  173. * called automatically by the OS loader code (not the CRT) when the module is
  174. * loaded and on thread creation. They are NOT called if the module has been
  175. * loaded by a LoadLibrary() call. It must have implicitly been loaded at
  176. * process startup.
  177. *
  178. * By implicitly loaded, I mean that it is directly referenced by the main EXE
  179. * or by one of its dependent DLLs. Delay-loaded DLL doesn't count as being
  180. * implicitly loaded.
  181. *
  182. * See VC\crt\src\tlssup.c for reference. */
  183. /* The linker must not discard p_thread_callback_boringssl. (We force a reference
  184. * to this variable with a linker /INCLUDE:symbol pragma to ensure that.) If
  185. * this variable is discarded, the OnThreadExit function will never be
  186. * called. */
  187. #ifdef _WIN64
  188. /* .CRT section is merged with .rdata on x64 so it must be constant data. */
  189. #pragma const_seg(".CRT$XLC")
  190. /* When defining a const variable, it must have external linkage to be sure the
  191. * linker doesn't discard it. */
  192. extern const PIMAGE_TLS_CALLBACK p_thread_callback_boringssl;
  193. const PIMAGE_TLS_CALLBACK p_thread_callback_boringssl = thread_local_destructor;
  194. /* Reset the default section. */
  195. #pragma const_seg()
  196. #else
  197. #pragma data_seg(".CRT$XLC")
  198. PIMAGE_TLS_CALLBACK p_thread_callback_boringssl = thread_local_destructor;
  199. /* Reset the default section. */
  200. #pragma data_seg()
  201. #endif /* _WIN64 */
  202. void *CRYPTO_get_thread_local(thread_local_data_t index) {
  203. CRYPTO_once(&g_thread_local_init_once, thread_local_init);
  204. if (g_thread_local_failed) {
  205. return NULL;
  206. }
  207. void **pointers = TlsGetValue(g_thread_local_key);
  208. if (pointers == NULL) {
  209. return NULL;
  210. }
  211. return pointers[index];
  212. }
  213. int CRYPTO_set_thread_local(thread_local_data_t index, void *value,
  214. thread_local_destructor_t destructor) {
  215. CRYPTO_once(&g_thread_local_init_once, thread_local_init);
  216. if (g_thread_local_failed) {
  217. destructor(value);
  218. return 0;
  219. }
  220. void **pointers = TlsGetValue(g_thread_local_key);
  221. if (pointers == NULL) {
  222. pointers = OPENSSL_malloc(sizeof(void *) * NUM_OPENSSL_THREAD_LOCALS);
  223. if (pointers == NULL) {
  224. destructor(value);
  225. return 0;
  226. }
  227. memset(pointers, 0, sizeof(void *) * NUM_OPENSSL_THREAD_LOCALS);
  228. if (TlsSetValue(g_thread_local_key, pointers) == 0) {
  229. OPENSSL_free(pointers);
  230. destructor(value);
  231. return 0;
  232. }
  233. }
  234. EnterCriticalSection(&g_destructors_lock);
  235. g_destructors[index] = destructor;
  236. LeaveCriticalSection(&g_destructors_lock);
  237. pointers[index] = value;
  238. return 1;
  239. }
  240. #endif /* OPENSSL_WINDOWS && !OPENSSL_NO_THREADS */