Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl40/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
#if !defined(__GNUC__) || !defined(__ATOMIC_ACQ_REL) || defined(BROKEN_CLANG_ATOMICS) || defined(OPENSSL_NO_STDIO)
14
/*
15
 * we only enable REPORT_RWLOCK_CONTENTION on clang/gcc when we have
16
 * atomics available.  We do this because we need to use an atomic to track
17
 * when we can close the log file.  We could use the CRYPTO_atomic_ api
18
 * but that requires lock creation which gets us into a bad recursive loop
19
 * when we try to initialize the file pointer
20
 */
21
#ifdef REPORT_RWLOCK_CONTENTION
22
#warning "RWLOCK CONTENTION REPORTING NOT SUPPORTED, Disabling"
23
#undef REPORT_RWLOCK_CONTENTION
24
#endif
25
#endif
26
27
#ifdef REPORT_RWLOCK_CONTENTION
28
#define _GNU_SOURCE
29
#include <execinfo.h>
30
#include <unistd.h>
31
#endif
32
33
#include <openssl/crypto.h>
34
#include <crypto/cryptlib.h>
35
#include <crypto/sparse_array.h>
36
#include "internal/cryptlib.h"
37
#include "internal/threads_common.h"
38
#include "internal/rcu.h"
39
#ifdef REPORT_RWLOCK_CONTENTION
40
#include <fcntl.h>
41
#include <stdbool.h>
42
#include <sys/syscall.h>
43
#include <sys/uio.h>
44
#include "internal/time.h"
45
#endif
46
#include "rcu_internal.h"
47
48
#if defined(__SANITIZE_THREAD__)
49
#define TSAN_FAKE_UNLOCK(x)          \
50
    __tsan_mutex_pre_unlock((x), 0); \
51
    __tsan_mutex_post_unlock((x), 0)
52
53
#define TSAN_FAKE_LOCK(x)          \
54
    __tsan_mutex_pre_lock((x), 0); \
55
    __tsan_mutex_post_lock((x), 0, 0)
56
#else
57
#define TSAN_FAKE_UNLOCK(x)
58
#define TSAN_FAKE_LOCK(x)
59
#endif
60
61
#if defined(__sun)
62
#include <atomic.h>
63
#endif
64
65
#if defined(__apple_build_version__) && __apple_build_version__ < 6000000
66
/*
67
 * OS/X 10.7 and 10.8 had a weird version of clang which has __ATOMIC_ACQUIRE and
68
 * __ATOMIC_ACQ_REL but which expects only one parameter for __atomic_is_lock_free()
69
 * rather than two which has signature __atomic_is_lock_free(sizeof(_Atomic(T))).
70
 * All of this makes impossible to use __atomic_is_lock_free here.
71
 *
72
 * See: https://github.com/llvm/llvm-project/commit/a4c2602b714e6c6edb98164550a5ae829b2de760
73
 */
74
#define BROKEN_CLANG_ATOMICS
75
#endif
76
77
#if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG) && !defined(OPENSSL_SYS_WINDOWS)
78
79
#if defined(OPENSSL_SYS_UNIX)
80
#include <sys/types.h>
81
#include <unistd.h>
82
#endif
83
84
#include <assert.h>
85
86
/*
87
 * The Non-Stop KLT thread model currently seems broken in its rwlock
88
 * implementation
89
 * Likewise is there a problem with the glibc implementation on riscv.
90
 */
91
#if defined(PTHREAD_RWLOCK_INITIALIZER) && !defined(_KLT_MODEL_) && !defined(_PUT_MODEL_) \
92
    && !defined(__riscv)
93
#define USE_RWLOCK
94
#endif
95
96
/*
97
 * For all GNU/clang atomic builtins, we also need fallbacks, to cover all
98
 * other compilers.
99
100
 * Unfortunately, we can't do that with some "generic type", because there's no
101
 * guarantee that the chosen generic type is large enough to cover all cases.
102
 * Therefore, we implement fallbacks for each applicable type, with composed
103
 * names that include the type they handle.
104
 *
105
 * (an anecdote: we previously tried to use |void *| as the generic type, with
106
 * the thought that the pointer itself is the largest type.  However, this is
107
 * not true on 32-bit pointer platforms, as a |uint64_t| is twice as large)
108
 *
109
 * All applicable ATOMIC_ macros take the intended type as first parameter, so
110
 * they can map to the correct fallback function.  In the GNU/clang case, that
111
 * parameter is simply ignored.
112
 */
113
114
/*
115
 * Internal types used with the ATOMIC_ macros, to make it possible to compose
116
 * fallback function names.
117
 */
118
typedef void *pvoid;
119
120
#if defined(__GNUC__) && defined(__ATOMIC_ACQUIRE) && !defined(BROKEN_CLANG_ATOMICS) \
121
    && !defined(USE_ATOMIC_FALLBACKS)
122
86.8M
#define ATOMIC_LOAD_N(t, p, o) __atomic_load_n(p, o)
123
811
#define ATOMIC_STORE_N(t, p, v, o) __atomic_store_n(p, v, o)
124
51.6k
#define ATOMIC_STORE(t, p, v, o) __atomic_store(p, v, o)
125
915
#define ATOMIC_ADD_FETCH(p, v, o) __atomic_add_fetch(p, v, o)
126
104
#define ATOMIC_SUB_FETCH(p, v, o) __atomic_sub_fetch(p, v, o)
127
#else
128
static pthread_mutex_t atomic_sim_lock = PTHREAD_MUTEX_INITIALIZER;
129
130
#define IMPL_fallback_atomic_load_n(t)                    \
131
    static ossl_inline t fallback_atomic_load_n_##t(t *p) \
132
    {                                                     \
133
        t ret;                                            \
134
                                                          \
135
        pthread_mutex_lock(&atomic_sim_lock);             \
136
        ret = *p;                                         \
137
        pthread_mutex_unlock(&atomic_sim_lock);           \
138
        return ret;                                       \
139
    }
140
IMPL_fallback_atomic_load_n(uint32_t)
141
    IMPL_fallback_atomic_load_n(uint64_t)
