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 #include <openssl/crypto.h>
11 #include "internal/cryptlib.h"
12 #include "internal/rcu.h"
13 #include "rcu_internal.h"
14
15 #if !defined(OPENSSL_THREADS) || defined(CRYPTO_TDEBUG)
16
17 # if defined(OPENSSL_SYS_UNIX)
18 # include <sys/types.h>
19 # include <unistd.h>
20 # endif
21
22 struct rcu_lock_st {
23 struct rcu_cb_item *cb_items;
24 };
25
ossl_rcu_lock_new(int num_writers,ossl_unused OSSL_LIB_CTX * ctx)26 CRYPTO_RCU_LOCK *ossl_rcu_lock_new(int num_writers,
27 ossl_unused OSSL_LIB_CTX *ctx)
28 {
29 struct rcu_lock_st *lock;
30
31 lock = OPENSSL_zalloc(sizeof(*lock));
32 return lock;
33 }
34
ossl_rcu_lock_free(CRYPTO_RCU_LOCK * lock)35 void ossl_rcu_lock_free(CRYPTO_RCU_LOCK *lock)
36 {
37 OPENSSL_free(lock);
38 }
39
ossl_rcu_read_lock(CRYPTO_RCU_LOCK * lock)40 void ossl_rcu_read_lock(CRYPTO_RCU_LOCK *lock)
41 {
42 return;
43 }
44
ossl_rcu_write_lock(CRYPTO_RCU_LOCK * lock)45 void ossl_rcu_write_lock(CRYPTO_RCU_LOCK *lock)
46 {
47 return;
48 }
49
ossl_rcu_write_unlock(CRYPTO_RCU_LOCK * lock)50 void ossl_rcu_write_unlock(CRYPTO_RCU_LOCK *lock)
51 {
52 return;
53 }
54
ossl_rcu_read_unlock(CRYPTO_RCU_LOCK * lock)55 void ossl_rcu_read_unlock(CRYPTO_RCU_LOCK *lock)
56 {
57 return;
58 }
59
ossl_synchronize_rcu(CRYPTO_RCU_LOCK * lock)60 void ossl_synchronize_rcu(CRYPTO_RCU_LOCK *lock)
61 {
62 struct rcu_cb_item *items = lock->cb_items;
63 struct rcu_cb_item *tmp;
64
65 lock->cb_items = NULL;
66
67 while (items != NULL) {
68 tmp = items->next;
69 items->fn(items->data);
70 OPENSSL_free(items);
71 items = tmp;
72 }
73 }
74
ossl_rcu_call(CRYPTO_RCU_LOCK * lock,rcu_cb_fn cb,void * data)75 int ossl_rcu_call(CRYPTO_RCU_LOCK *lock, rcu_cb_fn cb, void *data)
76 {
77 struct rcu_cb_item *new = OPENSSL_zalloc(sizeof(*new));
78
79 if (new == NULL)
80 return 0;
81
82 new->fn = cb;
83 new->data = data;
84 new->next = lock->cb_items;
85 lock->cb_items = new;
86 return 1;
87 }
88
ossl_rcu_uptr_deref(void ** p)89 void *ossl_rcu_uptr_deref(void **p)
90 {
91 return (void *)*p;
92 }
93
ossl_rcu_assign_uptr(void ** p,void ** v)94 void ossl_rcu_assign_uptr(void **p, void **v)
95 {
96 *(void **)p = *(void **)v;
97 }
98
CRYPTO_THREAD_lock_new(void)99 CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
100 {
101 CRYPTO_RWLOCK *lock;
102
103 if ((lock = CRYPTO_zalloc(sizeof(unsigned int), NULL, 0)) == NULL)
104 /* Don't set error, to avoid recursion blowup. */
105 return NULL;
106
107 *(unsigned int *)lock = 1;
108
109 return lock;
110 }
111
CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK * lock)112 __owur int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
113 {
114 if (!ossl_assert(*(unsigned int *)lock == 1))
115 return 0;
116 return 1;
117 }
118
CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK * lock)119 __owur int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
120 {
121 if (!ossl_assert(*(unsigned int *)lock == 1))
122 return 0;
123 return 1;
124 }
125
CRYPTO_THREAD_unlock(CRYPTO_RWLOCK * lock)126 int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
127 {
128 if (!ossl_assert(*(unsigned int *)lock == 1))
129 return 0;
130 return 1;
131 }
132
CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK * lock)133 void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock) {
134 if (lock == NULL)
135 return;
136
137 *(unsigned int *)lock = 0;
138 OPENSSL_free(lock);
139
140 return;
141 }
142
CRYPTO_THREAD_run_once(CRYPTO_ONCE * once,void (* init)(void))143 int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))
144 {
145 if (*once != 0)
146 return 1;
147
148 init();
149 *once = 1;
150
151 return 1;
152 }
153
154 # define OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX 256
155
156 struct thread_local_storage_entry {
157 void *data;
158 uint8_t used;
159 };
160
161 static struct thread_local_storage_entry thread_local_storage[OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX];
162
CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL * key,void (* cleanup)(void *))163 int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
164 {
165 int entry_idx = 0;
166
167 for (entry_idx = 0; entry_idx < OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX; entry_idx++) {
168 if (!thread_local_storage[entry_idx].used)
169 break;
170 }
171
172 if (entry_idx == OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
173 return 0;
174
175 *key = entry_idx;
176 thread_local_storage[*key].used = 1;
177 thread_local_storage[*key].data = NULL;
178
179 return 1;
180 }
181
CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL * key)182 void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
183 {
184 if (*key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
185 return NULL;
186
187 return thread_local_storage[*key].data;
188 }
189
CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL * key,void * val)190 int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
191 {
192 if (*key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
193 return 0;
194
195 thread_local_storage[*key].data = val;
196
197 return 1;
198 }
199
CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL * key)200 int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
201 {
202 if (*key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
203 return 0;
204
205 thread_local_storage[*key].used = 0;
206 thread_local_storage[*key].data = NULL;
207 *key = OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX + 1;
208 return 1;
209 }
210
CRYPTO_THREAD_get_current_id(void)211 CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
212 {
213 return 0;
214 }
215
CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a,CRYPTO_THREAD_ID b)216 int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
217 {
218 return (a == b);
219 }
220
CRYPTO_atomic_add(int * val,int amount,int * ret,CRYPTO_RWLOCK * lock)221 int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
222 {
223 *val += amount;
224 *ret = *val;
225
226 return 1;
227 }
228
CRYPTO_atomic_add64(uint64_t * val,uint64_t op,uint64_t * ret,CRYPTO_RWLOCK * lock)229 int CRYPTO_atomic_add64(uint64_t *val, uint64_t op, uint64_t *ret,
230 CRYPTO_RWLOCK *lock)
231 {
232 *val += op;
233 *ret = *val;
234
235 return 1;
236 }
237
CRYPTO_atomic_and(uint64_t * val,uint64_t op,uint64_t * ret,CRYPTO_RWLOCK * lock)238 int CRYPTO_atomic_and(uint64_t *val, uint64_t op, uint64_t *ret,
239 CRYPTO_RWLOCK *lock)
240 {
241 *val &= op;
242 *ret = *val;
243
244 return 1;
245 }
246
CRYPTO_atomic_or(uint64_t * val,uint64_t op,uint64_t * ret,CRYPTO_RWLOCK * lock)247 int CRYPTO_atomic_or(uint64_t *val, uint64_t op, uint64_t *ret,
248 CRYPTO_RWLOCK *lock)
249 {
250 *val |= op;
251 *ret = *val;
252
253 return 1;
254 }
255
CRYPTO_atomic_load(uint64_t * val,uint64_t * ret,CRYPTO_RWLOCK * lock)256 int CRYPTO_atomic_load(uint64_t *val, uint64_t *ret, CRYPTO_RWLOCK *lock)
257 {
258 *ret = *val;
259
260 return 1;
261 }
262
CRYPTO_atomic_store(uint64_t * dst,uint64_t val,CRYPTO_RWLOCK * lock)263 int CRYPTO_atomic_store(uint64_t *dst, uint64_t val, CRYPTO_RWLOCK *lock)
264 {
265 *dst = val;
266
267 return 1;
268 }
269
CRYPTO_atomic_load_int(int * val,int * ret,CRYPTO_RWLOCK * lock)270 int CRYPTO_atomic_load_int(int *val, int *ret, CRYPTO_RWLOCK *lock)
271 {
272 *ret = *val;
273
274 return 1;
275 }
276
openssl_init_fork_handlers(void)277 int openssl_init_fork_handlers(void)
278 {
279 return 0;
280 }
281
openssl_get_fork_id(void)282 int openssl_get_fork_id(void)
283 {
284 # if defined(OPENSSL_SYS_UNIX)
285 return getpid();
286 # else
287 return 0;
288 # endif
289 }
290 #endif
291