Coverage Report

Created: 2026-04-01 06:39

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