2015-03-16 19:48:56 +00:00
|
|
|
/* Copyright (c) 2015, Google Inc.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
|
|
|
|
|
|
|
|
#include "internal.h"
|
|
|
|
|
2016-09-20 22:35:09 +01:00
|
|
|
#if defined(OPENSSL_WINDOWS_THREADS)
|
2015-03-16 19:48:56 +00:00
|
|
|
|
2016-06-09 21:48:33 +01:00
|
|
|
OPENSSL_MSVC_PRAGMA(warning(push, 3))
|
2015-03-16 19:48:56 +00:00
|
|
|
#include <windows.h>
|
2016-06-09 21:48:33 +01:00
|
|
|
OPENSSL_MSVC_PRAGMA(warning(pop))
|
2015-03-16 19:48:56 +00:00
|
|
|
|
2015-04-13 19:04:08 +01:00
|
|
|
#include <stdlib.h>
|
2015-03-16 19:48:56 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <openssl/mem.h>
|
2015-04-13 19:04:08 +01:00
|
|
|
#include <openssl/type_check.h>
|
2015-03-16 19:48:56 +00:00
|
|
|
|
|
|
|
|
2018-10-24 23:08:00 +01:00
|
|
|
OPENSSL_STATIC_ASSERT(sizeof(CRYPTO_MUTEX) >= sizeof(SRWLOCK),
|
|
|
|
"CRYPTO_MUTEX is too small");
|
2015-04-13 19:04:08 +01:00
|
|
|
|
2016-03-31 00:04:28 +01:00
|
|
|
static BOOL CALLBACK call_once_init(INIT_ONCE *once, void *arg, void **out) {
|
|
|
|
void (**init)(void) = (void (**)(void))arg;
|
|
|
|
(**init)();
|
|
|
|
return TRUE;
|
2015-04-13 19:04:08 +01:00
|
|
|
}
|
|
|
|
|
2016-03-31 00:04:28 +01:00
|
|
|
void CRYPTO_once(CRYPTO_once_t *once, void (*init)(void)) {
|
|
|
|
if (!InitOnceExecuteOnce(once, call_once_init, &init, NULL)) {
|
|
|
|
abort();
|
|
|
|
}
|
2015-04-13 19:04:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CRYPTO_MUTEX_init(CRYPTO_MUTEX *lock) {
|
2016-05-24 16:41:11 +01:00
|
|
|
InitializeSRWLock((SRWLOCK *) lock);
|
2015-04-13 19:04:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CRYPTO_MUTEX_lock_read(CRYPTO_MUTEX *lock) {
|
2016-05-24 16:41:11 +01:00
|
|
|
AcquireSRWLockShared((SRWLOCK *) lock);
|
2015-04-13 19:04:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CRYPTO_MUTEX_lock_write(CRYPTO_MUTEX *lock) {
|
2016-05-24 16:41:11 +01:00
|
|
|
AcquireSRWLockExclusive((SRWLOCK *) lock);
|
2015-04-13 19:04:08 +01:00
|
|
|
}
|
|
|
|
|
2016-05-24 16:28:36 +01:00
|
|
|
void CRYPTO_MUTEX_unlock_read(CRYPTO_MUTEX *lock) {
|
2016-05-24 16:41:11 +01:00
|
|
|
ReleaseSRWLockShared((SRWLOCK *) lock);
|
2016-05-24 16:28:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CRYPTO_MUTEX_unlock_write(CRYPTO_MUTEX *lock) {
|
2016-05-24 16:41:11 +01:00
|
|
|
ReleaseSRWLockExclusive((SRWLOCK *) lock);
|
2015-04-13 19:04:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CRYPTO_MUTEX_cleanup(CRYPTO_MUTEX *lock) {
|
2017-08-18 19:06:02 +01:00
|
|
|
// SRWLOCKs require no cleanup.
|
2015-04-13 19:04:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CRYPTO_STATIC_MUTEX_lock_read(struct CRYPTO_STATIC_MUTEX *lock) {
|
2016-05-24 16:41:11 +01:00
|
|
|
AcquireSRWLockShared(&lock->lock);
|
2015-04-13 19:04:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CRYPTO_STATIC_MUTEX_lock_write(struct CRYPTO_STATIC_MUTEX *lock) {
|
2016-05-24 16:41:11 +01:00
|
|
|
AcquireSRWLockExclusive(&lock->lock);
|
2015-04-13 19:04:08 +01:00
|
|
|
}
|
|
|
|
|
2016-05-24 16:28:36 +01:00
|
|
|
void CRYPTO_STATIC_MUTEX_unlock_read(struct CRYPTO_STATIC_MUTEX *lock) {
|
2016-05-24 16:41:11 +01:00
|
|
|
ReleaseSRWLockShared(&lock->lock);
|
2016-05-24 16:28:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CRYPTO_STATIC_MUTEX_unlock_write(struct CRYPTO_STATIC_MUTEX *lock) {
|
2016-05-24 16:41:11 +01:00
|
|
|
ReleaseSRWLockExclusive(&lock->lock);
|
2015-04-13 19:04:08 +01:00
|
|
|
}
|
|
|
|
|
2015-03-16 19:48:56 +00:00
|
|
|
static CRITICAL_SECTION g_destructors_lock;
|
|
|
|
static thread_local_destructor_t g_destructors[NUM_OPENSSL_THREAD_LOCALS];
|
|
|
|
|
|
|
|
static CRYPTO_once_t g_thread_local_init_once = CRYPTO_ONCE_INIT;
|
|
|
|
static DWORD g_thread_local_key;
|
|
|
|
static int g_thread_local_failed;
|
|
|
|
|
|
|
|
static void thread_local_init(void) {
|
|
|
|
if (!InitializeCriticalSectionAndSpinCount(&g_destructors_lock, 0x400)) {
|
|
|
|
g_thread_local_failed = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
g_thread_local_key = TlsAlloc();
|
|
|
|
g_thread_local_failed = (g_thread_local_key == TLS_OUT_OF_INDEXES);
|
|
|
|
}
|
|
|
|
|
2016-03-29 19:17:27 +01:00
|
|
|
static void NTAPI thread_local_destructor(PVOID module, DWORD reason,
|
|
|
|
PVOID reserved) {
|
2017-08-18 19:06:02 +01:00
|
|
|
// Only free memory on |DLL_THREAD_DETACH|, not |DLL_PROCESS_DETACH|. In
|
|
|
|
// VS2015's debug runtime, the C runtime has been unloaded by the time
|
|
|
|
// |DLL_PROCESS_DETACH| runs. See https://crbug.com/575795. This is consistent
|
|
|
|
// with |pthread_key_create| which does not call destructors on process exit,
|
|
|
|
// only thread exit.
|
2016-03-29 19:17:27 +01:00
|
|
|
if (reason != DLL_THREAD_DETACH) {
|
2015-03-16 19:48:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CRYPTO_once(&g_thread_local_init_once, thread_local_init);
|
|
|
|
if (g_thread_local_failed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void **pointers = (void**) TlsGetValue(g_thread_local_key);
|
|
|
|
if (pointers == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
thread_local_destructor_t destructors[NUM_OPENSSL_THREAD_LOCALS];
|
|
|
|
|
|
|
|
EnterCriticalSection(&g_destructors_lock);
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memcpy(destructors, g_destructors, sizeof(destructors));
|
2015-03-16 19:48:56 +00:00
|
|
|
LeaveCriticalSection(&g_destructors_lock);
|
|
|
|
|
|
|
|
unsigned i;
|
|
|
|
for (i = 0; i < NUM_OPENSSL_THREAD_LOCALS; i++) {
|
|
|
|
if (destructors[i] != NULL) {
|
|
|
|
destructors[i](pointers[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
OPENSSL_free(pointers);
|
|
|
|
}
|
|
|
|
|
2017-08-18 19:06:02 +01:00
|
|
|
// Thread Termination Callbacks.
|
|
|
|
//
|
|
|
|
// Windows doesn't support a per-thread destructor with its TLS primitives.
|
|
|
|
// So, we build it manually by inserting a function to be called on each
|
|
|
|
// thread's exit. This magic is from http://www.codeproject.com/threads/tls.asp
|
|
|
|
// and it works for VC++ 7.0 and later.
|
|
|
|
//
|
|
|
|
// Force a reference to _tls_used to make the linker create the TLS directory
|
|
|
|
// if it's not already there. (E.g. if __declspec(thread) is not used). Force
|
|
|
|
// a reference to p_thread_callback_boringssl to prevent whole program
|
|
|
|
// optimization from discarding the variable.
|
2018-11-25 21:58:02 +00:00
|
|
|
//
|
|
|
|
// Note, in the prefixed build, |p_thread_callback_boringssl| may be a macro.
|
|
|
|
#define STRINGIFY(x) #x
|
|
|
|
#define EXPAND_AND_STRINGIFY(x) STRINGIFY(x)
|
2015-03-16 19:48:56 +00:00
|
|
|
#ifdef _WIN64
|
2018-11-25 21:58:02 +00:00
|
|
|
__pragma(comment(linker, "/INCLUDE:_tls_used"))
|
|
|
|
__pragma(comment(
|
|
|
|
linker, "/INCLUDE:" EXPAND_AND_STRINGIFY(p_thread_callback_boringssl)))
|
2015-03-16 19:48:56 +00:00
|
|
|
#else
|
2018-11-25 21:58:02 +00:00
|
|
|
__pragma(comment(linker, "/INCLUDE:__tls_used"))
|
|
|
|
__pragma(comment(
|
|
|
|
linker, "/INCLUDE:_" EXPAND_AND_STRINGIFY(p_thread_callback_boringssl)))
|
2015-03-16 19:48:56 +00:00
|
|
|
#endif
|
|
|
|
|
2017-08-18 19:06:02 +01:00
|
|
|
// .CRT$XLA to .CRT$XLZ is an array of PIMAGE_TLS_CALLBACK pointers that are
|
|
|
|
// called automatically by the OS loader code (not the CRT) when the module is
|
|
|
|
// loaded and on thread creation. They are NOT called if the module has been
|
|
|
|
// loaded by a LoadLibrary() call. It must have implicitly been loaded at
|
|
|
|
// process startup.
|
|
|
|
//
|
|
|
|
// By implicitly loaded, I mean that it is directly referenced by the main EXE
|
|
|
|
// or by one of its dependent DLLs. Delay-loaded DLL doesn't count as being
|
|
|
|
// implicitly loaded.
|
|
|
|
//
|
|
|
|
// See VC\crt\src\tlssup.c for reference.
|
|
|
|
|
|
|
|
// The linker must not discard p_thread_callback_boringssl. (We force a
|
|
|
|
// reference to this variable with a linker /INCLUDE:symbol pragma to ensure
|
|
|
|
// that.) If this variable is discarded, the OnThreadExit function will never
|
|
|
|
// be called.
|
2015-03-16 19:48:56 +00:00
|
|
|
#ifdef _WIN64
|
|
|
|
|
2017-08-18 19:06:02 +01:00
|
|
|
// .CRT section is merged with .rdata on x64 so it must be constant data.
|
2015-03-16 19:48:56 +00:00
|
|
|
#pragma const_seg(".CRT$XLC")
|
2017-08-18 19:06:02 +01:00
|
|
|
// When defining a const variable, it must have external linkage to be sure the
|
|
|
|
// linker doesn't discard it.
|
2015-04-03 16:41:56 +01:00
|
|
|
extern const PIMAGE_TLS_CALLBACK p_thread_callback_boringssl;
|
|
|
|
const PIMAGE_TLS_CALLBACK p_thread_callback_boringssl = thread_local_destructor;
|
2017-08-18 19:06:02 +01:00
|
|
|
// Reset the default section.
|
2015-03-16 19:48:56 +00:00
|
|
|
#pragma const_seg()
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#pragma data_seg(".CRT$XLC")
|
2015-04-03 16:41:56 +01:00
|
|
|
PIMAGE_TLS_CALLBACK p_thread_callback_boringssl = thread_local_destructor;
|
2017-08-18 19:06:02 +01:00
|
|
|
// Reset the default section.
|
2015-03-16 19:48:56 +00:00
|
|
|
#pragma data_seg()
|
|
|
|
|
2017-08-18 19:06:02 +01:00
|
|
|
#endif // _WIN64
|
2015-03-16 19:48:56 +00:00
|
|
|
|
2018-05-05 05:42:23 +01:00
|
|
|
static void **get_thread_locals(void) {
|
|
|
|
// |TlsGetValue| clears the last error even on success, so that callers may
|
|
|
|
// distinguish it successfully returning NULL or failing. It is documented to
|
|
|
|
// never fail if the argument is a valid index from |TlsAlloc|, so we do not
|
|
|
|
// need to handle this.
|
|
|
|
//
|
|
|
|
// However, this error-mangling behavior interferes with the caller's use of
|
|
|
|
// |GetLastError|. In particular |SSL_get_error| queries the error queue to
|
|
|
|
// determine whether the caller should look at the OS's errors. To avoid
|
|
|
|
// destroying state, save and restore the Windows error.
|
|
|
|
//
|
|
|
|
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms686812(v=vs.85).aspx
|
|
|
|
DWORD last_error = GetLastError();
|
|
|
|
void **ret = TlsGetValue(g_thread_local_key);
|
|
|
|
SetLastError(last_error);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-03-16 19:48:56 +00:00
|
|
|
void *CRYPTO_get_thread_local(thread_local_data_t index) {
|
|
|
|
CRYPTO_once(&g_thread_local_init_once, thread_local_init);
|
|
|
|
if (g_thread_local_failed) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-05-05 05:42:23 +01:00
|
|
|
void **pointers = get_thread_locals();
|
2015-03-16 19:48:56 +00:00
|
|
|
if (pointers == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return pointers[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
int CRYPTO_set_thread_local(thread_local_data_t index, void *value,
|
|
|
|
thread_local_destructor_t destructor) {
|
|
|
|
CRYPTO_once(&g_thread_local_init_once, thread_local_init);
|
|
|
|
if (g_thread_local_failed) {
|
|
|
|
destructor(value);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-05-05 05:42:23 +01:00
|
|
|
void **pointers = get_thread_locals();
|
2015-03-16 19:48:56 +00:00
|
|
|
if (pointers == NULL) {
|
|
|
|
pointers = OPENSSL_malloc(sizeof(void *) * NUM_OPENSSL_THREAD_LOCALS);
|
|
|
|
if (pointers == NULL) {
|
|
|
|
destructor(value);
|
|
|
|
return 0;
|
|
|
|
}
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memset(pointers, 0, sizeof(void *) * NUM_OPENSSL_THREAD_LOCALS);
|
2015-03-16 19:48:56 +00:00
|
|
|
if (TlsSetValue(g_thread_local_key, pointers) == 0) {
|
|
|
|
OPENSSL_free(pointers);
|
|
|
|
destructor(value);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
EnterCriticalSection(&g_destructors_lock);
|
|
|
|
g_destructors[index] = destructor;
|
|
|
|
LeaveCriticalSection(&g_destructors_lock);
|
|
|
|
|
|
|
|
pointers[index] = value;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-08-18 19:06:02 +01:00
|
|
|
#endif // OPENSSL_WINDOWS_THREADS
|