Coverage Report

Created: 2026-03-03 06:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/threads_pthread.c
Line
Count
Source
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
#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
1.82M
#define ATOMIC_LOAD_N(t, p, o) __atomic_load_n(p, o)
123
0
#define ATOMIC_STORE_N(t, p, v, o) __atomic_store_n(p, v, o)
124
298
#define ATOMIC_STORE(t, p, v, o) __atomic_store(p, v, o)
125
0
#define ATOMIC_ADD_FETCH(p, v, o) __atomic_add_fetch(p, v, o)
126
0
#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
0
#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
0
{
286
0
    uint32_t qp_idx;
287
288
    /* get the current qp index */
289
0
    for (;;) {
290
0
        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
0
        ATOMIC_ADD_FETCH(&lock->qp_group[qp_idx].users, (uint64_t)1,
304
0
            __ATOMIC_ACQUIRE);
305
306
        /* if the idx hasn't changed, we're good, else try again */
307
0
        if (qp_idx == ATOMIC_LOAD_N(uint32_t, &lock->reader_idx, __ATOMIC_ACQUIRE))
308
0
            break;
309
310
0
        ATOMIC_SUB_FETCH(&lock->qp_group[qp_idx].users, (uint64_t)1,
311
0
            __ATOMIC_RELAXED);
312
0
    }
313
314
0
    return &lock->qp_group[qp_idx];
315
0
}
316
317
static void ossl_rcu_free_local_data(void *arg)
318
0
{
319
0
    OSSL_LIB_CTX *ctx = arg;
320
0
    struct rcu_thr_data *data = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_RCU_KEY, ctx);
321
322
0
    CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_RCU_KEY, ctx, NULL);
323
0
    OPENSSL_free(data);
324
0
}
325
326
int ossl_rcu_read_lock(CRYPTO_RCU_LOCK *lock)
327
0
{
328
0
    struct rcu_thr_data *data;
329
0
    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
0
    data = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_RCU_KEY, lock->ctx);
336
337
0
    if (data == NULL) {
338
0
        data = OPENSSL_zalloc(sizeof(*data));
339
0
        if (data == NULL)
340
0
            return 0;
341
342
0
        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
0
        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
0
    }
352
353
0
    for (i = 0; i < MAX_QPS; i++) {
354
0
        if (data->thread_qps[i].qp == NULL && available_qp == -1)
355
0
            available_qp = i;
356
        /* If we have a hold on this lock already, we're good */
357
0
        if (data->thread_qps[i].lock == lock) {
358
0
            data->thread_qps[i].depth++;
359
0
            return 1;
360
0
        }
361
0
    }
362
363
    /*
364
     * if we get here, then we don't have a hold on this lock yet
365
     */
366
0
    assert(available_qp != -1);
367
368
0
    data->thread_qps[available_qp].qp = get_hold_current_qp(lock);
369
0
    data->thread_qps[available_qp].depth = 1;
370
0
    data->thread_qps[available_qp].lock = lock;
371
0
    return 1;
372
0
}
373
374
void ossl_rcu_read_unlock(CRYPTO_RCU_LOCK *lock)
375
0
{
376
0
    int i;
377
0
    struct rcu_thr_data *data = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_RCU_KEY, lock->ctx);
378
0
    uint64_t ret;
379
380
0
    assert(data != NULL);
381
382
0
    for (i = 0; i < MAX_QPS; i++) {
383
0
        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
0
            data->thread_qps[i].depth--;
390
0
            if (data->thread_qps[i].depth == 0) {
391
0
                ret = ATOMIC_SUB_FETCH(&data->thread_qps[i].qp->users,
392
0
                    (uint64_t)1, __ATOMIC_RELEASE);
393
0
                OPENSSL_assert(ret != UINT64_MAX);
394
0
                data->thread_qps[i].qp = NULL;
395
0
                data->thread_qps[i].lock = NULL;
396
0
            }
397
0
            return;
398
0
        }
399
0
    }
400
    /*
401
     * If we get here, we're trying to unlock a lock that we never acquired -
402
     * that's fatal.
403
     */
404
0
    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
