Coverage Report

Created: 2024-02-25 06:25

/src/openssl/crypto/threads_pthread.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2016-2023 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
#include <openssl/crypto.h>
14
#include <crypto/cryptlib.h>
15
#include "internal/cryptlib.h"
16
#include "internal/rcu.h"
17
#include "rcu_internal.h"
18
19
#if defined(__sun)
20
# include <atomic.h>
21
#endif
22
23
#if defined(__apple_build_version__) && __apple_build_version__ < 6000000
24
/*
25
 * OS/X 10.7 and 10.8 had a weird version of clang which has __ATOMIC_ACQUIRE and
26
 * __ATOMIC_ACQ_REL but which expects only one parameter for __atomic_is_lock_free()
27
 * rather than two which has signature __atomic_is_lock_free(sizeof(_Atomic(T))).
28
 * All of this makes impossible to use __atomic_is_lock_free here.
29
 *
30
 * See: https://github.com/llvm/llvm-project/commit/a4c2602b714e6c6edb98164550a5ae829b2de760
31
 */
32
#define BROKEN_CLANG_ATOMICS
33
#endif
34
35
#if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG) && !defined(OPENSSL_SYS_WINDOWS)
36
37
# if defined(OPENSSL_SYS_UNIX)
38
#  include <sys/types.h>
39
#  include <unistd.h>
40
#endif
41
42
# include <assert.h>
43
44
# ifdef PTHREAD_RWLOCK_INITIALIZER
45
#  define USE_RWLOCK
46
# endif
47
48
# if defined(__GNUC__) && defined(__ATOMIC_ACQUIRE) && !defined(BROKEN_CLANG_ATOMICS)
49
80
# define ATOMIC_LOAD_N(p,o) __atomic_load_n(p, o)
50
48
# define ATOMIC_STORE_N(p, v, o) __atomic_store_n(p, v, o)
51
32
# define ATOMIC_STORE(p, v, o) __atomic_store(p, v, o)
52
48
# define ATOMIC_EXCHANGE_N(p, v, o) __atomic_exchange_n(p, v, o)
53
0
# define ATOMIC_ADD_FETCH(p, v, o) __atomic_add_fetch(p, v, o)
54
# define ATOMIC_FETCH_ADD(p, v, o) __atomic_fetch_add(p, v, o)
55
0
# define ATOMIC_SUB_FETCH(p, v, o) __atomic_sub_fetch(p, v, o)
56
48
# define ATOMIC_AND_FETCH(p, m, o) __atomic_and_fetch(p, m, o)
57
48
# define ATOMIC_OR_FETCH(p, m, o) __atomic_or_fetch(p, m, o)
58
#else
59
static pthread_mutex_t atomic_sim_lock = PTHREAD_MUTEX_INITIALIZER;
60
61
static inline void *fallback_atomic_load_n(void **p)
62
{
63
    void *ret;
64
65
    pthread_mutex_lock(&atomic_sim_lock);
66
    ret = *(void **)p;
67
    pthread_mutex_unlock(&atomic_sim_lock);
68
    return ret;
69
}
70
71
# define ATOMIC_LOAD_N(p, o) fallback_atomic_load_n((void **)p)
72
73
static inline void *fallback_atomic_store_n(void **p, void *v)
74
{
75
    void *ret;
76
77
    pthread_mutex_lock(&atomic_sim_lock);
78
    ret = *p;
79
    *p = v;
80
    pthread_mutex_unlock(&atomic_sim_lock);
81
    return ret;
82
}
83
84
# define ATOMIC_STORE_N(p, v, o) fallback_atomic_store_n((void **)p, (void *)v)
85
86
static inline void fallback_atomic_store(void **p, void **v)
87
{
88
    void *ret;
89
90
    pthread_mutex_lock(&atomic_sim_lock);
91
    ret = *p;
92
    *p = *v;
93
    v = ret;
94
    pthread_mutex_unlock(&atomic_sim_lock);
95
}
96
97
# define ATOMIC_STORE(p, v, o) fallback_atomic_store((void **)p, (void **)v)
98
99
static inline void *fallback_atomic_exchange_n(void **p, void *v)
100
{
101
    void *ret;
102
103
    pthread_mutex_lock(&atomic_sim_lock);
104
    ret = *p;
105
    *p = v;
106
    pthread_mutex_unlock(&atomic_sim_lock);
107
    return ret;
108
}
109
110
#define ATOMIC_EXCHANGE_N(p, v, o) fallback_atomic_exchange_n((void **)p, (void *)v)
111
112
static inline uint64_t fallback_atomic_add_fetch(uint64_t *p, uint64_t v)
113
{
114
    uint64_t ret;
115
116
    pthread_mutex_lock(&atomic_sim_lock);
117
    *p += v;
118
    ret = *p;
119
    pthread_mutex_unlock(&atomic_sim_lock);
120
    return ret;
121
}
122
123
# define ATOMIC_ADD_FETCH(p, v, o) fallback_atomic_add_fetch(p, v)
124
125
static inline uint64_t fallback_atomic_fetch_add(uint64_t *p, uint64_t v)
126
{
127
    uint64_t ret;
128
129
    pthread_mutex_lock(&atomic_sim_lock);
130
    ret = *p;
131
    *p += v;
132
    pthread_mutex_unlock(&atomic_sim_lock);
133
    return ret;
134
}
135
136
# define ATOMIC_FETCH_ADD(p, v, o) fallback_atomic_fetch_add(p, v)
137
138
static inline uint64_t fallback_atomic_sub_fetch(uint64_t *p, uint64_t v)
139
{
140
    uint64_t ret;
141
142
    pthread_mutex_lock(&atomic_sim_lock);
143
    *p -= v;
144
    ret = *p;
145
    pthread_mutex_unlock(&atomic_sim_lock);
146
    return ret;
147
}
148
149
# define ATOMIC_SUB_FETCH(p, v, o) fallback_atomic_sub_fetch(p, v)
150
151
static inline uint64_t fallback_atomic_and_fetch(uint64_t *p, uint64_t m)
152
{
153
    uint64_t ret;
154
155
    pthread_mutex_lock(&atomic_sim_lock);
156
    *p &= m;
157
    ret = *p;
158
    pthread_mutex_unlock(&atomic_sim_lock);
159
    return ret;
160
}
161
162
# define ATOMIC_AND_FETCH(p, v, o) fallback_atomic_and_fetch(p, v)
163
164
static inline uint64_t fallback_atomic_or_fetch(uint64_t *p, uint64_t m)
165
{
166
    uint64_t ret;
167
168
    pthread_mutex_lock(&atomic_sim_lock);
169
    *p |= m;
170
    ret = *p;
171
    pthread_mutex_unlock(&atomic_sim_lock);
172
    return ret;
173
}
174
175
# define ATOMIC_OR_FETCH(p, v, o) fallback_atomic_or_fetch(p, v)
176
#endif
177
178
static CRYPTO_THREAD_LOCAL rcu_thr_key;
179
180
/*
181
 * users is broken up into 2 parts
182
 * bits 0-15 current readers
183
 * bit 32-63 - ID
184
 */