142
        IMPL_fallback_atomic_load_n(pvoid)
143
144
#define ATOMIC_LOAD_N(t, p, o) fallback_atomic_load_n_##t(p)
145
146
#define IMPL_fallback_atomic_store_n(t)                         \
147
    static ossl_inline t fallback_atomic_store_n_##t(t *p, t v) \
148
    {                                                           \
149
        t ret;                                                  \
150
                                                                \
151
        pthread_mutex_lock(&atomic_sim_lock);                   \
152
        ret = *p;                                               \
153
        *p = v;                                                 \
154
        pthread_mutex_unlock(&atomic_sim_lock);                 \
155
        return ret;                                             \
156
    }
157
            IMPL_fallback_atomic_store_n(uint32_t)
158
159
#define ATOMIC_STORE_N(t, p, v, o) fallback_atomic_store_n_##t(p, v)
160
161
#define IMPL_fallback_atomic_store(t)                             \
162
    static ossl_inline void fallback_atomic_store_##t(t *p, t *v) \
163
    {                                                             \
164
        pthread_mutex_lock(&atomic_sim_lock);                     \
165
        *p = *v;                                                  \
166
        pthread_mutex_unlock(&atomic_sim_lock);                   \
167
    }
168
                IMPL_fallback_atomic_store(pvoid)
169
170
#define ATOMIC_STORE(t, p, v, o) fallback_atomic_store_##t(p, v)
171
172
    /*
173
     * The fallbacks that follow don't need any per type implementation, as
174
     * they are designed for uint64_t only.  If there comes a time when multiple
175
     * types need to be covered, it's relatively easy to refactor them the same
176
     * way as the fallbacks above.
177
     */
178
179
    static ossl_inline uint64_t fallback_atomic_add_fetch(uint64_t *p, uint64_t v)
180
{
181
    uint64_t ret;
182
183
    pthread_mutex_lock(&atomic_sim_lock);
184
    *p += v;
185
    ret = *p;
186
    pthread_mutex_unlock(&atomic_sim_lock);
187
    return ret;
188
}
189
190
#define ATOMIC_ADD_FETCH(p, v, o) fallback_atomic_add_fetch(p, v)
191
192
static ossl_inline uint64_t fallback_atomic_sub_fetch(uint64_t *p, uint64_t v)
193
{
194
    uint64_t ret;
195
196
    pthread_mutex_lock(&atomic_sim_lock);
197
    *p -= v;
198
    ret = *p;
199
    pthread_mutex_unlock(&atomic_sim_lock);
200
    return ret;
201
}
202
203
#define ATOMIC_SUB_FETCH(p, v, o) fallback_atomic_sub_fetch(p, v)
204
#endif
205
206
/*
207
 * This is the core of an rcu lock. It tracks the readers and writers for the
208
 * current quiescence point for a given lock. Users is the 64 bit value that
209
 * stores the READERS/ID as defined above
210
 *
211
 */
212
struct rcu_qp {
213
    uint64_t users;
214
};
215
216
struct thread_qp {
217
    struct rcu_qp *qp;
218
    unsigned int depth;
219
    CRYPTO_RCU_LOCK *lock;
220
};
221
222
808
#define MAX_QPS 10
223
/*
224
 * This is the per thread tracking data
225
 * that is assigned to each thread participating
226
 * in an rcu qp
227
 *
228
 * qp points to the qp that it last acquired
229
 *
230
 */
231
struct rcu_thr_data {
232
    struct thread_qp thread_qps[MAX_QPS];
233
};
234
235
/*
236
 * This is the internal version of a CRYPTO_RCU_LOCK
237
 * it is cast from CRYPTO_RCU_LOCK
238
 */
239
struct rcu_lock_st {
240
    /* Callbacks to call for next ossl_synchronize_rcu */
241
    struct rcu_cb_item *cb_items;
242
243
    /* The context we are being created against */
244
    OSSL_LIB_CTX *ctx;
245
246
    /* Array of quiescent points for synchronization */
247
    struct rcu_qp *qp_group;
248
249
    /* rcu generation counter for in-order retirement */
250
    uint32_t id_ctr;
251
252
    /* Number of elements in qp_group array */
253
    uint32_t group_count;
254
255
    /* Index of the current qp in the qp_group array */
256
    uint32_t reader_idx;
257
258
    /* value of the next id_ctr value to be retired */
259
    uint32_t next_to_retire;
260
261
    /* index of the next free rcu_qp in the qp_group */
262
    uint32_t current_alloc_idx;
263
264
    /* number of qp's in qp_group array currently being retired */
265
    uint32_t writers_alloced;
266
267
    /* lock protecting write side operations */
268
    pthread_mutex_t write_lock;
269
270
    /* lock protecting updates to writers_alloced/current_alloc_idx */
271
    pthread_mutex_t alloc_lock;
272
273
    /* signal to wake threads waiting on alloc_lock */
274
    pthread_cond_t alloc_signal;
275
276
    /* lock to enforce in-order retirement */
277
    pthread_mutex_t prior_lock;
278
279
    /* signal to wake threads waiting on prior_lock */
280
    pthread_cond_t prior_signal;
281
};
282
283
/* Read side acquisition of the current qp */
284
static struct rcu_qp *get_hold_current_qp(struct rcu_lock_st *lock)
285
104
{
286
104
    uint32_t qp_idx;
287
288
    /* get the current qp index */
289
104
    for (;;) {
290
104
        qp_idx = ATOMIC_LOAD_N(uint32_t, &lock->reader_idx, __ATOMIC_RELAXED);
291
292
        /*
293
         * Notes on use of __ATOMIC_ACQUIRE
294
         * We need to ensure the following:
295
         * 1) That subsequent operations aren't optimized by hoisting them above
296
         * this operation.  Specifically, we don't want the below re-load of
297
         * qp_idx to get optimized away
298
         * 2) We want to ensure that any updating of reader_idx on the write side
299
         * of the lock is flushed from a local cpu cache so that we see any
300
         * updates prior to the load.  This is a non-issue on cache coherent
301
         * systems like x86, but is relevant on other arches
302
         */
303
104
        ATOMIC_ADD_FETCH(&lock->qp_group[qp_idx].users, (uint64_t)1,
304
104
            __ATOMIC_ACQUIRE);
305
306
        /* if the idx hasn't changed, we're good, else try again */
307
104
        if (qp_idx == ATOMIC_LOAD_N(uint32_t, &lock->reader_idx, __ATOMIC_ACQUIRE))
308
104
            break;
309
310
0
        ATOMIC_SUB_FETCH(&lock->qp_group[qp_idx].users, (uint64_t)1,
311
0
            __ATOMIC_RELAXED);
312
0
    }
313
314
104
    return &lock->qp_group[qp_idx];
315
104
}
316
317
static void ossl_rcu_free_local_data(void *arg)
318
3
{
319
3
    OSSL_LIB_CTX *ctx = arg;
320
3
    struct rcu_thr_data *data = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_RCU_KEY, ctx);
