Coverage Report

Created: 2026-02-22 06:11

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
2.19M
#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
1.31k
#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
9
{
468
9
    struct rcu_qp *new = OPENSSL_calloc(count, sizeof(*new));
469
470
9
    lock->group_count = count;
471
9
    return new;
472
9
}
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
2.19M
{
555
2.19M
    return ATOMIC_LOAD_N(pvoid, p, __ATOMIC_ACQUIRE);
556
2.19M
}
557
558
void ossl_rcu_assign_uptr(void **p, void **v)
559
1.31k
{
560
1.31k
    ATOMIC_STORE(pvoid, p, v, __ATOMIC_RELEASE);
561
1.31k
}
562
563
CRYPTO_RCU_LOCK *ossl_rcu_lock_new(int num_writers, OSSL_LIB_CTX *ctx)
564
9
{
565
9
    struct rcu_lock_st *new;
566
567
    /*
568
     * We need a minimum of 2 qp's
569
     */
570
9
    if (num_writers < 2)
571
9
        num_writers = 2;
572
573
9
    ctx = ossl_lib_ctx_get_concrete(ctx);
574
9
    if (ctx == NULL)
575
0
        return 0;
576
577
9
    new = OPENSSL_zalloc(sizeof(*new));
578
9
    if (new == NULL)
579
0
        return NULL;
580
581
9
    new->ctx = ctx;
582
9
    pthread_mutex_init(&new->write_lock, NULL);
583
9
    pthread_mutex_init(&new->prior_lock, NULL);
584
9
    pthread_mutex_init(&new->alloc_lock, NULL);
585
9
    pthread_cond_init(&new->prior_signal, NULL);
586
9
    pthread_cond_init(&new->alloc_signal, NULL);
587
588
9
    new->qp_group = allocate_new_qp_group(new, num_writers);
589
9
    if (new->qp_group == NULL) {
590
0
        OPENSSL_free(new);
591
0
        new = NULL;
592
0
    }
593
594
9
    return new;
595
9
}
596
597
void ossl_rcu_lock_free(CRYPTO_RCU_LOCK *lock)
598
0
{
599
0
    struct rcu_lock_st *rlock = (struct rcu_lock_st *)lock;
600
601
0
    if (lock == NULL)
602
0
        return;
603
604
    /* make sure we're synchronized */
605
0
    ossl_synchronize_rcu(rlock);
606
607
0
    OPENSSL_free(rlock->qp_group);
608
    /* There should only be a single qp left now */
609
0
    OPENSSL_free(rlock);
610
0
}
611
612
#ifdef REPORT_RWLOCK_CONTENTION
613
/*
614
 * Normally we would use a BIO here to do this, but we create locks during
615
 * library initialization, and creating a bio too early, creates a recursive set
616
 * of stack calls that leads us to call CRYPTO_thread_run_once while currently
617
 * executing the init routine for various run_once functions, which leads to
618
 * deadlock.  Avoid that by just using a FILE pointer.  Also note that we
619
 * directly use a pthread_mutex_t to protect access from multiple threads
620
 * to the contention log file.  We do this because we want to avoid use
621
 * of the CRYPTO_THREAD api so as to prevent recursive blocking reports.
622
 */