185
48
# define READER_SHIFT 0
186
96
# define ID_SHIFT 32
187
48
# define READER_SIZE 16
188
48
# define ID_SIZE 32
189
190
48
# define READER_MASK     (((uint64_t)1 << READER_SIZE) - 1)
191
48
# define ID_MASK         (((uint64_t)1 << ID_SIZE) - 1)
192
48
# define READER_COUNT(x) (((uint64_t)(x) >> READER_SHIFT) & READER_MASK)
193
48
# define ID_VAL(x)       (((uint64_t)(x) >> ID_SHIFT) & ID_MASK)
194
# define VAL_READER      ((uint64_t)1 << READER_SHIFT)
195
48
# define VAL_ID(x)       ((uint64_t)x << ID_SHIFT)
196
197
/*
198
 * This is the core of an rcu lock. It tracks the readers and writers for the
199
 * current quiescence point for a given lock. Users is the 64 bit value that
200
 * stores the READERS/ID as defined above
201
 *
202
 */
203
struct rcu_qp {
204
    uint64_t users;
205
};
206
207
struct thread_qp {
208
    struct rcu_qp *qp;
209
    unsigned int depth;
210
    CRYPTO_RCU_LOCK *lock;
211
};
212
213
0
#define MAX_QPS 10
214
/*
215
 * This is the per thread tracking data
216
 * that is assigned to each thread participating
217
 * in an rcu qp
218
 *
219
 * qp points to the qp that it last acquired
220
 *
221
 */