321
322
3
    CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_RCU_KEY, ctx, NULL);
323
3
    OPENSSL_free(data);
324
3
}
325
326
int ossl_rcu_read_lock(CRYPTO_RCU_LOCK *lock)
327
64
{
328
64
    struct rcu_thr_data *data;
329
64
    int i, available_qp = -1;
330
331
    /*
332
     * we're going to access current_qp here so ask the
333
     * processor to fetch it
334
     */
335
64
    data = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_RCU_KEY, lock->ctx);
336
337
64
    if (data == NULL) {
338
3
        data = OPENSSL_zalloc(sizeof(*data));
339
3
        if (data == NULL)
340
0
            return 0;
341
342
3
        if (!CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_RCU_KEY, lock->ctx, data)) {
343
0
            OPENSSL_free(data);
344
0
            return 0;
345
0
        }
346
3
        if (!ossl_init_thread_start(NULL, lock->ctx, ossl_rcu_free_local_data)) {
347
0
            OPENSSL_free(data);
348
0
            CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_RCU_KEY, lock->ctx, NULL);
349
0
            return 0;
350
0
        }
351
3
    }
352
353
704
    for (i = 0; i < MAX_QPS; i++) {
354
640
        if (data->thread_qps[i].qp == NULL && available_qp == -1)
355
64
            available_qp = i;
356
        /* If we have a hold on this lock already, we're good */
357
640
        if (data->thread_qps[i].lock == lock) {
358
0
            data->thread_qps[i].depth++;
359
0
            return 1;
360
0
        }
361
640
    }
362
363
    /*
364
     * if we get here, then we don't have a hold on this lock yet
365
     */
366
64
    assert(available_qp != -1);
367
368
64
    data->thread_qps[available_qp].qp = get_hold_current_qp(lock);
369
64
    data->thread_qps[available_qp].depth = 1;
370
64
    data->thread_qps[available_qp].lock = lock;
371
64
    return 1;
372
64
}
373
374
void ossl_rcu_read_unlock(CRYPTO_RCU_LOCK *lock)
375
104
{
376
104
    int i;
377
104
    struct rcu_thr_data *data = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_RCU_KEY, lock->ctx);
378
104
    uint64_t ret;
379
380
104
    assert(data != NULL);
381
382
104
    for (i = 0; i < MAX_QPS; i++) {
383
104
        if (data->thread_qps[i].lock == lock) {
384
            /*
385
             * we have to use __ATOMIC_RELEASE here
386
             * to ensure that all preceding read instructions complete
387
             * before the decrement is visible to ossl_synchronize_rcu
388
             */
389
104
            data->thread_qps[i].depth--;
390
104
            if (data->thread_qps[i].depth == 0) {
391
104
                ret = ATOMIC_SUB_FETCH(&data->thread_qps[i].qp->users,
392
104
                    (uint64_t)1, __ATOMIC_RELEASE);
393
104
                OPENSSL_assert(ret != UINT64_MAX);
394
104
                data->thread_qps[i].qp = NULL;
395
104
                data->thread_qps[i].lock = NULL;
396
104
            }
397
104
            return;
398
104
        }
399
104
    }
400
    /*
401
     * If we get here, we're trying to unlock a lock that we never acquired -
402
     * that's fatal.
403
     */
404
104
    assert(0);
405
0
}
406
407
/*
408
 * Write side allocation routine to get the current qp
409
 * and replace it with a new one
410
 */
411
static struct rcu_qp *update_qp(CRYPTO_RCU_LOCK *lock, uint32_t *curr_id)
412
811
{
413
811
    uint32_t current_idx;
414
415
811
    pthread_mutex_lock(&lock->alloc_lock);
416
417
    /*
418
     * we need at least one qp to be available with one
419
     * left over, so that readers can start working on
420
     * one that isn't yet being waited on
421
     */
422
811
    while (lock->group_count - lock->writers_alloced < 2)
423
        /* we have to wait for one to be free */
424
0
        pthread_cond_wait(&lock->alloc_signal, &lock->alloc_lock);
425
426
811
    current_idx = lock->current_alloc_idx;
427
428
    /* Allocate the qp */
429
811
    lock->writers_alloced++;
430
431
    /* increment the allocation index */
432
811
    lock->current_alloc_idx = (lock->current_alloc_idx + 1) % lock->group_count;
433
434
811
    *curr_id = lock->id_ctr;
435
811
    lock->id_ctr++;
436
437
    /*
438
     * make the current state of everything visible by this release
439
     * when get_hold_current_qp acquires the next qp
440
     */
441
811
    ATOMIC_STORE_N(uint32_t, &lock->reader_idx, lock->current_alloc_idx,
442
811
        __ATOMIC_RELEASE);
443
444
    /*
445
     * this should make sure that the new value of reader_idx is visible in
446
     * get_hold_current_qp, directly after incrementing the users count
447
     */
448
811
    ATOMIC_ADD_FETCH(&lock->qp_group[current_idx].users, (uint64_t)0,
449
811
        __ATOMIC_RELEASE);
450
451
    /* wake up any waiters */
452
811
    pthread_cond_signal(&lock->alloc_signal);
453
811
    pthread_mutex_unlock(&lock->alloc_lock);
454
811
    return &lock->qp_group[current_idx];
455
811
}
456
457
static void retire_qp(CRYPTO_RCU_LOCK *lock, struct rcu_qp *qp)
458
811
{
459
811
    pthread_mutex_lock(&lock->alloc_lock);
460
811
    lock->writers_alloced--;
461
811
    pthread_cond_signal(&lock->alloc_signal);
462
811
    pthread_mutex_unlock(&lock->alloc_lock);
463
811
}
464
465
static struct rcu_qp *allocate_new_qp_group(CRYPTO_RCU_LOCK *lock,
466
    uint32_t count)
