| 1 |
// Copyright (c) 2015, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
// Ensure we can't call OPENSSL_malloc circularly. |
| 5 |
#define _BORINGSSL_PROHIBIT_OPENSSL_MALLOC |
| 6 |
#include "internal.h" |
| 7 |
|
| 8 |
#if defined(OPENSSL_WINDOWS_THREADS) |
| 9 |
|
| 10 |
OPENSSL_MSVC_PRAGMA(warning(push, 3)) |
| 11 |
#include <windows.h> |
| 12 |
OPENSSL_MSVC_PRAGMA(warning(pop)) |
| 13 |
|
| 14 |
#include <stdlib.h> |
| 15 |
#include <string.h> |
| 16 |
|
| 17 |
#include <openssl/type_check.h> |
| 18 |
|
| 19 |
|
| 20 |
OPENSSL_STATIC_ASSERT(sizeof(CRYPTO_MUTEX) >= sizeof(SRWLOCK), |
| 21 |
CRYPTO_MUTEX_is_too_small) |
| 22 |
OPENSSL_STATIC_ASSERT(alignof(CRYPTO_MUTEX) >= alignof(SRWLOCK), |
| 23 |
CRYPTO_MUTEX_has_insufficient_alignment) |
| 24 |
|
| 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 |
|
| 31 |
void CRYPTO_once(CRYPTO_once_t *once, void (*init)(void)) { |
| 32 |
if (!InitOnceExecuteOnce(once, call_once_init, &init, NULL)) { |
| 33 |
abort(); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
void CRYPTO_MUTEX_init(CRYPTO_MUTEX *lock) { |
| 38 |
InitializeSRWLock((SRWLOCK *) lock); |
| 39 |
} |
| 40 |
|
| 41 |
void CRYPTO_MUTEX_lock_read(CRYPTO_MUTEX *lock) { |
| 42 |
AcquireSRWLockShared((SRWLOCK *) lock); |
| 43 |
} |
| 44 |
|
| 45 |
void CRYPTO_MUTEX_lock_write(CRYPTO_MUTEX *lock) { |
| 46 |
AcquireSRWLockExclusive((SRWLOCK *) lock); |
| 47 |
} |
| 48 |
|
| 49 |
void CRYPTO_MUTEX_unlock_read(CRYPTO_MUTEX *lock) { |
| 50 |
ReleaseSRWLockShared((SRWLOCK *) lock); |
| 51 |
} |
| 52 |
|
| 53 |
void CRYPTO_MUTEX_unlock_write(CRYPTO_MUTEX *lock) { |
| 54 |
ReleaseSRWLockExclusive((SRWLOCK *) lock); |
| 55 |
} |
| 56 |
|
| 57 |
void CRYPTO_MUTEX_cleanup(CRYPTO_MUTEX *lock) { |
| 58 |
// SRWLOCKs require no cleanup. |
| 59 |
} |
| 60 |
|
| 61 |
void CRYPTO_STATIC_MUTEX_lock_read(struct CRYPTO_STATIC_MUTEX *lock) { |
| 62 |
AcquireSRWLockShared(&lock->lock); |
| 63 |
} |
| 64 |
|
| 65 |
void CRYPTO_STATIC_MUTEX_lock_write(struct CRYPTO_STATIC_MUTEX *lock) { |
| 66 |
AcquireSRWLockExclusive(&lock->lock); |
| 67 |
} |
| 68 |
|
| 69 |
void CRYPTO_STATIC_MUTEX_unlock_read(struct CRYPTO_STATIC_MUTEX *lock) { |
| 70 |
ReleaseSRWLockShared(&lock->lock); |
| 71 |
} |
| 72 |
|
| 73 |
void CRYPTO_STATIC_MUTEX_unlock_write(struct CRYPTO_STATIC_MUTEX *lock) { |
| 74 |
ReleaseSRWLockExclusive(&lock->lock); |
| 75 |
} |
| 76 |
|
| 77 |
#if !defined(NDEBUG) |
| 78 |
int CRYPTO_STATIC_MUTEX_is_write_locked(struct CRYPTO_STATIC_MUTEX *lock) { |
| 79 |
assert(lock != NULL); |
| 80 |
|
| 81 |
if (TryAcquireSRWLockShared(&lock->lock)) { |
| 82 |
// If successful, the lock is not write-locked |
| 83 |
// Release it immediately and return false (0) |
| 84 |
ReleaseSRWLockShared(&lock->lock); |
| 85 |
return 0; |
| 86 |
} |
| 87 |
|
| 88 |
return 1; |
| 89 |
} |
| 90 |
#endif |
| 91 |
|
| 92 |
static SRWLOCK g_destructors_lock = SRWLOCK_INIT; |
| 93 |
static thread_local_destructor_t g_destructors[NUM_OPENSSL_THREAD_LOCALS]; |
| 94 |
|
| 95 |
static CRYPTO_once_t g_thread_local_init_once = CRYPTO_ONCE_INIT; |
| 96 |
static DWORD g_thread_local_key; |
| 97 |
static int g_thread_local_failed; |
| 98 |
|
| 99 |
static void thread_local_init(void) { |
| 100 |
g_thread_local_key = TlsAlloc(); |
| 101 |
g_thread_local_failed = (g_thread_local_key == TLS_OUT_OF_INDEXES); |
| 102 |
} |
| 103 |
|
| 104 |
static void NTAPI thread_local_destructor(PVOID module, DWORD reason, |
| 105 |
PVOID reserved) { |
| 106 |
// Only free memory on |DLL_THREAD_DETACH|, not |DLL_PROCESS_DETACH|. In |
| 107 |
// VS2015's debug runtime, the C runtime has been unloaded by the time |
| 108 |
// |DLL_PROCESS_DETACH| runs. See https://crbug.com/575795. This is consistent |
| 109 |
// with |pthread_key_create| which does not call destructors on process exit, |
| 110 |
// only thread exit. |
| 111 |
if (reason != DLL_THREAD_DETACH) { |
| 112 |
return; |
| 113 |
} |
| 114 |
|
| 115 |
CRYPTO_once(&g_thread_local_init_once, thread_local_init); |
| 116 |
if (g_thread_local_failed) { |
| 117 |
return; |
| 118 |
} |
| 119 |
|
| 120 |
void **pointers = (void**) TlsGetValue(g_thread_local_key); |
| 121 |
if (pointers == NULL) { |
| 122 |
return; |
| 123 |
} |
| 124 |
|
| 125 |
thread_local_destructor_t destructors[NUM_OPENSSL_THREAD_LOCALS]; |
| 126 |
|
| 127 |
AcquireSRWLockExclusive(&g_destructors_lock); |
| 128 |
OPENSSL_memcpy(destructors, g_destructors, sizeof(destructors)); |
| 129 |
ReleaseSRWLockExclusive(&g_destructors_lock); |
| 130 |
|
| 131 |
for (unsigned i = 0; i < NUM_OPENSSL_THREAD_LOCALS; i++) { |
| 132 |
if (destructors[i] != NULL) { |
| 133 |
destructors[i](pointers[i]); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
free(pointers); |
| 138 |
} |
| 139 |
|
| 140 |
// Thread Termination Callbacks. |
| 141 |
// |
| 142 |
// Windows doesn't support a per-thread destructor with its TLS primitives. |
| 143 |
// So, we build it manually by inserting a function to be called on each |
| 144 |
// thread's exit. This magic is from http://www.codeproject.com/threads/tls.asp |
| 145 |
// and it works for VC++ 7.0 and later. |
| 146 |
// |
| 147 |
// Force a reference to _tls_used to make the linker create the TLS directory |
| 148 |
// if it's not already there. (E.g. if __declspec(thread) is not used). Force |
| 149 |
// a reference to p_thread_callback_boringssl to prevent whole program |
| 150 |
// optimization from discarding the variable. |
| 151 |
// |
| 152 |
// Note, in the prefixed build, |p_thread_callback_boringssl| may be a macro. |
| 153 |
#ifdef _MSC_VER |
| 154 |
#define STRINGIFY(x) #x |
| 155 |
#define EXPAND_AND_STRINGIFY(x) STRINGIFY(x) |
| 156 |
#ifdef _WIN64 |
| 157 |
__pragma(comment(linker, "/INCLUDE:_tls_used")) |
| 158 |
__pragma(comment( |
| 159 |
linker, "/INCLUDE:" EXPAND_AND_STRINGIFY(p_thread_callback_boringssl))) |
| 160 |
#else |
| 161 |
__pragma(comment(linker, "/INCLUDE:__tls_used")) |
| 162 |
__pragma(comment( |
| 163 |
linker, "/INCLUDE:_" EXPAND_AND_STRINGIFY(p_thread_callback_boringssl))) |
| 164 |
#endif |
| 165 |
#endif |
| 166 |
|
| 167 |
// .CRT$XLA to .CRT$XLZ is an array of PIMAGE_TLS_CALLBACK pointers that are |
| 168 |
// called automatically by the OS loader code (not the CRT) when the module is |
| 169 |
// loaded and on thread creation. They are NOT called if the module has been |
| 170 |
// loaded by a LoadLibrary() call. It must have implicitly been loaded at |
| 171 |
// process startup. |
| 172 |
// |
| 173 |
// By implicitly loaded, I mean that it is directly referenced by the main EXE |
| 174 |
// or by one of its dependent DLLs. Delay-loaded DLL doesn't count as being |
| 175 |
// implicitly loaded. |
| 176 |
// |
| 177 |
// See VC\crt\src\tlssup.c for reference. |
| 178 |
|
| 179 |
// The linker must not discard p_thread_callback_boringssl. (We force a |
| 180 |
// reference to this variable with a linker /INCLUDE:symbol pragma to ensure |
| 181 |
// that.) If this variable is discarded, the OnThreadExit function will never |
| 182 |
// be called. |
| 183 |
#ifdef _WIN64 |
| 184 |
|
| 185 |
// .CRT section is merged with .rdata on x64 so it must be constant data. |
| 186 |
#pragma const_seg(".CRT$XLC") |
| 187 |
// When defining a const variable, it must have external linkage to be sure the |
| 188 |
// linker doesn't discard it. |
| 189 |
extern const PIMAGE_TLS_CALLBACK p_thread_callback_boringssl; |
| 190 |
const PIMAGE_TLS_CALLBACK p_thread_callback_boringssl = thread_local_destructor; |
| 191 |
// Reset the default section. |
| 192 |
#pragma const_seg() |
| 193 |
|
| 194 |
#else |
| 195 |
|
| 196 |
#pragma data_seg(".CRT$XLC") |
| 197 |
PIMAGE_TLS_CALLBACK p_thread_callback_boringssl = thread_local_destructor; |
| 198 |
// Reset the default section. |
| 199 |
#pragma data_seg() |
| 200 |
|
| 201 |
#endif // _WIN64 |
| 202 |
|
| 203 |
static void **get_thread_locals(void) { |
| 204 |
// |TlsGetValue| clears the last error even on success, so that callers may |
| 205 |
// distinguish it successfully returning NULL or failing. It is documented to |
| 206 |
// never fail if the argument is a valid index from |TlsAlloc|, so we do not |
| 207 |
// need to handle this. |
| 208 |
// |
| 209 |
// However, this error-mangling behavior interferes with the caller's use of |
| 210 |
// |GetLastError|. In particular |SSL_get_error| queries the error queue to |
| 211 |
// determine whether the caller should look at the OS's errors. To avoid |
| 212 |
// destroying state, save and restore the Windows error. |
| 213 |
// |
| 214 |
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms686812(v=vs.85).aspx |
| 215 |
DWORD last_error = GetLastError(); |
| 216 |
void **ret = TlsGetValue(g_thread_local_key); |
| 217 |
SetLastError(last_error); |
| 218 |
return ret; |
| 219 |
} |
| 220 |
|
| 221 |
void *CRYPTO_get_thread_local(thread_local_data_t index) { |
| 222 |
CRYPTO_once(&g_thread_local_init_once, thread_local_init); |
| 223 |
if (g_thread_local_failed) { |
| 224 |
return NULL; |
| 225 |
} |
| 226 |
|
| 227 |
void **pointers = get_thread_locals(); |
| 228 |
if (pointers == NULL) { |
| 229 |
return NULL; |
| 230 |
} |
| 231 |
return pointers[index]; |
| 232 |
} |
| 233 |
|
| 234 |
int CRYPTO_set_thread_local(thread_local_data_t index, void *value, |
| 235 |
thread_local_destructor_t destructor) { |
| 236 |
CRYPTO_once(&g_thread_local_init_once, thread_local_init); |
| 237 |
if (g_thread_local_failed) { |
| 238 |
destructor(value); |
| 239 |
return 0; |
| 240 |
} |
| 241 |
|
| 242 |
void **pointers = get_thread_locals(); |
| 243 |
if (pointers == NULL) { |
| 244 |
pointers = malloc(sizeof(void *) * NUM_OPENSSL_THREAD_LOCALS); |
| 245 |
if (pointers == NULL) { |
| 246 |
destructor(value); |
| 247 |
return 0; |
| 248 |
} |
| 249 |
OPENSSL_memset(pointers, 0, sizeof(void *) * NUM_OPENSSL_THREAD_LOCALS); |
| 250 |
if (TlsSetValue(g_thread_local_key, pointers) == 0) { |
| 251 |
free(pointers); |
| 252 |
destructor(value); |
| 253 |
return 0; |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
AcquireSRWLockExclusive(&g_destructors_lock); |
| 258 |
g_destructors[index] = destructor; |
| 259 |
ReleaseSRWLockExclusive(&g_destructors_lock); |
| 260 |
|
| 261 |
pointers[index] = value; |
| 262 |
return 1; |
| 263 |
} |
| 264 |
|
| 265 |
#endif // OPENSSL_WINDOWS_THREADS |