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