623
static CRYPTO_ONCE init_contention_data_flag = CRYPTO_ONCE_STATIC_INIT;
624
pthread_mutex_t log_lock = PTHREAD_MUTEX_INITIALIZER;
625
CRYPTO_THREAD_LOCAL thread_contention_data;
626
627
struct stack_info {
628
    unsigned int nptrs;
629
    int write;
630
    OSSL_TIME start;
631
    OSSL_TIME duration;
632
    char **strings;
633
};
634
635
#define STACKS_COUNT 32
636
#define BT_BUF_SIZE 1024
637
struct stack_traces {
638
    int fd;
639
    int lock_depth;
640
    size_t idx;
641
    struct stack_info stacks[STACKS_COUNT];
642
};
643
644
/* The glibc gettid() definition presents only since 2.30. */
645
static ossl_inline pid_t get_tid(void)
646
{
647
#ifdef OPENSSL_SYS_MACOSX
648
    /*
649
     * MACOS has the gettid call, but it does something completely different
650
     * here than on other unixes.  Specifically it returns the uid of the calling thread
651
     * (if set), or -1.  We need to use a MACOS specific call to get the thread id here
652
     */
653
    uint64_t tid;
654
655
    pthread_threadid_np(NULL, &tid);
656
    return (pid_t)tid;
657
#else
658
    return syscall(SYS_gettid);
659
#endif
660
}
661
662
#ifdef FIPS_MODULE
663
#define FIPS_SFX "-fips"
664
#else
665
#define FIPS_SFX ""
666
#endif
667
static void *init_contention_data(void)
668
{
669
    struct stack_traces *traces;
670
    char fname_fmt[] = "lock-contention-log" FIPS_SFX ".%d.txt";
671
    char fname[sizeof(fname_fmt) + sizeof(int) * 3];
672
673
    traces = OPENSSL_zalloc(sizeof(struct stack_traces));
674
675
    snprintf(fname, sizeof(fname), fname_fmt, get_tid());
676
677
    traces->fd = open(fname, O_WRONLY | O_APPEND | O_CLOEXEC | O_CREAT, 0600);
678
679
    return traces;
680
}
681
682
static void destroy_contention_data(void *data)
683
{
684
    struct stack_traces *st = data;
685
686
    close(st->fd);
687
    OPENSSL_free(data);
688
}
689
690
static void init_contention_data_once(void)
691
{
692
    /*
693
     * Create a thread local key here to store our list of stack traces
694
     * to be printed when we unlock the lock we are holding
695
     */
696
    CRYPTO_THREAD_init_local(&thread_contention_data, destroy_contention_data);
697
    return;
698
}
699
700
static struct stack_traces *get_stack_traces(bool init)
701
{
702
    struct stack_traces *traces = CRYPTO_THREAD_get_local(&thread_contention_data);
703
704
    if (!traces && init) {
705
        traces = init_contention_data();
706
        CRYPTO_THREAD_set_local(&thread_contention_data, traces);
707
    }
708
709
    return traces;
710
}
711
712
static void print_stack_traces(struct stack_traces *traces)
713
{
714
    unsigned int j;
715
    struct iovec *iov;
716
    int iovcnt;
717
718
    while (traces != NULL && traces->idx >= 1) {
719
        traces->idx--;
720
        dprintf(traces->fd,
721
            "lock blocked on %s for %zu usec at time %zu tid %d\n",
722
            traces->stacks[traces->idx].write == 1 ? "WRITE" : "READ",
723
            ossl_time2us(traces->stacks[traces->idx].duration),
724
            ossl_time2us(traces->stacks[traces->idx].start),
725
            get_tid());
726
        if (traces->stacks[traces->idx].strings != NULL) {
727
            static const char lf = '\n';
728
729
            iovcnt = traces->stacks[traces->idx].nptrs * 2 + 1;
730
            iov = alloca(iovcnt * sizeof(*iov));
731
            for (j = 0; j < traces->stacks[traces->idx].nptrs; j++) {
732
                iov[2 * j].iov_base = traces->stacks[traces->idx].strings[j];
733
                iov[2 * j].iov_len = strlen(traces->stacks[traces->idx].strings[j]);
734
                iov[2 * j + 1].iov_base = (char *)&lf;
735
                iov[2 * j + 1].iov_len = 1;
736
            }
737
            iov[traces->stacks[traces->idx].nptrs * 2].iov_base = (char *)&lf;
738
            iov[traces->stacks[traces->idx].nptrs * 2].iov_len = 1;
739
        } else {
740
            static const char no_bt[] = "No stack trace available\n\n";
741
742
            iovcnt = 1;
743
            iov = alloca(iovcnt * sizeof(*iov));
744
            iov[0].iov_base = (char *)no_bt;
745
            iov[0].iov_len = sizeof(no_bt) - 1;
746
        }
747
        writev(traces->fd, iov, iovcnt);
748
        free(traces->stacks[traces->idx].strings);
749
    }
750
}
751
752
static ossl_inline void ossl_init_rwlock_contention_data(void)
753
{
754
    CRYPTO_THREAD_run_once(&init_contention_data_flag, init_contention_data_once);
755
}
756
757
static int record_lock_contention(pthread_rwlock_t *lock,
758
    struct stack_traces *traces, bool write)