222
struct rcu_thr_data {
223
    struct thread_qp thread_qps[MAX_QPS];
224
};
225
226
/*
227
 * This is the internal version of a CRYPTO_RCU_LOCK
228
 * it is cast from CRYPTO_RCU_LOCK
229
 */
230
struct rcu_lock_st {
231
    /* Callbacks to call for next ossl_synchronize_rcu */
232
    struct rcu_cb_item *cb_items;
233
234
    /* rcu generation counter for in-order retirement */
235
    uint32_t id_ctr;
236
237
    /* Array of quiescent points for synchronization */
238
    struct rcu_qp *qp_group;
239
240
    /* Number of elements in qp_group array */
241
    size_t group_count;
242
243
    /* Index of the current qp in the qp_group array */
244
    uint64_t reader_idx;
245
246
    /* value of the next id_ctr value to be retired */
247
    uint32_t next_to_retire;
248
249
    /* index of the next free rcu_qp in the qp_group */
250
    uint64_t current_alloc_idx;
251
252
    /* number of qp's in qp_group array currently being retired */
253
    uint32_t writers_alloced;
254
255
    /* lock protecting write side operations */
256
    pthread_mutex_t write_lock;
257
258
    /* lock protecting updates to writers_alloced/current_alloc_idx */
259
    pthread_mutex_t alloc_lock;
260
261
    /* signal to wake threads waiting on alloc_lock */
262
    pthread_cond_t alloc_signal;
263
264
    /* lock to enforce in-order retirement */
265
    pthread_mutex_t prior_lock;
266
267
    /* signal to wake threads waiting on prior_lock */
268
    pthread_cond_t prior_signal;
269
};
270
271
/*
272
 * Called on thread exit to free the pthread key
273
 * associated with this thread, if any
274
 */
275
static void free_rcu_thr_data(void *ptr)
276
0
{
277
0
    struct rcu_thr_data *data =
278
0
                        (struct rcu_thr_data *)CRYPTO_THREAD_get_local(&rcu_thr_key);
279
280
0
    OPENSSL_free(data);
281
0
    CRYPTO_THREAD_set_local(&rcu_thr_key, NULL);
282
0
}
283
284
static void ossl_rcu_init(void)
285
16
{
286
16
    CRYPTO_THREAD_init_local(&rcu_thr_key, NULL);
287
16
}
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
    uint64_t qp_idx;
293
294
    /* get the current qp index */
295
0
    for (;;) {
296
        /*
297
         * Notes on use of __ATOMIC_ACQUIRE
298
         * We need to ensure the following:
299
         * 1) That subsequent operations aren't optimized by hoisting them above
300
         * this operation.  Specifically, we don't want the below re-load of
301
         * qp_idx to get optimized away
302
         * 2) We want to ensure that any updating of reader_idx on the write side
303
         * of the lock is flushed from a local cpu cache so that we see any
304
         * updates prior to the load.  This is a non-issue on cache coherent
305
         * systems like x86, but is relevant on other arches
306
         * Note: This applies to the reload below as well
307
         */
308
0
        qp_idx = (uint64_t)ATOMIC_LOAD_N(&lock->reader_idx, __ATOMIC_ACQUIRE);
309
310
        /*
311
         * Notes of use of __ATOMIC_RELEASE
312
         * This counter is only read by the write side of the lock, and so we
313
         * specify __ATOMIC_RELEASE here to ensure that the write side of the
314
         * lock see this during the spin loop read of users, as it waits for the
315
         * reader count to approach zero
316
         */
317
0
        ATOMIC_ADD_FETCH(&lock->qp_group[qp_idx].users, VAL_READER,
318
0
                         __ATOMIC_RELEASE);
319
320
        /* if the idx hasn't changed, we're good, else try again */
321
0
        if (qp_idx == (uint64_t)ATOMIC_LOAD_N(&lock->reader_idx, __ATOMIC_ACQUIRE))
322
0
            break;
323
324
        /*
325
         * Notes on use of __ATOMIC_RELEASE
326
         * As with the add above, we want to ensure that this decrement is
327
         * seen by the write side of the lock as soon as it happens to prevent
328
         * undue spinning waiting for write side completion
329
         */
330
0
        ATOMIC_SUB_FETCH(&lock->qp_group[qp_idx].users, VAL_READER,
331
0
                         __ATOMIC_RELEASE);
332
0
    }
