Coverage Report

Created: 2025-10-10 07:01

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