467
594
{
468
594
    struct rcu_qp *new = OPENSSL_calloc(count, sizeof(*new));
469
470
594
    lock->group_count = count;
471
594
    return new;
472
594
}
473
474
void ossl_rcu_write_lock(CRYPTO_RCU_LOCK *lock)
475
674
{
476
674
    pthread_mutex_lock(&lock->write_lock);
477
674
    TSAN_FAKE_UNLOCK(&lock->write_lock);
478
674
}
479
480
void ossl_rcu_write_unlock(CRYPTO_RCU_LOCK *lock)
481
674
{
482
674
    TSAN_FAKE_LOCK(&lock->write_lock);
483
674
    pthread_mutex_unlock(&lock->write_lock);
484
674
}
485
486
void ossl_synchronize_rcu(CRYPTO_RCU_LOCK *lock)
487
811
{
488
811
    struct rcu_qp *qp;
489
811
    uint64_t count;
490
811
    uint32_t curr_id;
491
811
    struct rcu_cb_item *cb_items, *tmpcb;
492
493
811
    pthread_mutex_lock(&lock->write_lock);
494
811
    cb_items = lock->cb_items;
495
811
    lock->cb_items = NULL;
496
811
    pthread_mutex_unlock(&lock->write_lock);
497
498
811
    qp = update_qp(lock, &curr_id);
499
500
    /* retire in order */
501
811
    pthread_mutex_lock(&lock->prior_lock);
502
811
    while (lock->next_to_retire != curr_id)
503
0
        pthread_cond_wait(&lock->prior_signal, &lock->prior_lock);
504
505
    /*
506
     * wait for the reader count to reach zero
507
     * Note the use of __ATOMIC_ACQUIRE here to ensure that any
508
     * prior __ATOMIC_RELEASE write operation in ossl_rcu_read_unlock
509
     * is visible prior to our read
510
     * however this is likely just necessary to silence a tsan warning
511
     * because the read side should not do any write operation
512
     * outside the atomic itself
513
     */
514
811
    do {
515
811
        count = ATOMIC_LOAD_N(uint64_t, &qp->users, __ATOMIC_ACQUIRE);
516
811
    } while (count != (uint64_t)0);
517
518
811
    lock->next_to_retire++;
519
811
    pthread_cond_broadcast(&lock->prior_signal);
520
811
    pthread_mutex_unlock(&lock->prior_lock);
521
522
811
    retire_qp(lock, qp);
523
524
    /* handle any callbacks that we have */
525
1.02k
    while (cb_items != NULL) {
526
216
        tmpcb = cb_items;
527
216
        cb_items = cb_items->next;
528
216
        tmpcb->fn(tmpcb->data);
529
216
        OPENSSL_free(tmpcb);
530
216
    }
531
811
}
532
533
CRYPTO_RCU_CB_ITEM *ossl_rcu_cb_item_new(void)
534
216
{
535
216
    return OPENSSL_zalloc(sizeof(CRYPTO_RCU_CB_ITEM));
536
216
}
537
538
void ossl_rcu_cb_item_free(CRYPTO_RCU_CB_ITEM *item)
539
0
{
540
0
    OPENSSL_free(item);
541
0
}
542
543
/*
544
 * Note: This call assumes its made under the protection of
545
 * ossl_rcu_write_lock
546
 */
547
void ossl_rcu_call(CRYPTO_RCU_LOCK *lock, CRYPTO_RCU_CB_ITEM *item,
548
    rcu_cb_fn cb, void *data)