759
{
760
    void *buffer[BT_BUF_SIZE];
761
    OSSL_TIME start, end;
762
    int ret;
763
764
    start = ossl_time_now();
765
    ret = (write ? pthread_rwlock_wrlock : pthread_rwlock_rdlock)(lock);
766
    if (ret)
767
        return ret;
768
    end = ossl_time_now();
769
    traces->stacks[traces->idx].nptrs = backtrace(buffer, BT_BUF_SIZE);
770
    traces->stacks[traces->idx].strings = backtrace_symbols(buffer,
771
        traces->stacks[traces->idx].nptrs);
772
    traces->stacks[traces->idx].duration = ossl_time_subtract(end, start);
773
    traces->stacks[traces->idx].start = start;
774
    traces->stacks[traces->idx].write = write;
775
    traces->idx++;
776
    if (traces->idx >= STACKS_COUNT) {
777
        fprintf(stderr, "STACK RECORD OVERFLOW!\n");
778
        print_stack_traces(traces);
779
    }
780
781
    return 0;
782
}
783
784
static ossl_inline int ossl_rwlock_rdlock(pthread_rwlock_t *lock)
785
{
786
    struct stack_traces *traces = get_stack_traces(true);
787
788
    if (ossl_unlikely(traces == NULL))
789
        return ENOMEM;
790
791
    traces->lock_depth++;
792
    if (pthread_rwlock_tryrdlock(lock)) {
793
        int ret = record_lock_contention(lock, traces, false);
794
795
        if (ret)
796
            traces->lock_depth--;
797
798
        return ret;
799
    }
800
801
    return 0;
802
}
803
804
static ossl_inline int ossl_rwlock_wrlock(pthread_rwlock_t *lock)
805
{
806
    struct stack_traces *traces = get_stack_traces(true);
807
808
    if (ossl_unlikely(traces == NULL))
809
        return ENOMEM;
810
811
    traces->lock_depth++;
812
    if (pthread_rwlock_trywrlock(lock)) {
813
        int ret = record_lock_contention(lock, traces, true);
814
815
        if (ret)
816
            traces->lock_depth--;
817
818
        return ret;
819
    }
820
821
    return 0;
822
}
823
824
static ossl_inline int ossl_rwlock_unlock(pthread_rwlock_t *lock)
825
{
826
    int ret;
827
828
    ret = pthread_rwlock_unlock(lock);
829
    if (ret)
830
        return ret;
831
832
    {
833
        struct stack_traces *traces = get_stack_traces(false);
834
835
        if (traces != NULL) {
836
            traces->lock_depth--;
837
            assert(traces->lock_depth >= 0);
838
            if (traces->lock_depth == 0)
839
                print_stack_traces(traces);
840
        }
841
    }
842
843
    return 0;
844
}
845
846
#else /* !REPORT_RWLOCK_CONTENTION */
847
848
#if defined(USE_RWLOCK)
849
static ossl_inline void ossl_init_rwlock_contention_data(void)
850
336
{
851
336
}
852
853
static ossl_inline int ossl_rwlock_rdlock(pthread_rwlock_t *rwlock)
854
902k
{
855
902k
    return pthread_rwlock_rdlock(rwlock);
856
902k
}
857
858
static ossl_inline int ossl_rwlock_wrlock(pthread_rwlock_t *rwlock)
859
6.81k
{
860
6.81k
    return pthread_rwlock_wrlock(rwlock);
861
6.81k
}
862
863
static ossl_inline int ossl_rwlock_unlock(pthread_rwlock_t *rwlock)
864
909k
{
865
909k
    return pthread_rwlock_unlock(rwlock);
866
909k
}
867
#endif /* USE_RWLOCK */
868
#endif /* REPORT_RWLOCK_CONTENTION */
869
870
CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
871
336
{
872
336
#ifdef USE_RWLOCK
873
336
    CRYPTO_RWLOCK *lock;
874
875
336
    ossl_init_rwlock_contention_data();
876
877
336
    if ((lock = OPENSSL_zalloc(sizeof(pthread_rwlock_t))) == NULL)
878
        /* Don't set error, to avoid recursion blowup. */
879
0
        return NULL;
880
881
336
    if (pthread_rwlock_init(lock, NULL) != 0) {
882
0
        OPENSSL_free(lock);
883
0
        return NULL;
884
0
    }
885
#else
886
    pthread_mutexattr_t attr;
887
    CRYPTO_RWLOCK *lock;
888
889
    if ((lock = OPENSSL_zalloc(sizeof(pthread_mutex_t))) == NULL)
890
        /* Don't set error, to avoid recursion blowup. */
891
        return NULL;
892
893
    /*
894
     * We don't use recursive mutexes, but try to catch errors if we do.
895
     */
896
    pthread_mutexattr_init(&attr);
897
#if !defined(__TANDEM) && !defined(_SPT_MODEL_)
898
#if !defined(NDEBUG) && !defined(OPENSSL_NO_MUTEX_ERRORCHECK)
899
    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
900
#endif
901
#else
902
    /* The SPT Thread Library does not define MUTEX attributes. */
903
#endif
904
905
    if (pthread_mutex_init(lock, &attr) != 0) {
906
        pthread_mutexattr_destroy(&attr);
907
        OPENSSL_free(lock);
908
        return NULL;
909
    }
910
911
    pthread_mutexattr_destroy(&attr);
912
#endif
913
914
336
    return lock;
915
336
}
916
917
__owur int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
918
902k
{
919
902k
#ifdef USE_RWLOCK
920
902k
    if (!ossl_assert(ossl_rwlock_rdlock(lock) == 0))
921
0
        return 0;
922
#else
923
    if (pthread_mutex_lock(lock) != 0) {
924
        assert(errno != EDEADLK && errno != EBUSY);
925
        return 0;
926
    }
927
#endif
928
929
902k
    return 1;
930
902k
}
931
932
__owur int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
933
6.81k
{
934
6.81k
#ifdef USE_RWLOCK
935
6.81k
    if (!ossl_assert(ossl_rwlock_wrlock(lock) == 0))
936
0
        return 0;
937
#else
938
    if (pthread_mutex_lock(lock) != 0) {
939
        assert(errno != EDEADLK && errno != EBUSY);
940
        return 0;
941
    }
942
#endif
943
944
6.81k
    return 1;
945
6.81k
}
946
947
int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
948
909k
{
949
909k
#ifdef USE_RWLOCK
950
909k
    if (ossl_rwlock_unlock(lock) != 0)
951
0
        return 0;
952
#else
953
    if (pthread_mutex_unlock(lock) != 0) {
954
        assert(errno != EPERM);
955
        return 0;
956
    }
957
#endif
958
959
909k
    return 1;
960
909k
}
961
962
void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock)
963
0
{
964
0
    if (lock == NULL)
965
0
        return;
966
967
0
#ifdef USE_RWLOCK
968
0
    pthread_rwlock_destroy(lock);
969
#else
970
    pthread_mutex_destroy(lock);
971
#endif
972
0
    OPENSSL_free(lock);
973
974
0
    return;
975
0
}
976
977
int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))
978
13.3k
{
979
13.3k
    if (ossl_unlikely(pthread_once(once, init) != 0))
980
0
        return 0;
981
982
13.3k
    return 1;
983
13.3k
}
984
985
int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
986
12
{
987
12
    if (pthread_key_create(key, cleanup) != 0)
988
0
        return 0;
989
990
12
    return 1;
991
12
}
992
993
void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
994
5.18k
{
995
5.18k
    return pthread_getspecific(*key);
996
5.18k
}
997
998
int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
999
7
{
1000
7
    if (pthread_setspecific(*key, val) != 0)
1001
0
        return 0;
1002
1003
7
    return 1;
1004
7
}
1005
1006
int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
1007
0
{
1008
0
    if (pthread_key_delete(*key) != 0)
1009
0
        return 0;
1010
1011
0
    return 1;
1012
0
}
1013
1014
CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
1015
0
{
1016
0
    return pthread_self();
1017
0
}
1018
1019
int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
1020
0
{
1021
0
    return pthread_equal(a, b);
1022
0
}
1023
1024
int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
1025
3.49k
{
1026
3.49k
#if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
1027
3.49k
    if (__atomic_is_lock_free(sizeof(*val), val)) {
1028
3.49k
        *ret = __atomic_add_fetch(val, amount, __ATOMIC_ACQ_REL);
1029
3.49k
        return 1;
1030
3.49k
    }
1031
#elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
1032
    /* This will work for all future Solaris versions. */
1033
    if (ret != NULL) {
1034
        *ret = atomic_add_int_nv((volatile unsigned int *)val, amount);
1035
        return 1;
1036
    }
1037
#endif
1038
0
    if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
1039
0
        return 0;
1040
1041
0
    *val += amount;
1042
0
    *ret = *val;
1043
1044
0
    if (!CRYPTO_THREAD_unlock(lock))
1045
0
        return 0;
1046
1047
0
    return 1;
1048
0
}
1049
1050
int CRYPTO_atomic_add64(uint64_t *val, uint64_t op, uint64_t *ret,
1051
    CRYPTO_RWLOCK *lock)
