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 | |
David Benjamin | 84759bd | 2016-09-20 17:35:09 -0400 | [diff] [blame] | 17 | #if defined(OPENSSL_WINDOWS_THREADS) |
Adam Langley | d7c5dfb | 2015-03-16 12:48:56 -0700 | [diff] [blame] | 18 | |
David Benjamin | a353cdb | 2016-06-09 16:48:33 -0400 | [diff] [blame] | 19 | OPENSSL_MSVC_PRAGMA(warning(push, 3)) |
Adam Langley | d7c5dfb | 2015-03-16 12:48:56 -0700 | [diff] [blame] | 20 | #include <windows.h> |
David Benjamin | a353cdb | 2016-06-09 16:48:33 -0400 | [diff] [blame] | 21 | OPENSSL_MSVC_PRAGMA(warning(pop)) |
Adam Langley | d7c5dfb | 2015-03-16 12:48:56 -0700 | [diff] [blame] | 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 | |
David Benjamin | 156edfe | 2016-05-24 15:41:11 +0000 | [diff] [blame] | 30 | OPENSSL_COMPILE_ASSERT(sizeof(CRYPTO_MUTEX) >= sizeof(SRWLOCK), |
Adam Langley | df1f5e7 | 2015-04-13 11:04:08 -0700 | [diff] [blame] | 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) { |
David Benjamin | 156edfe | 2016-05-24 15:41:11 +0000 | [diff] [blame] | 46 | InitializeSRWLock((SRWLOCK *) lock); |
Adam Langley | df1f5e7 | 2015-04-13 11:04:08 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void CRYPTO_MUTEX_lock_read(CRYPTO_MUTEX *lock) { |
David Benjamin | 156edfe | 2016-05-24 15:41:11 +0000 | [diff] [blame] | 50 | AcquireSRWLockShared((SRWLOCK *) lock); |
Adam Langley | df1f5e7 | 2015-04-13 11:04:08 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void CRYPTO_MUTEX_lock_write(CRYPTO_MUTEX *lock) { |
David Benjamin | 156edfe | 2016-05-24 15:41:11 +0000 | [diff] [blame] | 54 | AcquireSRWLockExclusive((SRWLOCK *) lock); |
Adam Langley | df1f5e7 | 2015-04-13 11:04:08 -0700 | [diff] [blame] | 55 | } |
| 56 | |
David Benjamin | 29270de | 2016-05-24 15:28:36 +0000 | [diff] [blame] | 57 | void CRYPTO_MUTEX_unlock_read(CRYPTO_MUTEX *lock) { |
David Benjamin | 156edfe | 2016-05-24 15:41:11 +0000 | [diff] [blame] | 58 | ReleaseSRWLockShared((SRWLOCK *) lock); |
David Benjamin | 29270de | 2016-05-24 15:28:36 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | void CRYPTO_MUTEX_unlock_write(CRYPTO_MUTEX *lock) { |
David Benjamin | 156edfe | 2016-05-24 15:41:11 +0000 | [diff] [blame] | 62 | ReleaseSRWLockExclusive((SRWLOCK *) lock); |
Adam Langley | df1f5e7 | 2015-04-13 11:04:08 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void CRYPTO_MUTEX_cleanup(CRYPTO_MUTEX *lock) { |
David Benjamin | 156edfe | 2016-05-24 15:41:11 +0000 | [diff] [blame] | 66 | /* SRWLOCKs require no cleanup. */ |
Adam Langley | df1f5e7 | 2015-04-13 11:04:08 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void CRYPTO_STATIC_MUTEX_lock_read(struct CRYPTO_STATIC_MUTEX *lock) { |
David Benjamin | 156edfe | 2016-05-24 15:41:11 +0000 | [diff] [blame] | 70 | AcquireSRWLockShared(&lock->lock); |
Adam Langley | df1f5e7 | 2015-04-13 11:04:08 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void CRYPTO_STATIC_MUTEX_lock_write(struct CRYPTO_STATIC_MUTEX *lock) { |
David Benjamin | 156edfe | 2016-05-24 15:41:11 +0000 | [diff] [blame] | 74 | AcquireSRWLockExclusive(&lock->lock); |
Adam Langley | df1f5e7 | 2015-04-13 11:04:08 -0700 | [diff] [blame] | 75 | } |
| 76 | |
David Benjamin | 29270de | 2016-05-24 15:28:36 +0000 | [diff] [blame] | 77 | void CRYPTO_STATIC_MUTEX_unlock_read(struct CRYPTO_STATIC_MUTEX *lock) { |
David Benjamin | 156edfe | 2016-05-24 15:41:11 +0000 | [diff] [blame] | 78 | ReleaseSRWLockShared(&lock->lock); |
David Benjamin | 29270de | 2016-05-24 15:28:36 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | void CRYPTO_STATIC_MUTEX_unlock_write(struct CRYPTO_STATIC_MUTEX *lock) { |
David Benjamin | 156edfe | 2016-05-24 15:41:11 +0000 | [diff] [blame] | 82 | ReleaseSRWLockExclusive(&lock->lock); |
Adam Langley | df1f5e7 | 2015-04-13 11:04:08 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Adam Langley | d7c5dfb | 2015-03-16 12:48:56 -0700 | [diff] [blame] | 85 | static CRITICAL_SECTION g_destructors_lock; |
| 86 | static thread_local_destructor_t g_destructors[NUM_OPENSSL_THREAD_LOCALS]; |
| 87 | |
| 88 | static CRYPTO_once_t g_thread_local_init_once = CRYPTO_ONCE_INIT; |
| 89 | static DWORD g_thread_local_key; |
| 90 | static int g_thread_local_failed; |
| 91 | |
| 92 | static void thread_local_init(void) { |
| 93 | if (!InitializeCriticalSectionAndSpinCount(&g_destructors_lock, 0x400)) { |
| 94 | g_thread_local_failed = 1; |
| 95 | return; |
| 96 | } |
| 97 | g_thread_local_key = TlsAlloc(); |
| 98 | g_thread_local_failed = (g_thread_local_key == TLS_OUT_OF_INDEXES); |
| 99 | } |
| 100 | |
David Benjamin | feaa57d | 2016-03-29 14:17:27 -0400 | [diff] [blame] | 101 | static void NTAPI thread_local_destructor(PVOID module, DWORD reason, |
| 102 | PVOID reserved) { |
| 103 | /* Only free memory on |DLL_THREAD_DETACH|, not |DLL_PROCESS_DETACH|. In |
| 104 | * VS2015's debug runtime, the C runtime has been unloaded by the time |
| 105 | * |DLL_PROCESS_DETACH| runs. See https://crbug.com/575795. This is consistent |
| 106 | * with |pthread_key_create| which does not call destructors on process exit, |
| 107 | * only thread exit. */ |
| 108 | if (reason != DLL_THREAD_DETACH) { |
Adam Langley | d7c5dfb | 2015-03-16 12:48:56 -0700 | [diff] [blame] | 109 | return; |
| 110 | } |
| 111 | |
| 112 | CRYPTO_once(&g_thread_local_init_once, thread_local_init); |
| 113 | if (g_thread_local_failed) { |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | void **pointers = (void**) TlsGetValue(g_thread_local_key); |
| 118 | if (pointers == NULL) { |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | thread_local_destructor_t destructors[NUM_OPENSSL_THREAD_LOCALS]; |
| 123 | |
| 124 | EnterCriticalSection(&g_destructors_lock); |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 125 | OPENSSL_memcpy(destructors, g_destructors, sizeof(destructors)); |
Adam Langley | d7c5dfb | 2015-03-16 12:48:56 -0700 | [diff] [blame] | 126 | LeaveCriticalSection(&g_destructors_lock); |
| 127 | |
| 128 | unsigned i; |
| 129 | for (i = 0; i < NUM_OPENSSL_THREAD_LOCALS; i++) { |
| 130 | if (destructors[i] != NULL) { |
| 131 | destructors[i](pointers[i]); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | OPENSSL_free(pointers); |
| 136 | } |
| 137 | |
| 138 | /* Thread Termination Callbacks. |
| 139 | * |
| 140 | * Windows doesn't support a per-thread destructor with its TLS primitives. |
| 141 | * So, we build it manually by inserting a function to be called on each |
| 142 | * thread's exit. This magic is from http://www.codeproject.com/threads/tls.asp |
| 143 | * and it works for VC++ 7.0 and later. |
| 144 | * |
| 145 | * Force a reference to _tls_used to make the linker create the TLS directory |
| 146 | * 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] | 147 | * a reference to p_thread_callback_boringssl to prevent whole program |
| 148 | * optimization from discarding the variable. */ |
Adam Langley | d7c5dfb | 2015-03-16 12:48:56 -0700 | [diff] [blame] | 149 | #ifdef _WIN64 |
| 150 | #pragma comment(linker, "/INCLUDE:_tls_used") |
David Benjamin | 40acdae | 2015-04-03 11:41:56 -0400 | [diff] [blame] | 151 | #pragma comment(linker, "/INCLUDE:p_thread_callback_boringssl") |
Adam Langley | d7c5dfb | 2015-03-16 12:48:56 -0700 | [diff] [blame] | 152 | #else |
| 153 | #pragma comment(linker, "/INCLUDE:__tls_used") |
David Benjamin | 40acdae | 2015-04-03 11:41:56 -0400 | [diff] [blame] | 154 | #pragma comment(linker, "/INCLUDE:_p_thread_callback_boringssl") |
Adam Langley | d7c5dfb | 2015-03-16 12:48:56 -0700 | [diff] [blame] | 155 | #endif |
| 156 | |
| 157 | /* .CRT$XLA to .CRT$XLZ is an array of PIMAGE_TLS_CALLBACK pointers that are |
| 158 | * called automatically by the OS loader code (not the CRT) when the module is |
| 159 | * loaded and on thread creation. They are NOT called if the module has been |
| 160 | * loaded by a LoadLibrary() call. It must have implicitly been loaded at |
| 161 | * process startup. |
| 162 | * |
| 163 | * By implicitly loaded, I mean that it is directly referenced by the main EXE |
| 164 | * or by one of its dependent DLLs. Delay-loaded DLL doesn't count as being |
| 165 | * implicitly loaded. |
| 166 | * |
| 167 | * See VC\crt\src\tlssup.c for reference. */ |
| 168 | |
David Benjamin | 40acdae | 2015-04-03 11:41:56 -0400 | [diff] [blame] | 169 | /* 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] | 170 | * to this variable with a linker /INCLUDE:symbol pragma to ensure that.) If |
| 171 | * this variable is discarded, the OnThreadExit function will never be |
| 172 | * called. */ |
| 173 | #ifdef _WIN64 |
| 174 | |
| 175 | /* .CRT section is merged with .rdata on x64 so it must be constant data. */ |
| 176 | #pragma const_seg(".CRT$XLC") |
| 177 | /* When defining a const variable, it must have external linkage to be sure the |
| 178 | * linker doesn't discard it. */ |
David Benjamin | 40acdae | 2015-04-03 11:41:56 -0400 | [diff] [blame] | 179 | extern const PIMAGE_TLS_CALLBACK p_thread_callback_boringssl; |
| 180 | const PIMAGE_TLS_CALLBACK p_thread_callback_boringssl = thread_local_destructor; |
Adam Langley | d7c5dfb | 2015-03-16 12:48:56 -0700 | [diff] [blame] | 181 | /* Reset the default section. */ |
| 182 | #pragma const_seg() |
| 183 | |
| 184 | #else |
| 185 | |
| 186 | #pragma data_seg(".CRT$XLC") |
David Benjamin | 40acdae | 2015-04-03 11:41:56 -0400 | [diff] [blame] | 187 | PIMAGE_TLS_CALLBACK p_thread_callback_boringssl = thread_local_destructor; |
Adam Langley | d7c5dfb | 2015-03-16 12:48:56 -0700 | [diff] [blame] | 188 | /* Reset the default section. */ |
| 189 | #pragma data_seg() |
| 190 | |
| 191 | #endif /* _WIN64 */ |
| 192 | |
| 193 | void *CRYPTO_get_thread_local(thread_local_data_t index) { |
| 194 | CRYPTO_once(&g_thread_local_init_once, thread_local_init); |
| 195 | if (g_thread_local_failed) { |
| 196 | return NULL; |
| 197 | } |
| 198 | |
| 199 | void **pointers = TlsGetValue(g_thread_local_key); |
| 200 | if (pointers == NULL) { |
| 201 | return NULL; |
| 202 | } |
| 203 | return pointers[index]; |
| 204 | } |
| 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 | |
| 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 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 221 | OPENSSL_memset(pointers, 0, sizeof(void *) * NUM_OPENSSL_THREAD_LOCALS); |
Adam Langley | d7c5dfb | 2015-03-16 12:48:56 -0700 | [diff] [blame] | 222 | if (TlsSetValue(g_thread_local_key, pointers) == 0) { |
| 223 | OPENSSL_free(pointers); |
| 224 | destructor(value); |
| 225 | return 0; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | EnterCriticalSection(&g_destructors_lock); |
| 230 | g_destructors[index] = destructor; |
| 231 | LeaveCriticalSection(&g_destructors_lock); |
| 232 | |
| 233 | pointers[index] = value; |
| 234 | return 1; |
| 235 | } |
| 236 | |
David Benjamin | 84759bd | 2016-09-20 17:35:09 -0400 | [diff] [blame] | 237 | #endif /* OPENSSL_WINDOWS_THREADS */ |