VirtualBox

source: vbox/trunk/src/libs/openssl-3.4.1/crypto/threads_pthread.c

Last change on this file was 109052, checked in by vboxsync, 3 weeks ago

openssl-3.4.1: Applied our changes, regenerated files, added missing files and functions. This time with a three way merge. ​bugref:10890

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 31.9 KB
Line 
1/*
2 * Copyright 2016-2025 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10/* We need to use the OPENSSL_fork_*() deprecated APIs */
11#define OPENSSL_SUPPRESS_DEPRECATED
12
13#include <openssl/crypto.h>
14#include <crypto/cryptlib.h>
15#include "internal/cryptlib.h"
16#include "internal/rcu.h"
17#include "rcu_internal.h"
18
19#if defined(__clang__) && defined(__has_feature)
20# if __has_feature(thread_sanitizer)
21# define __SANITIZE_THREAD__
22# endif
23#endif
24
25#if defined(__SANITIZE_THREAD__)
26# include <sanitizer/tsan_interface.h>
27# define TSAN_FAKE_UNLOCK(x) __tsan_mutex_pre_unlock((x), 0); \
28__tsan_mutex_post_unlock((x), 0)
29
30# define TSAN_FAKE_LOCK(x) __tsan_mutex_pre_lock((x), 0); \
31__tsan_mutex_post_lock((x), 0, 0)
32#else
33# define TSAN_FAKE_UNLOCK(x)
34# define TSAN_FAKE_LOCK(x)
35#endif
36
37#if defined(__sun)
38# include <atomic.h>
39#endif
40
41#if defined(__apple_build_version__) && __apple_build_version__ < 6000000
42/*
43 * OS/X 10.7 and 10.8 had a weird version of clang which has __ATOMIC_ACQUIRE and
44 * __ATOMIC_ACQ_REL but which expects only one parameter for __atomic_is_lock_free()
45 * rather than two which has signature __atomic_is_lock_free(sizeof(_Atomic(T))).
46 * All of this makes impossible to use __atomic_is_lock_free here.
47 *
48 * See: https://github.com/llvm/llvm-project/commit/a4c2602b714e6c6edb98164550a5ae829b2de760
49 */
50# define BROKEN_CLANG_ATOMICS
51#endif
52
53#if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG) && !defined(OPENSSL_SYS_WINDOWS)
54
55# if defined(OPENSSL_SYS_UNIX)
56# include <sys/types.h>
57# include <unistd.h>
58# endif
59
60# include <assert.h>
61
62/*
63 * The Non-Stop KLT thread model currently seems broken in its rwlock
64 * implementation
65 */
66# if defined(PTHREAD_RWLOCK_INITIALIZER) && !defined(_KLT_MODEL_)
67# define USE_RWLOCK
68# endif
69
70/*
71 * For all GNU/clang atomic builtins, we also need fallbacks, to cover all
72 * other compilers.
73
74 * Unfortunately, we can't do that with some "generic type", because there's no
75 * guarantee that the chosen generic type is large enough to cover all cases.
76 * Therefore, we implement fallbacks for each applicable type, with composed
77 * names that include the type they handle.
78 *
79 * (an anecdote: we previously tried to use |void *| as the generic type, with
80 * the thought that the pointer itself is the largest type. However, this is
81 * not true on 32-bit pointer platforms, as a |uint64_t| is twice as large)
82 *
83 * All applicable ATOMIC_ macros take the intended type as first parameter, so
84 * they can map to the correct fallback function. In the GNU/clang case, that
85 * parameter is simply ignored.
86 */
87
88/*
89 * Internal types used with the ATOMIC_ macros, to make it possible to compose
90 * fallback function names.
91 */
92typedef void *pvoid;
93typedef struct rcu_cb_item *prcu_cb_item;
94
95# if defined(__GNUC__) && defined(__ATOMIC_ACQUIRE) && !defined(BROKEN_CLANG_ATOMICS) \
96 && !defined(USE_ATOMIC_FALLBACKS)
97# if defined(__APPLE__) && defined(__clang__) && defined(__aarch64__) && defined(__LP64__)
98/*
99 * For pointers, Apple M1 virtualized cpu seems to have some problem using the
100 * ldapr instruction (see https://github.com/openssl/openssl/pull/23974)
101 * When using the native apple clang compiler, this instruction is emitted for
102 * atomic loads, which is bad. So, if
103 * 1) We are building on a target that defines __APPLE__ AND
104 * 2) We are building on a target using clang (__clang__) AND
105 * 3) We are building for an M1 processor (__aarch64__) AND
106 * 4) We are building with 64 bit pointers
107 * Then we should not use __atomic_load_n and instead implement our own
108 * function to issue the ldar instruction instead, which produces the proper
109 * sequencing guarantees
110 */
111static inline void *apple_atomic_load_n_pvoid(void **p,
112 ossl_unused int memorder)
113{
114 void *ret;
115
116 __asm volatile("ldar %0, [%1]" : "=r" (ret): "r" (p):);
117
118 return ret;
119}
120
121/* For uint64_t, we should be fine, though */
122# define apple_atomic_load_n_uint32_t(p, o) __atomic_load_n(p, o)
123# define apple_atomic_load_n_uint64_t(p, o) __atomic_load_n(p, o)
124
125# define ATOMIC_LOAD_N(t, p, o) apple_atomic_load_n_##t(p, o)
126# else
127# define ATOMIC_LOAD_N(t, p, o) __atomic_load_n(p, o)
128# endif
129# define ATOMIC_STORE_N(t, p, v, o) __atomic_store_n(p, v, o)
130# define ATOMIC_STORE(t, p, v, o) __atomic_store(p, v, o)
131# define ATOMIC_EXCHANGE_N(t, p, v, o) __atomic_exchange_n(p, v, o)
132# define ATOMIC_COMPARE_EXCHANGE_N(t, p, e, d, s, f) __atomic_compare_exchange_n(p, e, d, 0, s, f)
133# define ATOMIC_ADD_FETCH(p, v, o) __atomic_add_fetch(p, v, o)
134# define ATOMIC_FETCH_ADD(p, v, o) __atomic_fetch_add(p, v, o)
135# define ATOMIC_SUB_FETCH(p, v, o) __atomic_sub_fetch(p, v, o)
136# define ATOMIC_AND_FETCH(p, m, o) __atomic_and_fetch(p, m, o)
137# define ATOMIC_OR_FETCH(p, m, o) __atomic_or_fetch(p, m, o)
138# else
139static pthread_mutex_t atomic_sim_lock = PTHREAD_MUTEX_INITIALIZER;
140
141# define IMPL_fallback_atomic_load_n(t) \
142 static ossl_inline t fallback_atomic_load_n_##t(t *p) \
143 { \
144 t ret; \
145 \
146 pthread_mutex_lock(&atomic_sim_lock); \
147 ret = *p; \
148 pthread_mutex_unlock(&atomic_sim_lock); \
149 return ret; \
150 }
151IMPL_fallback_atomic_load_n(uint32_t)
152IMPL_fallback_atomic_load_n(uint64_t)
153IMPL_fallback_atomic_load_n(pvoid)
154
155# define ATOMIC_LOAD_N(t, p, o) fallback_atomic_load_n_##t(p)
156
157# define IMPL_fallback_atomic_store_n(t) \
158 static ossl_inline t fallback_atomic_store_n_##t(t *p, t v) \
159 { \
160 t ret; \
161 \
162 pthread_mutex_lock(&atomic_sim_lock); \
163 ret = *p; \
164 *p = v; \
165 pthread_mutex_unlock(&atomic_sim_lock); \
166 return ret; \
167 }
168IMPL_fallback_atomic_store_n(uint32_t)
169IMPL_fallback_atomic_store_n(uint64_t)
170
171# define ATOMIC_STORE_N(t, p, v, o) fallback_atomic_store_n_##t(p, v)
172
173# define IMPL_fallback_atomic_store(t) \
174 static ossl_inline void fallback_atomic_store_##t(t *p, t *v) \
175 { \
176 pthread_mutex_lock(&atomic_sim_lock); \
177 *p = *v; \
178 pthread_mutex_unlock(&atomic_sim_lock); \
179 }
180IMPL_fallback_atomic_store(uint64_t)
181IMPL_fallback_atomic_store(pvoid)
182
183# define ATOMIC_STORE(t, p, v, o) fallback_atomic_store_##t(p, v)
184
185# define IMPL_fallback_atomic_exchange_n(t) \
186 static ossl_inline t fallback_atomic_exchange_n_##t(t *p, t v) \
187 { \
188 t ret; \
189 \
190 pthread_mutex_lock(&atomic_sim_lock); \
191 ret = *p; \
192 *p = v; \
193 pthread_mutex_unlock(&atomic_sim_lock); \
194 return ret; \
195 }
196IMPL_fallback_atomic_exchange_n(uint64_t)
197IMPL_fallback_atomic_exchange_n(prcu_cb_item)
198
199# define ATOMIC_EXCHANGE_N(t, p, v, o) fallback_atomic_exchange_n_##t(p, v)
200
201# define IMPL_fallback_atomic_compare_exchange_n(t) \
202 static ossl_inline int fallback_atomic_compare_exchange_n_##t(t *p, t *e, t d, s, f) \
203 { \
204 int ret = 1; \
205 pthread_mutex_lock(&atomic_sim_lock); \
206 if (*p == *e) \
207 *p = d; \
208 else \
209 ret = 0; \
210 pthread_mutex_unlock(&atomic_sim_lock); \
211 return ret; \
212 }
213
214IMPL_fallback_atomic_exchange_n(uint64_t)
215
216# define ATOMIC_COMPARE_EXCHANGE_N(t, p, e, d, s, f) fallback_atomic_compare_exchange_n_##t(p, e, d, s, f)
217
218/*
219 * The fallbacks that follow don't need any per type implementation, as
220 * they are designed for uint64_t only. If there comes a time when multiple
221 * types need to be covered, it's relatively easy to refactor them the same
222 * way as the fallbacks above.
223 */
224
225static ossl_inline uint64_t fallback_atomic_add_fetch(uint64_t *p, uint64_t v)
226{
227 uint64_t ret;
228
229 pthread_mutex_lock(&atomic_sim_lock);
230 *p += v;
231 ret = *p;
232 pthread_mutex_unlock(&atomic_sim_lock);
233 return ret;
234}
235
236# define ATOMIC_ADD_FETCH(p, v, o) fallback_atomic_add_fetch(p, v)
237
238static ossl_inline uint64_t fallback_atomic_fetch_add(uint64_t *p, uint64_t v)
239{
240 uint64_t ret;
241
242 pthread_mutex_lock(&atomic_sim_lock);
243 ret = *p;
244 *p += v;
245 pthread_mutex_unlock(&atomic_sim_lock);
246 return ret;
247}
248
249# define ATOMIC_FETCH_ADD(p, v, o) fallback_atomic_fetch_add(p, v)
250
251static ossl_inline uint64_t fallback_atomic_sub_fetch(uint64_t *p, uint64_t v)
252{
253 uint64_t ret;
254
255 pthread_mutex_lock(&atomic_sim_lock);
256 *p -= v;
257 ret = *p;
258 pthread_mutex_unlock(&atomic_sim_lock);
259 return ret;
260}
261
262# define ATOMIC_SUB_FETCH(p, v, o) fallback_atomic_sub_fetch(p, v)
263
264static ossl_inline uint64_t fallback_atomic_and_fetch(uint64_t *p, uint64_t m)
265{
266 uint64_t ret;
267
268 pthread_mutex_lock(&atomic_sim_lock);
269 *p &= m;
270 ret = *p;
271 pthread_mutex_unlock(&atomic_sim_lock);
272 return ret;
273}
274
275# define ATOMIC_AND_FETCH(p, v, o) fallback_atomic_and_fetch(p, v)
276
277static ossl_inline uint64_t fallback_atomic_or_fetch(uint64_t *p, uint64_t m)
278{
279 uint64_t ret;
280
281 pthread_mutex_lock(&atomic_sim_lock);
282 *p |= m;
283 ret = *p;
284 pthread_mutex_unlock(&atomic_sim_lock);
285 return ret;
286}
287
288# define ATOMIC_OR_FETCH(p, v, o) fallback_atomic_or_fetch(p, v)
289# endif
290
291/*
292 * users is broken up into 2 parts
293 * bits 0-15 current readers
294 * bit 32-63 ID
295 */
296# define READER_SHIFT 0
297# define ID_SHIFT 32
298/* TODO: READER_SIZE 32 in threads_win.c */
299# define READER_SIZE 16
300# define ID_SIZE 32
301
302# define READER_MASK (((uint64_t)1 << READER_SIZE) - 1)
303# define ID_MASK (((uint64_t)1 << ID_SIZE) - 1)
304# define READER_COUNT(x) ((uint32_t)(((uint64_t)(x) >> READER_SHIFT) & \
305 READER_MASK))
306# define ID_VAL(x) ((uint32_t)(((uint64_t)(x) >> ID_SHIFT) & ID_MASK))
307# define VAL_READER ((uint64_t)1 << READER_SHIFT)
308# define VAL_ID(x) ((uint64_t)x << ID_SHIFT)
309
310/*
311 * This is the core of an rcu lock. It tracks the readers and writers for the
312 * current quiescence point for a given lock. Users is the 64 bit value that
313 * stores the READERS/ID as defined above
314 *
315 */
316struct rcu_qp {
317 uint64_t users;
318};
319
320struct thread_qp {
321 struct rcu_qp *qp;
322 unsigned int depth;
323 CRYPTO_RCU_LOCK *lock;
324};
325
326# define MAX_QPS 10
327/*
328 * This is the per thread tracking data
329 * that is assigned to each thread participating
330 * in an rcu qp
331 *
332 * qp points to the qp that it last acquired
333 *
334 */
335struct rcu_thr_data {
336 struct thread_qp thread_qps[MAX_QPS];
337};
338
339/*
340 * This is the internal version of a CRYPTO_RCU_LOCK
341 * it is cast from CRYPTO_RCU_LOCK
342 */
343struct rcu_lock_st {
344 /* Callbacks to call for next ossl_synchronize_rcu */
345 struct rcu_cb_item *cb_items;
346
347 /* The context we are being created against */
348 OSSL_LIB_CTX *ctx;
349
350 /* rcu generation counter for in-order retirement */
351 uint32_t id_ctr;
352
353 /* TODO: can be moved before id_ctr for better alignment */
354 /* Array of quiescent points for synchronization */
355 struct rcu_qp *qp_group;
356
357 /* Number of elements in qp_group array */
358 uint32_t group_count;
359
360 /* Index of the current qp in the qp_group array */
361 uint32_t reader_idx;
362
363 /* value of the next id_ctr value to be retired */
364 uint32_t next_to_retire;
365
366 /* index of the next free rcu_qp in the qp_group */
367 uint32_t current_alloc_idx;
368
369 /* number of qp's in qp_group array currently being retired */
370 uint32_t writers_alloced;
371
372 /* lock protecting write side operations */
373 pthread_mutex_t write_lock;
374
375 /* lock protecting updates to writers_alloced/current_alloc_idx */
376 pthread_mutex_t alloc_lock;
377
378 /* signal to wake threads waiting on alloc_lock */
379 pthread_cond_t alloc_signal;
380
381 /* lock to enforce in-order retirement */
382 pthread_mutex_t prior_lock;
383
384 /* signal to wake threads waiting on prior_lock */
385 pthread_cond_t prior_signal;
386};
387
388/* Read side acquisition of the current qp */
389static struct rcu_qp *get_hold_current_qp(struct rcu_lock_st *lock)
390{
391 uint32_t qp_idx;
392
393 /* get the current qp index */
394 for (;;) {
395 /*
396 * Notes on use of __ATOMIC_ACQUIRE
397 * We need to ensure the following:
398 * 1) That subsequent operations aren't optimized by hoisting them above
399 * this operation. Specifically, we don't want the below re-load of
400 * qp_idx to get optimized away
401 * 2) We want to ensure that any updating of reader_idx on the write side
402 * of the lock is flushed from a local cpu cache so that we see any
403 * updates prior to the load. This is a non-issue on cache coherent
404 * systems like x86, but is relevant on other arches
405 * Note: This applies to the reload below as well
406 */
407 qp_idx = ATOMIC_LOAD_N(uint32_t, &lock->reader_idx, __ATOMIC_ACQUIRE);
408
409 /*
410 * Notes of use of __ATOMIC_RELEASE
411 * This counter is only read by the write side of the lock, and so we
412 * specify __ATOMIC_RELEASE here to ensure that the write side of the
413 * lock see this during the spin loop read of users, as it waits for the
414 * reader count to approach zero
415 */
416 ATOMIC_ADD_FETCH(&lock->qp_group[qp_idx].users, VAL_READER,
417 __ATOMIC_RELEASE);
418
419 /* if the idx hasn't changed, we're good, else try again */
420 if (qp_idx == ATOMIC_LOAD_N(uint32_t, &lock->reader_idx, __ATOMIC_ACQUIRE))
421 break;
422
423 /*
424 * Notes on use of __ATOMIC_RELEASE
425 * As with the add above, we want to ensure that this decrement is
426 * seen by the write side of the lock as soon as it happens to prevent
427 * undue spinning waiting for write side completion
428 */
429 ATOMIC_SUB_FETCH(&lock->qp_group[qp_idx].users, VAL_READER,
430 __ATOMIC_RELEASE);
431 }
432
433 return &lock->qp_group[qp_idx];
434}
435
436static void ossl_rcu_free_local_data(void *arg)
437{
438 OSSL_LIB_CTX *ctx = arg;
439 CRYPTO_THREAD_LOCAL *lkey = ossl_lib_ctx_get_rcukey(ctx);
440 struct rcu_thr_data *data = CRYPTO_THREAD_get_local(lkey);
441
442 OPENSSL_free(data);
443 CRYPTO_THREAD_set_local(lkey, NULL);
444}
445
446void ossl_rcu_read_lock(CRYPTO_RCU_LOCK *lock)
447{
448 struct rcu_thr_data *data;
449 int i, available_qp = -1;
450 CRYPTO_THREAD_LOCAL *lkey = ossl_lib_ctx_get_rcukey(lock->ctx);
451
452 /*
453 * we're going to access current_qp here so ask the
454 * processor to fetch it
455 */
456 data = CRYPTO_THREAD_get_local(lkey);
457
458 if (data == NULL) {
459 data = OPENSSL_zalloc(sizeof(*data));
460 OPENSSL_assert(data != NULL);
461 CRYPTO_THREAD_set_local(lkey, data);
462 ossl_init_thread_start(NULL, lock->ctx, ossl_rcu_free_local_data);
463 }
464
465 for (i = 0; i < MAX_QPS; i++) {
466 if (data->thread_qps[i].qp == NULL && available_qp == -1)
467 available_qp = i;
468 /* If we have a hold on this lock already, we're good */
469 if (data->thread_qps[i].lock == lock) {
470 data->thread_qps[i].depth++;
471 return;
472 }
473 }
474
475 /*
476 * if we get here, then we don't have a hold on this lock yet
477 */
478 assert(available_qp != -1);
479
480 data->thread_qps[available_qp].qp = get_hold_current_qp(lock);
481 data->thread_qps[available_qp].depth = 1;
482 data->thread_qps[available_qp].lock = lock;
483}
484
485void ossl_rcu_read_unlock(CRYPTO_RCU_LOCK *lock)
486{
487 int i;
488 CRYPTO_THREAD_LOCAL *lkey = ossl_lib_ctx_get_rcukey(lock->ctx);
489 struct rcu_thr_data *data = CRYPTO_THREAD_get_local(lkey);
490 uint64_t ret;
491
492 assert(data != NULL);
493
494 for (i = 0; i < MAX_QPS; i++) {
495 if (data->thread_qps[i].lock == lock) {
496 /*
497 * As with read side acquisition, we use __ATOMIC_RELEASE here
498 * to ensure that the decrement is published immediately
499 * to any write side waiters
500 */
501 data->thread_qps[i].depth--;
502 if (data->thread_qps[i].depth == 0) {
503 ret = ATOMIC_SUB_FETCH(&data->thread_qps[i].qp->users, VAL_READER,
504 __ATOMIC_RELEASE);
505 OPENSSL_assert(ret != UINT64_MAX);
506 data->thread_qps[i].qp = NULL;
507 data->thread_qps[i].lock = NULL;
508 }
509 return;
510 }
511 }
512 /*
513 * If we get here, we're trying to unlock a lock that we never acquired -
514 * that's fatal.
515 */
516 assert(0);
517}
518
519/*
520 * Write side allocation routine to get the current qp
521 * and replace it with a new one
522 */
523static struct rcu_qp *update_qp(CRYPTO_RCU_LOCK *lock)
524{
525 uint64_t new_id;
526 uint64_t update;
527 uint64_t ret;
528 uint32_t current_idx;
529
530 pthread_mutex_lock(&lock->alloc_lock);
531
532 /*
533 * we need at least one qp to be available with one
534 * left over, so that readers can start working on
535 * one that isn't yet being waited on
536 */
537 while (lock->group_count - lock->writers_alloced < 2)
538 /* we have to wait for one to be free */
539 pthread_cond_wait(&lock->alloc_signal, &lock->alloc_lock);
540
541 current_idx = lock->current_alloc_idx;
542
543 /* Allocate the qp */
544 lock->writers_alloced++;
545
546 /* increment the allocation index */
547 lock->current_alloc_idx =
548 (lock->current_alloc_idx + 1) % lock->group_count;
549
550 /* get and insert a new id */
551 new_id = VAL_ID(lock->id_ctr);
552 lock->id_ctr++;
553
554 /*
555 * Even though we are under a write side lock here
556 * We need to use atomic instructions to ensure that the results
557 * of this update are published to the read side prior to updating the
558 * reader idx below
559 */
560try_again:
561 ret = ATOMIC_LOAD_N(uint64_t, &lock->qp_group[current_idx].users, __ATOMIC_ACQUIRE);
562 update = ret & ID_MASK;
563 update |= new_id;
564 if (!ATOMIC_COMPARE_EXCHANGE_N(uint64_t, &lock->qp_group[current_idx].users, &ret, update,
565 __ATOMIC_ACQ_REL, __ATOMIC_RELAXED))
566 goto try_again;
567
568 /*
569 * Update the reader index to be the prior qp.
570 * Note the use of __ATOMIC_RELEASE here is based on the corresponding use
571 * of __ATOMIC_ACQUIRE in get_hold_current_qp, as we want any publication
572 * of this value to be seen on the read side immediately after it happens
573 */
574 ATOMIC_STORE_N(uint32_t, &lock->reader_idx, lock->current_alloc_idx,
575 __ATOMIC_RELEASE);
576
577 /* wake up any waiters */
578 pthread_cond_signal(&lock->alloc_signal);
579 pthread_mutex_unlock(&lock->alloc_lock);
580 return &lock->qp_group[current_idx];
581}
582
583static void retire_qp(CRYPTO_RCU_LOCK *lock, struct rcu_qp *qp)
584{
585 pthread_mutex_lock(&lock->alloc_lock);
586 lock->writers_alloced--;
587 pthread_cond_signal(&lock->alloc_signal);
588 pthread_mutex_unlock(&lock->alloc_lock);
589}
590
591/* TODO: count should be unsigned, e.g uint32_t */
592/* a negative value could result in unexpected behaviour */
593static struct rcu_qp *allocate_new_qp_group(CRYPTO_RCU_LOCK *lock,
594 int count)
595{
596 struct rcu_qp *new =
597 OPENSSL_zalloc(sizeof(*new) * count);
598
599 lock->group_count = count;
600 return new;
601}
602
603void ossl_rcu_write_lock(CRYPTO_RCU_LOCK *lock)
604{
605 pthread_mutex_lock(&lock->write_lock);
606 TSAN_FAKE_UNLOCK(&lock->write_lock);
607}
608
609void ossl_rcu_write_unlock(CRYPTO_RCU_LOCK *lock)
610{
611 TSAN_FAKE_LOCK(&lock->write_lock);
612 pthread_mutex_unlock(&lock->write_lock);
613}
614
615void ossl_synchronize_rcu(CRYPTO_RCU_LOCK *lock)
616{
617 struct rcu_qp *qp;
618 uint64_t count;
619 struct rcu_cb_item *cb_items, *tmpcb;
620
621 pthread_mutex_lock(&lock->write_lock);
622 cb_items = lock->cb_items;
623 lock->cb_items = NULL;
624 pthread_mutex_unlock(&lock->write_lock);
625
626 qp = update_qp(lock);
627
628 /*
629 * wait for the reader count to reach zero
630 * Note the use of __ATOMIC_ACQUIRE here to ensure that any
631 * prior __ATOMIC_RELEASE write operation in get_hold_current_qp
632 * is visible prior to our read
633 */
634 do {
635 count = ATOMIC_LOAD_N(uint64_t, &qp->users, __ATOMIC_ACQUIRE);
636 } while (READER_COUNT(count) != 0);
637
638 /* retire in order */
639 pthread_mutex_lock(&lock->prior_lock);
640 while (lock->next_to_retire != ID_VAL(count))
641 pthread_cond_wait(&lock->prior_signal, &lock->prior_lock);
642 lock->next_to_retire++;
643 pthread_cond_broadcast(&lock->prior_signal);
644 pthread_mutex_unlock(&lock->prior_lock);
645
646 retire_qp(lock, qp);
647
648 /* handle any callbacks that we have */
649 while (cb_items != NULL) {
650 tmpcb = cb_items;
651 cb_items = cb_items->next;
652 tmpcb->fn(tmpcb->data);
653 OPENSSL_free(tmpcb);
654 }
655}
656
657int ossl_rcu_call(CRYPTO_RCU_LOCK *lock, rcu_cb_fn cb, void *data)
658{
659 struct rcu_cb_item *new =
660 OPENSSL_zalloc(sizeof(*new));
661
662 if (new == NULL)
663 return 0;
664
665 new->data = data;
666 new->fn = cb;
667 /*
668 * Use __ATOMIC_ACQ_REL here to indicate that any prior writes to this
669 * list are visible to us prior to reading, and publish the new value
670 * immediately
671 */
672 new->next = ATOMIC_EXCHANGE_N(prcu_cb_item, &lock->cb_items, new,
673 __ATOMIC_ACQ_REL);
674
675 return 1;
676}
677
678void *ossl_rcu_uptr_deref(void **p)
679{
680 return ATOMIC_LOAD_N(pvoid, p, __ATOMIC_ACQUIRE);
681}
682
683void ossl_rcu_assign_uptr(void **p, void **v)
684{
685 ATOMIC_STORE(pvoid, p, v, __ATOMIC_RELEASE);
686}
687
688CRYPTO_RCU_LOCK *ossl_rcu_lock_new(int num_writers, OSSL_LIB_CTX *ctx)
689{
690 struct rcu_lock_st *new;
691
692 /*
693 * We need a minimum of 3 qp's
694 */
695 if (num_writers < 3)
696 num_writers = 3;
697
698 ctx = ossl_lib_ctx_get_concrete(ctx);
699 if (ctx == NULL)
700 return 0;
701
702 new = OPENSSL_zalloc(sizeof(*new));
703 if (new == NULL)
704 return NULL;
705
706 new->ctx = ctx;
707 pthread_mutex_init(&new->write_lock, NULL);
708 pthread_mutex_init(&new->prior_lock, NULL);
709 pthread_mutex_init(&new->alloc_lock, NULL);
710 pthread_cond_init(&new->prior_signal, NULL);
711 pthread_cond_init(&new->alloc_signal, NULL);
712 /* By default our first writer is already alloced */
713 new->writers_alloced = 1;
714
715 new->qp_group = allocate_new_qp_group(new, num_writers);
716 if (new->qp_group == NULL) {
717 OPENSSL_free(new);
718 new = NULL;
719 }
720
721 return new;
722}
723
724void ossl_rcu_lock_free(CRYPTO_RCU_LOCK *lock)
725{
726 struct rcu_lock_st *rlock = (struct rcu_lock_st *)lock;
727
728 if (lock == NULL)
729 return;
730
731 /* make sure we're synchronized */
732 ossl_synchronize_rcu(rlock);
733
734 OPENSSL_free(rlock->qp_group);
735 /* There should only be a single qp left now */
736 OPENSSL_free(rlock);
737}
738
739CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
740{
741# ifdef USE_RWLOCK
742 CRYPTO_RWLOCK *lock;
743
744 if ((lock = OPENSSL_zalloc(sizeof(pthread_rwlock_t))) == NULL)
745 /* Don't set error, to avoid recursion blowup. */
746 return NULL;
747
748 if (pthread_rwlock_init(lock, NULL) != 0) {
749 OPENSSL_free(lock);
750 return NULL;
751 }
752# else
753 pthread_mutexattr_t attr;
754 CRYPTO_RWLOCK *lock;
755
756 if ((lock = OPENSSL_zalloc(sizeof(pthread_mutex_t))) == NULL)
757 /* Don't set error, to avoid recursion blowup. */
758 return NULL;
759
760 /*
761 * We don't use recursive mutexes, but try to catch errors if we do.
762 */
763 pthread_mutexattr_init(&attr);
764# if !defined (__TANDEM) && !defined (_SPT_MODEL_)
765# if !defined(NDEBUG) && !defined(OPENSSL_NO_MUTEX_ERRORCHECK)
766 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
767# endif
768# else
769 /* The SPT Thread Library does not define MUTEX attributes. */
770# endif
771
772 if (pthread_mutex_init(lock, &attr) != 0) {
773 pthread_mutexattr_destroy(&attr);
774 OPENSSL_free(lock);
775 return NULL;
776 }
777
778 pthread_mutexattr_destroy(&attr);
779# endif
780
781 return lock;
782}
783
784__owur int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
785{
786# ifdef USE_RWLOCK
787 if (pthread_rwlock_rdlock(lock) != 0)
788 return 0;
789# else
790 if (pthread_mutex_lock(lock) != 0) {
791 assert(errno != EDEADLK && errno != EBUSY);
792 return 0;
793 }
794# endif
795
796 return 1;
797}
798
799__owur int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
800{
801# ifdef USE_RWLOCK
802 if (pthread_rwlock_wrlock(lock) != 0)
803 return 0;
804# else
805 if (pthread_mutex_lock(lock) != 0) {
806 assert(errno != EDEADLK && errno != EBUSY);
807 return 0;
808 }
809# endif
810
811 return 1;
812}
813
814int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
815{
816# ifdef USE_RWLOCK
817 if (pthread_rwlock_unlock(lock) != 0)
818 return 0;
819# else
820 if (pthread_mutex_unlock(lock) != 0) {
821 assert(errno != EPERM);
822 return 0;
823 }
824# endif
825
826 return 1;
827}
828
829void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock)
830{
831 if (lock == NULL)
832 return;
833
834# ifdef USE_RWLOCK
835 pthread_rwlock_destroy(lock);
836# else
837 pthread_mutex_destroy(lock);
838# endif
839 OPENSSL_free(lock);
840
841 return;
842}
843
844int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))
845{
846 if (pthread_once(once, init) != 0)
847 return 0;
848
849 return 1;
850}
851
852int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
853{
854 if (pthread_key_create(key, cleanup) != 0)
855 return 0;
856
857 return 1;
858}
859
860void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
861{
862 return pthread_getspecific(*key);
863}
864
865int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
866{
867 if (pthread_setspecific(*key, val) != 0)
868 return 0;
869
870 return 1;
871}
872
873int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
874{
875 if (pthread_key_delete(*key) != 0)
876 return 0;
877
878 return 1;
879}
880
881CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
882{
883 return pthread_self();
884}
885
886int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
887{
888 return pthread_equal(a, b);
889}
890
891int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
892{
893# if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
894 if (__atomic_is_lock_free(sizeof(*val), val)) {
895 *ret = __atomic_add_fetch(val, amount, __ATOMIC_ACQ_REL);
896 return 1;
897 }
898# elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
899 /* This will work for all future Solaris versions. */
900 if (ret != NULL) {
901 *ret = atomic_add_int_nv((volatile unsigned int *)val, amount);
902 return 1;
903 }
904# endif
905 if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
906 return 0;
907
908 *val += amount;
909 *ret = *val;
910
911 if (!CRYPTO_THREAD_unlock(lock))
912 return 0;
913
914 return 1;
915}
916
917int CRYPTO_atomic_add64(uint64_t *val, uint64_t op, uint64_t *ret,
918 CRYPTO_RWLOCK *lock)
919{
920# if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
921 if (__atomic_is_lock_free(sizeof(*val), val)) {
922 *ret = __atomic_add_fetch(val, op, __ATOMIC_ACQ_REL);
923 return 1;
924 }
925# elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
926 /* This will work for all future Solaris versions. */
927 if (ret != NULL) {
928 *ret = atomic_add_64_nv(val, op);
929 return 1;
930 }
931# endif
932 if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
933 return 0;
934 *val += op;
935 *ret = *val;
936
937 if (!CRYPTO_THREAD_unlock(lock))
938 return 0;
939
940 return 1;
941}
942
943int CRYPTO_atomic_and(uint64_t *val, uint64_t op, uint64_t *ret,
944 CRYPTO_RWLOCK *lock)
945{
946# if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
947 if (__atomic_is_lock_free(sizeof(*val), val)) {
948 *ret = __atomic_and_fetch(val, op, __ATOMIC_ACQ_REL);
949 return 1;
950 }
951# elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
952 /* This will work for all future Solaris versions. */
953 if (ret != NULL) {
954 *ret = atomic_and_64_nv(val, op);
955 return 1;
956 }
957# endif
958 if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
959 return 0;
960 *val &= op;
961 *ret = *val;
962
963 if (!CRYPTO_THREAD_unlock(lock))
964 return 0;
965
966 return 1;
967}
968
969int CRYPTO_atomic_or(uint64_t *val, uint64_t op, uint64_t *ret,
970 CRYPTO_RWLOCK *lock)
971{
972# if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
973 if (__atomic_is_lock_free(sizeof(*val), val)) {
974 *ret = __atomic_or_fetch(val, op, __ATOMIC_ACQ_REL);
975 return 1;
976 }
977# elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
978 /* This will work for all future Solaris versions. */
979 if (ret != NULL) {
980 *ret = atomic_or_64_nv(val, op);
981 return 1;
982 }
983# endif
984 if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
985 return 0;
986 *val |= op;
987 *ret = *val;
988
989 if (!CRYPTO_THREAD_unlock(lock))
990 return 0;
991
992 return 1;
993}
994
995int CRYPTO_atomic_load(uint64_t *val, uint64_t *ret, CRYPTO_RWLOCK *lock)
996{
997# if defined(__GNUC__) && defined(__ATOMIC_ACQUIRE) && !defined(BROKEN_CLANG_ATOMICS)
998 if (__atomic_is_lock_free(sizeof(*val), val)) {
999 __atomic_load(val, ret, __ATOMIC_ACQUIRE);
1000 return 1;
1001 }
1002# elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
1003 /* This will work for all future Solaris versions. */
1004 if (ret != NULL) {
1005 *ret = atomic_or_64_nv(val, 0);
1006 return 1;
1007 }
1008# endif
1009 if (lock == NULL || !CRYPTO_THREAD_read_lock(lock))
1010 return 0;
1011 *ret = *val;
1012 if (!CRYPTO_THREAD_unlock(lock))
1013 return 0;
1014
1015 return 1;
1016}
1017
1018int CRYPTO_atomic_store(uint64_t *dst, uint64_t val, CRYPTO_RWLOCK *lock)
1019{
1020# if defined(__GNUC__) && defined(__ATOMIC_ACQUIRE) && !defined(BROKEN_CLANG_ATOMICS)
1021 if (__atomic_is_lock_free(sizeof(*dst), dst)) {
1022 __atomic_store(dst, &val, __ATOMIC_RELEASE);
1023 return 1;
1024 }
1025# elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
1026 /* This will work for all future Solaris versions. */
1027 if (dst != NULL) {
1028 atomic_swap_64(dst, val);
1029 return 1;
1030 }
1031# endif
1032 if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
1033 return 0;
1034 *dst = val;
1035 if (!CRYPTO_THREAD_unlock(lock))
1036 return 0;
1037
1038 return 1;
1039}
1040
1041int CRYPTO_atomic_load_int(int *val, int *ret, CRYPTO_RWLOCK *lock)
1042{
1043# if defined(__GNUC__) && defined(__ATOMIC_ACQUIRE) && !defined(BROKEN_CLANG_ATOMICS)
1044 if (__atomic_is_lock_free(sizeof(*val), val)) {
1045 __atomic_load(val, ret, __ATOMIC_ACQUIRE);
1046 return 1;
1047 }
1048# elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
1049 /* This will work for all future Solaris versions. */
1050 if (ret != NULL) {
1051 *ret = (int)atomic_or_uint_nv((unsigned int *)val, 0);
1052 return 1;
1053 }
1054# endif
1055 if (lock == NULL || !CRYPTO_THREAD_read_lock(lock))
1056 return 0;
1057 *ret = *val;
1058 if (!CRYPTO_THREAD_unlock(lock))
1059 return 0;
1060
1061 return 1;
1062}
1063
1064# ifndef FIPS_MODULE
1065int openssl_init_fork_handlers(void)
1066{
1067 return 1;
1068}
1069# endif /* FIPS_MODULE */
1070
1071int openssl_get_fork_id(void)
1072{
1073 return getpid();
1074}
1075#endif
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette