Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

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