Coverage Report

Created: 2025-12-31 06:58

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_) \
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
76.6M
#define ATOMIC_LOAD_N(t, p, o) __atomic_load_n(p, o)
130
934
#define ATOMIC_STORE_N(t, p, v, o) __atomic_store_n(p, v, o)
131
39.4k
#define ATOMIC_STORE(t, p, v, o) __atomic_store(p, v, o)
132
998
#define ATOMIC_ADD_FETCH(p, v, o) __atomic_add_fetch(p, v, o)
133
64
#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
460
#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
64
{
293
64
    uint32_t qp_idx;
294
295
    /* get the current qp index */
296
64
    for (;;) {
297
64
        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
64
        ATOMIC_ADD_FETCH(&lock->qp_group[qp_idx].users, (uint64_t)1,
311
64
            __ATOMIC_ACQUIRE);
312
313
        /* if the idx hasn't changed, we're good, else try again */
314
64
        if (qp_idx == ATOMIC_LOAD_N(uint32_t, &lock->reader_idx, __ATOMIC_ACQUIRE))
315
64
            break;
316
317
0
        ATOMIC_SUB_FETCH(&lock->qp_group[qp_idx].users, (uint64_t)1,
318
0
            __ATOMIC_RELAXED);
319
0
    }
320
321
64
    return &lock->qp_group[qp_idx];
322
64
}
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
36
{
335
36
    struct rcu_thr_data *data;
336
36
    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
36
    data = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_RCU_KEY, lock->ctx);
343
344
36
    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
396
    for (i = 0; i < MAX_QPS; i++) {
361
360
        if (data->thread_qps[i].qp == NULL && available_qp == -1)
362
36
            available_qp = i;
363
        /* If we have a hold on this lock already, we're good */
364
360
        if (data->thread_qps[i].lock == lock) {
365
0
            data->thread_qps[i].depth++;
366
0
            return 1;
367
0
        }
368
360
    }
369
370
    /*
371
     * if we get here, then we don't have a hold on this lock yet
372
     */
373
36
    assert(available_qp != -1);
374
375
36
    data->thread_qps[available_qp].qp = get_hold_current_qp(lock);
376
36
    data->thread_qps[available_qp].depth = 1;
377
36
    data->thread_qps[available_qp].lock = lock;
378
36
    return 1;
379
36
}
380
381
void ossl_rcu_read_unlock(CRYPTO_RCU_LOCK *lock)
382
64
{
383
64
    int i;
384
64
    struct rcu_thr_data *data = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_RCU_KEY, lock->ctx);
385
64
    uint64_t ret;
386
387
64
    assert(data != NULL);
388
389
64
    for (i = 0; i < MAX_QPS; i++) {
390
64
        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
64
            data->thread_qps[i].depth--;
397
64
            if (data->thread_qps[i].depth == 0) {
398
64
                ret = ATOMIC_SUB_FETCH(&data->thread_qps[i].qp->users,
399
64
                    (uint64_t)1, __ATOMIC_RELEASE);
400
64
                OPENSSL_assert(ret != UINT64_MAX);
401
64
                data->thread_qps[i].qp = NULL;
402
64
                data->thread_qps[i].lock = NULL;
403
64
            }
404
64
            return;
405
64
        }
406
64
    }
407
    /*
408
     * If we get here, we're trying to unlock a lock that we never acquired -
409
     * that's fatal.
410
     */
411
64
    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
