Coverage Report

Created: 2026-07-12 07:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/crypto/threads_pthread.c
Line
Count
Source
1
/*
2
 * Copyright 2016-2026 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)          \
28
    __tsan_mutex_pre_unlock((x), 0); \
29
    __tsan_mutex_post_unlock((x), 0)
30
31
#define TSAN_FAKE_LOCK(x)          \
32
    __tsan_mutex_pre_lock((x), 0); \
33
    __tsan_mutex_post_lock((x), 0, 0)
34
#else
35
#define TSAN_FAKE_UNLOCK(x)
36
#define TSAN_FAKE_LOCK(x)
37
#endif
38
39
#if defined(__sun)
40
#include <atomic.h>
41
#endif
42
43
#if defined(__apple_build_version__) && __apple_build_version__ < 6000000
44
/*
45
 * OS/X 10.7 and 10.8 had a weird version of clang which has __ATOMIC_ACQUIRE and
46
 * __ATOMIC_ACQ_REL but which expects only one parameter for __atomic_is_lock_free()
47
 * rather than two which has signature __atomic_is_lock_free(sizeof(_Atomic(T))).
48
 * All of this makes impossible to use __atomic_is_lock_free here.
49
 *
50
 * See: https://github.com/llvm/llvm-project/commit/a4c2602b714e6c6edb98164550a5ae829b2de760
51
 */
52
#define BROKEN_CLANG_ATOMICS
53
#endif
54
55
#if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG) && !defined(OPENSSL_SYS_WINDOWS)
56
57
#if defined(OPENSSL_SYS_UNIX)
58
#include <sys/types.h>
59
#include <unistd.h>
60
#endif
61
62
#include <assert.h>
63
64
/*
65
 * The Non-Stop KLT thread model currently seems broken in its rwlock
66
 * implementation
67
 * Likewise is there a problem with the glibc implementation on riscv.
68
 */
69
#if defined(PTHREAD_RWLOCK_INITIALIZER) && !defined(_KLT_MODEL_) \
70
    && !defined(__riscv)
71
#define USE_RWLOCK
72
#endif
73
74
/*
75
 * For all GNU/clang atomic builtins, we also need fallbacks, to cover all
76
 * other compilers.
77
78
 * Unfortunately, we can't do that with some "generic type", because there's no
79
 * guarantee that the chosen generic type is large enough to cover all cases.
80
 * Therefore, we implement fallbacks for each applicable type, with composed
81
 * names that include the type they handle.
82
 *
83
 * (an anecdote: we previously tried to use |void *| as the generic type, with
84
 * the thought that the pointer itself is the largest type.  However, this is
85
 * not true on 32-bit pointer platforms, as a |uint64_t| is twice as large)
86
 *
87
 * All applicable ATOMIC_ macros take the intended type as first parameter, so
88
 * they can map to the correct fallback function.  In the GNU/clang case, that
89
 * parameter is simply ignored.
90
 */
91
92
/*
93
 * Internal types used with the ATOMIC_ macros, to make it possible to compose
94
 * fallback function names.
95
 */
96
typedef void *pvoid;
97
98
#if defined(__GNUC__) && defined(__ATOMIC_ACQUIRE) && !defined(BROKEN_CLANG_ATOMICS) \
99
    && !defined(USE_ATOMIC_FALLBACKS)
100
77.1M
#define ATOMIC_LOAD_N(t, p, o) __atomic_load_n(p, o)
101
920
#define ATOMIC_STORE_N(t, p, v, o) __atomic_store_n(p, v, o)
102
39.6k
#define ATOMIC_STORE(t, p, v, o) __atomic_store(p, v, o)
103
978
#define ATOMIC_ADD_FETCH(p, v, o) __atomic_add_fetch(p, v, o)
104
58
#define ATOMIC_SUB_FETCH(p, v, o) __atomic_sub_fetch(p, v, o)
105
#else
106
static pthread_mutex_t atomic_sim_lock = PTHREAD_MUTEX_INITIALIZER;
107
108
#define IMPL_fallback_atomic_load_n(t)                    \
109
    static ossl_inline t fallback_atomic_load_n_##t(t *p) \
110
    {                                                     \
111
        t ret;                                            \
112
                                                          \
113
        pthread_mutex_lock(&atomic_sim_lock);             \
114
        ret = *p;                                         \
115
        pthread_mutex_unlock(&atomic_sim_lock);           \
116
        return ret;                                       \
117
    }
118
IMPL_fallback_atomic_load_n(uint32_t)
119
    IMPL_fallback_atomic_load_n(uint64_t)
