Coverage Report

Created: 2025-11-16 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/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
#include <openssl/crypto.h>
14
#include "internal/cryptlib.h"
15
16
#if defined(__sun)
17
# include <atomic.h>
18
#endif
19
20
#if defined(__apple_build_version__) && __apple_build_version__ < 6000000
21
/*
22
 * OS/X 10.7 and 10.8 had a weird version of clang which has __ATOMIC_ACQUIRE and
23
 * __ATOMIC_ACQ_REL but which expects only one parameter for __atomic_is_lock_free()
24
 * rather than two which has signature __atomic_is_lock_free(sizeof(_Atomic(T))).
25
 * All of this makes impossible to use __atomic_is_lock_free here.
26
 *
27
 * See: https://github.com/llvm/llvm-project/commit/a4c2602b714e6c6edb98164550a5ae829b2de760
28
 */
29
#define BROKEN_CLANG_ATOMICS
30
#endif
31
32
#if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG) && !defined(OPENSSL_SYS_WINDOWS)
33
34
# if defined(OPENSSL_SYS_UNIX)
35
#  include <sys/types.h>
36
#  include <unistd.h>
37
#endif
38
39
# include <assert.h>
40
41
/*
42
 * The Non-Stop KLT thread model currently seems broken in its rwlock
43
 * implementation
44
 * Likewise is there a problem with the glibc implementation on riscv.
45
 */
46
# if defined(PTHREAD_RWLOCK_INITIALIZER) && !defined(_KLT_MODEL_) \
47
                                         && !defined(__riscv)
48
#  define USE_RWLOCK
49
# endif
50
51
CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
52
9.94M
{
53
9.94M
# ifdef USE_RWLOCK
54
9.94M
    CRYPTO_RWLOCK *lock;
55
56
9.94M
    if ((lock = OPENSSL_zalloc(sizeof(pthread_rwlock_t))) == NULL) {
57
        /* Don't set error, to avoid recursion blowup. */
58
0
        return NULL;
59
0
    }
60
61
9.94M
    if (pthread_rwlock_init(lock, NULL) != 0) {
62
0
        OPENSSL_free(lock);
63
0
        return NULL;
64
0
    }
65
# else
66
    pthread_mutexattr_t attr;
67
    CRYPTO_RWLOCK *lock;
68
69
    if ((lock = OPENSSL_zalloc(sizeof(pthread_mutex_t))) == NULL) {
70
        /* Don't set error, to avoid recursion blowup. */
71
        return NULL;
72
    }
73
74
    /*
75
     * We don't use recursive mutexes, but try to catch errors if we do.
76
     */
77
    pthread_mutexattr_init(&attr);
78
#  if !defined (__TANDEM) && !defined (_SPT_MODEL_)
79
#   if !defined(NDEBUG) && !defined(OPENSSL_NO_MUTEX_ERRORCHECK)
80
    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
81
#   endif
82
#  else
83
    /* The SPT Thread Library does not define MUTEX attributes. */
84
#  endif
85
86
    if (pthread_mutex_init(lock, &attr) != 0) {
87
        pthread_mutexattr_destroy(&attr);
88
        OPENSSL_free(lock);
89
        return NULL;
90
    }
91
92
    pthread_mutexattr_destroy(&attr);
93
# endif
94
95
9.94M
    return lock;
96
9.94M
}
97
98
__owur int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
99
1.09G
{
100
1.09G
# ifdef USE_RWLOCK
101
1.09G
    if (pthread_rwlock_rdlock(lock) != 0)
102
0
        return 0;
103
# else
104
    if (pthread_mutex_lock(lock) != 0) {
105
        assert(errno != EDEADLK && errno != EBUSY);
106
        return 0;
107
    }
108
# endif
109
110
1.09G
    return 1;
111
1.09G
}
112
113
__owur int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
114
77.7M
{
115
77.7M
# ifdef USE_RWLOCK
116
77.7M
    if (pthread_rwlock_wrlock(lock) != 0)
117
0
        return 0;
118
# else
119
    if (pthread_mutex_lock(lock) != 0) {
120
        assert(errno != EDEADLK && errno != EBUSY);
121
        return 0;
122
    }
123
# endif
124
125
77.7M
    return 1;
126
77.7M
}
127
128
int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
129
1.26G
{
130
1.26G
# ifdef USE_RWLOCK
131
1.26G
    if (pthread_rwlock_unlock(lock) != 0)
132
0
        return 0;
133
# else
134
    if (pthread_mutex_unlock(lock) != 0) {
135
        assert(errno != EPERM);
136
        return 0;
137
    }
138
# endif
139
140
1.26G
    return 1;
141
1.26G
}
142
143
void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock)
144
9.94M
{
145
9.94M
    if (lock == NULL)
146
2.20k
        return;
147
148
9.94M
# ifdef USE_RWLOCK
149
9.94M
    pthread_rwlock_destroy(lock);
150
# else
151
    pthread_mutex_destroy(lock);
152
# endif
153
9.94M
    OPENSSL_free(lock);
154
155
9.94M
    return;
156
9.94M
}
157
158
int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))
159
2.92G
{
160
2.92G
    if (pthread_once(once, init) != 0)
161
0
        return 0;
162
163
2.92G
    return 1;
164
2.92G
}
165
166
int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
167
1.89k
{
168
1.89k
    if (pthread_key_create(key, cleanup) != 0)
169
0
        return 0;
170
171
1.89k
    return 1;
172
1.89k
}
173
174
void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
175
1.83G
{
176
1.83G
    return pthread_getspecific(*key);
177
1.83G
}
178
179
int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
180
1.91k
{
181
1.91k
    if (pthread_setspecific(*key, val) != 0)
182
0
        return 0;
183
184
1.91k
    return 1;
185
1.91k
}
186
187
int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
188
1.57k
{
189
1.57k
    if (pthread_key_delete(*key) != 0)
190
0
        return 0;
191
192
1.57k
    return 1;
193
1.57k
}
194
195
CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
196
183k
{
197
183k
    return pthread_self();
198
183k
}
199
200
int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
201
11.8k
{
202
11.8k
    return pthread_equal(a, b);
203
11.8k
}
204
205
int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
206
10.9M
{
207
10.9M
# if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
208
10.9M
    if (__atomic_is_lock_free(sizeof(*val), val)) {
209
10.9M
        *ret = __atomic_add_fetch(val, amount, __ATOMIC_ACQ_REL);
210
10.9M
        return 1;
211
10.9M
    }
212
# elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
213
    /* This will work for all future Solaris versions. */
214
    if (ret != NULL) {
215
        *ret = atomic_add_int_nv((volatile unsigned int *)val, amount);
216
        return 1;
217
    }
218
# endif
219
0
    if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
220
0
        return 0;
221
222
0
    *val += amount;
223
0
    *ret  = *val;
224
225
0
    if (!CRYPTO_THREAD_unlock(lock))
226
0
        return 0;
227
228
0
    return 1;
229
0
}
230
231
int CRYPTO_atomic_or(uint64_t *val, uint64_t op, uint64_t *ret,
232
                     CRYPTO_RWLOCK *lock)
