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