120
        IMPL_fallback_atomic_load_n(pvoid)
121
122
#define ATOMIC_LOAD_N(t, p, o) fallback_atomic_load_n_##t(p)
123
124
#define IMPL_fallback_atomic_store_n(t)                         \
125
    static ossl_inline t fallback_atomic_store_n_##t(t *p, t v) \
126
    {                                                           \
127
        t ret;                                                  \
128
                                                                \
129
        pthread_mutex_lock(&atomic_sim_lock);                   \
130
        ret = *p;                                               \
131
        *p = v;                                                 \
132
        pthread_mutex_unlock(&atomic_sim_lock);                 \
133
        return ret;                                             \
134
    }
135
            IMPL_fallback_atomic_store_n(uint32_t)
136
137
#define ATOMIC_STORE_N(t, p, v, o) fallback_atomic_store_n_##t(p, v)
138
139
#define IMPL_fallback_atomic_store(t)                             \
140
    static ossl_inline void fallback_atomic_store_##t(t *p, t *v) \
141
    {                                                             \
142
        pthread_mutex_lock(&atomic_sim_lock);                     \
143
        *p = *v;                                                  \
144
        pthread_mutex_unlock(&atomic_sim_lock);                   \
145
    }
146
                IMPL_fallback_atomic_store(pvoid)
147
148
#define ATOMIC_STORE(t, p, v, o) fallback_atomic_store_##t(p, v)
149
150
    /*
151
     * The fallbacks that follow don't need any per type implementation, as
152
     * they are designed for uint64_t only.  If there comes a time when multiple
153
     * types need to be covered, it's relatively easy to refactor them the same
154
     * way as the fallbacks above.
155
     */
156
157
    static ossl_inline uint64_t fallback_atomic_add_fetch(uint64_t *p, uint64_t v)
158
{
159
    uint64_t ret;
160
161
    pthread_mutex_lock(&atomic_sim_lock);
162
    *p += v;
163
    ret = *p;
164
    pthread_mutex_unlock(&atomic_sim_lock);
165
    return ret;
166
}
167
168
#define ATOMIC_ADD_FETCH(p, v, o) fallback_atomic_add_fetch(p, v)
169
170
static ossl_inline uint64_t fallback_atomic_sub_fetch(uint64_t *p, uint64_t v)
171
{
172
    uint64_t ret;
173
174
    pthread_mutex_lock(&atomic_sim_lock);
175
    *p -= v;
176
    ret = *p;
177
    pthread_mutex_unlock(&atomic_sim_lock);
178
    return ret;
179
}
180
181
#define ATOMIC_SUB_FETCH(p, v, o) fallback_atomic_sub_fetch(p, v)
182
#endif
183
184
/*
185
 * This is the core of an rcu lock. It tracks the readers and writers for the
186
 * current quiescence point for a given lock. Users is the 64 bit value that
187
 * stores the READERS/ID as defined above
188
 *
189
 */
190
struct rcu_qp {
191
    uint64_t users;
192
};
193
194
struct thread_qp {
195
    struct rcu_qp *qp;
196
    unsigned int depth;
197
    CRYPTO_RCU_LOCK *lock;
198
};
199
200
377
#define MAX_QPS 10
201
/*
202
 * This is the per thread tracking data
203
 * that is assigned to each thread participating
204
 * in an rcu qp
205
 *
206
 * qp points to the qp that it last acquired
207
 *
208
 */
209
struct rcu_thr_data {
210
    struct thread_qp thread_qps[MAX_QPS];
211
};
212
213
/*
214
 * This is the internal version of a CRYPTO_RCU_LOCK
215
 * it is cast from CRYPTO_RCU_LOCK
216
 */