1052
0
{
1053
0
#if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
1054
0
    if (__atomic_is_lock_free(sizeof(*val), val)) {
1055
0
        *ret = __atomic_add_fetch(val, op, __ATOMIC_ACQ_REL);
1056
0
        return 1;
1057
0
    }
1058
#elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
1059
    /* This will work for all future Solaris versions. */
1060
    if (ret != NULL) {
1061
        *ret = atomic_add_64_nv(val, op);
1062
        return 1;
1063
    }
1064
#endif
1065
0
    if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
1066
0
        return 0;
1067
0
    *val += op;
1068
0
    *ret = *val;
1069
1070
0
    if (!CRYPTO_THREAD_unlock(lock))
1071
0
        return 0;
1072
1073
0
    return 1;
1074
0
}
1075
1076
int CRYPTO_atomic_and(uint64_t *val, uint64_t op, uint64_t *ret,
1077
    CRYPTO_RWLOCK *lock)
1078
0
{
1079
0
#if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
1080
0
    if (__atomic_is_lock_free(sizeof(*val), val)) {
1081
0
        *ret = __atomic_and_fetch(val, op, __ATOMIC_ACQ_REL);
1082
0
        return 1;
1083
0
    }
1084
#elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
1085
    /* This will work for all future Solaris versions. */
1086
    if (ret != NULL) {
1087
        *ret = atomic_and_64_nv(val, op);
1088
        return 1;
1089
    }
1090
#endif
1091
0
    if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
1092
0
        return 0;
1093
0
    *val &= op;
1094
0
    *ret = *val;
1095
1096
0
    if (!CRYPTO_THREAD_unlock(lock))
1097
0
        return 0;
1098
1099
0
    return 1;
1100
0
}
1101
1102
int CRYPTO_atomic_or(uint64_t *val, uint64_t op, uint64_t *ret,
1103
    CRYPTO_RWLOCK *lock)
