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 "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# ifdef PTHREAD_RWLOCK_INITIALIZER
42#  define USE_RWLOCK
43# endif
44
45CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
46{
47# ifdef USE_RWLOCK
48    CRYPTO_RWLOCK *lock;
49
50    if ((lock = OPENSSL_zalloc(sizeof(pthread_rwlock_t))) == NULL) {
51        /* Don't set error, to avoid recursion blowup. */
52        return NULL;
53    }
54
55    if (pthread_rwlock_init(lock, NULL) != 0) {
56        OPENSSL_free(lock);
57        return NULL;
58    }
59# else
60    pthread_mutexattr_t attr;
61    CRYPTO_RWLOCK *lock;
62
63    if ((lock = OPENSSL_zalloc(sizeof(pthread_mutex_t))) == NULL) {
64        /* Don't set error, to avoid recursion blowup. */
65        return NULL;
66    }
67
68    /*
69     * We don't use recursive mutexes, but try to catch errors if we do.
70     */
71    pthread_mutexattr_init(&attr);
72#  if !defined (__TANDEM) && !defined (_SPT_MODEL_)
73#   if !defined(NDEBUG) && !defined(OPENSSL_NO_MUTEX_ERRORCHECK)
74    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
75#   endif
76#  else
77    /* The SPT Thread Library does not define MUTEX attributes. */
78#  endif
79
80    if (pthread_mutex_init(lock, &attr) != 0) {
81        pthread_mutexattr_destroy(&attr);
82        OPENSSL_free(lock);
83        return NULL;
84    }
85
86    pthread_mutexattr_destroy(&attr);
87# endif
88
89    return lock;
90}
91
92__owur int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
93{
94# ifdef USE_RWLOCK
95    if (pthread_rwlock_rdlock(lock) != 0)
96        return 0;
97# else
98    if (pthread_mutex_lock(lock) != 0) {
99        assert(errno != EDEADLK && errno != EBUSY);
100        return 0;
101    }
102# endif
103
104    return 1;
105}
106
107__owur int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
108{
109# ifdef USE_RWLOCK
110    if (pthread_rwlock_wrlock(lock) != 0)
111        return 0;
112# else
113    if (pthread_mutex_lock(lock) != 0) {
114        assert(errno != EDEADLK && errno != EBUSY);
115        return 0;
116    }
117# endif
118
119    return 1;
120}
121
122int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
123{
124# ifdef USE_RWLOCK
125    if (pthread_rwlock_unlock(lock) != 0)
126        return 0;
127# else
128    if (pthread_mutex_unlock(lock) != 0) {
129        assert(errno != EPERM);
130        return 0;
131    }
132# endif
133
134    return 1;
135}
136
137void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock)
138{
139    if (lock == NULL)
140        return;
141
142# ifdef USE_RWLOCK
143    pthread_rwlock_destroy(lock);
144# else
145    pthread_mutex_destroy(lock);
146# endif
147    OPENSSL_free(lock);
148
149    return;
150}
151
152int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))
153{
154    if (pthread_once(once, init) != 0)
155        return 0;
156
157    return 1;
158}
159
160int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
161{
162    if (pthread_key_create(key, cleanup) != 0)
163        return 0;
164
165    return 1;
166}
167
168void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
169{
170    return pthread_getspecific(*key);
171}
172
173int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
174{
175    if (pthread_setspecific(*key, val) != 0)
176        return 0;
177
178    return 1;
179}
180
181int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
182{
183    if (pthread_key_delete(*key) != 0)
184        return 0;
185
186    return 1;
187}
188
189CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
190{
191    return pthread_self();
192}
193
194int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
195{
196    return pthread_equal(a, b);
197}
198
199int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
200{
201# if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
202    if (__atomic_is_lock_free(sizeof(*val), val)) {
203        *ret = __atomic_add_fetch(val, amount, __ATOMIC_ACQ_REL);
204        return 1;
205    }
206# elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
207    /* This will work for all future Solaris versions. */
208    if (ret != NULL) {
209        *ret = atomic_add_int_nv((volatile unsigned int *)val, amount);
210        return 1;
211    }
212# endif
213    if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
214        return 0;
215
216    *val += amount;
217    *ret  = *val;
218
219    if (!CRYPTO_THREAD_unlock(lock))
220        return 0;
221
222    return 1;
223}
224
225int CRYPTO_atomic_or(uint64_t *val, uint64_t op, uint64_t *ret,
226                     CRYPTO_RWLOCK *lock)
227{
228# if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) && !defined(BROKEN_CLANG_ATOMICS)
229    if (__atomic_is_lock_free(sizeof(*val), val)) {
230        *ret = __atomic_or_fetch(val, op, __ATOMIC_ACQ_REL);
231        return 1;
232    }
233# elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
234    /* This will work for all future Solaris versions. */
235    if (ret != NULL) {
236        *ret = atomic_or_64_nv(val, op);
237        return 1;
238    }
239# endif
240    if (lock == NULL || !CRYPTO_THREAD_write_lock(lock))
241        return 0;
242    *val |= op;
243    *ret  = *val;
244
245    if (!CRYPTO_THREAD_unlock(lock))
246        return 0;
247
248    return 1;
249}
250
251int CRYPTO_atomic_load(uint64_t *val, uint64_t *ret, CRYPTO_RWLOCK *lock)
252{
253# if defined(__GNUC__) && defined(__ATOMIC_ACQUIRE) && !defined(BROKEN_CLANG_ATOMICS)
254    if (__atomic_is_lock_free(sizeof(*val), val)) {
255        __atomic_load(val, ret, __ATOMIC_ACQUIRE);
256        return 1;
257    }
258# elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
259    /* This will work for all future Solaris versions. */
260    if (ret != NULL) {
261        *ret = atomic_or_64_nv(val, 0);
262        return 1;
263    }
264# endif
265    if (lock == NULL || !CRYPTO_THREAD_read_lock(lock))
266        return 0;
267    *ret  = *val;
268    if (!CRYPTO_THREAD_unlock(lock))
269        return 0;
270
271    return 1;
272}
273# ifndef FIPS_MODULE
274int openssl_init_fork_handlers(void)
275{
276    return 1;
277}
278# endif /* FIPS_MODULE */
279
280int openssl_get_fork_id(void)
281{
282    return getpid();
283}
284#endif
285