217
struct rcu_lock_st {
218
    /* Callbacks to call for next ossl_synchronize_rcu */
219
    struct rcu_cb_item *cb_items;
220
221
    /* The context we are being created against */
222
    OSSL_LIB_CTX *ctx;
223
224
    /* Array of quiescent points for synchronization */
225
    struct rcu_qp *qp_group;
226
227
    /* rcu generation counter for in-order retirement */
228
    uint32_t id_ctr;
229
230
    /* Number of elements in qp_group array */
231
    uint32_t group_count;
232
233
    /* Index of the current qp in the qp_group array */
234
    uint32_t reader_idx;
235
236
    /* value of the next id_ctr value to be retired */
237
    uint32_t next_to_retire;
238
239
    /* index of the next free rcu_qp in the qp_group */
240
    uint32_t current_alloc_idx;
241
242
    /* number of qp's in qp_group array currently being retired */
243
    uint32_t writers_alloced;
244
245
    /* lock protecting write side operations */
246
    pthread_mutex_t write_lock;
247
248
    /* lock protecting updates to writers_alloced/current_alloc_idx */
249
    pthread_mutex_t alloc_lock;
250
251
    /* signal to wake threads waiting on alloc_lock */
252
    pthread_cond_t alloc_signal;
253
254
    /* lock to enforce in-order retirement */
255
    pthread_mutex_t prior_lock;
256
257
    /* signal to wake threads waiting on prior_lock */
258
    pthread_cond_t prior_signal;
259
};
260
261
/* Read side acquisition of the current qp */
262
static struct rcu_qp *get_hold_current_qp(struct rcu_lock_st *lock)
263
58
{
264
58
    uint32_t qp_idx;
265
266
    /* get the current qp index */
267
58
    for (;;) {
268
58
        qp_idx = ATOMIC_LOAD_N(uint32_t, &lock->reader_idx, __ATOMIC_RELAXED);
269
270
        /*
271
         * Notes on use of __ATOMIC_ACQUIRE
272
         * We need to ensure the following:
273
         * 1) That subsequent operations aren't optimized by hoisting them above
274
         * this operation.  Specifically, we don't want the below re-load of
275
         * qp_idx to get optimized away
276
         * 2) We want to ensure that any updating of reader_idx on the write side
277
         * of the lock is flushed from a local cpu cache so that we see any
278
         * updates prior to the load.  This is a non-issue on cache coherent
279
         * systems like x86, but is relevant on other arches
280
         */
281
58
        ATOMIC_ADD_FETCH(&lock->qp_group[qp_idx].users, (uint64_t)1,
282
58
            __ATOMIC_ACQUIRE);
283
284
        /* if the idx hasn't changed, we're good, else try again */
285
58
        if (qp_idx == ATOMIC_LOAD_N(uint32_t, &lock->reader_idx, __ATOMIC_ACQUIRE))
286
58
            break;
287
288
0
        ATOMIC_SUB_FETCH(&lock->qp_group[qp_idx].users, (uint64_t)1,
289
0
            __ATOMIC_RELAXED);
290
0
    }
291
292
58
    return &lock->qp_group[qp_idx];
293
58
}
294
295
static void ossl_rcu_free_local_data(void *arg)
296
3
{
297
3
    OSSL_LIB_CTX *ctx = arg;
298
3
    CRYPTO_THREAD_LOCAL *lkey = ossl_lib_ctx_get_rcukey(ctx);
299
3
    struct rcu_thr_data *data = CRYPTO_THREAD_get_local(lkey);
300
301
3
    OPENSSL_free(data);
302
3
    CRYPTO_THREAD_set_local(lkey, NULL);
303
3
}
304
305
void ossl_rcu_read_lock(CRYPTO_RCU_LOCK *lock)
306
29
{
307
29
    struct rcu_thr_data *data;
308
29
    int i, available_qp = -1;
309
29
    CRYPTO_THREAD_LOCAL *lkey = ossl_lib_ctx_get_rcukey(lock->ctx);
310
311
    /*
312
     * we're going to access current_qp here so ask the
313
     * processor to fetch it
314
     */
315
29
    data = CRYPTO_THREAD_get_local(lkey);
316
317
29
    if (data == NULL) {
318
2
        data = OPENSSL_zalloc(sizeof(*data));
319
2
        OPENSSL_assert(data != NULL);
320
2
        CRYPTO_THREAD_set_local(lkey, data);
321
2
        ossl_init_thread_start(NULL, lock->ctx, ossl_rcu_free_local_data);
322
2
    }
323
324
319
    for (i = 0; i < MAX_QPS; i++) {
325
290
        if (data->thread_qps[i].qp == NULL && available_qp == -1)
326
29
            available_qp = i;
327
        /* If we have a hold on this lock already, we're good */
328
290
        if (data->thread_qps[i].lock == lock) {
329
0
            data->thread_qps[i].depth++;
330
0
            return;
331
0
        }
332
290
    }
333
334
    /*
335
     * if we get here, then we don't have a hold on this lock yet
336
     */
337
29
    assert(available_qp != -1);
338
339
29
    data->thread_qps[available_qp].qp = get_hold_current_qp(lock);
340
29
    data->thread_qps[available_qp].depth = 1;
341
29
    data->thread_qps[available_qp].lock = lock;
342
29
}
343
344
void ossl_rcu_read_unlock(CRYPTO_RCU_LOCK *lock)
345
58
{
346
58
    int i;
347
58
    CRYPTO_THREAD_LOCAL *lkey = ossl_lib_ctx_get_rcukey(lock->ctx);
348
58
    struct rcu_thr_data *data = CRYPTO_THREAD_get_local(lkey);
349
58
    uint64_t ret;
350
351
58
    assert(data != NULL);
352
353
58
    for (i = 0; i < MAX_QPS; i++) {
354
58
        if (data->thread_qps[i].lock == lock) {
355
            /*
356
             * we have to use __ATOMIC_RELEASE here
357
             * to ensure that all preceding read instructions complete
358
             * before the decrement is visible to ossl_synchronize_rcu
359
             */
360
58
            data->thread_qps[i].depth--;
361
58
            if (data->thread_qps[i].depth == 0) {
362
58
                ret = ATOMIC_SUB_FETCH(&data->thread_qps[i].qp->users,
363
58
                    (uint64_t)1, __ATOMIC_RELEASE);
364
58
                OPENSSL_assert(ret != UINT64_MAX);
365
58
                data->thread_qps[i].qp = NULL;
366
58
                data->thread_qps[i].lock = NULL;
367
58
            }
368
58
            return;
369
58
        }
370
58
    }
371
    /*
372
     * If we get here, we're trying to unlock a lock that we never acquired -
373
     * that's fatal.
374
     */
375
58
    assert(0);
376
0
}
377
378
/*
379
 * Write side allocation routine to get the current qp
380
 * and replace it with a new one
381
 */