333
334
0
    return &lock->qp_group[qp_idx];
335
0
}
336
337
void ossl_rcu_read_lock(CRYPTO_RCU_LOCK *lock)
338
0
{
339
0
    struct rcu_thr_data *data;
340
0
    int i, available_qp = -1;
341
342
    /*
343
     * we're going to access current_qp here so ask the
344
     * processor to fetch it
345
     */
346
0
    data = CRYPTO_THREAD_get_local(&rcu_thr_key);
347
348
0
    if (data == NULL) {
349
0
        data = OPENSSL_zalloc(sizeof(*data));
350
0
        OPENSSL_assert(data != NULL);
351
0
        CRYPTO_THREAD_set_local(&rcu_thr_key, data);
352
0
        ossl_init_thread_start(NULL, NULL, free_rcu_thr_data);
353
0
    }
354
355
0
    for (i = 0; i < MAX_QPS; i++) {
356
0
        if (data->thread_qps[i].qp == NULL && available_qp == -1)
357
0
            available_qp = i;
358
        /* If we have a hold on this lock already, we're good */
359
0
        if (data->thread_qps[i].lock == lock) {
360
0
            data->thread_qps[i].depth++;
361
0
            return;
362
0
        }
363
0
    }
364
365
    /*
366
     * if we get here, then we don't have a hold on this lock yet
367
     */
368
0
    assert(available_qp != -1);
369
370
0
    data->thread_qps[available_qp].qp = get_hold_current_qp(lock);
371
0
    data->thread_qps[available_qp].depth = 1;
372
0
    data->thread_qps[available_qp].lock = lock;
373
0
}
374
375
void ossl_rcu_read_unlock(CRYPTO_RCU_LOCK *lock)
376
0
{
377
0
    int i;
378
0
    struct rcu_thr_data *data = CRYPTO_THREAD_get_local(&rcu_thr_key);
379
0
    uint64_t ret;
380
381
0
    assert(data != NULL);
382
383
0
    for (i = 0; i < MAX_QPS; i++) {
384
0
        if (data->thread_qps[i].lock == lock) {
385
            /*
386
             * As with read side acquisition, we use __ATOMIC_RELEASE here
387
             * to ensure that the decrement is published immediately
388
             * to any write side waiters
389
             */
390
0
            data->thread_qps[i].depth--;
391
0
            if (data->thread_qps[i].depth == 0) {
392
0
                ret = ATOMIC_SUB_FETCH(&data->thread_qps[i].qp->users, VAL_READER,
393
0
                                       __ATOMIC_RELEASE);
394
0
                OPENSSL_assert(ret != UINT64_MAX);
395
0
                data->thread_qps[i].qp = NULL;
396
0
                data->thread_qps[i].lock = NULL;
397
0
            }
398
0
            return;
399
0
        }
400
0
    }
401
    /*
402
     * If we get here, we're trying to unlock a lock that we never acquired -
403
     * that's fatal.
404
     */
405
0
    assert(0);
406
0
}
407
408
/*
409
 * Write side allocation routine to get the current qp
410
 * and replace it with a new one
411
 */