549
216
{
550
216
    item->fn = cb;
551
216
    item->data = data;
552
216
    item->next = lock->cb_items;
553
216
    lock->cb_items = item;
554
216
}
555
556
void *ossl_rcu_uptr_deref(void **p)
557
86.8M
{
558
86.8M
    return ATOMIC_LOAD_N(pvoid, p, __ATOMIC_ACQUIRE);
559
86.8M
}
560
561
void ossl_rcu_assign_uptr(void **p, void **v)
562
51.6k
{
563
51.6k
    ATOMIC_STORE(pvoid, p, v, __ATOMIC_RELEASE);
564
51.6k
}
565
566
CRYPTO_RCU_LOCK *ossl_rcu_lock_new(int num_writers, OSSL_LIB_CTX *ctx)
567
594
{
568
594
    struct rcu_lock_st *new;
569
594
    pthread_mutex_t *mutexes[3] = { NULL };
570
594
    pthread_cond_t *conds[2] = { NULL };
571
594
    int i;
572
573
    /*
574
     * We need a minimum of 2 qp's
575
     */
576
594
    if (num_writers < 2)
577
594
        num_writers = 2;
578
579
594
    ctx = ossl_lib_ctx_get_concrete(ctx);
580
594
    if (ctx == NULL)
581
0
        return 0;
582
583
594
    new = OPENSSL_zalloc(sizeof(*new));
584
594
    if (new == NULL)
585
0
        return NULL;
586
587
594
    new->ctx = ctx;
588
594
    i = 0;
589
594
    mutexes[i] = pthread_mutex_init(&new->write_lock, NULL) == 0 ? &new->write_lock : NULL;
590
594
    if (mutexes[i++] == NULL)
591
0
        goto err;
592
594
    mutexes[i] = pthread_mutex_init(&new->prior_lock, NULL) == 0 ? &new->prior_lock : NULL;
593
594
    if (mutexes[i++] == NULL)
594
0
        goto err;
595
594
    mutexes[i] = pthread_mutex_init(&new->alloc_lock, NULL) == 0 ? &new->alloc_lock : NULL;
596
594
    if (mutexes[i++] == NULL)
597
0
        goto err;
598
594
    conds[i - 3] = pthread_cond_init(&new->prior_signal, NULL) == 0 ? &new->prior_signal : NULL;
599
594
    if (conds[i - 3] == NULL)
600
0
        goto err;
601
594
    i++;
602
594
    conds[i - 3] = pthread_cond_init(&new->alloc_signal, NULL) == 0 ? &new->alloc_signal : NULL;
603
594
    if (conds[i - 3] == NULL)
604
0
        goto err;
605
594
    i++;
606
594
    new->qp_group = allocate_new_qp_group(new, num_writers);
607
594
    if (new->qp_group == NULL)
608
0
        goto err;
609
610
594
    return new;
611
612
0
err:
613
0
    for (i = 0; i < 3; i++)
614
0
        if (mutexes[i] != NULL)
615
0
            pthread_mutex_destroy(mutexes[i]);
616
0
    for (i = 0; i < 2; i++)
617
0
        if (conds[i] != NULL)
618
0
            pthread_cond_destroy(conds[i]);
619
0
    OPENSSL_free(new->qp_group);
620
0
    OPENSSL_free(new);
621
0
    return NULL;
622
594
}
623
624
void ossl_rcu_lock_free(CRYPTO_RCU_LOCK *lock)
625
308
{
626
308
    struct rcu_lock_st *rlock = (struct rcu_lock_st *)lock;
627
628
308
    if (lock == NULL)
629
0
        return;
630
631
    /* make sure we're synchronized */
632
308
    ossl_synchronize_rcu(rlock);
633
634
308
    OPENSSL_free(rlock->qp_group);
635
    /*
636
     * Some targets (BSD) allocate heap when initializing
637
     * a mutex or condition, to prevent leaks, those need
638
     * to be destroyed here
639
     */
640
308
    pthread_mutex_destroy(&rlock->write_lock);
641
308
    pthread_mutex_destroy(&rlock->prior_lock);
642
308
    pthread_mutex_destroy(&rlock->alloc_lock);
643
308
    pthread_cond_destroy(&rlock->prior_signal);
644
308
    pthread_cond_destroy(&rlock->alloc_signal);
645
646
    /* There should only be a single qp left now */
647
308
    OPENSSL_free(rlock);
648
308
}
649
650
#ifdef REPORT_RWLOCK_CONTENTION
651
/*
652
 * Normally we would use a BIO here to do this, but we create locks during
653
 * library initialization, and creating a bio too early, creates a recursive set
654
 * of stack calls that leads us to call CRYPTO_thread_run_once while currently
655
 * executing the init routine for various run_once functions, which leads to
656
 * deadlock.  Avoid that by just using a FILE pointer.  Also note that we
657
 * directly use a pthread_mutex_t to protect access from multiple threads
658
 * to the contention log file.  We do this because we want to avoid use
659
 * of the CRYPTO_THREAD api so as to prevent recursive blocking reports.
660
 */
661
static CRYPTO_ONCE init_contention_data_flag = CRYPTO_ONCE_STATIC_INIT;
662
pthread_mutex_t log_lock = PTHREAD_MUTEX_INITIALIZER;
663
CRYPTO_THREAD_LOCAL thread_contention_data;
664
665
struct stack_info {
666
    unsigned int nptrs;
667
    int write;
668
    OSSL_TIME start;
669
    OSSL_TIME duration;
670
    char **strings;
671
};
672
673
#define STACKS_COUNT 32
674
#define BT_BUF_SIZE 1024
675
struct stack_traces {
676
    int fd;
677
    int lock_depth;
678
    size_t idx;
679
    struct stack_info stacks[STACKS_COUNT];
680
};
681
682
/* The glibc gettid() definition presents only since 2.30. */
683
static ossl_inline pid_t get_tid(void)
684
{
685
#ifdef OPENSSL_SYS_MACOSX
686
    /*
687
     * MACOS has the gettid call, but it does something completely different
688
     * here than on other unixes.  Specifically it returns the uid of the calling thread
689
     * (if set), or -1.  We need to use a MACOS specific call to get the thread id here
690
     */
691
    uint64_t tid;
692
693
    pthread_threadid_np(NULL, &tid);
694
    return (pid_t)tid;
695
#else
696
    return syscall(SYS_gettid);
697
#endif
698
}
699
700
#ifdef FIPS_MODULE
701
#define FIPS_SFX "-fips"
702
#else
703
#define FIPS_SFX ""
704
#endif
705
static void *init_contention_data(void)
706
{
707
    struct stack_traces *traces;
708
    char fname_fmt[] = "lock-contention-log" FIPS_SFX ".%d.txt";
709
    char fname[sizeof(fname_fmt) + sizeof(int) * 3];
710
711
    traces = OPENSSL_zalloc(sizeof(struct stack_traces));
712
713
    snprintf(fname, sizeof(fname), fname_fmt, get_tid());
714
715
    traces->fd = open(fname, O_WRONLY | O_APPEND | O_CLOEXEC | O_CREAT, 0600);
716
717
    return traces;
718
}
719
720
static void destroy_contention_data(void *data)
721
{
722
    struct stack_traces *st = data;
723
724
    close(st->fd);
725
    OPENSSL_free(data);
726
}
727
728
static void init_contention_data_once(void)
729
{
730
    /*
731
     * Create a thread local key here to store our list of stack traces
732
     * to be printed when we unlock the lock we are holding
733
     */
734
    CRYPTO_THREAD_init_local(&thread_contention_data, destroy_contention_data);
735
    return;
736
}
737
738
static struct stack_traces *get_stack_traces(bool init)
739
{
740
    struct stack_traces *traces = CRYPTO_THREAD_get_local(&thread_contention_data);
741
742
    if (!traces && init) {
743
        traces = init_contention_data();
744
        CRYPTO_THREAD_set_local(&thread_contention_data, traces);
745
    }
746
747
    return traces;
748
}
749
750
static void print_stack_traces(struct stack_traces *traces)
751
{
752
    unsigned int j;
753
    struct iovec *iov;
754
    int iovcnt;
755
756
    while (traces != NULL && traces->idx >= 1) {
757
        traces->idx--;
758
        dprintf(traces->fd,
759
            "lock blocked on %s for %zu usec at time %zu tid %d\n",
760
            traces->stacks[traces->idx].write == 1 ? "WRITE" : "READ",
761
            ossl_time2us(traces->stacks[traces->idx].duration),
762
            ossl_time2us(traces->stacks[traces->idx].start),
763
            get_tid());
764
        if (traces->stacks[traces->idx].strings != NULL) {
765
            static const char lf = '\n';
766
767
            iovcnt = traces->stacks[traces->idx].nptrs * 2 + 1;
768
            iov = alloca(iovcnt * sizeof(*iov));
769
            for (j = 0; j < traces->stacks[traces->idx].nptrs; j++) {
770
                iov[2 * j].iov_base = traces->stacks[traces->idx].strings[j];
771
                iov[2 * j].iov_len = strlen(traces->stacks[traces->idx].strings[j]);
772
                iov[2 * j + 1].iov_base = (char *)&lf;
773
                iov[2 * j + 1].iov_len = 1;
774
            }
775
            iov[traces->stacks[traces->idx].nptrs * 2].iov_base = (char *)&lf;
776
            iov[traces->stacks[traces->idx].nptrs * 2].iov_len = 1;
777
        } else {
778
            static const char no_bt[] = "No stack trace available\n\n";
779
780
            iovcnt = 1;
781
            iov = alloca(iovcnt * sizeof(*iov));
782
            iov[0].iov_base = (char *)no_bt;
783
            iov[0].iov_len = sizeof(no_bt) - 1;
784
        }
785
        writev(traces->fd, iov, iovcnt);
786
        free(traces->stacks[traces->idx].strings);
787
    }
788
}
789
790
static ossl_inline void ossl_init_rwlock_contention_data(void)
791
{
792
    CRYPTO_THREAD_run_once(&init_contention_data_flag, init_contention_data_once);
793
}
794
795
static int record_lock_contention(pthread_rwlock_t *lock,
796
    struct stack_traces *traces, bool write)