934
{
420
934
    uint32_t current_idx;
421
422
934
    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
934
    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
934
    current_idx = lock->current_alloc_idx;
434
435
    /* Allocate the qp */
436
934
    lock->writers_alloced++;
437
438
    /* increment the allocation index */
439
934
    lock->current_alloc_idx = (lock->current_alloc_idx + 1) % lock->group_count;
440
441
934
    *curr_id = lock->id_ctr;
442
934
    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
934
    ATOMIC_STORE_N(uint32_t, &lock->reader_idx, lock->current_alloc_idx,
449
934
        __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
934
    ATOMIC_ADD_FETCH(&lock->qp_group[current_idx].users, (uint64_t)0,
456
934
        __ATOMIC_RELEASE);
457
458
    /* wake up any waiters */
459
934
    pthread_cond_signal(&lock->alloc_signal);
460
934
    pthread_mutex_unlock(&lock->alloc_lock);
461
934
    return &lock->qp_group[current_idx];
462
934
}
463
464
static void retire_qp(CRYPTO_RCU_LOCK *lock, struct rcu_qp *qp)
465
934
{
466
934
    pthread_mutex_lock(&lock->alloc_lock);
467
934
    lock->writers_alloced--;
468
934
    pthread_cond_signal(&lock->alloc_signal);
469
934
    pthread_mutex_unlock(&lock->alloc_lock);
470
934
}
471
472
static struct rcu_qp *allocate_new_qp_group(CRYPTO_RCU_LOCK *lock,
473
    uint32_t count)
474
516
{
475
516
    struct rcu_qp *new = OPENSSL_calloc(count, sizeof(*new));
476
477
516
    lock->group_count = count;
478
516
    return new;
479
516
}
480
481
void ossl_rcu_write_lock(CRYPTO_RCU_LOCK *lock)
482
710
{
483
710
    pthread_mutex_lock(&lock->write_lock);
484
710
    TSAN_FAKE_UNLOCK(&lock->write_lock);
485
710
}
486
487
void ossl_rcu_write_unlock(CRYPTO_RCU_LOCK *lock)
488
710
{
489
710
    TSAN_FAKE_LOCK(&lock->write_lock);
490
710
    pthread_mutex_unlock(&lock->write_lock);
491
710
}
492
493
void ossl_synchronize_rcu(CRYPTO_RCU_LOCK *lock)
494
934
{
495
934
    struct rcu_qp *qp;
496
934
    uint64_t count;
497
934
    uint32_t curr_id;
498
934
    struct rcu_cb_item *cb_items, *tmpcb;
499
500
934
    pthread_mutex_lock(&lock->write_lock);
501
934
    cb_items = lock->cb_items;
502
934
    lock->cb_items = NULL;
503
934
    pthread_mutex_unlock(&lock->write_lock);
504
505
934
    qp = update_qp(lock, &curr_id);
506
507
    /* retire in order */
508
934
    pthread_mutex_lock(&lock->prior_lock);
509
934
    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
934
    do {
522
934
        count = ATOMIC_LOAD_N(uint64_t, &qp->users, __ATOMIC_ACQUIRE);
523
934
    } while (count != (uint64_t)0);
524
525
934
    lock->next_to_retire++;
526
934
    pthread_cond_broadcast(&lock->prior_signal);
527
934
    pthread_mutex_unlock(&lock->prior_lock);
528
529
934
    retire_qp(lock, qp);
530
531
    /* handle any callbacks that we have */
532
1.13k
    while (cb_items != NULL) {
533
200
        tmpcb = cb_items;
534
200
        cb_items = cb_items->next;
535
200
        tmpcb->fn(tmpcb->data);
536
200
        OPENSSL_free(tmpcb);
537
200
    }
538
934
}
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
200
{
546
200
    struct rcu_cb_item *new = OPENSSL_zalloc(sizeof(*new));
547
548
200
    if (new == NULL)
549
0
        return 0;
550
551
200
    new->data = data;
552
200
    new->fn = cb;
553
554
200
    new->next = lock->cb_items;
555
200
    lock->cb_items = new;
556
557
200
    return 1;
558
200
}
559
560
void *ossl_rcu_uptr_deref(void **p)
561
76.6M
{
562
76.6M
    return ATOMIC_LOAD_N(pvoid, p, __ATOMIC_ACQUIRE);
563
76.6M
}
564
565
void ossl_rcu_assign_uptr(void **p, void **v)
566
39.4k
{
567
39.4k
    ATOMIC_STORE(pvoid, p, v, __ATOMIC_RELEASE);
568
39.4k
}
569
570
CRYPTO_RCU_LOCK *ossl_rcu_lock_new(int num_writers, OSSL_LIB_CTX *ctx)
571
516
{
572
516
    struct rcu_lock_st *new;
573
574
    /*
575
     * We need a minimum of 2 qp's
576
     */
577
516
    if (num_writers < 2)
578
516
        num_writers = 2;
579
580
516
    ctx = ossl_lib_ctx_get_concrete(ctx);
581
516
    if (ctx == NULL)
582
0
        return 0;
583
584
516
    new = OPENSSL_zalloc(sizeof(*new));
585
516
    if (new == NULL)
586
0
        return NULL;
587
588
516
    new->ctx = ctx;
589
516
    pthread_mutex_init(&new->write_lock, NULL);
590
516
    pthread_mutex_init(&new->prior_lock, NULL);
591
516
    pthread_mutex_init(&new->alloc_lock, NULL);
592
516
    pthread_cond_init(&new->prior_signal, NULL);
593
516
    pthread_cond_init(&new->alloc_signal, NULL);
594
595
516
    new->qp_group = allocate_new_qp_group(new, num_writers);
596
516
    if (new->qp_group == NULL) {
597
0
        OPENSSL_free(new);
598
0
        new = NULL;
599
0
    }
600
601
516
    return new;
602
516
}
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
static ossl_inline void ossl_init_rwlock_contention_data(void)
844
2.03M
{
845
2.03M
}
846
847
static ossl_inline int ossl_rwlock_rdlock(pthread_rwlock_t *rwlock)
848
57.4M
{
849
57.4M
    return pthread_rwlock_rdlock(rwlock);
850
57.4M
}
851
852
static ossl_inline int ossl_rwlock_wrlock(pthread_rwlock_t *rwlock)
853
37.8M
{
854
37.8M
    return pthread_rwlock_wrlock(rwlock);
855
37.8M
}
856
857
static ossl_inline int ossl_rwlock_unlock(pthread_rwlock_t *rwlock)
858
95.3M
{
859
95.3M
    return pthread_rwlock_unlock(rwlock);
860
95.3M
}
861
#endif /* REPORT_RWLOCK_CONTENTION */
862
863
CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
864
10.3M
{
865
10.3M
#ifdef USE_RWLOCK
866
10.3M
    CRYPTO_RWLOCK *lock;
867
868
10.3M
    ossl_init_rwlock_contention_data();
869
870
10.3M
    if ((lock = OPENSSL_zalloc(sizeof(pthread_rwlock_t))) == NULL)
871
        /* Don't set error, to avoid recursion blowup. */
872
0
        return NULL;
873
874
10.3M
    if (pthread_rwlock_init(lock, NULL) != 0) {
875
0
        OPENSSL_free(lock);
876
0
        return NULL;
877
0
    }
878
#else
879
    pthread_mutexattr_t attr;
880
    CRYPTO_RWLOCK *lock;
881
882
    if ((lock = OPENSSL_zalloc(sizeof(pthread_mutex_t))) == NULL)
883
        /* Don't set error, to avoid recursion blowup. */
884
        return NULL;
885
886
    /*
887
     * We don't use recursive mutexes, but try to catch errors if we do.
888
     */
889
    pthread_mutexattr_init(&attr);
890
#if !defined(__TANDEM) && !defined(_SPT_MODEL_)
891
#if !defined(NDEBUG) && !defined(OPENSSL_NO_MUTEX_ERRORCHECK)
892
    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
893
#endif
894
#else
895
    /* The SPT Thread Library does not define MUTEX attributes. */
896
#endif
897
898
    if (pthread_mutex_init(lock, &attr) != 0) {
899
        pthread_mutexattr_destroy(&attr);
900
        OPENSSL_free(lock);
901
        return NULL;
902
    }
903
904
    pthread_mutexattr_destroy(&attr);
905
#endif
906
907
10.3M
    return lock;
908
10.3M
}
909
910
__owur int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
911
89.2M
{
912
89.2M
#ifdef USE_RWLOCK
913
89.2M
    if (!ossl_assert(ossl_rwlock_rdlock(lock) == 0))
914
0
        return 0;
915
#else
916
    if (pthread_mutex_lock(lock) != 0) {
917
        assert(errno != EDEADLK && errno != EBUSY);
918
        return 0;
919
    }
920
#endif
921
922
89.2M
    return 1;
923
89.2M
}
924
925
__owur int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
926
52.1M
{
927
52.1M
#ifdef USE_RWLOCK
928
52.1M
    if (!ossl_assert(ossl_rwlock_wrlock(lock) == 0))
929
0
        return 0;
930
#else
931
    if (pthread_mutex_lock(lock) != 0) {
932
        assert(errno != EDEADLK && errno != EBUSY);
933
        return 0;
934
    }
935
#endif
936
937
52.1M
    return 1;
938
52.1M
}
939
940
int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
941
1.31G
{
942
1.31G
#ifdef USE_RWLOCK
943
1.31G
    if (ossl_rwlock_unlock(lock) != 0)
944
0
        return 0;
945
#else
946
    if (pthread_mutex_unlock(lock) != 0) {
947
        assert(errno != EPERM);
948
        return 0;
949
    }
950
#endif
951
952
1.31G
    return 1;
953
1.31G
}
954
955
void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock)
956
10.3M
{
957
10.3M
    if (lock == NULL)
958
2.35k
        return;
959
960
10.3M
#ifdef USE_RWLOCK
961
10.3M
    pthread_rwlock_destroy(lock);
962
#else
963
    pthread_mutex_destroy(lock);
964
#endif
965
10.3M
    OPENSSL_free(lock);
966
967
10.3M
    return;
968
10.3M
}
969
970
int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))
971
1.82G
{
972
1.82G
    if (ossl_unlikely(pthread_once(once, init) != 0))
973
0
        return 0;
974
975
1.82G
    return 1;
976
1.82G
}
977
978
int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
979
1.54k
{
980
981
1.54k
#ifndef FIPS_MODULE
982
1.54k
    if (!ossl_init_thread())
983
0
        return 0;
984
1.54k
#endif
985
986
1.54k
    if (pthread_key_create(key, cleanup) != 0)
987
0
        return 0;
988
989
1.54k
    return 1;
990
1.54k
}
991
992
void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
993
2.30G
{
994
2.30G
    return pthread_getspecific(*key);
995
2.30G
}
996
997
int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
998
1.78k
{
999
1.78k
    if (pthread_setspecific(*key, val) != 0)
1000
0
        return 0;
1001
1002
1.78k
    return 1;
1003
1.78k
}
1004
1005
int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
1006
1.38k
{
1007
1.38k
    if (pthread_key_delete(*key) != 0)
1008
0
        return 0;
1009
1010
1.38k
    return 1;
1011
1.38k
}
1012
1013
CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
1014
208k
{
1015
208k
    return pthread_self();
1016
208k
}
1017
1018
int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
1019
12.5k
{
1020
12.5k
    return pthread_equal(a, b);
1021
12.5k
}
1022
1023
int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
1024
11.7M
{
1025
11.7M
#if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
1026
11.7M
    if (__atomic_is_lock_free(sizeof(*val), val)) {
1027
11.7M
        *ret = __atomic_add_fetch(val, amount, __ATOMIC_ACQ_REL);
1028
11.7M
        return 1;
1029
11.7M
    }
1030
#elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
1031
    /* This will work for all future Solaris versions. */
1032
    if (ret != NULL) {
1033
        *ret = atomic_add_int_nv((volatile unsigned int *)val, amount);
1034
        return 1;
1035
    }
1036
#endif
1037
0
    if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
1038
0
        return 0;
1039
1040
0
    *val += amount;
1041
0
    *ret = *val;
1042
1043
0
    if (!CRYPTO_THREAD_unlock(lock))
1044
0
        return 0;
1045
1046
0
    return 1;
1047
0
}
1048
1049
int CRYPTO_atomic_add64(uint64_t *val, uint64_t op, uint64_t *ret,
1050
    CRYPTO_RWLOCK *lock)
