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_PTHREADS)
18
19 #include <pthread.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include <openssl/mem.h>
24 #include <openssl/type_check.h>
25
26
27 OPENSSL_STATIC_ASSERT(sizeof(CRYPTO_MUTEX) >= sizeof(pthread_rwlock_t),
28 "CRYPTO_MUTEX is too small");
29 #if defined(__GNUC__) || defined(__clang__)
30 OPENSSL_STATIC_ASSERT(alignof(CRYPTO_MUTEX) >= alignof(pthread_rwlock_t),
31 "CRYPTO_MUTEX has insufficient alignment");
32 #endif
33
CRYPTO_MUTEX_init(CRYPTO_MUTEX * lock)34 void CRYPTO_MUTEX_init(CRYPTO_MUTEX *lock) {
35 if (pthread_rwlock_init((pthread_rwlock_t *) lock, NULL) != 0) {
36 abort();
37 }
38 }
39
CRYPTO_MUTEX_lock_read(CRYPTO_MUTEX * lock)40 void CRYPTO_MUTEX_lock_read(CRYPTO_MUTEX *lock) {
41 if (pthread_rwlock_rdlock((pthread_rwlock_t *) lock) != 0) {
42 abort();
43 }
44 }
45
CRYPTO_MUTEX_lock_write(CRYPTO_MUTEX * lock)46 void CRYPTO_MUTEX_lock_write(CRYPTO_MUTEX *lock) {
47 if (pthread_rwlock_wrlock((pthread_rwlock_t *) lock) != 0) {
48 abort();
49 }
50 }
51
CRYPTO_MUTEX_unlock_read(CRYPTO_MUTEX * lock)52 void CRYPTO_MUTEX_unlock_read(CRYPTO_MUTEX *lock) {
53 if (pthread_rwlock_unlock((pthread_rwlock_t *) lock) != 0) {
54 abort();
55 }
56 }
57
CRYPTO_MUTEX_unlock_write(CRYPTO_MUTEX * lock)58 void CRYPTO_MUTEX_unlock_write(CRYPTO_MUTEX *lock) {
59 if (pthread_rwlock_unlock((pthread_rwlock_t *) lock) != 0) {
60 abort();
61 }
62 }
63
CRYPTO_MUTEX_cleanup(CRYPTO_MUTEX * lock)64 void CRYPTO_MUTEX_cleanup(CRYPTO_MUTEX *lock) {
65 pthread_rwlock_destroy((pthread_rwlock_t *) lock);
66 }
67
CRYPTO_STATIC_MUTEX_lock_read(struct CRYPTO_STATIC_MUTEX * lock)68 void CRYPTO_STATIC_MUTEX_lock_read(struct CRYPTO_STATIC_MUTEX *lock) {
69 if (pthread_rwlock_rdlock(&lock->lock) != 0) {
70 abort();
71 }
72 }
73
CRYPTO_STATIC_MUTEX_lock_write(struct CRYPTO_STATIC_MUTEX * lock)74 void CRYPTO_STATIC_MUTEX_lock_write(struct CRYPTO_STATIC_MUTEX *lock) {
75 if (pthread_rwlock_wrlock(&lock->lock) != 0) {
76 abort();
77 }
78 }
79
CRYPTO_STATIC_MUTEX_unlock_read(struct CRYPTO_STATIC_MUTEX * lock)80 void CRYPTO_STATIC_MUTEX_unlock_read(struct CRYPTO_STATIC_MUTEX *lock) {
81 if (pthread_rwlock_unlock(&lock->lock) != 0) {
82 abort();
83 }
84 }
85
CRYPTO_STATIC_MUTEX_unlock_write(struct CRYPTO_STATIC_MUTEX * lock)86 void CRYPTO_STATIC_MUTEX_unlock_write(struct CRYPTO_STATIC_MUTEX *lock) {
87 if (pthread_rwlock_unlock(&lock->lock) != 0) {
88 abort();
89 }
90 }
91
CRYPTO_once(CRYPTO_once_t * once,void (* init)(void))92 void CRYPTO_once(CRYPTO_once_t *once, void (*init)(void)) {
93 if (pthread_once(once, init) != 0) {
94 abort();
95 }
96 }
97
98 static pthread_mutex_t g_destructors_lock = PTHREAD_MUTEX_INITIALIZER;
99 static thread_local_destructor_t g_destructors[NUM_OPENSSL_THREAD_LOCALS];
100
101 // thread_local_destructor is called when a thread exits. It releases thread
102 // local data for that thread only.
thread_local_destructor(void * arg)103 static void thread_local_destructor(void *arg) {
104 if (arg == NULL) {
105 return;
106 }
107
108 thread_local_destructor_t destructors[NUM_OPENSSL_THREAD_LOCALS];
109 if (pthread_mutex_lock(&g_destructors_lock) != 0) {
110 return;
111 }
112 OPENSSL_memcpy(destructors, g_destructors, sizeof(destructors));
113 pthread_mutex_unlock(&g_destructors_lock);
114
115 unsigned i;
116 void **pointers = arg;
117 for (i = 0; i < NUM_OPENSSL_THREAD_LOCALS; i++) {
118 if (destructors[i] != NULL) {
119 destructors[i](pointers[i]);
120 }
121 }
122
123 OPENSSL_free(pointers);
124 }
125
126 static pthread_once_t g_thread_local_init_once = PTHREAD_ONCE_INIT;
127 static pthread_key_t g_thread_local_key;
128 static int g_thread_local_key_created = 0;
129
130 // OPENSSL_DANGEROUS_RELEASE_PTHREAD_KEY can be defined to cause
131 // |pthread_key_delete| to be called in a destructor function. This can be
132 // useful for programs that dlclose BoringSSL.
133 //
134 // Note that dlclose()ing BoringSSL is not supported and will leak memory:
135 // thread-local values will be leaked as well as anything initialised via a
136 // once. The |pthread_key_t| is destroyed because they run out very quickly,
137 // while the other leaks are slow, and this allows code that happens to use
138 // dlclose() despite all the problems to continue functioning.
139 //
140 // This is marked "dangerous" because it can cause multi-threaded processes to
141 // crash (even if they don't use dlclose): if the destructor runs while other
142 // threads are still executing then they may end up using an invalid key to
143 // access thread-local variables.
144 //
145 // This may be removed after February 2020.
146 #if defined(OPENSSL_DANGEROUS_RELEASE_PTHREAD_KEY) && \
147 (defined(__GNUC__) || defined(__clang__))
148 // thread_key_destructor is called when the library is unloaded with dlclose.
149 static void thread_key_destructor(void) __attribute__((destructor, unused));
thread_key_destructor(void)150 static void thread_key_destructor(void) {
151 if (g_thread_local_key_created) {
152 g_thread_local_key_created = 0;
153 pthread_key_delete(g_thread_local_key);
154 }
155 }
156 #endif
157
thread_local_init(void)158 static void thread_local_init(void) {
159 g_thread_local_key_created =
160 pthread_key_create(&g_thread_local_key, thread_local_destructor) == 0;
161 }
162
CRYPTO_get_thread_local(thread_local_data_t index)163 void *CRYPTO_get_thread_local(thread_local_data_t index) {
164 CRYPTO_once(&g_thread_local_init_once, thread_local_init);
165 if (!g_thread_local_key_created) {
166 return NULL;
167 }
168
169 void **pointers = pthread_getspecific(g_thread_local_key);
170 if (pointers == NULL) {
171 return NULL;
172 }
173 return pointers[index];
174 }
175
CRYPTO_set_thread_local(thread_local_data_t index,void * value,thread_local_destructor_t destructor)176 int CRYPTO_set_thread_local(thread_local_data_t index, void *value,
177 thread_local_destructor_t destructor) {
178 CRYPTO_once(&g_thread_local_init_once, thread_local_init);
179 if (!g_thread_local_key_created) {
180 destructor(value);
181 return 0;
182 }
183
184 void **pointers = pthread_getspecific(g_thread_local_key);
185 if (pointers == NULL) {
186 pointers = OPENSSL_malloc(sizeof(void *) * NUM_OPENSSL_THREAD_LOCALS);
187 if (pointers == NULL) {
188 destructor(value);
189 return 0;
190 }
191 OPENSSL_memset(pointers, 0, sizeof(void *) * NUM_OPENSSL_THREAD_LOCALS);
192 if (pthread_setspecific(g_thread_local_key, pointers) != 0) {
193 OPENSSL_free(pointers);
194 destructor(value);
195 return 0;
196 }
197 }
198
199 if (pthread_mutex_lock(&g_destructors_lock) != 0) {
200 destructor(value);
201 return 0;
202 }
203 g_destructors[index] = destructor;
204 pthread_mutex_unlock(&g_destructors_lock);
205
206 pointers[index] = value;
207 return 1;
208 }
209
210 #endif // OPENSSL_PTHREADS
211