797
{
798
    void *buffer[BT_BUF_SIZE];
799
    OSSL_TIME start, end;
800
    int ret;
801
802
    start = ossl_time_now();
803
    ret = (write ? pthread_rwlock_wrlock : pthread_rwlock_rdlock)(lock);
804
    if (ret)
805
        return ret;
806
    end = ossl_time_now();
807
    traces->stacks[traces->idx].nptrs = backtrace(buffer, BT_BUF_SIZE);
808
    traces->stacks[traces->idx].strings = backtrace_symbols(buffer,
809
        traces->stacks[traces->idx].nptrs);
810
    traces->stacks[traces->idx].duration = ossl_time_subtract(end, start);
811
    traces->stacks[traces->idx].start = start;
812
    traces->stacks[traces->idx].write = write;
813
    traces->idx++;
814
    if (traces->idx >= STACKS_COUNT) {
815
        fprintf(stderr, "STACK RECORD OVERFLOW!\n");
816
        print_stack_traces(traces);
817
    }
818
819
    return 0;
820
}
821
822
static ossl_inline int ossl_rwlock_rdlock(pthread_rwlock_t *lock)
823
{
824
    struct stack_traces *traces = get_stack_traces(true);
825
826
    if (ossl_unlikely(traces == NULL))
827
        return ENOMEM;
828
829
    traces->lock_depth++;
830
    if (pthread_rwlock_tryrdlock(lock)) {
831
        int ret = record_lock_contention(lock, traces, false);
832
833
        if (ret)
834
            traces->lock_depth--;
835
836
        return ret;
837
    }
838
839
    return 0;
840
}
841
842
static ossl_inline int ossl_rwlock_wrlock(pthread_rwlock_t *lock)
843
{
844
    struct stack_traces *traces = get_stack_traces(true);
845
846
    if (ossl_unlikely(traces == NULL))
847
        return ENOMEM;
848
849
    traces->lock_depth++;
850
    if (pthread_rwlock_trywrlock(lock)) {
851
        int ret = record_lock_contention(lock, traces, true);
852
853
        if (ret)
854
            traces->lock_depth--;
855
856
        return ret;
857
    }
858
859
    return 0;
860
}
861
862
static ossl_inline int ossl_rwlock_unlock(pthread_rwlock_t *lock)
863
{
864
    int ret;
865
866
    ret = pthread_rwlock_unlock(lock);
867
    if (ret)
868
        return ret;
869
870
    {
871
        struct stack_traces *traces = get_stack_traces(false);
872
873
        if (traces != NULL) {
874
            traces->lock_depth--;
875
            assert(traces->lock_depth >= 0);
876
            if (traces->lock_depth == 0)
877
                print_stack_traces(traces);
878
        }
879
    }
880
881
    return 0;
882
}
883
884
#else /* !REPORT_RWLOCK_CONTENTION */
885
886
#if defined(USE_RWLOCK)
887
static ossl_inline void ossl_init_rwlock_contention_data(void)
888
2.91M
{
889
2.91M
}
890
891
static ossl_inline int ossl_rwlock_rdlock(pthread_rwlock_t *rwlock)
892
75.4M
{
893
75.4M
    return pthread_rwlock_rdlock(rwlock);
894
75.4M
}
895
896
static ossl_inline int ossl_rwlock_wrlock(pthread_rwlock_t *rwlock)
897
32.6M
{
898
32.6M
    return pthread_rwlock_wrlock(rwlock);
899
32.6M
}
900
901
static ossl_inline int ossl_rwlock_unlock(pthread_rwlock_t *rwlock)
902
108M
{
903
108M
    return pthread_rwlock_unlock(rwlock);
904
108M
}
905
#endif /* USE_RWLOCK */
906
#endif /* REPORT_RWLOCK_CONTENTION */
907
908
CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
909
5.06M
{
910
5.06M
#ifdef USE_RWLOCK
911
5.06M
    CRYPTO_RWLOCK *lock;
912
913
5.06M
    ossl_init_rwlock_contention_data();
914
915
5.06M
    if ((lock = OPENSSL_zalloc(sizeof(pthread_rwlock_t))) == NULL)
916
        /* Don't set error, to avoid recursion blowup. */
917
0
        return NULL;
918
919
5.06M
    if (pthread_rwlock_init(lock, NULL) != 0) {
920
0
        OPENSSL_free(lock);
921
0
        return NULL;
922
0
    }
923
#else
924
    pthread_mutexattr_t attr;
925
    CRYPTO_RWLOCK *lock;
926
927
    if ((lock = OPENSSL_zalloc(sizeof(pthread_mutex_t))) == NULL)
928
        /* Don't set error, to avoid recursion blowup. */
929
        return NULL;
930
931
    /*
932
     * We don't use recursive mutexes, but try to catch errors if we do.
933
     */
934
    pthread_mutexattr_init(&attr);
935
#if !defined(__TANDEM) && !defined(_SPT_MODEL_)
936
#if !defined(NDEBUG) && !defined(OPENSSL_NO_MUTEX_ERRORCHECK)
937
    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
938
#endif
939
#else
940
    /* The SPT Thread Library does not define MUTEX attributes. */
941
#endif
942
943
    if (pthread_mutex_init(lock, &attr) != 0) {
944
        pthread_mutexattr_destroy(&attr);
945
        OPENSSL_free(lock);
946
        return NULL;
947
    }
948
949
    pthread_mutexattr_destroy(&attr);
950
#endif
951
952
5.06M
    return lock;
953
5.06M
}
954
955
__owur int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
956
106M
{
957
106M
#ifdef USE_RWLOCK
958
106M
    if (!ossl_assert(ossl_rwlock_rdlock(lock) == 0))
959
0
        return 0;
960
#else
961
    if (pthread_mutex_lock(lock) != 0) {
962
        assert(errno != EDEADLK && errno != EBUSY);
963
        return 0;
964
    }
965
#endif
966
967
106M
    return 1;
968
106M
}
969
970
__owur int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
971
54.2M
{
972
54.2M
#ifdef USE_RWLOCK
973
54.2M
    if (!ossl_assert(ossl_rwlock_wrlock(lock) == 0))
974
0
        return 0;
975
#else
976
    if (pthread_mutex_lock(lock) != 0) {
977
        assert(errno != EDEADLK && errno != EBUSY);
978
        return 0;
979
    }
980
#endif
981
982
54.2M
    return 1;
983
54.2M
}
984
985
int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
986
204M
{
987
204M
#ifdef USE_RWLOCK
988
204M
    if (ossl_rwlock_unlock(lock) != 0)
989
0
        return 0;
990
#else
991
    if (pthread_mutex_unlock(lock) != 0) {
992
        assert(errno != EPERM);
993
        return 0;
994
    }
995
#endif
996
997
204M
    return 1;
998
204M
}
999
1000
void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock)
1001
5.05M
{
1002
5.05M
    if (lock == NULL)
1003
2.27k
        return;
1004
1005
5.05M
#ifdef USE_RWLOCK
1006
5.05M
    pthread_rwlock_destroy(lock);
1007
#else
1008
    pthread_mutex_destroy(lock);
1009
#endif
1010
5.05M
    OPENSSL_free(lock);
1011
1012
5.05M
    return;
1013
5.05M
}
1014
1015
int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))
1016
2.81G
{
1017
2.81G
    if (ossl_unlikely(pthread_once(once, init) != 0))
1018
0
        return 0;
1019
1020
2.81G
    return 1;
1021
2.81G
}
1022
1023
int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
1024
535
{
1025
535
    if (pthread_key_create(key, cleanup) != 0)
1026
0
        return 0;
1027
1028
535
    return 1;
1029
535
}
1030
1031
void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
1032
2.36G
{
1033
2.36G
    return pthread_getspecific(*key);
1034
2.36G
}
1035
1036
int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
1037
1.49k
{
1038
1.49k
    if (pthread_setspecific(*key, val) != 0)
1039
0
        return 0;
1040
1041
1.49k
    return 1;
1042
1.49k
}
1043
1044
int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
1045
934
{
1046
934
    if (pthread_key_delete(*key) != 0)
1047
0
        return 0;
1048
1049
934
    return 1;
1050
934
}
1051
1052
CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
1053
143k
{
1054
143k
    return pthread_self();
1055
143k
}
1056
1057
int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
1058
6.60k
{
1059
6.60k
    return pthread_equal(a, b);
1060
6.60k
}
1061
1062
int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
1063
10.5M
{
1064
10.5M
#if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
1065
10.5M
    if (__atomic_is_lock_free(sizeof(*val), val)) {
1066
10.5M
        *ret = __atomic_add_fetch(val, amount, __ATOMIC_ACQ_REL);
1067
10.5M
        return 1;
1068
10.5M
    }
1069
#elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
1070
    /* This will work for all future Solaris versions. */
1071
    if (ret != NULL) {
1072
        *ret = atomic_add_int_nv((volatile unsigned int *)val, amount);
1073
        return 1;
1074
    }
1075
#endif
1076
0
    if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
1077
0
        return 0;
1078
1079
0
    *val += amount;
1080
0
    *ret = *val;
1081
1082
0
    if (!CRYPTO_THREAD_unlock(lock))
1083
0
        return 0;
1084
1085
0
    return 1;
1086
0
}
1087
1088
int CRYPTO_atomic_add64(uint64_t *val, uint64_t op, uint64_t *ret,
1089
    CRYPTO_RWLOCK *lock)