233
644
{
234
644
# if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
235
644
    if (__atomic_is_lock_free(sizeof(*val), val)) {
236
644
        *ret = __atomic_or_fetch(val, op, __ATOMIC_ACQ_REL);
237
644
        return 1;
238
644
    }
239
# elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
240
    /* This will work for all future Solaris versions. */
241
    if (ret != NULL) {
242
        *ret = atomic_or_64_nv(val, op);
243
        return 1;
244
    }
245
# endif
246
0
    if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
247
0
        return 0;
248
0
    *val |= op;
249
0
    *ret  = *val;
250
251
0
    if (!CRYPTO_THREAD_unlock(lock))
252
0
        return 0;
253
254
0
    return 1;
255
0
}
256
257
int CRYPTO_atomic_load(uint64_t *val, uint64_t *ret, CRYPTO_RWLOCK *lock)
258
2.43G
{
259
2.43G
# if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
260
2.43G
    if (__atomic_is_lock_free(sizeof(*val), val)) {
261
2.43G
        __atomic_load(val, ret, __ATOMIC_ACQUIRE);
262
2.43G
        return 1;
263
2.43G
    }
264
# elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
265
    /* This will work for all future Solaris versions. */
266
    if (ret != NULL) {
267
        *ret = atomic_or_64_nv(val, 0);
268
        return 1;
269
    }
270
# endif
271
0
    if (lock == NULL || !CRYPTO_THREAD_read_lock(lock))
272
0
        return 0;
273
0
    *ret  = *val;
274
0
    if (!CRYPTO_THREAD_unlock(lock))
275
0
        return 0;
276
277
0
    return 1;
278
0
}
279
# ifndef FIPS_MODULE
280
int openssl_init_fork_handlers(void)
281
0
{
282
0
    return 1;
283
0
}
284
# endif /* FIPS_MODULE */
285
286
int openssl_get_fork_id(void)
287
74.8k
{
288
74.8k
    return getpid();
289
74.8k
}
290
#endif