382
static struct rcu_qp *update_qp(CRYPTO_RCU_LOCK *lock, uint32_t *curr_id)
383
920
{
384
920
    uint32_t current_idx;
385
386
920
    pthread_mutex_lock(&lock->alloc_lock);
387
388
    /*
389
     * we need at least one qp to be available with one
390
     * left over, so that readers can start working on
391
     * one that isn't yet being waited on
392
     */
393
920
    while (lock->group_count - lock->writers_alloced < 2)
394
        /* we have to wait for one to be free */
395
0
        pthread_cond_wait(&lock->alloc_signal, &lock->alloc_lock);
396
397
920
    current_idx = lock->current_alloc_idx;
398
399
    /* Allocate the qp */
400
920
    lock->writers_alloced++;
401
402
    /* increment the allocation index */
403
920
    lock->current_alloc_idx = (lock->current_alloc_idx + 1) % lock->group_count;
404
405
920
    *curr_id = lock->id_ctr;
406
920
    lock->id_ctr++;
407
408
    /*
409
     * make the current state of everything visible by this release
410
     * when get_hold_current_qp acquires the next qp
411
     */
412
920
    ATOMIC_STORE_N(uint32_t, &lock->reader_idx, lock->current_alloc_idx,
413
920
        __ATOMIC_RELEASE);
414
415
    /*
416
     * this should make sure that the new value of reader_idx is visible in
417
     * get_hold_current_qp, directly after incrementing the users count
418
     */
419
920
    ATOMIC_ADD_FETCH(&lock->qp_group[current_idx].users, (uint64_t)0,
420
920
        __ATOMIC_RELEASE);
421
422
    /* wake up any waiters */
423
920
    pthread_cond_signal(&lock->alloc_signal);
424
920
    pthread_mutex_unlock(&lock->alloc_lock);
425
920
    return &lock->qp_group[current_idx];
426
920
}
427
428
static void retire_qp(CRYPTO_RCU_LOCK *lock, struct rcu_qp *qp)
429
920
{
430
920
    pthread_mutex_lock(&lock->alloc_lock);
431
920
    lock->writers_alloced--;
432
920
    pthread_cond_signal(&lock->alloc_signal);
433
920
    pthread_mutex_unlock(&lock->alloc_lock);
434
920
}
435
436
static struct rcu_qp *allocate_new_qp_group(CRYPTO_RCU_LOCK *lock,
437
    uint32_t count)