1051
0
{
1052
0
#if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
1053
0
    if (__atomic_is_lock_free(sizeof(*val), val)) {
1054
0
        *ret = __atomic_add_fetch(val, op, __ATOMIC_ACQ_REL);
1055
0
        return 1;
1056
0
    }
1057
#elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
1058
    /* This will work for all future Solaris versions. */
1059
    if (ret != NULL) {
1060
        *ret = atomic_add_64_nv(val, op);
1061
        return 1;
1062
    }
1063
#endif
1064
0
    if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
1065
0
        return 0;
1066
0
    *val += op;
1067
0
    *ret = *val;
1068
1069
0
    if (!CRYPTO_THREAD_unlock(lock))
1070
0
        return 0;
1071
1072
0
    return 1;
1073
0
}
1074
1075
int CRYPTO_atomic_and(uint64_t *val, uint64_t op, uint64_t *ret,
1076
    CRYPTO_RWLOCK *lock)
1077
0
{
1078
0
#if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
1079
0
    if (__atomic_is_lock_free(sizeof(*val), val)) {
1080
0
        *ret = __atomic_and_fetch(val, op, __ATOMIC_ACQ_REL);
1081
0
        return 1;
1082
0
    }
1083
#elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
1084
    /* This will work for all future Solaris versions. */
1085
    if (ret != NULL) {
1086
        *ret = atomic_and_64_nv(val, op);
1087
        return 1;
1088
    }
1089
#endif
1090
0
    if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
1091
0
        return 0;
1092
0
    *val &= op;
1093
0
    *ret = *val;
1094
1095
0
    if (!CRYPTO_THREAD_unlock(lock))
1096
0
        return 0;
1097
1098
0
    return 1;
1099
0
}
1100
1101
int CRYPTO_atomic_or(uint64_t *val, uint64_t op, uint64_t *ret,
1102
    CRYPTO_RWLOCK *lock)