412
static struct rcu_qp *update_qp(CRYPTO_RCU_LOCK *lock)
413
48
{
414
48
    uint64_t new_id;
415
48
    uint64_t current_idx;
416
417
48
    pthread_mutex_lock(&lock->alloc_lock);
418
419
    /*
420
     * we need at least one qp to be available with one
421
     * left over, so that readers can start working on
422
     * one that isn't yet being waited on
423
     */
424
48
    while (lock->group_count - lock->writers_alloced < 2)
425
        /* we have to wait for one to be free */
426
0
        pthread_cond_wait(&lock->alloc_signal, &lock->alloc_lock);
427
428
48
    current_idx = lock->current_alloc_idx;
429
430
    /* Allocate the qp */
431
48
    lock->writers_alloced++;
432
433
    /* increment the allocation index */
434
48
    lock->current_alloc_idx =
435
48
        (lock->current_alloc_idx + 1) % lock->group_count;
436
437
    /* get and insert a new id */
438
48
    new_id = lock->id_ctr;
439
48
    lock->id_ctr++;
440
441
48
    new_id = VAL_ID(new_id);
442
    /*
443
     * Even though we are under a write side lock here
444
     * We need to use atomic instructions to ensure that the results
445
     * of this update are published to the read side prior to updating the
446
     * reader idx below
447
     */
448
48
    ATOMIC_AND_FETCH(&lock->qp_group[current_idx].users, ID_MASK,
449
48
                     __ATOMIC_RELEASE);
450
48
    ATOMIC_OR_FETCH(&lock->qp_group[current_idx].users, new_id,
451
48
                    __ATOMIC_RELEASE);
452
453
    /*
454
     * Update the reader index to be the prior qp.
455
     * Note the use of __ATOMIC_RELEASE here is based on the corresponding use
456
     * of __ATOMIC_ACQUIRE in get_hold_current_qp, as we want any publication
457
     * of this value to be seen on the read side immediately after it happens
458
     */
459
48
    ATOMIC_STORE_N(&lock->reader_idx, lock->current_alloc_idx,
460
48
                   __ATOMIC_RELEASE);
461
462
    /* wake up any waiters */
463
48
    pthread_cond_signal(&lock->alloc_signal);
464
48
    pthread_mutex_unlock(&lock->alloc_lock);
465
48
    return &lock->qp_group[current_idx];
466
48
}
467
468
static void retire_qp(CRYPTO_RCU_LOCK *lock, struct rcu_qp *qp)
469
48
{
470
48
    pthread_mutex_lock(&lock->alloc_lock);
471
48
    lock->writers_alloced--;
472
48
    pthread_cond_signal(&lock->alloc_signal);
473
48
    pthread_mutex_unlock(&lock->alloc_lock);
474
48
}
475
476
static struct rcu_qp *allocate_new_qp_group(CRYPTO_RCU_LOCK *lock,
477
                                            int count)