438
516
{
439
516
    struct rcu_qp *new = OPENSSL_zalloc(sizeof(*new) * count);
440
441
516
    lock->group_count = count;
442
516
    return new;
443
516
}
444
445
void ossl_rcu_write_lock(CRYPTO_RCU_LOCK *lock)
446
679
{
447
679
    pthread_mutex_lock(&lock->write_lock);
448
679
    TSAN_FAKE_UNLOCK(&lock->write_lock);
449
679
}
450
451
void ossl_rcu_write_unlock(CRYPTO_RCU_LOCK *lock)
452
679
{
453
679
    TSAN_FAKE_LOCK(&lock->write_lock);
454
679
    pthread_mutex_unlock(&lock->write_lock);
455
679
}
456
457
void ossl_synchronize_rcu(CRYPTO_RCU_LOCK *lock)
458
920
{
459
920
    struct rcu_qp *qp;
460
920
    uint64_t count;
461
920
    uint32_t curr_id;
462
920
    struct rcu_cb_item *cb_items, *tmpcb;
463
464
920
    pthread_mutex_lock(&lock->write_lock);
465
920
    cb_items = lock->cb_items;
466
920
    lock->cb_items = NULL;
467
920
    pthread_mutex_unlock(&lock->write_lock);
468
469
920
    qp = update_qp(lock, &curr_id);
470
471
    /* retire in order */
472
920
    pthread_mutex_lock(&lock->prior_lock);
473
920
    while (lock->next_to_retire != curr_id)
474
0
        pthread_cond_wait(&lock->prior_signal, &lock->prior_lock);
475
476
    /*
477
     * wait for the reader count to reach zero
478
     * Note the use of __ATOMIC_ACQUIRE here to ensure that any
479
     * prior __ATOMIC_RELEASE write operation in ossl_rcu_read_unlock
480
     * is visible prior to our read
481
     * however this is likely just necessary to silence a tsan warning
482
     * because the read side should not do any write operation
483
     * outside the atomic itself
484
     */
485
920
    do {
486
920
        count = ATOMIC_LOAD_N(uint64_t, &qp->users, __ATOMIC_ACQUIRE);
487
920
    } while (count != (uint64_t)0);
488
489
920
    lock->next_to_retire++;
490
920
    pthread_cond_broadcast(&lock->prior_signal);
491
920
    pthread_mutex_unlock(&lock->prior_lock);
492
493
920
    retire_qp(lock, qp);
494
495
    /* handle any callbacks that we have */
496
1.10k
    while (cb_items != NULL) {
497
187
        tmpcb = cb_items;
498
187
        cb_items = cb_items->next;
499
187
        tmpcb->fn(tmpcb->data);
500
187
        OPENSSL_free(tmpcb);
501
187
    }
502
920
}
503
504
CRYPTO_RCU_CB_ITEM *ossl_rcu_cb_item_new(void)
505
187
{
506
187
    return OPENSSL_zalloc(sizeof(CRYPTO_RCU_CB_ITEM));
507
187
}
508
509
void ossl_rcu_cb_item_free(CRYPTO_RCU_CB_ITEM *item)
510
0
{
511
0
    OPENSSL_free(item);
512
0
}
513
514
/*
515
 * Note: This call assumes its made under the protection of
516
 * ossl_rcu_write_lock
517
 */
518
void ossl_rcu_call(CRYPTO_RCU_LOCK *lock, CRYPTO_RCU_CB_ITEM *item,
519
    rcu_cb_fn cb, void *data)
