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