Coverage Report

Created: 2025-12-10 06:24

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