478
16
{
479
16
    struct rcu_qp *new =
480
16
        OPENSSL_zalloc(sizeof(*new) * count);
481
482
16
    lock->group_count = count;
483
16
    return new;
484
16
}
485
486
void ossl_rcu_write_lock(CRYPTO_RCU_LOCK *lock)
487
32
{
488
32
    pthread_mutex_lock(&lock->write_lock);
489
32
}
490
491
void ossl_rcu_write_unlock(CRYPTO_RCU_LOCK *lock)
492
32
{
493
32
    pthread_mutex_unlock(&lock->write_lock);
494
32
}
495
496
void ossl_synchronize_rcu(CRYPTO_RCU_LOCK *lock)
497
48
{
498
48
    struct rcu_qp *qp;
499
48
    uint64_t count;
500
48
    struct rcu_cb_item *cb_items, *tmpcb;
501
502
    /*
503
     * __ATOMIC_ACQ_REL is used here to ensure that we get any prior published
504
     * writes before we read, and publish our write immediately
505
     */
506
48
    cb_items = ATOMIC_EXCHANGE_N(&lock->cb_items, NULL, __ATOMIC_ACQ_REL);
507
508
48
    qp = update_qp(lock);
509
510
    /*
511
     * wait for the reader count to reach zero
512
     * Note the use of __ATOMIC_ACQUIRE here to ensure that any
513
     * prior __ATOMIC_RELEASE write operation in get_hold_current_qp
514
     * is visible prior to our read
515
     */
516
48
    do {
517
48
        count = (uint64_t)ATOMIC_LOAD_N(&qp->users, __ATOMIC_ACQUIRE);
518
48
    } while (READER_COUNT(count) != 0);
519
520
    /* retire in order */
521
48
    pthread_mutex_lock(&lock->prior_lock);
522
48
    while (lock->next_to_retire != ID_VAL(count))
523
0
        pthread_cond_wait(&lock->prior_signal, &lock->prior_lock);
524
48
    lock->next_to_retire++;
525
48
    pthread_cond_broadcast(&lock->prior_signal);
526
48
    pthread_mutex_unlock(&lock->prior_lock);
527
528
48
    retire_qp(lock, qp);
529
530
    /* handle any callbacks that we have */
531
48
    while (cb_items != NULL) {
532
0
        tmpcb = cb_items;
533
0
        cb_items = cb_items->next;
534
0
        tmpcb->fn(tmpcb->data);
535
0
        OPENSSL_free(tmpcb);
536
0
    }
537
48
}
538
539
int ossl_rcu_call(CRYPTO_RCU_LOCK *lock, rcu_cb_fn cb, void *data)
540
0
{
541
0
    struct rcu_cb_item *new =
542
0
        OPENSSL_zalloc(sizeof(*new));
543
544
0
    if (new == NULL)
545
0
        return 0;
546
547
0
    new->data = data;
548
0
    new->fn = cb;
549
    /*
550
     * Use __ATOMIC_ACQ_REL here to indicate that any prior writes to this
551
     * list are visible to us prior to reading, and publish the new value
552
     * immediately
553
     */
554
0
    new->next = ATOMIC_EXCHANGE_N(&lock->cb_items, new, __ATOMIC_ACQ_REL);
555
556
0
    return 1;
557
0
}
558
559
void *ossl_rcu_uptr_deref(void **p)
560
32
{
561
32
    return (void *)ATOMIC_LOAD_N(p, __ATOMIC_ACQUIRE);
562
32
}
563
564
void ossl_rcu_assign_uptr(void **p, void **v)
565
32
{
566
32
    ATOMIC_STORE(p, v, __ATOMIC_RELEASE);
567
32
}
568
569
static CRYPTO_ONCE rcu_init_once = CRYPTO_ONCE_STATIC_INIT;
570
571
CRYPTO_RCU_LOCK *ossl_rcu_lock_new(int num_writers)
572
16
{
573
16
    struct rcu_lock_st *new;
574
575
16
    if (!CRYPTO_THREAD_run_once(&rcu_init_once, ossl_rcu_init))
576
0
        return NULL;
577
578
16
    if (num_writers < 1)
579
0
        num_writers = 1;
580
581
16
    new = OPENSSL_zalloc(sizeof(*new));
582
16
    if (new == NULL)
583
0
        return NULL;
584
585
16
    pthread_mutex_init(&new->write_lock, NULL);
586
16
    pthread_mutex_init(&new->prior_lock, NULL);
587
16
    pthread_mutex_init(&new->alloc_lock, NULL);
588
16
    pthread_cond_init(&new->prior_signal, NULL);
589
16
    pthread_cond_init(&new->alloc_signal, NULL);
590
16
    new->qp_group = allocate_new_qp_group(new, num_writers + 1);
591
16
    if (new->qp_group == NULL) {
592
0
        OPENSSL_free(new);
593
0
        new = NULL;
594
0
    }
595
16
    return new;
596
16
}
597
598
void ossl_rcu_lock_free(CRYPTO_RCU_LOCK *lock)
599
16
{
600
16
    struct rcu_lock_st *rlock = (struct rcu_lock_st *)lock;
601
602
16
    if (lock == NULL)
603
0
        return;
604
605
    /* make sure we're synchronized */
606
16
    ossl_synchronize_rcu(rlock);
607
608
16
    OPENSSL_free(rlock->qp_group);
609
    /* There should only be a single qp left now */
610
16
    OPENSSL_free(rlock);
611
16
}
612
613
CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
614
97.6k
{
615
97.6k
# ifdef USE_RWLOCK
616
97.6k
    CRYPTO_RWLOCK *lock;
617
618
97.6k
    if ((lock = OPENSSL_zalloc(sizeof(pthread_rwlock_t))) == NULL)
619
        /* Don't set error, to avoid recursion blowup. */
620
0
        return NULL;
621
622
97.6k
    if (pthread_rwlock_init(lock, NULL) != 0) {
623
0
        OPENSSL_free(lock);
624
0
        return NULL;
625
0
    }
626
# else
627
    pthread_mutexattr_t attr;
628
    CRYPTO_RWLOCK *lock;
629
630
    if ((lock = OPENSSL_zalloc(sizeof(pthread_mutex_t))) == NULL)
631
        /* Don't set error, to avoid recursion blowup. */
632
        return NULL;
633
634
    /*
635
     * We don't use recursive mutexes, but try to catch errors if we do.
636
     */
637
    pthread_mutexattr_init(&attr);
638
#  if !defined (__TANDEM) && !defined (_SPT_MODEL_)
639
#   if !defined(NDEBUG) && !defined(OPENSSL_NO_MUTEX_ERRORCHECK)
640
    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
641
#   endif
642
#  else
643
    /* The SPT Thread Library does not define MUTEX attributes. */
644
#  endif
645
646
    if (pthread_mutex_init(lock, &attr) != 0) {
647
        pthread_mutexattr_destroy(&attr);
648
        OPENSSL_free(lock);
649
        return NULL;
650
    }
651
652
    pthread_mutexattr_destroy(&attr);
653
# endif
654
655
97.6k
    return lock;
656
97.6k
}
657
658
__owur int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
659
481k
{
660
481k
# ifdef USE_RWLOCK
661
481k
    if (pthread_rwlock_rdlock(lock) != 0)
662
0
        return 0;
663
# else
664
    if (pthread_mutex_lock(lock) != 0) {
665
        assert(errno != EDEADLK && errno != EBUSY);
666
        return 0;
667
    }
668
# endif
669
670
481k
    return 1;
671
481k
}
672
673
__owur int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
674
28.6k
{
675
28.6k
# ifdef USE_RWLOCK
676
28.6k
    if (pthread_rwlock_wrlock(lock) != 0)
677
0
        return 0;
678
# else
679
    if (pthread_mutex_lock(lock) != 0) {
680
        assert(errno != EDEADLK && errno != EBUSY);
681
        return 0;
682
    }
683
# endif
684
685
28.6k
    return 1;
686
28.6k
}
687
688
int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
689
510k
{
690
510k
# ifdef USE_RWLOCK
691
510k
    if (pthread_rwlock_unlock(lock) != 0)
692
0
        return 0;
693
# else
694
    if (pthread_mutex_unlock(lock) != 0) {
695
        assert(errno != EPERM);
696
        return 0;
697
    }
698
# endif
699
700
510k
    return 1;
701
510k
}
702
703
void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock)
704
97.8k
{
705
97.8k
    if (lock == NULL)
706
128
        return;
707
708
97.6k
# ifdef USE_RWLOCK
709
97.6k
    pthread_rwlock_destroy(lock);
710
# else
711
    pthread_mutex_destroy(lock);
712
# endif
713
97.6k
    OPENSSL_free(lock);
714
715
97.6k
    return;
716
97.8k
}
717
718
int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))
719
931k
{
720
931k
    if (pthread_once(once, init) != 0)
721
0
        return 0;
722
723
931k
    return 1;
724
931k
}
725
726
int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
727
112
{
728
112
    if (pthread_key_create(key, cleanup) != 0)
729
0
        return 0;
730
731
112
    return 1;
732
112
}
733
734
void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
735
591k
{
736
591k
    return pthread_getspecific(*key);
737
591k
}
738
739
int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
740
176
{
741
176
    if (pthread_setspecific(*key, val) != 0)
742
0
        return 0;
743
744
176
    return 1;
745
176
}
746
747
int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
748
96
{
749
96
    if (pthread_key_delete(*key) != 0)
750
0
        return 0;
751
752
96
    return 1;
753
96
}
754
755
CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
756
0
{
757
0
    return pthread_self();
758
0
}
759
760
int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
761
0
{
762
0
    return pthread_equal(a, b);
763
0
}
764
765
int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
766
202
{
767
202
# if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
768
202
    if (__atomic_is_lock_free(sizeof(*val), val)) {
769
202
        *ret = __atomic_add_fetch(val, amount, __ATOMIC_ACQ_REL);
770
202
        return 1;
771
202
    }
772
# elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
773
    /* This will work for all future Solaris versions. */
774
    if (ret != NULL) {
775
        *ret = atomic_add_int_nv((volatile unsigned int *)val, amount);
776
        return 1;
777
    }
778
# endif
779
0
    if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
780
0
        return 0;
781
782
0
    *val += amount;
783
0
    *ret  = *val;
784
785
0
    if (!CRYPTO_THREAD_unlock(lock))
786
0
        return 0;
787
788
0
    return 1;
789
0
}
790
791
int CRYPTO_atomic_or(uint64_t *val, uint64_t op, uint64_t *ret,
792
                     CRYPTO_RWLOCK *lock)
