Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

256 řádky
8.2 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_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_COMPILE_ASSERT(sizeof(CRYPTO_MUTEX) >= sizeof(SRWLOCK),
  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. 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 CRITICAL_SECTION g_destructors_lock;
  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. if (!InitializeCriticalSectionAndSpinCount(&g_destructors_lock, 0x400)) {
  72. g_thread_local_failed = 1;
  73. return;
  74. }
  75. g_thread_local_key = TlsAlloc();
  76. g_thread_local_failed = (g_thread_local_key == TLS_OUT_OF_INDEXES);
  77. }
  78. static void NTAPI thread_local_destructor(PVOID module, DWORD reason,
  79. PVOID reserved) {
  80. // Only free memory on |DLL_THREAD_DETACH|, not |DLL_PROCESS_DETACH|. In
  81. // VS2015's debug runtime, the C runtime has been unloaded by the time
  82. // |DLL_PROCESS_DETACH| runs. See https://crbug.com/575795. This is consistent
  83. // with |pthread_key_create| which does not call destructors on process exit,
  84. // only thread exit.
  85. if (reason != DLL_THREAD_DETACH) {
  86. return;
  87. }
  88. CRYPTO_once(&g_thread_local_init_once, thread_local_init);
  89. if (g_thread_local_failed) {
  90. return;
  91. }
  92. void **pointers = (void**) TlsGetValue(g_thread_local_key);
  93. if (pointers == NULL) {
  94. return;
  95. }
  96. thread_local_destructor_t destructors[NUM_OPENSSL_THREAD_LOCALS];
  97. EnterCriticalSection(&g_destructors_lock);
  98. OPENSSL_memcpy(destructors, g_destructors, sizeof(destructors));
  99. LeaveCriticalSection(&g_destructors_lock);
  100. unsigned i;
  101. for (i = 0; i < NUM_OPENSSL_THREAD_LOCALS; i++) {
  102. if (destructors[i] != NULL) {
  103. destructors[i](pointers[i]);
  104. }
  105. }
  106. OPENSSL_free(pointers);
  107. }
  108. // Thread Termination Callbacks.
  109. //
  110. // Windows doesn't support a per-thread destructor with its TLS primitives.
  111. // So, we build it manually by inserting a function to be called on each
  112. // thread's exit. This magic is from http://www.codeproject.com/threads/tls.asp
  113. // and it works for VC++ 7.0 and later.
  114. //
  115. // Force a reference to _tls_used to make the linker create the TLS directory
  116. // if it's not already there. (E.g. if __declspec(thread) is not used). Force
  117. // a reference to p_thread_callback_boringssl to prevent whole program
  118. // optimization from discarding the variable.
  119. #ifdef _WIN64
  120. #pragma comment(linker, "/INCLUDE:_tls_used")
  121. #pragma comment(linker, "/INCLUDE:p_thread_callback_boringssl")
  122. #else
  123. #pragma comment(linker, "/INCLUDE:__tls_used")
  124. #pragma comment(linker, "/INCLUDE:_p_thread_callback_boringssl")
  125. #endif
  126. // .CRT$XLA to .CRT$XLZ is an array of PIMAGE_TLS_CALLBACK pointers that are
  127. // called automatically by the OS loader code (not the CRT) when the module is
  128. // loaded and on thread creation. They are NOT called if the module has been
  129. // loaded by a LoadLibrary() call. It must have implicitly been loaded at
  130. // process startup.
  131. //
  132. // By implicitly loaded, I mean that it is directly referenced by the main EXE
  133. // or by one of its dependent DLLs. Delay-loaded DLL doesn't count as being
  134. // implicitly loaded.
  135. //
  136. // See VC\crt\src\tlssup.c for reference.
  137. // The linker must not discard p_thread_callback_boringssl. (We force a
  138. // reference to this variable with a linker /INCLUDE:symbol pragma to ensure
  139. // that.) If this variable is discarded, the OnThreadExit function will never
  140. // be called.
  141. #ifdef _WIN64
  142. // .CRT section is merged with .rdata on x64 so it must be constant data.
  143. #pragma const_seg(".CRT$XLC")
  144. // When defining a const variable, it must have external linkage to be sure the
  145. // linker doesn't discard it.
  146. extern const PIMAGE_TLS_CALLBACK p_thread_callback_boringssl;
  147. const PIMAGE_TLS_CALLBACK p_thread_callback_boringssl = thread_local_destructor;
  148. // Reset the default section.
  149. #pragma const_seg()
  150. #else
  151. #pragma data_seg(".CRT$XLC")
  152. PIMAGE_TLS_CALLBACK p_thread_callback_boringssl = thread_local_destructor;
  153. // Reset the default section.
  154. #pragma data_seg()
  155. #endif // _WIN64
  156. static void **get_thread_locals(void) {
  157. // |TlsGetValue| clears the last error even on success, so that callers may
  158. // distinguish it successfully returning NULL or failing. It is documented to
  159. // never fail if the argument is a valid index from |TlsAlloc|, so we do not
  160. // need to handle this.
  161. //
  162. // However, this error-mangling behavior interferes with the caller's use of
  163. // |GetLastError|. In particular |SSL_get_error| queries the error queue to
  164. // determine whether the caller should look at the OS's errors. To avoid
  165. // destroying state, save and restore the Windows error.
  166. //
  167. // https://msdn.microsoft.com/en-us/library/windows/desktop/ms686812(v=vs.85).aspx
  168. DWORD last_error = GetLastError();
  169. void **ret = TlsGetValue(g_thread_local_key);
  170. SetLastError(last_error);
  171. return ret;
  172. }
  173. void *CRYPTO_get_thread_local(thread_local_data_t index) {
  174. CRYPTO_once(&g_thread_local_init_once, thread_local_init);
  175. if (g_thread_local_failed) {
  176. return NULL;
  177. }
  178. void **pointers = get_thread_locals();
  179. if (pointers == NULL) {
  180. return NULL;
  181. }
  182. return pointers[index];
  183. }
  184. int CRYPTO_set_thread_local(thread_local_data_t index, void *value,
  185. thread_local_destructor_t destructor) {
  186. CRYPTO_once(&g_thread_local_init_once, thread_local_init);
  187. if (g_thread_local_failed) {
  188. destructor(value);
  189. return 0;
  190. }
  191. void **pointers = get_thread_locals();
  192. if (pointers == NULL) {
  193. pointers = OPENSSL_malloc(sizeof(void *) * NUM_OPENSSL_THREAD_LOCALS);
  194. if (pointers == NULL) {
  195. destructor(value);
  196. return 0;
  197. }
  198. OPENSSL_memset(pointers, 0, sizeof(void *) * NUM_OPENSSL_THREAD_LOCALS);
  199. if (TlsSetValue(g_thread_local_key, pointers) == 0) {
  200. OPENSSL_free(pointers);
  201. destructor(value);
  202. return 0;
  203. }
  204. }
  205. EnterCriticalSection(&g_destructors_lock);
  206. g_destructors[index] = destructor;
  207. LeaveCriticalSection(&g_destructors_lock);
  208. pointers[index] = value;
  209. return 1;
  210. }
  211. #endif // OPENSSL_WINDOWS_THREADS