Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

247 rindas
7.8 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 <stdlib.h>
  20. #include <string.h>
  21. #include <openssl/mem.h>
  22. #include <openssl/type_check.h>
  23. OPENSSL_COMPILE_ASSERT(sizeof(CRYPTO_MUTEX) >= sizeof(CRITICAL_SECTION),
  24. CRYPTO_MUTEX_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. if (!InitializeCriticalSectionAndSpinCount((CRITICAL_SECTION *) lock, 0x400)) {
  37. abort();
  38. }
  39. }
  40. void CRYPTO_MUTEX_lock_read(CRYPTO_MUTEX *lock) {
  41. /* Since we have to support Windows XP, read locks are actually exclusive. */
  42. EnterCriticalSection((CRITICAL_SECTION *) lock);
  43. }
  44. void CRYPTO_MUTEX_lock_write(CRYPTO_MUTEX *lock) {
  45. EnterCriticalSection((CRITICAL_SECTION *) lock);
  46. }
  47. void CRYPTO_MUTEX_unlock(CRYPTO_MUTEX *lock) {
  48. LeaveCriticalSection((CRITICAL_SECTION *) lock);
  49. }
  50. void CRYPTO_MUTEX_cleanup(CRYPTO_MUTEX *lock) {
  51. DeleteCriticalSection((CRITICAL_SECTION *) lock);
  52. }
  53. static BOOL CALLBACK static_lock_init(INIT_ONCE *once, void *arg, void **out) {
  54. struct CRYPTO_STATIC_MUTEX *lock = arg;
  55. if (!InitializeCriticalSectionAndSpinCount(&lock->lock, 0x400)) {
  56. abort();
  57. }
  58. return TRUE;
  59. }
  60. void CRYPTO_STATIC_MUTEX_lock_read(struct CRYPTO_STATIC_MUTEX *lock) {
  61. /* TODO(davidben): Consider replacing these with SRWLOCK now that we no longer
  62. * need to support Windows XP. Currently, read locks are actually
  63. * exclusive. */
  64. if (!InitOnceExecuteOnce(&lock->once, static_lock_init, lock, NULL)) {
  65. abort();
  66. }
  67. EnterCriticalSection(&lock->lock);
  68. }
  69. void CRYPTO_STATIC_MUTEX_lock_write(struct CRYPTO_STATIC_MUTEX *lock) {
  70. CRYPTO_STATIC_MUTEX_lock_read(lock);
  71. }
  72. void CRYPTO_STATIC_MUTEX_unlock(struct CRYPTO_STATIC_MUTEX *lock) {
  73. LeaveCriticalSection(&lock->lock);
  74. }
  75. static CRITICAL_SECTION g_destructors_lock;
  76. static thread_local_destructor_t g_destructors[NUM_OPENSSL_THREAD_LOCALS];
  77. static CRYPTO_once_t g_thread_local_init_once = CRYPTO_ONCE_INIT;
  78. static DWORD g_thread_local_key;
  79. static int g_thread_local_failed;
  80. static void thread_local_init(void) {
  81. if (!InitializeCriticalSectionAndSpinCount(&g_destructors_lock, 0x400)) {
  82. g_thread_local_failed = 1;
  83. return;
  84. }
  85. g_thread_local_key = TlsAlloc();
  86. g_thread_local_failed = (g_thread_local_key == TLS_OUT_OF_INDEXES);
  87. }
  88. static void NTAPI thread_local_destructor(PVOID module, DWORD reason,
  89. PVOID reserved) {
  90. /* Only free memory on |DLL_THREAD_DETACH|, not |DLL_PROCESS_DETACH|. In
  91. * VS2015's debug runtime, the C runtime has been unloaded by the time
  92. * |DLL_PROCESS_DETACH| runs. See https://crbug.com/575795. This is consistent
  93. * with |pthread_key_create| which does not call destructors on process exit,
  94. * only thread exit. */
  95. if (reason != DLL_THREAD_DETACH) {
  96. return;
  97. }
  98. CRYPTO_once(&g_thread_local_init_once, thread_local_init);
  99. if (g_thread_local_failed) {
  100. return;
  101. }
  102. void **pointers = (void**) TlsGetValue(g_thread_local_key);
  103. if (pointers == NULL) {
  104. return;
  105. }
  106. thread_local_destructor_t destructors[NUM_OPENSSL_THREAD_LOCALS];
  107. EnterCriticalSection(&g_destructors_lock);
  108. memcpy(destructors, g_destructors, sizeof(destructors));
  109. LeaveCriticalSection(&g_destructors_lock);
  110. unsigned i;
  111. for (i = 0; i < NUM_OPENSSL_THREAD_LOCALS; i++) {
  112. if (destructors[i] != NULL) {
  113. destructors[i](pointers[i]);
  114. }
  115. }
  116. OPENSSL_free(pointers);
  117. }
  118. /* Thread Termination Callbacks.
  119. *
  120. * Windows doesn't support a per-thread destructor with its TLS primitives.
  121. * So, we build it manually by inserting a function to be called on each
  122. * thread's exit. This magic is from http://www.codeproject.com/threads/tls.asp
  123. * and it works for VC++ 7.0 and later.
  124. *
  125. * Force a reference to _tls_used to make the linker create the TLS directory
  126. * if it's not already there. (E.g. if __declspec(thread) is not used). Force
  127. * a reference to p_thread_callback_boringssl to prevent whole program
  128. * optimization from discarding the variable. */
  129. #ifdef _WIN64
  130. #pragma comment(linker, "/INCLUDE:_tls_used")
  131. #pragma comment(linker, "/INCLUDE:p_thread_callback_boringssl")
  132. #else
  133. #pragma comment(linker, "/INCLUDE:__tls_used")
  134. #pragma comment(linker, "/INCLUDE:_p_thread_callback_boringssl")
  135. #endif
  136. /* .CRT$XLA to .CRT$XLZ is an array of PIMAGE_TLS_CALLBACK pointers that are
  137. * called automatically by the OS loader code (not the CRT) when the module is
  138. * loaded and on thread creation. They are NOT called if the module has been
  139. * loaded by a LoadLibrary() call. It must have implicitly been loaded at
  140. * process startup.
  141. *
  142. * By implicitly loaded, I mean that it is directly referenced by the main EXE
  143. * or by one of its dependent DLLs. Delay-loaded DLL doesn't count as being
  144. * implicitly loaded.
  145. *
  146. * See VC\crt\src\tlssup.c for reference. */
  147. /* The linker must not discard p_thread_callback_boringssl. (We force a reference
  148. * to this variable with a linker /INCLUDE:symbol pragma to ensure that.) If
  149. * this variable is discarded, the OnThreadExit function will never be
  150. * called. */
  151. #ifdef _WIN64
  152. /* .CRT section is merged with .rdata on x64 so it must be constant data. */
  153. #pragma const_seg(".CRT$XLC")
  154. /* When defining a const variable, it must have external linkage to be sure the
  155. * linker doesn't discard it. */
  156. extern const PIMAGE_TLS_CALLBACK p_thread_callback_boringssl;
  157. const PIMAGE_TLS_CALLBACK p_thread_callback_boringssl = thread_local_destructor;
  158. /* Reset the default section. */
  159. #pragma const_seg()
  160. #else
  161. #pragma data_seg(".CRT$XLC")
  162. PIMAGE_TLS_CALLBACK p_thread_callback_boringssl = thread_local_destructor;
  163. /* Reset the default section. */
  164. #pragma data_seg()
  165. #endif /* _WIN64 */
  166. void *CRYPTO_get_thread_local(thread_local_data_t index) {
  167. CRYPTO_once(&g_thread_local_init_once, thread_local_init);
  168. if (g_thread_local_failed) {
  169. return NULL;
  170. }
  171. void **pointers = TlsGetValue(g_thread_local_key);
  172. if (pointers == NULL) {
  173. return NULL;
  174. }
  175. return pointers[index];
  176. }
  177. int CRYPTO_set_thread_local(thread_local_data_t index, void *value,
  178. thread_local_destructor_t destructor) {
  179. CRYPTO_once(&g_thread_local_init_once, thread_local_init);
  180. if (g_thread_local_failed) {
  181. destructor(value);
  182. return 0;
  183. }
  184. void **pointers = TlsGetValue(g_thread_local_key);
  185. if (pointers == NULL) {
  186. pointers = OPENSSL_malloc(sizeof(void *) * NUM_OPENSSL_THREAD_LOCALS);
  187. if (pointers == NULL) {
  188. destructor(value);
  189. return 0;
  190. }
  191. memset(pointers, 0, sizeof(void *) * NUM_OPENSSL_THREAD_LOCALS);
  192. if (TlsSetValue(g_thread_local_key, pointers) == 0) {
  193. OPENSSL_free(pointers);
  194. destructor(value);
  195. return 0;
  196. }
  197. }
  198. EnterCriticalSection(&g_destructors_lock);
  199. g_destructors[index] = destructor;
  200. LeaveCriticalSection(&g_destructors_lock);
  201. pointers[index] = value;
  202. return 1;
  203. }
  204. #endif /* OPENSSL_WINDOWS && !OPENSSL_NO_THREADS */