1104
7
{
1105
7
#if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
1106
7
    if (__atomic_is_lock_free(sizeof(*val), val)) {
1107
7
        *ret = __atomic_or_fetch(val, op, __ATOMIC_ACQ_REL);
1108
7
        return 1;
1109
7
    }
1110
#elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
1111
    /* This will work for all future Solaris versions. */
1112
    if (ret != NULL) {
1113
        *ret = atomic_or_64_nv(val, op);
1114
        return 1;
1115
    }
1116
#endif
1117
0
    if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
1118
0
        return 0;
1119
0
    *val |= op;
1120
0
    *ret = *val;
1121
1122
0
    if (!CRYPTO_THREAD_unlock(lock))
1123
0
        return 0;
1124
1125
0
    return 1;
1126
0
}
1127
1128
int CRYPTO_atomic_load(uint64_t *val, uint64_t *ret, CRYPTO_RWLOCK *lock)
1129
1.29M
{
1130
1.29M
#if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
1131
1.29M
    if (__atomic_is_lock_free(sizeof(*val), val)) {
1132
1.29M
        __atomic_load(val, ret, __ATOMIC_ACQUIRE);
1133
1.29M
        return 1;
1134
1.29M
    }
1135
#elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
1136
    /* This will work for all future Solaris versions. */
1137
    if (ret != NULL) {
1138
        *ret = atomic_or_64_nv(val, 0);
1139
        return 1;
1140
    }
1141
#endif
1142
0
    if (lock == NULL || !CRYPTO_THREAD_read_lock(lock))
1143
0
        return 0;
1144
0
    *ret = *val;
1145
0
    if (!CRYPTO_THREAD_unlock(lock))
1146
0
        return 0;
1147
1148
0
    return 1;
1149
0
}
1150
1151
int CRYPTO_atomic_store(uint64_t *dst, uint64_t val, CRYPTO_RWLOCK *lock)
1152
1.31k
{
1153
1.31k
#if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
1154
1.31k
    if (__atomic_is_lock_free(sizeof(*dst), dst)) {
1155
1.31k
        __atomic_store(dst, &val, __ATOMIC_RELEASE);
1156
1.31k
        return 1;
1157
1.31k
    }
1158
#elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
1159
    /* This will work for all future Solaris versions. */
1160
    if (dst != NULL) {
1161
        atomic_swap_64(dst, val);
1162
        return 1;
1163
    }
1164
#endif
1165
0
    if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
1166
0
        return 0;
1167
0
    *dst = val;
1168
0
    if (!CRYPTO_THREAD_unlock(lock))
1169
0
        return 0;
1170
1171
0
    return 1;
1172
0
}
1173
1174
int CRYPTO_atomic_load_int(int *val, int *ret, CRYPTO_RWLOCK *lock)
1175
0
{
1176
0
#if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
1177
0
    if (__atomic_is_lock_free(sizeof(*val), val)) {
1178
0
        __atomic_load(val, ret, __ATOMIC_ACQUIRE);
1179
0
        return 1;
1180
0
    }
1181
#elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
1182
    /* This will work for all future Solaris versions. */
1183
    if (ret != NULL) {
1184
        *ret = (int)atomic_or_uint_nv((unsigned int *)val, 0);
1185
        return 1;
1186
    }
1187
#endif
1188
0
    if (lock == NULL || !CRYPTO_THREAD_read_lock(lock))
1189
0
        return 0;
1190
0
    *ret = *val;
1191
0
    if (!CRYPTO_THREAD_unlock(lock))
1192
0
        return 0;
1193
1194
0
    return 1;
1195
0
}
1196
1197
int CRYPTO_atomic_store_int(int *dst, int val, CRYPTO_RWLOCK *lock)
1198
0
{
1199
0
#if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
1200
0
    if (__atomic_is_lock_free(sizeof(*dst), dst)) {
1201
0
        __atomic_store(dst, &val, __ATOMIC_RELEASE);
1202
0
        return 1;
1203
0
    }
1204
#elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
1205
    /* This will work for all future Solaris versions. */
1206
    if (dst != NULL) {
1207
        atomic_swap_uint((unsigned int)dst, (unsigned int)val);
1208
        return 1;
1209
    }
1210
#endif
1211
0
    if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
1212
0
        return 0;
1213
0
    *dst = val;
1214
0
    if (!CRYPTO_THREAD_unlock(lock))
1215
0
        return 0;
1216
1217
0
    return 1;
1218
0
}
1219
1220
#ifndef FIPS_MODULE
1221
int openssl_init_fork_handlers(void)
1222
0
{
1223
0
    return 1;
1224
0
}
1225
#endif /* FIPS_MODULE */
1226
1227
int openssl_get_fork_id(void)
1228
0
{
1229
0
    return getpid();
1230
0
}
1231
#endif