Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/thread_pthread.c

raw
1 // Copyright (c) 2015, Google Inc.
2 // SPDX-License-Identifier: ISC
3
4 // Ensure we can't call OPENSSL_malloc circularly.
5 #define _BORINGSSL_PROHIBIT_OPENSSL_MALLOC
6 #include <errno.h>
7
8 #include "internal.h"
9
10 #if defined(OPENSSL_PTHREADS)
11
12 #include <pthread.h>
13 #include <stdlib.h>
14 #include <string.h>
15
16 #include <openssl/target.h>
17 #include <openssl/type_check.h>
18
19
20 OPENSSL_STATIC_ASSERT(sizeof(CRYPTO_MUTEX) >= sizeof(pthread_rwlock_t),
21 CRYPTO_MUTEX_is_too_small)
22 OPENSSL_STATIC_ASSERT(alignof(CRYPTO_MUTEX) >= alignof(pthread_rwlock_t),
23 CRYPTO_MUTEX_has_insufficient_alignment)
24
25 void CRYPTO_MUTEX_init(CRYPTO_MUTEX *lock) {
26 if (pthread_rwlock_init((pthread_rwlock_t *) lock, NULL) != 0) {
27 abort();
28 }
29 }
30
31 void CRYPTO_MUTEX_lock_read(CRYPTO_MUTEX *lock) {
32 if (pthread_rwlock_rdlock((pthread_rwlock_t *) lock) != 0) {
33 abort();
34 }
35 }
36
37 void CRYPTO_MUTEX_lock_write(CRYPTO_MUTEX *lock) {
38 if (pthread_rwlock_wrlock((pthread_rwlock_t *) lock) != 0) {
39 abort();
40 }
41 }
42
43 void CRYPTO_MUTEX_unlock_read(CRYPTO_MUTEX *lock) {
44 if (pthread_rwlock_unlock((pthread_rwlock_t *) lock) != 0) {
45 abort();
46 }
47 }
48
49 void CRYPTO_MUTEX_unlock_write(CRYPTO_MUTEX *lock) {
50 if (pthread_rwlock_unlock((pthread_rwlock_t *) lock) != 0) {
51 abort();
52 }
53 }
54
55 void CRYPTO_MUTEX_cleanup(CRYPTO_MUTEX *lock) {
56 pthread_rwlock_destroy((pthread_rwlock_t *) lock);
57 }
58
59 #ifdef __MINGW32__
60
61 #ifndef MIN
62 #define MINGW_AWSLC_MIN(X,Y) (((X) < (Y)) ? (X) : (Y))
63 #else
64 #define MINGW_AWSLC_MIN(X,Y) MIN(X,Y)
65 #endif
66
67 // MINGW implements nanosleep on X86_64 but not on other architectures.
68 // Prefer using nanosleep because of its higher resolution.
69 // https://gist.github.com/scivision/dbbbf33c2faf5a16f31fd6d144adc314 claims
70 // that MINGW only implements nanpsleep on X86, but not Arm64. The linked
71 // MINGW source code
72 // https://github.com/mingw-w64/mingw-w64/blob/master/mingw-w64-libraries/winpthreads/src/nanosleep.c
73 // does not seem to exclude any platforms though. But since this code is only
74 // really needed for X86 in the first place, avoid creating more availability
75 // risks.
76 #if defined(OPENSSL_X86_64)
77
78 #include <time.h>
79
80 // 100ms second in nanoseconds.
81 #define MINGW_MILLISECONDS_100 INT64_C(100000000)
82 #define MINGW_INITIAL_BACKOFF_DELAY 10
83
84 static void mingw_do_backoff(long *backoff) {
85
86 // Exponential backoff.
87 //
88 // iteration delay
89 // --------- ----------------
90 // 1 10 nsec
91 // 2 100 nsec
92 // 3 1,000 nsec
93 // 4 10,000 nsec
94 // 5 100,000 nsec
95 // 6 1,000,000 nsec
96 // 7 10,000,000 nsec
97 // 8 100,000,000 nsec
98 // 9 100,000,000 nsec
99 // ...
100 struct timespec sleep_time = {.tv_sec = 0, .tv_nsec = 0 };
101
102 // Cap backoff at 100,000,000 nsec (100ms).
103 *backoff = MINGW_AWSLC_MIN((*backoff) * 10, MINGW_MILLISECONDS_100);
104 // |nanosleep| can mutate |sleep_time|. Hence, we use |backoff| for state.
105 sleep_time.tv_nsec = *backoff;
106
107 nanosleep(&sleep_time, &sleep_time);
108 }
109
110 #else // defined(OPENSSL_X86_64)
111
112 #include <windows.h>
113
114 // 100 milliseconds.
115 #define MINGW_MILLISECONDS_100 INT64_C(100)
116 #define MINGW_INITIAL_BACKOFF_DELAY 1
117
118 static void mingw_do_backoff(long *backoff) {
119
120 // Additive backoff.
121 //
122 // iteration delay
123 // --------- -------
124 // 1 1 ms
125 // 2 10 ms
126 // 3 20 ms
127 // 4 30 ms
128 // 5 40 ms
129 // 6 50 ms
130 // 7 60 ms
131 // 8 70 ms
132 // 9 80 ms
133 // 10 90 ms
134 // 11 100 ms
135 // ...
136
137 // Cap backoff at 100ms.
138 *backoff = MINGW_AWSLC_MIN((*backoff) + 10, MINGW_MILLISECONDS_100);
139 Sleep((int)*backoff);
140 }
141 #endif // defined(OPENSSL_X86_64)
142
143 #endif // __MINGW32__
144
145 // Some MinGW pthreads implementations might fail on first use of
146 // locks initialized using PTHREAD_RWLOCK_INITIALIZER.
147 // See: https://sourceforge.net/p/mingw-w64/bugs/883/
148 typedef int (*pthread_rwlock_func_ptr)(pthread_rwlock_t *);
149 static int rwlock_EINVAL_fallback_retry(const pthread_rwlock_func_ptr func_ptr, pthread_rwlock_t* lock) {
150
151 int result = EINVAL;
152 #ifdef __MINGW32__
153 // This will be about 1 second of total backoff.
154 const int MAX_ATTEMPTS = 20;
155 int attempt_num = 0;
156 long backoff = MINGW_INITIAL_BACKOFF_DELAY;
157 do {
158 sched_yield();
159 attempt_num += 1;
160 result = func_ptr(lock);
161 mingw_do_backoff(&backoff);
162 } while(result == EINVAL && attempt_num < MAX_ATTEMPTS);
163
164 // To aid in debugging
165 if (result == EINVAL && attempt_num >= MAX_ATTEMPTS) {
166 fprintf(stderr, "ERROR: rwlock_EINVAL_fallback_retry() failed after %i attempts\n", MAX_ATTEMPTS);
167 }
168 #endif
169 return result;
170 }
171
172 void CRYPTO_STATIC_MUTEX_lock_read(struct CRYPTO_STATIC_MUTEX *lock) {
173 const int result = pthread_rwlock_rdlock(&lock->lock);
174 if (result != 0) {
175 if (result == EINVAL &&
176 0 == rwlock_EINVAL_fallback_retry(pthread_rwlock_rdlock, &lock->lock)) {
177 return;
178 }
179 abort();
180 }
181 }
182
183 void CRYPTO_STATIC_MUTEX_lock_write(struct CRYPTO_STATIC_MUTEX *lock) {
184 const int result = pthread_rwlock_wrlock(&lock->lock);
185 if (result != 0) {
186 if (result == EINVAL &&
187 0 == rwlock_EINVAL_fallback_retry(pthread_rwlock_wrlock, &lock->lock)) {
188 return;
189 }
190 abort();
191 }
192 }
193
194 void CRYPTO_STATIC_MUTEX_unlock_read(struct CRYPTO_STATIC_MUTEX *lock) {
195 if (pthread_rwlock_unlock(&lock->lock) != 0) {
196 abort();
197 }
198 }
199
200 void CRYPTO_STATIC_MUTEX_unlock_write(struct CRYPTO_STATIC_MUTEX *lock) {
201 if (pthread_rwlock_unlock(&lock->lock) != 0) {
202 abort();
203 }
204 }
205
206 #if !defined(NDEBUG)
207 int CRYPTO_STATIC_MUTEX_is_write_locked(struct CRYPTO_STATIC_MUTEX *lock) {
208 assert(lock != NULL);
209
210 int result = pthread_rwlock_tryrdlock(&lock->lock);
211
212 if (result == 0) {
213 // If we successfully acquired the lock, it wasn't locked
214 // Release it immediately and return false
215 pthread_rwlock_unlock(&lock->lock);
216 return 0;
217 }
218 // errno may be set to EDEADLK if the current thread is already has a write lock
219 if (result == EBUSY || result == EDEADLK) {
220 return 1;
221 }
222
223 return -1;
224 }
225 #endif
226
227 void CRYPTO_once(CRYPTO_once_t *once, void (*init)(void)) {
228 if (pthread_once(once, init) != 0) {
229 abort();
230 }
231 }
232
233 static pthread_mutex_t g_destructors_lock = PTHREAD_MUTEX_INITIALIZER;
234 static thread_local_destructor_t g_destructors[NUM_OPENSSL_THREAD_LOCALS];
235
236 // thread_local_destructor is called when a thread exits. It releases thread
237 // local data for that thread only.
238 static void thread_local_destructor(void *arg) {
239 if (arg == NULL) {
240 return;
241 }
242
243 thread_local_destructor_t destructors[NUM_OPENSSL_THREAD_LOCALS];
244 if (pthread_mutex_lock(&g_destructors_lock) != 0) {
245 return;
246 }
247 OPENSSL_memcpy(destructors, g_destructors, sizeof(destructors));
248 pthread_mutex_unlock(&g_destructors_lock);
249
250 unsigned i;
251 void **pointers = arg;
252 for (i = 0; i < NUM_OPENSSL_THREAD_LOCALS; i++) {
253 if (destructors[i] != NULL) {
254 destructors[i](pointers[i]);
255 }
256 }
257
258 free(pointers);
259 }
260
261 static pthread_once_t g_thread_local_init_once = PTHREAD_ONCE_INIT;
262 static pthread_key_t g_thread_local_key;
263 static int g_thread_local_key_created = 0;
264
265 static void thread_local_init(void) {
266 g_thread_local_key_created =
267 pthread_key_create(&g_thread_local_key, thread_local_destructor) == 0;
268 }
269
270 void *CRYPTO_get_thread_local(thread_local_data_t index) {
271 CRYPTO_once(&g_thread_local_init_once, thread_local_init);
272 if (!g_thread_local_key_created) {
273 return NULL;
274 }
275
276 void **pointers = pthread_getspecific(g_thread_local_key);
277 if (pointers == NULL) {
278 return NULL;
279 }
280 return pointers[index];
281 }
282
283 int CRYPTO_set_thread_local(thread_local_data_t index, void *value,
284 thread_local_destructor_t destructor) {
285 CRYPTO_once(&g_thread_local_init_once, thread_local_init);
286 if (!g_thread_local_key_created) {
287 destructor(value);
288 return 0;
289 }
290
291 void **pointers = pthread_getspecific(g_thread_local_key);
292 if (pointers == NULL) {
293 pointers = malloc(sizeof(void *) * NUM_OPENSSL_THREAD_LOCALS);
294 if (pointers == NULL) {
295 destructor(value);
296 return 0;
297 }
298 OPENSSL_memset(pointers, 0, sizeof(void *) * NUM_OPENSSL_THREAD_LOCALS);
299 if (pthread_setspecific(g_thread_local_key, pointers) != 0) {
300 free(pointers);
301 destructor(value);
302 return 0;
303 }
304 }
305
306 if (pthread_mutex_lock(&g_destructors_lock) != 0) {
307 destructor(value);
308 return 0;
309 }
310 g_destructors[index] = destructor;
311 pthread_mutex_unlock(&g_destructors_lock);
312
313 pointers[index] = value;
314 return 1;
315 }
316
317 int AWSLC_thread_local_clear(void) {
318 if (!g_thread_local_key_created) {
319 return 1;
320 }
321 void *pointers = pthread_getspecific(g_thread_local_key);
322 thread_local_destructor(pointers);
323 // By setting the value NULL, thread_local_destructor will not be called when
324 // this thread dies.
325 if (0 != pthread_setspecific(g_thread_local_key, NULL)) {
326 return 0;
327 }
328 return 1;
329 }
330
331 // This function is not thread-safe. It should only be called on a thread that
332 // is prepared to also call `dlclose` to unload our shared library.
333 int AWSLC_thread_local_shutdown(void) {
334 if (!g_thread_local_key_created) {
335 return 1;
336 }
337 // This deletes the thread local key
338 if (0 != pthread_key_delete(g_thread_local_key)) {
339 return 0;
340 }
341
342 g_thread_local_key_created = 0;
343 return 1;
344 }
345
346 #endif // OPENSSL_PTHREADS