Coverage Report

Created: 2026-02-14 07:20

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