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.
 
 
 
 
 
 

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