0
{
413
0
    uint32_t current_idx;
414
415
0
    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
0
    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
0
    current_idx = lock->current_alloc_idx;
427
428
    /* Allocate the qp */
429
0
    lock->writers_alloced++;
430
431
    /* increment the allocation index */
432
0
    lock->current_alloc_idx = (lock->current_alloc_idx + 1) % lock->group_count;
433
434
0
    *curr_id = lock->id_ctr;
435
0
    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
0
    ATOMIC_STORE_N(uint32_t, &lock->reader_idx, lock->current_alloc_idx,
442
0
        __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
0
    ATOMIC_ADD_FETCH(&lock->qp_group[current_idx].users, (uint64_t)0,
449
0
        __ATOMIC_RELEASE);
450
451
    /* wake up any waiters */
452
0
    pthread_cond_signal(&lock->alloc_signal);
453
0
    pthread_mutex_unlock(&lock->alloc_lock);
454
0
    return &lock->qp_group[current_idx];
455
0
}
456
457
static void retire_qp(CRYPTO_RCU_LOCK *lock, struct rcu_qp *qp)
458
0
{
459
0
    pthread_mutex_lock(&lock->alloc_lock);
460
0
    lock->writers_alloced--;
461
0
    pthread_cond_signal(&lock->alloc_signal);
462
0
    pthread_mutex_unlock(&lock->alloc_lock);
463
0
}
464
465
static struct rcu_qp *allocate_new_qp_group(CRYPTO_RCU_LOCK *lock,
466
    uint32_t count)
467
3
{
468
3
    struct rcu_qp *new = OPENSSL_calloc(count, sizeof(*new));
469
470
3
    lock->group_count = count;
471
3
    return new;
472
3
}
473
474
void ossl_rcu_write_lock(CRYPTO_RCU_LOCK *lock)
475
0
{
476
0
    pthread_mutex_lock(&lock->write_lock);
477
0
    TSAN_FAKE_UNLOCK(&lock->write_lock);
478
0
}
479
480
void ossl_rcu_write_unlock(CRYPTO_RCU_LOCK *lock)
481
0
{
482
0
    TSAN_FAKE_LOCK(&lock->write_lock);
483
0
    pthread_mutex_unlock(&lock->write_lock);
484
0
}
485
486
void ossl_synchronize_rcu(CRYPTO_RCU_LOCK *lock)
487
0
{
488
0
    struct rcu_qp *qp;
489
0
    uint64_t count;
490
0
    uint32_t curr_id;
491
0
    struct rcu_cb_item *cb_items, *tmpcb;
492
493
0
    pthread_mutex_lock(&lock->write_lock);
494
0
    cb_items = lock->cb_items;
495
0
    lock->cb_items = NULL;
496
0
    pthread_mutex_unlock(&lock->write_lock);
497
498
0
    qp = update_qp(lock, &curr_id);
499
500
    /* retire in order */
501
0
    pthread_mutex_lock(&lock->prior_lock);
502
0
    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
0
    do {
515
0
        count = ATOMIC_LOAD_N(uint64_t, &qp->users, __ATOMIC_ACQUIRE);
516
0
    } while (count != (uint64_t)0);
517
518
0
    lock->next_to_retire++;
519
0
    pthread_cond_broadcast(&lock->prior_signal);
520
0
    pthread_mutex_unlock(&lock->prior_lock);
521
522
0
    retire_qp(lock, qp);
523
524
    /* handle any callbacks that we have */
525
0
    while (cb_items != NULL) {
526
0
        tmpcb = cb_items;
527
0
        cb_items = cb_items->next;
528
0
        tmpcb->fn(tmpcb->data);
529
0
        OPENSSL_free(tmpcb);
530
0
    }
531
0
}
532
533
/*
534
 * Note: This call assumes its made under the protection of
535
 * ossl_rcu_write_lock
536
 */
537
int ossl_rcu_call(CRYPTO_RCU_LOCK *lock, rcu_cb_fn cb, void *data)
538
0
{
539
0
    struct rcu_cb_item *new = OPENSSL_zalloc(sizeof(*new));
540
541
0
    if (new == NULL)
542
0
        return 0;
543
544
0
    new->data = data;
545
0
    new->fn = cb;
546
547
0
    new->next = lock->cb_items;
548
0
    lock->cb_items = new;
549
550
0
    return 1;
551
0
}
552
553
void *ossl_rcu_uptr_deref(void **p)
554
1.82M
{
555
1.82M
    return ATOMIC_LOAD_N(pvoid, p, __ATOMIC_ACQUIRE);
556
1.82M
}
557
558
void ossl_rcu_assign_uptr(void **p, void **v)
559
298
{
560
298
    ATOMIC_STORE(pvoid, p, v, __ATOMIC_RELEASE);
561
298
}
562
563
CRYPTO_RCU_LOCK *ossl_rcu_lock_new(int num_writers, OSSL_LIB_CTX *ctx)
564
3
{
565
3
    struct rcu_lock_st *new;
566
3
    pthread_mutex_t *mutexes[3] = { NULL };
567
3
    pthread_cond_t *conds[2] = { NULL };
568
3
    int i;
569
570
    /*
571
     * We need a minimum of 2 qp's
572
     */
573
3
    if (num_writers < 2)
574
3
        num_writers = 2;
575
576
3
    ctx = ossl_lib_ctx_get_concrete(ctx);
577
3
    if (ctx == NULL)
578
0
        return 0;
579
580
3
    new = OPENSSL_zalloc(sizeof(*new));
581
3
    if (new == NULL)
582
0
        return NULL;
583
584
3
    new->ctx = ctx;
585
3
    i = 0;
586
3
    mutexes[i] = pthread_mutex_init(&new->write_lock, NULL) == 0 ? &new->write_lock : NULL;
587
3
    if (mutexes[i++] == NULL)
588
0
        goto err;
589
3
    mutexes[i] = pthread_mutex_init(&new->prior_lock, NULL) == 0 ? &new->prior_lock : NULL;
590
3
    if (mutexes[i++] == NULL)
591
0
        goto err;
592
3
    mutexes[i] = pthread_mutex_init(&new->alloc_lock, NULL) == 0 ? &new->alloc_lock : NULL;
593
3
    if (mutexes[i++] == NULL)
594
0
        goto err;
595
3
    conds[i - 3] = pthread_cond_init(&new->prior_signal, NULL) == 0 ? &new->prior_signal : NULL;
596
3
    if (conds[i - 3] == NULL)
597
0
        goto err;
598
3
    i++;
599
3
    conds[i - 3] = pthread_cond_init(&new->alloc_signal, NULL) == 0 ? &new->alloc_signal : NULL;
600
3
    if (conds[i - 3] == NULL)
601
0
        goto err;
602
3
    i++;
603
3
    new->qp_group = allocate_new_qp_group(new, num_writers);
604
3
    if (new->qp_group == NULL)
605
0
        goto err;
606
607
3
    return new;
608
609
0
err:
610
0
    for (i = 0; i < 3; i++)
611
0
        if (mutexes[i] != NULL)
612
0
            pthread_mutex_destroy(mutexes[i]);
613
0
    for (i = 0; i < 2; i++)
614
0
        if (conds[i] != NULL)
615
0
            pthread_cond_destroy(conds[i]);
616
0
    OPENSSL_free(new->qp_group);
617
0
    OPENSSL_free(new);
618
0
    return NULL;
619
3
}
620
621
void ossl_rcu_lock_free(CRYPTO_RCU_LOCK *lock)
622
0
{
623
0
    struct rcu_lock_st *rlock = (struct rcu_lock_st *)lock;
624
625
0
    if (lock == NULL)
626
0
        return;
627
628
    /* make sure we're synchronized */
629
0
    ossl_synchronize_rcu(rlock);
630
631
0
    OPENSSL_free(rlock->qp_group);
632
    /*
633
     * Some targets (BSD) allocate heap when initializing
634
     * a mutex or condition, to prevent leaks, those need
635
     * to be destroyed here
636
     */
637
0
    pthread_mutex_destroy(&rlock->write_lock);
638
0
    pthread_mutex_destroy(&rlock->prior_lock);
639
0
    pthread_mutex_destroy(&rlock->alloc_lock);
640
0
    pthread_cond_destroy(&rlock->prior_signal);
641
0
    pthread_cond_destroy(&rlock->alloc_signal);
642
643
    /* There should only be a single qp left now */
644
0
    OPENSSL_free(rlock);
645
0
}
646
647
#ifdef REPORT_RWLOCK_CONTENTION
648
/*
649
 * Normally we would use a BIO here to do this, but we create locks during
650
 * library initialization, and creating a bio too early, creates a recursive set
651
 * of stack calls that leads us to call CRYPTO_thread_run_once while currently
652
 * executing the init routine for various run_once functions, which leads to
653
 * deadlock.  Avoid that by just using a FILE pointer.  Also note that we
654
 * directly use a pthread_mutex_t to protect access from multiple threads
655
 * to the contention log file.  We do this because we want to avoid use
656
 * of the CRYPTO_THREAD api so as to prevent recursive blocking reports.
657
 */
658
static CRYPTO_ONCE init_contention_data_flag = CRYPTO_ONCE_STATIC_INIT;
659
pthread_mutex_t log_lock = PTHREAD_MUTEX_INITIALIZER;
660
CRYPTO_THREAD_LOCAL thread_contention_data;
661
662
struct stack_info {
663
    unsigned int nptrs;
664
    int write;
665
    OSSL_TIME start;
666
    OSSL_TIME duration;
667
    char **strings;
668
};
669
670
#define STACKS_COUNT 32
671
#define BT_BUF_SIZE 1024
672
struct stack_traces {
673
    int fd;
674
    int lock_depth;
675
    size_t idx;
676
    struct stack_info stacks[STACKS_COUNT];
677
};
678
679
/* The glibc gettid() definition presents only since 2.30. */
680
static ossl_inline pid_t get_tid(void)
681
{
682
#ifdef OPENSSL_SYS_MACOSX
683
    /*
684
     * MACOS has the gettid call, but it does something completely different
685
     * here than on other unixes.  Specifically it returns the uid of the calling thread
686
     * (if set), or -1.  We need to use a MACOS specific call to get the thread id here
687
     */
688
    uint64_t tid;
689
690
    pthread_threadid_np(NULL, &tid);
691
    return (pid_t)tid;
692
#else
693
    return syscall(SYS_gettid);
694
#endif
695
}
696
697
#ifdef FIPS_MODULE
698
#define FIPS_SFX "-fips"
699
#else
700
#define FIPS_SFX ""
701
#endif
702
static void *init_contention_data(void)
703
{
704
    struct stack_traces *traces;
705
    char fname_fmt[] = "lock-contention-log" FIPS_SFX ".%d.txt";
706
    char fname[sizeof(fname_fmt) + sizeof(int) * 3];
707
708
    traces = OPENSSL_zalloc(sizeof(struct stack_traces));
709
710
    snprintf(fname, sizeof(fname), fname_fmt, get_tid());
711
712
    traces->fd = open(fname, O_WRONLY | O_APPEND | O_CLOEXEC | O_CREAT, 0600);
713
714
    return traces;
715
}
716
717
static void destroy_contention_data(void *data)
718
{
719
    struct stack_traces *st = data;
720
721
    close(st->fd);
722
    OPENSSL_free(data);
723
}
724
725
static void init_contention_data_once(void)
726
{
727
    /*
728
     * Create a thread local key here to store our list of stack traces
729
     * to be printed when we unlock the lock we are holding
730
     */
731
    CRYPTO_THREAD_init_local(&thread_contention_data, destroy_contention_data);
732
    return;
733
}
734
735
static struct stack_traces *get_stack_traces(bool init)
736
{
737
    struct stack_traces *traces = CRYPTO_THREAD_get_local(&thread_contention_data);
738
739
    if (!traces && init) {
740
        traces = init_contention_data();
741
        CRYPTO_THREAD_set_local(&thread_contention_data, traces);
742
    }
743
744
    return traces;
745
}
746
747
static void print_stack_traces(struct stack_traces *traces)
748
{
749
    unsigned int j;
750
    struct iovec *iov;
751
    int iovcnt;
752
753
    while (traces != NULL && traces->idx >= 1) {
754
        traces->idx--;
755
        dprintf(traces->fd,
756
            "lock blocked on %s for %zu usec at time %zu tid %d\n",
757
            traces->stacks[traces->idx].write == 1 ? "WRITE" : "READ",
758
            ossl_time2us(traces->stacks[traces->idx].duration),
759
            ossl_time2us(traces->stacks[traces->idx].start),
760
            get_tid());
761
        if (traces->stacks[traces->idx].strings != NULL) {
762
            static const char lf = '\n';
763
764
            iovcnt = traces->stacks[traces->idx].nptrs * 2 + 1;
765
            iov = alloca(iovcnt * sizeof(*iov));
766
            for (j = 0; j < traces->stacks[traces->idx].nptrs; j++) {
767
                iov[2 * j].iov_base = traces->stacks[traces->idx].strings[j];
768
                iov[2 * j].iov_len = strlen(traces->stacks[traces->idx].strings[j]);
769
                iov[2 * j + 1].iov_base = (char *)&lf;
770
                iov[2 * j + 1].iov_len = 1;
771
            }
772
            iov[traces->stacks[traces->idx].nptrs * 2].iov_base = (char *)&lf;
773
            iov[traces->stacks[traces->idx].nptrs * 2].iov_len = 1;
774
        } else {
775
            static const char no_bt[] = "No stack trace available\n\n";
776
777
            iovcnt = 1;
778
            iov = alloca(iovcnt * sizeof(*iov));
779
            iov[0].iov_base = (char *)no_bt;
780
            iov[0].iov_len = sizeof(no_bt) - 1;
781
        }
782
        writev(traces->fd, iov, iovcnt);
783
        free(traces->stacks[traces->idx].strings);
784
    }
785
}
786
787
static ossl_inline void ossl_init_rwlock_contention_data(void)
788
{
789
    CRYPTO_THREAD_run_once(&init_contention_data_flag, init_contention_data_once);
790
}
791
792
static int record_lock_contention(pthread_rwlock_t *lock,
793
    struct stack_traces *traces, bool write)
794
{
795
    void *buffer[BT_BUF_SIZE];
796
    OSSL_TIME start, end;
797
    int ret;
798
799
    start = ossl_time_now();
800
    ret = (write ? pthread_rwlock_wrlock : pthread_rwlock_rdlock)(lock);
801
    if (ret)
802
        return ret;
803
    end = ossl_time_now();
804
    traces->stacks[traces->idx].nptrs = backtrace(buffer, BT_BUF_SIZE);
805
    traces->stacks[traces->idx].strings = backtrace_symbols(buffer,
806
        traces->stacks[traces->idx].nptrs);
807
    traces->stacks[traces->idx].duration = ossl_time_subtract(end, start);
808
    traces->stacks[traces->idx].start = start;
809
    traces->stacks[traces->idx].write = write;
810
    traces->idx++;
811
    if (traces->idx >= STACKS_COUNT) {
812
        fprintf(stderr, "STACK RECORD OVERFLOW!\n");
813
        print_stack_traces(traces);
814
    }
815
816
    return 0;
817
}
818
819
static ossl_inline int ossl_rwlock_rdlock(pthread_rwlock_t *lock)
820
{
821
    struct stack_traces *traces = get_stack_traces(true);
822
823
    if (ossl_unlikely(traces == NULL))
824
        return ENOMEM;
825
826
    traces->lock_depth++;
827
    if (pthread_rwlock_tryrdlock(lock)) {
828
        int ret = record_lock_contention(lock, traces, false);
829
830
        if (ret)
831
            traces->lock_depth--;
832
833
        return ret;
834
    }
835
836
    return 0;
837
}
838
839
static ossl_inline int ossl_rwlock_wrlock(pthread_rwlock_t *lock)
840
{
841
    struct stack_traces *traces = get_stack_traces(true);
842
843
    if (ossl_unlikely(traces == NULL))
844
        return ENOMEM;
845
846
    traces->lock_depth++;
847
    if (pthread_rwlock_trywrlock(lock)) {
848
        int ret = record_lock_contention(lock, traces, true);
849
850
        if (ret)
851
            traces->lock_depth--;
852
853
        return ret;
854
    }
855
856
    return 0;
857
}
858
859
static ossl_inline int ossl_rwlock_unlock(pthread_rwlock_t *lock)
860
{
861
    int ret;
862
863
    ret = pthread_rwlock_unlock(lock);
864
    if (ret)
865
        return ret;
866
867
    {
868
        struct stack_traces *traces = get_stack_traces(false);
869
870
        if (traces != NULL) {
871
            traces->lock_depth--;
872
            assert(traces->lock_depth >= 0);
873
            if (traces->lock_depth == 0)
874
                print_stack_traces(traces);
875
        }
876
    }
877
878
    return 0;
879
}
880
881
#else /* !REPORT_RWLOCK_CONTENTION */
882
883
#if defined(USE_RWLOCK)
884
static ossl_inline void ossl_init_rwlock_contention_data(void)
885
111
{
886
111
}
887
888
static ossl_inline int ossl_rwlock_rdlock(pthread_rwlock_t *rwlock)
889
711k
{
890
711k
    return pthread_rwlock_rdlock(rwlock);
891
711k
}
892
893
static ossl_inline int ossl_rwlock_wrlock(pthread_rwlock_t *rwlock)
894
2.37k
{
895
2.37k
    return pthread_rwlock_wrlock(rwlock);
896
2.37k
}
897
898
static ossl_inline int ossl_rwlock_unlock(pthread_rwlock_t *rwlock)
899
714k
{
900
714k
    return pthread_rwlock_unlock(rwlock);
901
714k
}
902
#endif /* USE_RWLOCK */
903
#endif /* REPORT_RWLOCK_CONTENTION */
904
905
CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
906
111
{
907
111
#ifdef USE_RWLOCK
908
111
    CRYPTO_RWLOCK *lock;
909
910
111
    ossl_init_rwlock_contention_data();
911
912
111
    if ((lock = OPENSSL_zalloc(sizeof(pthread_rwlock_t))) == NULL)
913
        /* Don't set error, to avoid recursion blowup. */
914
0
        return NULL;
915
916
111
    if (pthread_rwlock_init(lock, NULL) != 0) {
917
0
        OPENSSL_free(lock);
918
0
        return NULL;
919
0
    }
920
#else
921
    pthread_mutexattr_t attr;
922
    CRYPTO_RWLOCK *lock;
923
924
    if ((lock = OPENSSL_zalloc(sizeof(pthread_mutex_t))) == NULL)
925
        /* Don't set error, to avoid recursion blowup. */
926
        return NULL;
927
928
    /*
929
     * We don't use recursive mutexes, but try to catch errors if we do.
930
     */
931
    pthread_mutexattr_init(&attr);
932
#if !defined(__TANDEM) && !defined(_SPT_MODEL_)
933
#if !defined(NDEBUG) && !defined(OPENSSL_NO_MUTEX_ERRORCHECK)
934
    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
935
#endif
936
#else
937
    /* The SPT Thread Library does not define MUTEX attributes. */
938
#endif
939
940
    if (pthread_mutex_init(lock, &attr) != 0) {
941
        pthread_mutexattr_destroy(&attr);
942
        OPENSSL_free(lock);
943
        return NULL;
944
    }
945
946
    pthread_mutexattr_destroy(&attr);
947
#endif
948
949
111
    return lock;
950
111
}
951
952
__owur int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
953
711k
{
954
711k
#ifdef USE_RWLOCK
955
711k
    if (!ossl_assert(ossl_rwlock_rdlock(lock) == 0))
956
0
        return 0;
957
#else
958
    if (pthread_mutex_lock(lock) != 0) {
959
        assert(errno != EDEADLK && errno != EBUSY);
960
        return 0;
961
    }
962
#endif
963
964
711k
    return 1;
965
711k
}
966
967
__owur int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
968
2.37k
{
969
2.37k
#ifdef USE_RWLOCK
970
2.37k
    if (!ossl_assert(ossl_rwlock_wrlock(lock) == 0))
971
0
        return 0;
972
#else
973
    if (pthread_mutex_lock(lock) != 0) {
974
        assert(errno != EDEADLK && errno != EBUSY);
975
        return 0;
976
    }
977
#endif
978
979
2.37k
    return 1;
980
2.37k
}
981
982
int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
983
714k
{
984
714k
#ifdef USE_RWLOCK
985
714k
    if (ossl_rwlock_unlock(lock) != 0)
986
0
        return 0;
987
#else
988
    if (pthread_mutex_unlock(lock) != 0) {
989
        assert(errno != EPERM);
990
        return 0;
991
    }
992
#endif
993
994
714k
    return 1;
995
714k
}
996
997
void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock)
998
0
{
999
0
    if (lock == NULL)
1000
0
        return;
1001
1002
0
#ifdef USE_RWLOCK
1003
0
    pthread_rwlock_destroy(lock);
1004
#else
1005
    pthread_mutex_destroy(lock);
1006
#endif
1007
0
    OPENSSL_free(lock);
1008
1009
0
    return;
1010
0
}
1011
1012
int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))
1013
7.19k
{
1014
7.19k
    if (ossl_unlikely(pthread_once(once, init) != 0))
1015
0
        return 0;
1016
1017
7.19k
    return 1;
1018
7.19k
}
1019
1020
int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
1021
4
{
1022
4
    if (pthread_key_create(key, cleanup) != 0)
1023
0
        return 0;
1024
1025
4
    return 1;
1026
4
}
1027
1028
void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
1029
3.19k
{
1030
3.19k
    return pthread_getspecific(*key);
1031
3.19k
}
1032
1033
int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
1034
2
{
1035
2
    if (pthread_setspecific(*key, val) != 0)
1036
0
        return 0;
1037
1038
2
    return 1;
1039
2
}
1040
1041
int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
1042
0
{
1043
0
    if (pthread_key_delete(*key) != 0)
1044
0
        return 0;
1045
1046
0
    return 1;
1047
0
}
1048
1049
CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
1050
0
{
1051
0
    return pthread_self();
1052
0
}
1053
1054
int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
1055
0
{
1056
0
    return pthread_equal(a, b);
1057
0
}
1058
1059
int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
1060
2.02k
{
1061
2.02k
#if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
1062
2.02k
    if (__atomic_is_lock_free(sizeof(*val), val)) {
1063
2.02k
        *ret = __atomic_add_fetch(val, amount, __ATOMIC_ACQ_REL);
1064
2.02k
        return 1;
1065
2.02k
    }
1066
#elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
1067
    /* This will work for all future Solaris versions. */
1068
    if (ret != NULL) {
1069
        *ret = atomic_add_int_nv((volatile unsigned int *)val, amount);
1070
        return 1;
1071
    }
1072
#endif
1073
0
    if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
1074
0
        return 0;
1075
1076
0
    *val += amount;
1077
0
    *ret = *val;
1078
1079
0
    if (!CRYPTO_THREAD_unlock(lock))
1080
0
        return 0;
1081
1082
0
    return 1;
1083
0
}
1084
1085
int CRYPTO_atomic_add64(uint64_t *val, uint64_t op, uint64_t *ret,
1086
    CRYPTO_RWLOCK *lock)