1103
712
{
1104
712
#if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
1105
712
    if (__atomic_is_lock_free(sizeof(*val), val)) {
1106
712
        *ret = __atomic_or_fetch(val, op, __ATOMIC_ACQ_REL);
1107
712
        return 1;
1108
712
    }
1109
#elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
1110
    /* This will work for all future Solaris versions. */
1111
    if (ret != NULL) {
1112
        *ret = atomic_or_64_nv(val, op);
1113
        return 1;
1114
    }
1115
#endif
1116
0
    if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
1117
0
        return 0;
1118
0
    *val |= op;
1119
0
    *ret = *val;
1120
1121
0
    if (!CRYPTO_THREAD_unlock(lock))
1122
0
        return 0;
1123
1124
0
    return 1;
1125
0
}
1126
1127
int CRYPTO_atomic_load(uint64_t *val, uint64_t *ret, CRYPTO_RWLOCK *lock)
1128
3.12G
{
1129
3.12G
#if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
1130
3.12G
    if (__atomic_is_lock_free(sizeof(*val), val)) {
1131
3.12G
        __atomic_load(val, ret, __ATOMIC_ACQUIRE);
1132
3.12G
        return 1;
1133
3.12G
    }
1134
#elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
1135
    /* This will work for all future Solaris versions. */
1136
    if (ret != NULL) {
1137
        *ret = atomic_or_64_nv(val, 0);
1138
        return 1;
1139
    }
1140
#endif
1141
0
    if (lock == NULL || !CRYPTO_THREAD_read_lock(lock))
1142
0
        return 0;
1143
0
    *ret = *val;
1144
0
    if (!CRYPTO_THREAD_unlock(lock))
1145
0
        return 0;
1146
1147
0
    return 1;
1148
0
}
1149
1150
int CRYPTO_atomic_store(uint64_t *dst, uint64_t val, CRYPTO_RWLOCK *lock)
1151
38.9k
{
1152
38.9k
#if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
1153
38.9k
    if (__atomic_is_lock_free(sizeof(*dst), dst)) {
1154
38.9k
        __atomic_store(dst, &val, __ATOMIC_RELEASE);
1155
38.9k
        return 1;
1156
38.9k
    }
1157
#elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
1158
    /* This will work for all future Solaris versions. */
1159
    if (dst != NULL) {
1160
        atomic_swap_64(dst, val);
1161
        return 1;
1162
    }
1163
#endif
1164
0
    if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
1165
0
        return 0;
1166
0
    *dst = val;
1167
0
    if (!CRYPTO_THREAD_unlock(lock))
1168
0
        return 0;
1169
1170
0
    return 1;
1171
0
}
1172
1173
int CRYPTO_atomic_load_int(int *val, int *ret, CRYPTO_RWLOCK *lock)
1174
0
{
1175
0
#if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
1176
0
    if (__atomic_is_lock_free(sizeof(*val), val)) {
1177
0
        __atomic_load(val, ret, __ATOMIC_ACQUIRE);
1178
0
        return 1;
1179
0
    }
1180
#elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
1181
    /* This will work for all future Solaris versions. */
1182
    if (ret != NULL) {
1183
        *ret = (int)atomic_or_uint_nv((unsigned int *)val, 0);
1184
        return 1;
1185
    }
1186
#endif
1187
0
    if (lock == NULL || !CRYPTO_THREAD_read_lock(lock))
1188
0
        return 0;
1189
0
    *ret = *val;
1190
0
    if (!CRYPTO_THREAD_unlock(lock))
1191
0
        return 0;
1192
1193
0
    return 1;
1194
0
}
1195
1196
#ifndef FIPS_MODULE
1197
int openssl_init_fork_handlers(void)
1198
0
{
1199
0
    return 1;
1200
0
}
1201
#endif /* FIPS_MODULE */
1202
1203
int openssl_get_fork_id(void)
1204
116k
{
1205
116k
    return getpid();
1206
116k
}
1207
#endif