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