520
187
{
521
187
    item->fn = cb;
522
187
    item->data = data;
523
187
    item->next = lock->cb_items;
524
187
    lock->cb_items = item;
525
187
}
526
527
void *ossl_rcu_uptr_deref(void **p)
528
77.1M
{
529
77.1M
    return ATOMIC_LOAD_N(pvoid, p, __ATOMIC_ACQUIRE);
530
77.1M
}
531
532
void ossl_rcu_assign_uptr(void **p, void **v)
533
39.6k
{
534
39.6k
    ATOMIC_STORE(pvoid, p, v, __ATOMIC_RELEASE);
535
39.6k
}
536
537
CRYPTO_RCU_LOCK *ossl_rcu_lock_new(int num_writers, OSSL_LIB_CTX *ctx)
538
516
{
539
516
    struct rcu_lock_st *new;
540
516
    pthread_mutex_t *mutexes[3] = { NULL };
541
516
    pthread_cond_t *conds[2] = { NULL };
542
516
    int i;
543
544
    /*
545
     * We need a minimum of 2 qp's
546
     */
547
516
    if (num_writers < 2)
548
516
        num_writers = 2;
549
550
516
    ctx = ossl_lib_ctx_get_concrete(ctx);
551
516
    if (ctx == NULL)
552
0
        return 0;
553
554
516
    new = OPENSSL_zalloc(sizeof(*new));
555
516
    if (new == NULL)
556
0
        return NULL;
557
558
516
    new->ctx = ctx;
559
516
    i = 0;
560
516
    mutexes[i] = pthread_mutex_init(&new->write_lock, NULL) == 0 ? &new->write_lock : NULL;
561
516
    if (mutexes[i++] == NULL)
562
0
        goto err;
563
516
    mutexes[i] = pthread_mutex_init(&new->prior_lock, NULL) == 0 ? &new->prior_lock : NULL;
564
516
    if (mutexes[i++] == NULL)
565
0
        goto err;
566
516
    mutexes[i] = pthread_mutex_init(&new->alloc_lock, NULL) == 0 ? &new->alloc_lock : NULL;
567
516
    if (mutexes[i++] == NULL)
568
0
        goto err;
569
516
    conds[i - 3] = pthread_cond_init(&new->prior_signal, NULL) == 0 ? &new->prior_signal : NULL;
570
516
    if (conds[i - 3] == NULL)
571
0
        goto err;
572
516
    i++;
573
516
    conds[i - 3] = pthread_cond_init(&new->alloc_signal, NULL) == 0 ? &new->alloc_signal : NULL;
574
516
    if (conds[i - 3] == NULL)
575
0
        goto err;
576
516
    i++;
577
516
    new->qp_group = allocate_new_qp_group(new, num_writers);
578
516
    if (new->qp_group == NULL)
579
0
        goto err;
580
581
516
    return new;
582
583
0
err:
584
0
    for (i = 0; i < 3; i++)
585
0
        if (mutexes[i] != NULL)
586
0
            pthread_mutex_destroy(mutexes[i]);
587
0
    for (i = 0; i < 2; i++)
588
0
        if (conds[i] != NULL)
589
0
            pthread_cond_destroy(conds[i]);
590
0
    OPENSSL_free(new->qp_group);
591
0
    OPENSSL_free(new);
592
0
    return NULL;
593
516
}
594
595
void ossl_rcu_lock_free(CRYPTO_RCU_LOCK *lock)
596
350
{
597
350
    struct rcu_lock_st *rlock = (struct rcu_lock_st *)lock;
598
599
350
    if (lock == NULL)
600
0
        return;
601
602
    /* make sure we're synchronized */
603
350
    ossl_synchronize_rcu(rlock);
604
605
350
    OPENSSL_free(rlock->qp_group);
606
    /*
607
     * Some targets (BSD) allocate heap when initializing
608
     * a mutex or condition, to prevent leaks, those need
609
     * to be destroyed here
610
     */
611
350
    pthread_mutex_destroy(&rlock->write_lock);
612
350
    pthread_mutex_destroy(&rlock->prior_lock);
613
350
    pthread_mutex_destroy(&rlock->alloc_lock);
614
350
    pthread_cond_destroy(&rlock->prior_signal);
615
350
    pthread_cond_destroy(&rlock->alloc_signal);
616
617
    /* There should only be a single qp left now */
618
350
    OPENSSL_free(rlock);
619
350
}
620
621
CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
622
10.3M
{
623
10.3M
#ifdef USE_RWLOCK
624
10.3M
    CRYPTO_RWLOCK *lock;
625
626
10.3M
    if ((lock = OPENSSL_zalloc(sizeof(pthread_rwlock_t))) == NULL)
627
        /* Don't set error, to avoid recursion blowup. */
628
0
        return NULL;
629
630
10.3M
    if (pthread_rwlock_init(lock, NULL) != 0) {
631
0
        OPENSSL_free(lock);
632
0
        return NULL;
633
0
    }
634
#else
635
    pthread_mutexattr_t attr;
636
    CRYPTO_RWLOCK *lock;
637
638
    if ((lock = OPENSSL_zalloc(sizeof(pthread_mutex_t))) == NULL)
639
        /* Don't set error, to avoid recursion blowup. */
640
        return NULL;
641
642
    /*
643
     * We don't use recursive mutexes, but try to catch errors if we do.
644
     */
645
    pthread_mutexattr_init(&attr);
646
#if !defined(__TANDEM) && !defined(_SPT_MODEL_)
647
#if !defined(NDEBUG) && !defined(OPENSSL_NO_MUTEX_ERRORCHECK)
648
    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
649
#endif
650
#else
651
    /* The SPT Thread Library does not define MUTEX attributes. */
652
#endif
653
654
    if (pthread_mutex_init(lock, &attr) != 0) {
655
        pthread_mutexattr_destroy(&attr);
656
        OPENSSL_free(lock);
657
        return NULL;
658
    }
659
660
    pthread_mutexattr_destroy(&attr);
661
#endif
662
663
10.3M
    return lock;
664
10.3M
}
665
666
__owur int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
667
89.0M
{
668
89.0M
#ifdef USE_RWLOCK
669
89.0M
    if (!ossl_assert(pthread_rwlock_rdlock(lock) == 0))
670
0
        return 0;
671
#else
672
    if (pthread_mutex_lock(lock) != 0) {
673
        assert(errno != EDEADLK && errno != EBUSY);
674
        return 0;
675
    }
676
#endif
677
678
89.0M
    return 1;
679
89.0M
}
680
681
__owur int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
682
51.6M
{
683
51.6M
#ifdef USE_RWLOCK
684
51.6M
    if (!ossl_assert(pthread_rwlock_wrlock(lock) == 0))
685
0
        return 0;
686
#else
687
    if (pthread_mutex_lock(lock) != 0) {
688
        assert(errno != EDEADLK && errno != EBUSY);
689
        return 0;
690
    }
691
#endif
692
693
51.6M
    return 1;
694
51.6M
}
695
696
int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
697
1.23G
{
698
1.23G
#ifdef USE_RWLOCK
699
1.23G
    if (pthread_rwlock_unlock(lock) != 0)
700
0
        return 0;
701
#else
702
    if (pthread_mutex_unlock(lock) != 0) {
703
        assert(errno != EPERM);
704
        return 0;
705
    }
706
#endif
707
708
1.23G
    return 1;
709
1.23G
}
710
711
void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock)
712
10.3M
{
713
10.3M
    if (lock == NULL)
714
2.55k
        return;
715
716
10.3M
#ifdef USE_RWLOCK
717
10.3M
    pthread_rwlock_destroy(lock);
718
#else
719
    pthread_mutex_destroy(lock);
720
#endif
721
10.3M
    OPENSSL_free(lock);
722
723
10.3M
    return;
724
10.3M
}
725
726
int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))
727
3.57G
{
728
3.57G
    if (pthread_once(once, init) != 0)
729
0
        return 0;
730
731
3.57G
    return 1;
732
3.57G
}
733
734
int ossl_thread_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
735
703
{
736
737
703
    if (pthread_key_create(key, cleanup) != 0)
738
0
        return 0;
739
740
703
    return 1;
741
703
}
742
743
void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
744
3.29G
{
745
3.29G
    return pthread_getspecific(*key);
746
3.29G
}
747
748
int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
749
1.79k
{
750
1.79k
    if (pthread_setspecific(*key, val) != 0)
751
0
        return 0;
752
753
1.79k
    return 1;
754
1.79k
}
755
756
int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
757
1.38k
{
758
1.38k
    if (pthread_key_delete(*key) != 0)
759
0
        return 0;
760
761
1.38k
    return 1;
762
1.38k
}
763
764
CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
765
201k
{
766
201k
    return pthread_self();
767
201k
}
768
769
int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
770
11.6k
{
771
11.6k
    return pthread_equal(a, b);
772
11.6k
}
773
774
int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
775
11.9M
{
776
11.9M
#if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
777
11.9M
    if (__atomic_is_lock_free(sizeof(*val), val)) {
778
11.9M
        *ret = __atomic_add_fetch(val, amount, __ATOMIC_ACQ_REL);
779
11.9M
        return 1;
780
11.9M
    }
781
#elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
782
    /* This will work for all future Solaris versions. */
783
    if (ret != NULL) {
784
        *ret = atomic_add_int_nv((volatile unsigned int *)val, amount);
785
        return 1;
786
    }
787
#endif
788
0
    if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
789
0
        return 0;
790
791
0
    *val += amount;
792
0
    *ret = *val;
793
794
0
    if (!CRYPTO_THREAD_unlock(lock))
795
0
        return 0;
796
797
0
    return 1;
798
0
}
799
800
int CRYPTO_atomic_add64(uint64_t *val, uint64_t op, uint64_t *ret,
801
    CRYPTO_RWLOCK *lock)