793
32
{
794
32
# if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
795
32
    if (__atomic_is_lock_free(sizeof(*val), val)) {
796
32
        *ret = __atomic_or_fetch(val, op, __ATOMIC_ACQ_REL);
797
32
        return 1;
798
32
    }
799
# elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
800
    /* This will work for all future Solaris versions. */
801
    if (ret != NULL) {
802
        *ret = atomic_or_64_nv(val, op);
803
        return 1;
804
    }
805
# endif
806
0
    if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
807
0
        return 0;
808
0
    *val |= op;
809
0
    *ret  = *val;
810
811
0
    if (!CRYPTO_THREAD_unlock(lock))
812
0
        return 0;
813
814
0
    return 1;
815
0
}
816
817
int CRYPTO_atomic_load(uint64_t *val, uint64_t *ret, CRYPTO_RWLOCK *lock)
818
410k
{
819
410k
# if defined(__GNUC__) && defined(__ATOMIC_ACQUIRE) && !defined(BROKEN_CLANG_ATOMICS)
820
410k
    if (__atomic_is_lock_free(sizeof(*val), val)) {
821
410k
        __atomic_load(val, ret, __ATOMIC_ACQUIRE);
822
410k
        return 1;
823
410k
    }
824
# elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
825
    /* This will work for all future Solaris versions. */
826
    if (ret != NULL) {
827
        *ret = atomic_or_64_nv(val, 0);
828
        return 1;
829
    }
830
# endif
831
0
    if (lock == NULL || !CRYPTO_THREAD_read_lock(lock))
832
0
        return 0;
833
0
    *ret  = *val;
834
0
    if (!CRYPTO_THREAD_unlock(lock))
835
0
        return 0;
836
837
0
    return 1;
838
0
}
839
840
int CRYPTO_atomic_load_int(int *val, int *ret, CRYPTO_RWLOCK *lock)
841
0
{
842
0
# if defined(__GNUC__) && defined(__ATOMIC_ACQUIRE) && !defined(BROKEN_CLANG_ATOMICS)
843
0
    if (__atomic_is_lock_free(sizeof(*val), val)) {
844
0
        __atomic_load(val, ret, __ATOMIC_ACQUIRE);
845
0
        return 1;
846
0
    }
847
# elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
848
    /* This will work for all future Solaris versions. */
849
    if (ret != NULL) {
850
        *ret = (int *)atomic_or_uint_nv((unsigned int *)val, 0);
851
        return 1;
852
    }
853
# endif
854
0
    if (lock == NULL || !CRYPTO_THREAD_read_lock(lock))
855
0
        return 0;
856
0
    *ret  = *val;
857
0
    if (!CRYPTO_THREAD_unlock(lock))
858
0
        return 0;
859
860
0
    return 1;
861
0
}
862
863
# ifndef FIPS_MODULE
864
int openssl_init_fork_handlers(void)
865
0
{
866
0
    return 1;
867
0
}
868
# endif /* FIPS_MODULE */
869
870
int openssl_get_fork_id(void)
871
96
{
872
96
    return getpid();
873
96
}
874
#endif