xref: /openssl/crypto/threads_none.c (revision 9f4d8c63)
1 /*
2  * Copyright 2016-2024 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 static void *thread_local_storage[OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX];
157 
CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL * key,void (* cleanup)(void *))158 int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
159 {
160     static unsigned int thread_local_key = 0;
161 
162     if (thread_local_key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
163         return 0;
164 
165     *key = thread_local_key++;
166 
167     thread_local_storage[*key] = NULL;
168 
169     return 1;
170 }
171 
CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL * key)172 void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
173 {
174     if (*key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
175         return NULL;
176 
177     return thread_local_storage[*key];
178 }
179 
CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL * key,void * val)180 int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
181 {
182     if (*key >= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX)
183         return 0;
184 
185     thread_local_storage[*key] = val;
186 
187     return 1;
188 }
189 
CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL * key)190 int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
191 {
192     *key = OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX + 1;
193     return 1;
194 }
195 
CRYPTO_THREAD_get_current_id(void)196 CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
197 {
198     return 0;
199 }
200 
CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a,CRYPTO_THREAD_ID b)201 int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
202 {
203     return (a == b);
204 }
205 
CRYPTO_atomic_add(int * val,int amount,int * ret,CRYPTO_RWLOCK * lock)206 int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
207 {
208     *val += amount;
209     *ret  = *val;
210 
211     return 1;
212 }
213 
CRYPTO_atomic_add64(uint64_t * val,uint64_t op,uint64_t * ret,CRYPTO_RWLOCK * lock)214 int CRYPTO_atomic_add64(uint64_t *val, uint64_t op, uint64_t *ret,
215                         CRYPTO_RWLOCK *lock)
216 {
217     *val += op;
218     *ret  = *val;
219 
220     return 1;
221 }
222 
CRYPTO_atomic_and(uint64_t * val,uint64_t op,uint64_t * ret,CRYPTO_RWLOCK * lock)223 int CRYPTO_atomic_and(uint64_t *val, uint64_t op, uint64_t *ret,
224                       CRYPTO_RWLOCK *lock)
225 {
226     *val &= op;
227     *ret  = *val;
228 
229     return 1;
230 }
231 
CRYPTO_atomic_or(uint64_t * val,uint64_t op,uint64_t * ret,CRYPTO_RWLOCK * lock)232 int CRYPTO_atomic_or(uint64_t *val, uint64_t op, uint64_t *ret,
233                      CRYPTO_RWLOCK *lock)
234 {
235     *val |= op;
236     *ret  = *val;
237 
238     return 1;
239 }
240 
CRYPTO_atomic_load(uint64_t * val,uint64_t * ret,CRYPTO_RWLOCK * lock)241 int CRYPTO_atomic_load(uint64_t *val, uint64_t *ret, CRYPTO_RWLOCK *lock)
242 {
243     *ret  = *val;
244 
245     return 1;
246 }
247 
CRYPTO_atomic_store(uint64_t * dst,uint64_t val,CRYPTO_RWLOCK * lock)248 int CRYPTO_atomic_store(uint64_t *dst, uint64_t val, CRYPTO_RWLOCK *lock)
249 {
250     *dst = val;
251 
252     return 1;
253 }
254 
CRYPTO_atomic_load_int(int * val,int * ret,CRYPTO_RWLOCK * lock)255 int CRYPTO_atomic_load_int(int *val, int *ret, CRYPTO_RWLOCK *lock)
256 {
257     *ret = *val;
258 
259     return 1;
260 }
261 
openssl_init_fork_handlers(void)262 int openssl_init_fork_handlers(void)
263 {
264     return 0;
265 }
266 
openssl_get_fork_id(void)267 int openssl_get_fork_id(void)
268 {
269 # if defined(OPENSSL_SYS_UNIX)
270     return getpid();
271 # else
272     return 0;
273 # endif
274 }
275 #endif
276