1090
0
{
1091
0
#if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
1092
0
    if (__atomic_is_lock_free(sizeof(*val), val)) {
1093
0
        *ret = __atomic_add_fetch(val, op, __ATOMIC_ACQ_REL);
1094
0
        return 1;
1095
0
    }
1096
#elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
1097
    /* This will work for all future Solaris versions. */
1098
    if (ret != NULL) {
1099
        *ret = atomic_add_64_nv(val, op);
1100
        return 1;
1101
    }
1102
#endif
1103
0
    if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
1104
0
        return 0;
1105
0
    *val += op;
1106
0
    *ret = *val;
1107
1108
0
    if (!CRYPTO_THREAD_unlock(lock))
1109
0
        return 0;
1110
1111
0
    return 1;
1112
0
}
1113
1114
int CRYPTO_atomic_and(uint64_t *val, uint64_t op, uint64_t *ret,
1115
    CRYPTO_RWLOCK *lock)
1116
0
{
1117
0
#if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
1118
0
    if (__atomic_is_lock_free(sizeof(*val), val)) {
1119
0
        *ret = __atomic_and_fetch(val, op, __ATOMIC_ACQ_REL);
1120
0
        return 1;
1121
0
    }
1122
#elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
1123
    /* This will work for all future Solaris versions. */
1124
    if (ret != NULL) {
1125
        *ret = atomic_and_64_nv(val, op);
1126
        return 1;
1127
    }
1128
#endif
1129
0
    if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
1130
0
        return 0;
1131
0
    *val &= op;
1132
0
    *ret = *val;
1133
1134
0
    if (!CRYPTO_THREAD_unlock(lock))
1135
0
        return 0;
1136
1137
0
    return 1;
1138
0
}
1139
1140
int CRYPTO_atomic_or(uint64_t *val, uint64_t op, uint64_t *ret,
1141
    CRYPTO_RWLOCK *lock)