1087
0
{
1088
0
#if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
1089
0
    if (__atomic_is_lock_free(sizeof(*val), val)) {
1090
0
        *ret = __atomic_add_fetch(val, op, __ATOMIC_ACQ_REL);
1091
0
        return 1;
1092
0
    }
1093
#elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
1094
    /* This will work for all future Solaris versions. */
1095
    if (ret != NULL) {
1096
        *ret = atomic_add_64_nv(val, op);
1097
        return 1;
1098
    }
1099
#endif
1100
0
    if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
1101
0
        return 0;
1102
0
    *val += op;
1103
0
    *ret = *val;
1104
1105
0
    if (!CRYPTO_THREAD_unlock(lock))
1106
0
        return 0;
1107
1108
0
    return 1;
1109
0
}
1110
1111
int CRYPTO_atomic_and(uint64_t *val, uint64_t op, uint64_t *ret,
1112
    CRYPTO_RWLOCK *lock)
1113
0
{
1114
0
#if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
1115
0
    if (__atomic_is_lock_free(sizeof(*val), val)) {
1116
0
        *ret = __atomic_and_fetch(val, op, __ATOMIC_ACQ_REL);
1117
0
        return 1;
1118
0
    }
1119
#elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
1120
    /* This will work for all future Solaris versions. */
1121
    if (ret != NULL) {
1122
        *ret = atomic_and_64_nv(val, op);
1123
        return 1;
1124
    }
1125
#endif
1126
0
    if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
1127
0
        return 0;
1128
0
    *val &= op;
1129
0
    *ret = *val;
1130
1131
0
    if (!CRYPTO_THREAD_unlock(lock))
1132
0
        return 0;
1133
1134
0
    return 1;
1135
0
}
1136
1137
int CRYPTO_atomic_or(uint64_t *val, uint64_t op, uint64_t *ret,
1138
    CRYPTO_RWLOCK *lock)