802
0
{
803
0
#if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
804
0
    if (__atomic_is_lock_free(sizeof(*val), val)) {
805
0
        *ret = __atomic_add_fetch(val, op, __ATOMIC_ACQ_REL);
806
0
        return 1;
807
0
    }
808
#elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
809
    /* This will work for all future Solaris versions. */
810
    if (ret != NULL) {
811
        *ret = atomic_add_64_nv(val, op);
812
        return 1;
813
    }
814
#endif
815
0
    if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
816
0
        return 0;
817
0
    *val += op;
818
0
    *ret = *val;
819
820
0
    if (!CRYPTO_THREAD_unlock(lock))
821
0
        return 0;
822
823
0
    return 1;
824
0
}
825
826
int CRYPTO_atomic_and(uint64_t *val, uint64_t op, uint64_t *ret,
827
    CRYPTO_RWLOCK *lock)
828
0
{
829
0
#if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
830
0
    if (__atomic_is_lock_free(sizeof(*val), val)) {
831
0
        *ret = __atomic_and_fetch(val, op, __ATOMIC_ACQ_REL);
832
0
        return 1;
833
0
    }
834
#elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
835
    /* This will work for all future Solaris versions. */
836
    if (ret != NULL) {
837
        *ret = atomic_and_64_nv(val, op);
838
        return 1;
839
    }
840
#endif
841
0
    if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
842
0
        return 0;
843
0
    *val &= op;
844
0
    *ret = *val;
845
846
0
    if (!CRYPTO_THREAD_unlock(lock))
847
0
        return 0;
848
849
0
    return 1;
850
0
}
851
852
int CRYPTO_atomic_or(uint64_t *val, uint64_t op, uint64_t *ret,
853
    CRYPTO_RWLOCK *lock)