1142
761
{
1143
761
#if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
1144
761
    if (__atomic_is_lock_free(sizeof(*val), val)) {
1145
761
        *ret = __atomic_or_fetch(val, op, __ATOMIC_ACQ_REL);
1146
761
        return 1;
1147
761
    }
1148
#elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
1149
    /* This will work for all future Solaris versions. */
1150
    if (ret != NULL) {
1151
        *ret = atomic_or_64_nv(val, op);
1152
        return 1;
1153
    }
1154
#endif
1155
0
    if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
1156
0
        return 0;
1157
0
    *val |= op;
1158
0
    *ret = *val;
1159
1160
0
    if (!CRYPTO_THREAD_unlock(lock))
1161
0
        return 0;
1162
1163
0
    return 1;
1164
0
}
1165
1166
int CRYPTO_atomic_load(uint64_t *val, uint64_t *ret, CRYPTO_RWLOCK *lock)
1167
2.98G
{
1168
2.98G
#if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
1169
2.98G
    if (__atomic_is_lock_free(sizeof(*val), val)) {
1170
2.98G
        __atomic_load(val, ret, __ATOMIC_ACQUIRE);
1171
2.98G
        return 1;
1172
2.98G
    }
1173
#elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
1174
    /* This will work for all future Solaris versions. */
1175
    if (ret != NULL) {
1176
        *ret = atomic_or_64_nv(val, 0);
1177
        return 1;
1178
    }
1179
#endif
1180
0
    if (lock == NULL || !CRYPTO_THREAD_read_lock(lock))
1181
0
        return 0;
1182
0
    *ret = *val;
1183
0
    if (!CRYPTO_THREAD_unlock(lock))
1184
0
        return 0;
1185
1186
0
    return 1;
1187
0
}
1188
1189
int CRYPTO_atomic_store(uint64_t *dst, uint64_t val, CRYPTO_RWLOCK *lock)
1190
51.1k
{
1191
51.1k
#if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
1192
51.1k
    if (__atomic_is_lock_free(sizeof(*dst), dst)) {
1193
51.1k
        __atomic_store(dst, &val, __ATOMIC_RELEASE);
1194
51.1k
        return 1;
1195
51.1k
    }
1196
#elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
1197
    /* This will work for all future Solaris versions. */
1198
    if (dst != NULL) {
1199
        atomic_swap_64(dst, val);
1200
        return 1;
1201
    }
1202
#endif
1203
0
    if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
1204
0
        return 0;
1205
0
    *dst = val;
1206
0
    if (!CRYPTO_THREAD_unlock(lock))
1207
0
        return 0;
1208
1209
0
    return 1;
1210
0
}
1211
1212
int CRYPTO_atomic_load_int(int *val, int *ret, CRYPTO_RWLOCK *lock)
1213
117M
{
1214
117M
#if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
1215
117M
    if (__atomic_is_lock_free(sizeof(*val), val)) {
1216
117M
        __atomic_load(val, ret, __ATOMIC_ACQUIRE);
1217
117M
        return 1;
1218
117M
    }
1219
#elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
1220
    /* This will work for all future Solaris versions. */
1221
    if (ret != NULL) {
1222
        *ret = (int)atomic_or_uint_nv((unsigned int *)val, 0);
1223
        return 1;
1224
    }
1225
#endif
1226
0
    if (lock == NULL || !CRYPTO_THREAD_read_lock(lock))
1227
0
        return 0;
1228
0
    *ret = *val;
1229
0
    if (!CRYPTO_THREAD_unlock(lock))
1230
0
        return 0;
1231
1232
0
    return 1;
1233
0
}
1234
1235
int CRYPTO_atomic_store_int(int *dst, int val, CRYPTO_RWLOCK *lock)
1236
0
{
1237
0
#if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
1238
0
    if (__atomic_is_lock_free(sizeof(*dst), dst)) {
1239
0
        __atomic_store(dst, &val, __ATOMIC_RELEASE);
1240
0
        return 1;
1241
0
    }
1242
#elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
1243
    /* This will work for all future Solaris versions. */
1244
    if (dst != NULL) {
1245
        atomic_swap_uint((unsigned int)dst, (unsigned int)val);
1246
        return 1;
1247
    }
1248
#endif
1249
0
    if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
1250
0
        return 0;
1251
0
    *dst = val;
1252
0
    if (!CRYPTO_THREAD_unlock(lock))
1253
0
        return 0;
1254
1255
0
    return 1;
1256
0
}
1257
1258
#ifndef FIPS_MODULE
1259
int openssl_init_fork_handlers(void)
1260
0
{
1261
0
    return 1;
1262
0
}
1263
#endif /* FIPS_MODULE */
1264
1265
int openssl_get_fork_id(void)
1266
118k
{
1267
118k
    return getpid();
1268
118k
}
1269
#endif