blob: 85e599394e8d11ef168ab4cfe45055803d0f096e [file] [log] [blame]
Adam Langleyd7c5dfb2015-03-16 12:48:56 -07001/* 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 Langley65a7e942015-05-07 18:28:27 -070017#if defined(OPENSSL_WINDOWS) && !defined(OPENSSL_NO_THREADS)
Adam Langleyd7c5dfb2015-03-16 12:48:56 -070018
19#pragma warning(push, 3)
20#include <windows.h>
21#pragma warning(pop)
22
Adam Langleydf1f5e72015-04-13 11:04:08 -070023#include <stdlib.h>
Adam Langleyd7c5dfb2015-03-16 12:48:56 -070024#include <string.h>
25
26#include <openssl/mem.h>
Adam Langleydf1f5e72015-04-13 11:04:08 -070027#include <openssl/type_check.h>
Adam Langleyd7c5dfb2015-03-16 12:48:56 -070028
29
Adam Langleydf1f5e72015-04-13 11:04:08 -070030OPENSSL_COMPILE_ASSERT(sizeof(CRYPTO_MUTEX) >= sizeof(CRITICAL_SECTION),
31 CRYPTO_MUTEX_too_small);
32
David Benjamin9dadc3b2016-03-30 19:04:28 -040033static 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 Langleyd7c5dfb2015-03-16 12:48:56 -070037}
38
David Benjamin9dadc3b2016-03-30 19:04:28 -040039void CRYPTO_once(CRYPTO_once_t *once, void (*init)(void)) {
40 if (!InitOnceExecuteOnce(once, call_once_init, &init, NULL)) {
41 abort();
42 }
Adam Langleydf1f5e72015-04-13 11:04:08 -070043}
44
45void CRYPTO_MUTEX_init(CRYPTO_MUTEX *lock) {
46 if (!InitializeCriticalSectionAndSpinCount((CRITICAL_SECTION *) lock, 0x400)) {
47 abort();
48 }
49}
50
51void 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
56void CRYPTO_MUTEX_lock_write(CRYPTO_MUTEX *lock) {
57 EnterCriticalSection((CRITICAL_SECTION *) lock);
58}
59
David Benjamin29270de2016-05-24 15:28:36 +000060void CRYPTO_MUTEX_unlock_read(CRYPTO_MUTEX *lock) {
61 LeaveCriticalSection((CRITICAL_SECTION *) lock);
62}
63
64void CRYPTO_MUTEX_unlock_write(CRYPTO_MUTEX *lock) {
Adam Langleydf1f5e72015-04-13 11:04:08 -070065 LeaveCriticalSection((CRITICAL_SECTION *) lock);
66}
67
68void CRYPTO_MUTEX_cleanup(CRYPTO_MUTEX *lock) {
69 DeleteCriticalSection((CRITICAL_SECTION *) lock);
70}
71
David Benjamin9dadc3b2016-03-30 19:04:28 -040072static BOOL CALLBACK static_lock_init(INIT_ONCE *once, void *arg, void **out) {
73 struct CRYPTO_STATIC_MUTEX *lock = arg;
Adam Langleydf1f5e72015-04-13 11:04:08 -070074 if (!InitializeCriticalSectionAndSpinCount(&lock->lock, 0x400)) {
75 abort();
76 }
David Benjamin9dadc3b2016-03-30 19:04:28 -040077 return TRUE;
Adam Langleydf1f5e72015-04-13 11:04:08 -070078}
79
80void CRYPTO_STATIC_MUTEX_lock_read(struct CRYPTO_STATIC_MUTEX *lock) {
David Benjamin9dadc3b2016-03-30 19:04:28 -040081 /* 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 Langleydf1f5e72015-04-13 11:04:08 -070087 EnterCriticalSection(&lock->lock);
88}
89
90void CRYPTO_STATIC_MUTEX_lock_write(struct CRYPTO_STATIC_MUTEX *lock) {
91 CRYPTO_STATIC_MUTEX_lock_read(lock);
92}
93
David Benjamin29270de2016-05-24 15:28:36 +000094void CRYPTO_STATIC_MUTEX_unlock_read(struct CRYPTO_STATIC_MUTEX *lock) {
95 LeaveCriticalSection(&lock->lock);
96}
97
98void CRYPTO_STATIC_MUTEX_unlock_write(struct CRYPTO_STATIC_MUTEX *lock) {
Adam Langleydf1f5e72015-04-13 11:04:08 -070099 LeaveCriticalSection(&lock->lock);
100}
101
Adam Langleyd7c5dfb2015-03-16 12:48:56 -0700102static CRITICAL_SECTION g_destructors_lock;
103static thread_local_destructor_t g_destructors[NUM_OPENSSL_THREAD_LOCALS];
104
105static CRYPTO_once_t g_thread_local_init_once = CRYPTO_ONCE_INIT;
106static DWORD g_thread_local_key;
107static int g_thread_local_failed;
108
109static 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 Benjaminfeaa57d2016-03-29 14:17:27 -0400118static 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 Langleyd7c5dfb2015-03-16 12:48:56 -0700126 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 Benjamin40acdae2015-04-03 11:41:56 -0400164 * a reference to p_thread_callback_boringssl to prevent whole program
165 * optimization from discarding the variable. */
Adam Langleyd7c5dfb2015-03-16 12:48:56 -0700166#ifdef _WIN64
167#pragma comment(linker, "/INCLUDE:_tls_used")
David Benjamin40acdae2015-04-03 11:41:56 -0400168#pragma comment(linker, "/INCLUDE:p_thread_callback_boringssl")
Adam Langleyd7c5dfb2015-03-16 12:48:56 -0700169#else
170#pragma comment(linker, "/INCLUDE:__tls_used")
David Benjamin40acdae2015-04-03 11:41:56 -0400171#pragma comment(linker, "/INCLUDE:_p_thread_callback_boringssl")
Adam Langleyd7c5dfb2015-03-16 12:48:56 -0700172#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 Benjamin40acdae2015-04-03 11:41:56 -0400186/* The linker must not discard p_thread_callback_boringssl. (We force a reference
Adam Langleyd7c5dfb2015-03-16 12:48:56 -0700187 * 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 Benjamin40acdae2015-04-03 11:41:56 -0400196extern const PIMAGE_TLS_CALLBACK p_thread_callback_boringssl;
197const PIMAGE_TLS_CALLBACK p_thread_callback_boringssl = thread_local_destructor;
Adam Langleyd7c5dfb2015-03-16 12:48:56 -0700198/* Reset the default section. */
199#pragma const_seg()
200
201#else
202
203#pragma data_seg(".CRT$XLC")
David Benjamin40acdae2015-04-03 11:41:56 -0400204PIMAGE_TLS_CALLBACK p_thread_callback_boringssl = thread_local_destructor;
Adam Langleyd7c5dfb2015-03-16 12:48:56 -0700205/* Reset the default section. */
206#pragma data_seg()
207
208#endif /* _WIN64 */
209
210void *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
223int 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 Langley65a7e942015-05-07 18:28:27 -0700254#endif /* OPENSSL_WINDOWS && !OPENSSL_NO_THREADS */