1139
2
{
1140
2
#if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
1141
2
    if (__atomic_is_lock_free(sizeof(*val), val)) {
1142
2
        *ret = __atomic_or_fetch(val, op, __ATOMIC_ACQ_REL);
1143
2
        return 1;
1144
2
    }
1145
#elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
1146
    /* This will work for all future Solaris versions. */
1147
    if (ret != NULL) {
1148
        *ret = atomic_or_64_nv(val, op);
1149
        return 1;
1150
    }
1151
#endif
1152
0
    if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
1153
0
        return 0;
1154
0
    *val |= op;
1155
0
    *ret = *val;
1156
1157
0
    if (!CRYPTO_THREAD_unlock(lock))
1158
0
        return 0;
1159
1160
0
    return 1;
1161
0
}
1162
1163
int CRYPTO_atomic_load(uint64_t *val, uint64_t *ret, CRYPTO_RWLOCK *lock)
1164
1.11M
{
1165
1.11M
#if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
1166
1.11M
    if (__atomic_is_lock_free(sizeof(*val), val)) {
1167
1.11M
        __atomic_load(val, ret, __ATOMIC_ACQUIRE);
1168
1.11M
        return 1;
1169
1.11M
    }
1170
#elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
1171
    /* This will work for all future Solaris versions. */
1172
    if (ret != NULL) {
1173
        *ret = atomic_or_64_nv(val, 0);
1174
        return 1;
1175
    }
1176
#endif
1177
0
    if (lock == NULL || !CRYPTO_THREAD_read_lock(lock))
1178
0
        return 0;
1179
0
    *ret = *val;
1180
0
    if (!CRYPTO_THREAD_unlock(lock))
1181
0
        return 0;
1182
1183
0
    return 1;
1184
0
}
1185
1186
int CRYPTO_atomic_store(uint64_t *dst, uint64_t val, CRYPTO_RWLOCK *lock)
1187
298
{
1188
298
#if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
1189
298
    if (__atomic_is_lock_free(sizeof(*dst), dst)) {
1190
298
        __atomic_store(dst, &val, __ATOMIC_RELEASE);
1191
298
        return 1;
1192
298
    }
1193
#elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
1194
    /* This will work for all future Solaris versions. */
1195
    if (dst != NULL) {
1196
        atomic_swap_64(dst, val);
1197
        return 1;
1198
    }
1199
#endif
1200
0
    if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
1201
0
        return 0;
1202
0
    *dst = val;
1203
0
    if (!CRYPTO_THREAD_unlock(lock))
1204
0
        return 0;
1205
1206
0
    return 1;
1207
0
}
1208
1209
int CRYPTO_atomic_load_int(int *val, int *ret, CRYPTO_RWLOCK *lock)
1210
0
{
1211
0
#if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
1212
0
    if (__atomic_is_lock_free(sizeof(*val), val)) {
1213
0
        __atomic_load(val, ret, __ATOMIC_ACQUIRE);
1214
0
        return 1;
1215
0
    }
1216
#elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
1217
    /* This will work for all future Solaris versions. */
1218
    if (ret != NULL) {
1219
        *ret = (int)atomic_or_uint_nv((unsigned int *)val, 0);
1220
        return 1;
1221
    }
1222
#endif
1223
0
    if (lock == NULL || !CRYPTO_THREAD_read_lock(lock))
1224
0
        return 0;
1225
0
    *ret = *val;
1226
0
    if (!CRYPTO_THREAD_unlock(lock))
1227
0
        return 0;
1228
1229
0
    return 1;
1230
0
}
1231
1232
int CRYPTO_atomic_store_int(int *dst, int val, CRYPTO_RWLOCK *lock)
1233
0
{
1234
0
#if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
1235
0
    if (__atomic_is_lock_free(sizeof(*dst), dst)) {
1236
0
        __atomic_store(dst, &val, __ATOMIC_RELEASE);
1237
0
        return 1;
1238
0
    }
1239
#elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
1240
    /* This will work for all future Solaris versions. */
1241
    if (dst != NULL) {
1242
        atomic_swap_uint((unsigned int)dst, (unsigned int)val);
1243
        return 1;
1244
    }
1245
#endif
1246
0
    if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
1247
0
        return 0;
1248
0
    *dst = val;
1249
0
    if (!CRYPTO_THREAD_unlock(lock))
1250
0
        return 0;
1251
1252
0
    return 1;
1253
0
}
1254
1255
#ifndef FIPS_MODULE
1256
int openssl_init_fork_handlers(void)
1257
0
{
1258
0
    return 1;
1259
0
}
1260
#endif /* FIPS_MODULE */
1261
1262
int openssl_get_fork_id(void)
1263
0
{
1264
0
    return getpid();
1265
0
}
1266
#endif