854
713
{
855
713
#if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
856
713
    if (__atomic_is_lock_free(sizeof(*val), val)) {
857
713
        *ret = __atomic_or_fetch(val, op, __ATOMIC_ACQ_REL);
858
713
        return 1;
859
713
    }
860
#elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
861
    /* This will work for all future Solaris versions. */
862
    if (ret != NULL) {
863
        *ret = atomic_or_64_nv(val, op);
864
        return 1;
865
    }
866
#endif
867
0
    if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
868
0
        return 0;
869
0
    *val |= op;
870
0
    *ret = *val;
871
872
0
    if (!CRYPTO_THREAD_unlock(lock))
873
0
        return 0;
874
875
0
    return 1;
876
0
}
877
878
int CRYPTO_atomic_load(uint64_t *val, uint64_t *ret, CRYPTO_RWLOCK *lock)
879
4.02G
{
880
4.02G
#if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
881
4.02G
    if (__atomic_is_lock_free(sizeof(*val), val)) {
882
4.02G
        __atomic_load(val, ret, __ATOMIC_ACQUIRE);
883
4.02G
        return 1;
884
4.02G
    }
885
#elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
886
    /* This will work for all future Solaris versions. */
887
    if (ret != NULL) {
888
        *ret = atomic_or_64_nv(val, 0);
889
        return 1;
890
    }
891
#endif
892
0
    if (lock == NULL || !CRYPTO_THREAD_read_lock(lock))
893
0
        return 0;
894
0
    *ret = *val;
895
0
    if (!CRYPTO_THREAD_unlock(lock))
896
0
        return 0;
897
898
0
    return 1;
899
0
}
900
901
int CRYPTO_atomic_store(uint64_t *dst, uint64_t val, CRYPTO_RWLOCK *lock)
902
39.1k
{
903
39.1k
#if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
904
39.1k
    if (__atomic_is_lock_free(sizeof(*dst), dst)) {
905
39.1k
        __atomic_store(dst, &val, __ATOMIC_RELEASE);
906
39.1k
        return 1;
907
39.1k
    }
908
#elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
909
    /* This will work for all future Solaris versions. */
910
    if (dst != NULL) {
911
        atomic_swap_64(dst, val);
912
        return 1;
913
    }
914
#endif
915
0
    if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
916
0
        return 0;
917
0
    *dst = val;
918
0
    if (!CRYPTO_THREAD_unlock(lock))
919
0
        return 0;
920
921
0
    return 1;
922
0
}
923
924
int CRYPTO_atomic_load_int(int *val, int *ret, CRYPTO_RWLOCK *lock)
925
113M
{
926
113M
#if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
927
113M
    if (__atomic_is_lock_free(sizeof(*val), val)) {
928
113M
        __atomic_load(val, ret, __ATOMIC_ACQUIRE);
929
113M
        return 1;
930
113M
    }
931
#elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
932
    /* This will work for all future Solaris versions. */
933
    if (ret != NULL) {
934
        *ret = (int)atomic_or_uint_nv((unsigned int *)val, 0);
935
        return 1;
936
    }
937
#endif
938
0
    if (lock == NULL || !CRYPTO_THREAD_read_lock(lock))
939
0
        return 0;
940
0
    *ret = *val;
941
0
    if (!CRYPTO_THREAD_unlock(lock))
942
0
        return 0;
943
944
0
    return 1;
945
0
}
946
947
#ifndef FIPS_MODULE
948
int openssl_init_fork_handlers(void)
949
0
{
950
0
    return 1;
951
0
}
952
#endif /* FIPS_MODULE */
953
954
int openssl_get_fork_id(void)
955
139k
{
956
139k
    return getpid();
957
139k
}
958
#endif