xref: /openssl/test/threadstest.c (revision 16beec98)
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 /*
11  * The test_multi_downgrade_shared_pkey function tests the thread safety of a
12  * deprecated function.
13  */
14 #ifndef OPENSSL_NO_DEPRECATED_3_0
15 # define OPENSSL_SUPPRESS_DEPRECATED
16 #endif
17 
18 #if defined(_WIN32)
19 # include <windows.h>
20 #endif
21 
22 #include <string.h>
23 #include <openssl/crypto.h>
24 #include <openssl/rsa.h>
25 #include <openssl/aes.h>
26 #include <openssl/err.h>
27 #include <openssl/rand.h>
28 #include <openssl/pem.h>
29 #include <openssl/evp.h>
30 #include "internal/tsan_assist.h"
31 #include "internal/nelem.h"
32 #include "internal/time.h"
33 #include "internal/rcu.h"
34 #include "testutil.h"
35 #include "threadstest.h"
36 
37 #ifdef __SANITIZE_THREAD__
38 #include <sanitizer/tsan_interface.h>
39 #define TSAN_ACQUIRE(s) __tsan_acquire(s)
40 #else
41 #define TSAN_ACQUIRE(s)
42 #endif
43 
44 /* Limit the maximum number of threads */
45 #define MAXIMUM_THREADS     10
46 
47 /* Limit the maximum number of providers loaded into a library context */
48 #define MAXIMUM_PROVIDERS   4
49 
50 static int do_fips = 0;
51 static char *privkey;
52 static char *config_file = NULL;
53 static int multidefault_run = 0;
54 
55 static const char *default_provider[] = { "default", NULL };
56 static const char *fips_provider[] = { "fips", NULL };
57 static const char *fips_and_default_providers[] = { "default", "fips", NULL };
58 
59 static CRYPTO_RWLOCK *global_lock;
60 
61 #ifdef TSAN_REQUIRES_LOCKING
62 static CRYPTO_RWLOCK *tsan_lock;
63 #endif
64 
65 /* Grab a globally unique integer value, return 0 on failure */
get_new_uid(void)66 static int get_new_uid(void)
67 {
68     /*
69      * Start with a nice large number to avoid potential conflicts when
70      * we generate a new OID.
71      */
72     static TSAN_QUALIFIER int current_uid = 1 << (sizeof(int) * 8 - 2);
73 #ifdef TSAN_REQUIRES_LOCKING
74     int r;
75 
76     if (!TEST_true(CRYPTO_THREAD_write_lock(tsan_lock)))
77         return 0;
78     r = ++current_uid;
79     if (!TEST_true(CRYPTO_THREAD_unlock(tsan_lock)))
80         return 0;
81     return r;
82 
83 #else
84     return tsan_counter(&current_uid);
85 #endif
86 }
87 
test_lock(void)88 static int test_lock(void)
89 {
90     CRYPTO_RWLOCK *lock = CRYPTO_THREAD_lock_new();
91     int res;
92 
93     if (!TEST_ptr(lock))
94         return 0;
95 
96     res = TEST_true(CRYPTO_THREAD_read_lock(lock))
97           && TEST_true(CRYPTO_THREAD_unlock(lock))
98           && TEST_true(CRYPTO_THREAD_write_lock(lock))
99           && TEST_true(CRYPTO_THREAD_unlock(lock));
100 
101     CRYPTO_THREAD_lock_free(lock);
102 
103     return res;
104 }
105 
106 #if defined(OPENSSL_THREADS)
107 static int contention = 0;
108 static int rwwriter1_done = 0;
109 static int rwwriter2_done = 0;
110 static int rwreader1_iterations = 0;
111 static int rwreader2_iterations = 0;
112 static int rwwriter1_iterations = 0;
113 static int rwwriter2_iterations = 0;
114 static int *rwwriter_ptr = NULL;
115 static int rw_torture_result = 1;
116 static CRYPTO_RWLOCK *rwtorturelock = NULL;
117 static CRYPTO_RWLOCK *atomiclock = NULL;
118 
rwwriter_fn(int id,int * iterations)119 static void rwwriter_fn(int id, int *iterations)
120 {
121     int count;
122     int *old, *new;
123     OSSL_TIME t1, t2;
124     t1 = ossl_time_now();
125 
126     for (count = 0; ; count++) {
127         new = CRYPTO_zalloc(sizeof (int), NULL, 0);
128         if (contention == 0)
129             OSSL_sleep(1000);
130         if (!CRYPTO_THREAD_write_lock(rwtorturelock))
131             abort();
132         if (rwwriter_ptr != NULL) {
133             *new = *rwwriter_ptr + 1;
134         } else {
135             *new = 0;
136         }
137         old = rwwriter_ptr;
138         rwwriter_ptr = new;
139         if (!CRYPTO_THREAD_unlock(rwtorturelock))
140             abort();
141         if (old != NULL)
142             CRYPTO_free(old, __FILE__, __LINE__);
143         t2 = ossl_time_now();
144         if ((ossl_time2seconds(t2) - ossl_time2seconds(t1)) >= 4)
145             break;
146     }
147     *iterations = count;
148     return;
149 }
150 
rwwriter1_fn(void)151 static void rwwriter1_fn(void)
152 {
153     int local;
154 
155     TEST_info("Starting writer1");
156     rwwriter_fn(1, &rwwriter1_iterations);
157     CRYPTO_atomic_add(&rwwriter1_done, 1, &local, atomiclock);
158 }
159 
rwwriter2_fn(void)160 static void rwwriter2_fn(void)
161 {
162     int local;
163 
164     TEST_info("Starting writer 2");
165     rwwriter_fn(2, &rwwriter2_iterations);
166     CRYPTO_atomic_add(&rwwriter2_done, 1, &local, atomiclock);
167 }
168 
rwreader_fn(int * iterations)169 static void rwreader_fn(int *iterations)
170 {
171     unsigned int count = 0;
172 
173     int old = 0;
174     int lw1 = 0;
175     int lw2 = 0;
176 
177     if (CRYPTO_THREAD_read_lock(rwtorturelock) == 0)
178             abort();
179 
180     while (lw1 != 1 || lw2 != 1) {
181         CRYPTO_atomic_add(&rwwriter1_done, 0, &lw1, atomiclock);
182         CRYPTO_atomic_add(&rwwriter2_done, 0, &lw2, atomiclock);
183 
184         count++;
185         if (rwwriter_ptr != NULL && old > *rwwriter_ptr) {
186             TEST_info("rwwriter pointer went backwards\n");
187             rw_torture_result = 0;
188         }
189         if (CRYPTO_THREAD_unlock(rwtorturelock) == 0)
190             abort();
191         *iterations = count;
192         if (rw_torture_result == 0) {
193             *iterations = count;
194             return;
195         }
196         if (CRYPTO_THREAD_read_lock(rwtorturelock) == 0)
197             abort();
198     }
199     *iterations = count;
200     if (CRYPTO_THREAD_unlock(rwtorturelock) == 0)
201             abort();
202 }
203 
rwreader1_fn(void)204 static void rwreader1_fn(void)
205 {
206     TEST_info("Starting reader 1");
207     rwreader_fn(&rwreader1_iterations);
208 }
209 
rwreader2_fn(void)210 static void rwreader2_fn(void)
211 {
212     TEST_info("Starting reader 2");
213     rwreader_fn(&rwreader2_iterations);
214 }
215 
216 static thread_t rwwriter1;
217 static thread_t rwwriter2;
218 static thread_t rwreader1;
219 static thread_t rwreader2;
220 
_torture_rw(void)221 static int _torture_rw(void)
222 {
223     double tottime = 0;
224     int ret = 0;
225     double avr, avw;
226     OSSL_TIME t1, t2;
227     struct timeval dtime;
228 
229     rwtorturelock = CRYPTO_THREAD_lock_new();
230     atomiclock = CRYPTO_THREAD_lock_new();
231     if (!TEST_ptr(rwtorturelock) || !TEST_ptr(atomiclock))
232         goto out;
233 
234     rwwriter1_iterations = 0;
235     rwwriter2_iterations = 0;
236     rwreader1_iterations = 0;
237     rwreader2_iterations = 0;
238     rwwriter1_done = 0;
239     rwwriter2_done = 0;
240     rw_torture_result = 1;
241 
242     memset(&rwwriter1, 0, sizeof(thread_t));
243     memset(&rwwriter2, 0, sizeof(thread_t));
244     memset(&rwreader1, 0, sizeof(thread_t));
245     memset(&rwreader2, 0, sizeof(thread_t));
246 
247     TEST_info("Staring rw torture");
248     t1 = ossl_time_now();
249     if (!TEST_true(run_thread(&rwreader1, rwreader1_fn))
250         || !TEST_true(run_thread(&rwreader2, rwreader2_fn))
251         || !TEST_true(run_thread(&rwwriter1, rwwriter1_fn))
252         || !TEST_true(run_thread(&rwwriter2, rwwriter2_fn))
253         || !TEST_true(wait_for_thread(rwwriter1))
254         || !TEST_true(wait_for_thread(rwwriter2))
255         || !TEST_true(wait_for_thread(rwreader1))
256         || !TEST_true(wait_for_thread(rwreader2)))
257         goto out;
258 
259     t2 = ossl_time_now();
260     dtime = ossl_time_to_timeval(ossl_time_subtract(t2, t1));
261     tottime = dtime.tv_sec + (dtime.tv_usec / 1e6);
262     TEST_info("rw_torture_result is %d\n", rw_torture_result);
263     TEST_info("performed %d reads and %d writes over 2 read and 2 write threads in %e seconds",
264               rwreader1_iterations + rwreader2_iterations,
265               rwwriter1_iterations + rwwriter2_iterations, tottime);
266     if ((rwreader1_iterations + rwreader2_iterations == 0)
267         || (rwwriter1_iterations + rwwriter2_iterations == 0)) {
268         TEST_info("Threads did not iterate\n");
269         goto out;
270     }
271     avr = tottime / (rwreader1_iterations + rwreader2_iterations);
272     avw = (tottime / (rwwriter1_iterations + rwwriter2_iterations));
273     TEST_info("Average read time %e/read", avr);
274     TEST_info("Averate write time %e/write", avw);
275 
276     if (TEST_int_eq(rw_torture_result, 1))
277         ret = 1;
278 out:
279     CRYPTO_THREAD_lock_free(rwtorturelock);
280     CRYPTO_THREAD_lock_free(atomiclock);
281     rwtorturelock = NULL;
282     return ret;
283 }
284 
torture_rw_low(void)285 static int torture_rw_low(void)
286 {
287     contention = 0;
288     return _torture_rw();
289 }
290 
torture_rw_high(void)291 static int torture_rw_high(void)
292 {
293     contention = 1;
294     return _torture_rw();
295 }
296 
297 
298 # ifndef OPENSSL_SYS_MACOSX
299 static CRYPTO_RCU_LOCK *rcu_lock = NULL;
300 
301 static int writer1_done = 0;
302 static int writer2_done = 0;
303 static int reader1_iterations = 0;
304 static int reader2_iterations = 0;
305 static int writer1_iterations = 0;
306 static int writer2_iterations = 0;
307 static uint64_t *writer_ptr = NULL;
308 static uint64_t global_ctr = 0;
309 static int rcu_torture_result = 1;
free_old_rcu_data(void * data)310 static void free_old_rcu_data(void *data)
311 {
312     CRYPTO_free(data, NULL, 0);
313 }
314 
writer_fn(int id,int * iterations)315 static void writer_fn(int id, int *iterations)
316 {
317     int count;
318     OSSL_TIME t1, t2;
319     uint64_t *old, *new;
320 
321     t1 = ossl_time_now();
322 
323     for (count = 0; ; count++) {
324         new = CRYPTO_zalloc(sizeof(uint64_t), NULL, 0);
325         if (contention == 0)
326             OSSL_sleep(1000);
327         ossl_rcu_write_lock(rcu_lock);
328         old = ossl_rcu_deref(&writer_ptr);
329         TSAN_ACQUIRE(&writer_ptr);
330         *new = global_ctr++;
331         ossl_rcu_assign_ptr(&writer_ptr, &new);
332         if (contention == 0)
333             ossl_rcu_call(rcu_lock, free_old_rcu_data, old);
334         ossl_rcu_write_unlock(rcu_lock);
335         if (contention != 0) {
336             ossl_synchronize_rcu(rcu_lock);
337             CRYPTO_free(old, NULL, 0);
338         }
339         t2 = ossl_time_now();
340         if ((ossl_time2seconds(t2) - ossl_time2seconds(t1)) >= 4)
341             break;
342     }
343     *iterations = count;
344     return;
345 }
346 
writer1_fn(void)347 static void writer1_fn(void)
348 {
349     int local;
350 
351     TEST_info("Starting writer1");
352     writer_fn(1, &writer1_iterations);
353     CRYPTO_atomic_add(&writer1_done, 1, &local, atomiclock);
354 }
355 
writer2_fn(void)356 static void writer2_fn(void)
357 {
358     int local;
359 
360     TEST_info("Starting writer2");
361     writer_fn(2, &writer2_iterations);
362     CRYPTO_atomic_add(&writer2_done, 1, &local, atomiclock);
363 }
364 
reader_fn(int * iterations)365 static void reader_fn(int *iterations)
366 {
367     unsigned int count = 0;
368     uint64_t *valp;
369     uint64_t val;
370     uint64_t oldval = 0;
371     int lw1 = 0;
372     int lw2 = 0;
373 
374     while (lw1 != 1 || lw2 != 1) {
375         CRYPTO_atomic_add(&writer1_done, 0, &lw1, atomiclock);
376         CRYPTO_atomic_add(&writer2_done, 0, &lw2, atomiclock);
377         count++;
378         ossl_rcu_read_lock(rcu_lock);
379         valp = ossl_rcu_deref(&writer_ptr);
380         val = (valp == NULL) ? 0 : *valp;
381 
382         if (oldval > val) {
383             TEST_info("rcu torture value went backwards! %llu : %llu", (unsigned long long)oldval, (unsigned long long)val);
384             rcu_torture_result = 0;
385         }
386         oldval = val; /* just try to deref the pointer */
387         ossl_rcu_read_unlock(rcu_lock);
388         if (rcu_torture_result == 0) {
389             *iterations = count;
390             return;
391         }
392     }
393     *iterations = count;
394 }
395 
reader1_fn(void)396 static void reader1_fn(void)
397 {
398     TEST_info("Starting reader 1");
399     reader_fn(&reader1_iterations);
400 }
401 
reader2_fn(void)402 static void reader2_fn(void)
403 {
404     TEST_info("Starting reader 2");
405     reader_fn(&reader2_iterations);
406 }
407 
408 static thread_t writer1;
409 static thread_t writer2;
410 static thread_t reader1;
411 static thread_t reader2;
412 
_torture_rcu(void)413 static int _torture_rcu(void)
414 {
415     OSSL_TIME t1, t2;
416     struct timeval dtime;
417     double tottime;
418     double avr, avw;
419     int rc = 0;
420 
421     atomiclock = CRYPTO_THREAD_lock_new();
422     if (!TEST_ptr(atomiclock))
423         goto out;
424 
425     memset(&writer1, 0, sizeof(thread_t));
426     memset(&writer2, 0, sizeof(thread_t));
427     memset(&reader1, 0, sizeof(thread_t));
428     memset(&reader2, 0, sizeof(thread_t));
429 
430     writer1_iterations = 0;
431     writer2_iterations = 0;
432     reader1_iterations = 0;
433     reader2_iterations = 0;
434     writer1_done = 0;
435     writer2_done = 0;
436     rcu_torture_result = 1;
437 
438     rcu_lock = ossl_rcu_lock_new(1, NULL);
439     if (rcu_lock == NULL)
440         goto out;
441 
442     TEST_info("Staring rcu torture");
443     t1 = ossl_time_now();
444     if (!TEST_true(run_thread(&reader1, reader1_fn))
445         || !TEST_true(run_thread(&reader2, reader2_fn))
446         || !TEST_true(run_thread(&writer1, writer1_fn))
447         || !TEST_true(run_thread(&writer2, writer2_fn))
448         || !TEST_true(wait_for_thread(writer1))
449         || !TEST_true(wait_for_thread(writer2))
450         || !TEST_true(wait_for_thread(reader1))
451         || !TEST_true(wait_for_thread(reader2)))
452         goto out;
453 
454     t2 = ossl_time_now();
455     dtime = ossl_time_to_timeval(ossl_time_subtract(t2, t1));
456     tottime = dtime.tv_sec + (dtime.tv_usec / 1e6);
457     TEST_info("rcu_torture_result is %d\n", rcu_torture_result);
458     TEST_info("performed %d reads and %d writes over 2 read and 2 write threads in %e seconds",
459               reader1_iterations + reader2_iterations,
460               writer1_iterations + writer2_iterations, tottime);
461     if ((reader1_iterations + reader2_iterations == 0)
462         || (writer1_iterations + writer2_iterations == 0)) {
463         TEST_info("Threads did not iterate\n");
464         goto out;
465     }
466     avr = tottime / (reader1_iterations + reader2_iterations);
467     avw = tottime / (writer1_iterations + writer2_iterations);
468     TEST_info("Average read time %e/read", avr);
469     TEST_info("Average write time %e/write", avw);
470 
471     if (!TEST_int_eq(rcu_torture_result, 1))
472         goto out;
473 
474     rc = 1;
475 out:
476     ossl_rcu_lock_free(rcu_lock);
477     CRYPTO_THREAD_lock_free(atomiclock);
478     if (!TEST_int_eq(rcu_torture_result, 1))
479         return 0;
480 
481     return rc;
482 }
483 
torture_rcu_low(void)484 static int torture_rcu_low(void)
485 {
486     contention = 0;
487     return _torture_rcu();
488 }
489 
torture_rcu_high(void)490 static int torture_rcu_high(void)
491 {
492     contention = 1;
493     return _torture_rcu();
494 }
495 # endif
496 #endif
497 
498 static CRYPTO_ONCE once_run = CRYPTO_ONCE_STATIC_INIT;
499 static unsigned once_run_count = 0;
500 
once_do_run(void)501 static void once_do_run(void)
502 {
503     once_run_count++;
504 }
505 
once_run_thread_cb(void)506 static void once_run_thread_cb(void)
507 {
508     CRYPTO_THREAD_run_once(&once_run, once_do_run);
509 }
510 
test_once(void)511 static int test_once(void)
512 {
513     thread_t thread;
514 
515     if (!TEST_true(run_thread(&thread, once_run_thread_cb))
516         || !TEST_true(wait_for_thread(thread))
517         || !CRYPTO_THREAD_run_once(&once_run, once_do_run)
518         || !TEST_int_eq(once_run_count, 1))
519         return 0;
520     return 1;
521 }
522 
523 static CRYPTO_THREAD_LOCAL thread_local_key;
524 static unsigned destructor_run_count = 0;
525 static int thread_local_thread_cb_ok = 0;
526 
thread_local_destructor(void * arg)527 static void thread_local_destructor(void *arg)
528 {
529     unsigned *count;
530 
531     if (arg == NULL)
532         return;
533 
534     count = arg;
535 
536     (*count)++;
537 }
538 
thread_local_thread_cb(void)539 static void thread_local_thread_cb(void)
540 {
541     void *ptr;
542 
543     ptr = CRYPTO_THREAD_get_local(&thread_local_key);
544     if (!TEST_ptr_null(ptr)
545         || !TEST_true(CRYPTO_THREAD_set_local(&thread_local_key,
546                                               &destructor_run_count)))
547         return;
548 
549     ptr = CRYPTO_THREAD_get_local(&thread_local_key);
550     if (!TEST_ptr_eq(ptr, &destructor_run_count))
551         return;
552 
553     thread_local_thread_cb_ok = 1;
554 }
555 
test_thread_local(void)556 static int test_thread_local(void)
557 {
558     thread_t thread;
559     void *ptr = NULL;
560 
561     if (!TEST_true(CRYPTO_THREAD_init_local(&thread_local_key,
562                                             thread_local_destructor)))
563         return 0;
564 
565     ptr = CRYPTO_THREAD_get_local(&thread_local_key);
566     if (!TEST_ptr_null(ptr)
567         || !TEST_true(run_thread(&thread, thread_local_thread_cb))
568         || !TEST_true(wait_for_thread(thread))
569         || !TEST_int_eq(thread_local_thread_cb_ok, 1))
570         return 0;
571 
572 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
573 
574     ptr = CRYPTO_THREAD_get_local(&thread_local_key);
575     if (!TEST_ptr_null(ptr))
576         return 0;
577 
578 # if !defined(OPENSSL_SYS_WINDOWS)
579     if (!TEST_int_eq(destructor_run_count, 1))
580         return 0;
581 # endif
582 #endif
583 
584     if (!TEST_true(CRYPTO_THREAD_cleanup_local(&thread_local_key)))
585         return 0;
586     return 1;
587 }
588 
test_atomic(void)589 static int test_atomic(void)
590 {
591     int val = 0, ret = 0, testresult = 0;
592     uint64_t val64 = 1, ret64 = 0;
593     CRYPTO_RWLOCK *lock = CRYPTO_THREAD_lock_new();
594 
595     if (!TEST_ptr(lock))
596         return 0;
597 
598     if (CRYPTO_atomic_add(&val, 1, &ret, NULL)) {
599         /* This succeeds therefore we're on a platform with lockless atomics */
600         if (!TEST_int_eq(val, 1) || !TEST_int_eq(val, ret))
601             goto err;
602     } else {
603         /* This failed therefore we're on a platform without lockless atomics */
604         if (!TEST_int_eq(val, 0) || !TEST_int_eq(val, ret))
605             goto err;
606     }
607     val = 0;
608     ret = 0;
609 
610     if (!TEST_true(CRYPTO_atomic_add(&val, 1, &ret, lock)))
611         goto err;
612     if (!TEST_int_eq(val, 1) || !TEST_int_eq(val, ret))
613         goto err;
614 
615     if (CRYPTO_atomic_or(&val64, 2, &ret64, NULL)) {
616         /* This succeeds therefore we're on a platform with lockless atomics */
617         if (!TEST_uint_eq((unsigned int)val64, 3)
618                 || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
619             goto err;
620     } else {
621         /* This failed therefore we're on a platform without lockless atomics */
622         if (!TEST_uint_eq((unsigned int)val64, 1)
623                 || !TEST_int_eq((unsigned int)ret64, 0))
624             goto err;
625     }
626     val64 = 1;
627     ret64 = 0;
628 
629     if (!TEST_true(CRYPTO_atomic_or(&val64, 2, &ret64, lock)))
630         goto err;
631 
632     if (!TEST_uint_eq((unsigned int)val64, 3)
633             || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
634         goto err;
635 
636     ret64 = 0;
637     if (CRYPTO_atomic_load(&val64, &ret64, NULL)) {
638         /* This succeeds therefore we're on a platform with lockless atomics */
639         if (!TEST_uint_eq((unsigned int)val64, 3)
640                 || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
641             goto err;
642     } else {
643         /* This failed therefore we're on a platform without lockless atomics */
644         if (!TEST_uint_eq((unsigned int)val64, 3)
645                 || !TEST_int_eq((unsigned int)ret64, 0))
646             goto err;
647     }
648 
649     ret64 = 0;
650     if (!TEST_true(CRYPTO_atomic_load(&val64, &ret64, lock)))
651         goto err;
652 
653     if (!TEST_uint_eq((unsigned int)val64, 3)
654             || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
655         goto err;
656 
657     ret64 = 0;
658 
659     if (CRYPTO_atomic_and(&val64, 5, &ret64, NULL)) {
660         /* This succeeds therefore we're on a platform with lockless atomics */
661         if (!TEST_uint_eq((unsigned int)val64, 1)
662                 || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
663             goto err;
664     } else {
665         /* This failed therefore we're on a platform without lockless atomics */
666         if (!TEST_uint_eq((unsigned int)val64, 3)
667                 || !TEST_int_eq((unsigned int)ret64, 0))
668             goto err;
669     }
670     val64 = 3;
671     ret64 = 0;
672 
673     if (!TEST_true(CRYPTO_atomic_and(&val64, 5, &ret64, lock)))
674         goto err;
675 
676     if (!TEST_uint_eq((unsigned int)val64, 1)
677             || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
678         goto err;
679 
680     ret64 = 0;
681 
682     if (CRYPTO_atomic_add64(&val64, 2, &ret64, NULL)) {
683         /* This succeeds therefore we're on a platform with lockless atomics */
684         if (!TEST_uint_eq((unsigned int)val64, 3)
685                 || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
686             goto err;
687     } else {
688         /* This failed therefore we're on a platform without lockless atomics */
689         if (!TEST_uint_eq((unsigned int)val64, 1)
690                 || !TEST_int_eq((unsigned int)ret64, 0))
691             goto err;
692     }
693     val64 = 1;
694     ret64 = 0;
695 
696     if (!TEST_true(CRYPTO_atomic_add64(&val64, 2, &ret64, lock)))
697         goto err;
698 
699     if (!TEST_uint_eq((unsigned int)val64, 3)
700             || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
701         goto err;
702 
703     testresult = 1;
704  err:
705     CRYPTO_THREAD_lock_free(lock);
706     return testresult;
707 }
708 
709 static OSSL_LIB_CTX *multi_libctx = NULL;
710 static int multi_success;
711 static OSSL_PROVIDER *multi_provider[MAXIMUM_PROVIDERS + 1];
712 static size_t multi_num_threads;
713 static thread_t multi_threads[MAXIMUM_THREADS];
714 
multi_intialise(void)715 static void multi_intialise(void)
716 {
717     multi_success = 1;
718     multi_libctx = NULL;
719     multi_num_threads = 0;
720     memset(multi_threads, 0, sizeof(multi_threads));
721     memset(multi_provider, 0, sizeof(multi_provider));
722 }
723 
multi_set_success(int ok)724 static void multi_set_success(int ok)
725 {
726     if (CRYPTO_THREAD_write_lock(global_lock) == 0) {
727         /* not synchronized, but better than not reporting failure */
728         multi_success = ok;
729         return;
730     }
731 
732     multi_success = ok;
733 
734     CRYPTO_THREAD_unlock(global_lock);
735 }
736 
thead_teardown_libctx(void)737 static void thead_teardown_libctx(void)
738 {
739     OSSL_PROVIDER **p;
740 
741     for (p = multi_provider; *p != NULL; p++)
742         OSSL_PROVIDER_unload(*p);
743     OSSL_LIB_CTX_free(multi_libctx);
744     multi_intialise();
745 }
746 
thread_setup_libctx(int libctx,const char * providers[])747 static int thread_setup_libctx(int libctx, const char *providers[])
748 {
749     size_t n;
750 
751     if (libctx && !TEST_true(test_get_libctx(&multi_libctx, NULL, config_file,
752                                              NULL, NULL)))
753         return 0;
754 
755     if (providers != NULL)
756         for (n = 0; providers[n] != NULL; n++)
757             if (!TEST_size_t_lt(n, MAXIMUM_PROVIDERS)
758                 || !TEST_ptr(multi_provider[n] = OSSL_PROVIDER_load(multi_libctx,
759                                                                     providers[n]))) {
760                 thead_teardown_libctx();
761                 return 0;
762             }
763     return 1;
764 }
765 
teardown_threads(void)766 static int teardown_threads(void)
767 {
768     size_t i;
769 
770     for (i = 0; i < multi_num_threads; i++)
771         if (!TEST_true(wait_for_thread(multi_threads[i])))
772             return 0;
773     return 1;
774 }
775 
start_threads(size_t n,void (* thread_func)(void))776 static int start_threads(size_t n, void (*thread_func)(void))
777 {
778     size_t i;
779 
780     if (!TEST_size_t_le(multi_num_threads + n, MAXIMUM_THREADS))
781         return 0;
782 
783     for (i = 0 ; i < n; i++)
784         if (!TEST_true(run_thread(multi_threads + multi_num_threads++, thread_func)))
785             return 0;
786     return 1;
787 }
788 
789 /* Template multi-threaded test function */
thread_run_test(void (* main_func)(void),size_t num_threads,void (* thread_func)(void),int libctx,const char * providers[])790 static int thread_run_test(void (*main_func)(void),
791                            size_t num_threads, void (*thread_func)(void),
792                            int libctx, const char *providers[])
793 {
794     int testresult = 0;
795 
796     multi_intialise();
797     if (!thread_setup_libctx(libctx, providers)
798             || !start_threads(num_threads, thread_func))
799         goto err;
800 
801     if (main_func != NULL)
802         main_func();
803 
804     if (!teardown_threads()
805             || !TEST_true(multi_success))
806         goto err;
807     testresult = 1;
808  err:
809     thead_teardown_libctx();
810     return testresult;
811 }
812 
thread_general_worker(void)813 static void thread_general_worker(void)
814 {
815     EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
816     EVP_MD *md = EVP_MD_fetch(multi_libctx, "SHA2-256", NULL);
817     EVP_CIPHER_CTX *cipherctx = EVP_CIPHER_CTX_new();
818     EVP_CIPHER *ciph = EVP_CIPHER_fetch(multi_libctx, "AES-128-CBC", NULL);
819     const char *message = "Hello World";
820     size_t messlen = strlen(message);
821     /* Should be big enough for encryption output too */
822     unsigned char out[EVP_MAX_MD_SIZE];
823     const unsigned char key[AES_BLOCK_SIZE] = {
824         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
825         0x0c, 0x0d, 0x0e, 0x0f
826     };
827     const unsigned char iv[AES_BLOCK_SIZE] = {
828         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
829         0x0c, 0x0d, 0x0e, 0x0f
830     };
831     unsigned int mdoutl;
832     int ciphoutl;
833     EVP_PKEY *pkey = NULL;
834     int testresult = 0;
835     int i, isfips;
836 
837     isfips = OSSL_PROVIDER_available(multi_libctx, "fips");
838 
839     if (!TEST_ptr(mdctx)
840             || !TEST_ptr(md)
841             || !TEST_ptr(cipherctx)
842             || !TEST_ptr(ciph))
843         goto err;
844 
845     /* Do some work */
846     for (i = 0; i < 5; i++) {
847         if (!TEST_true(EVP_DigestInit_ex(mdctx, md, NULL))
848                 || !TEST_true(EVP_DigestUpdate(mdctx, message, messlen))
849                 || !TEST_true(EVP_DigestFinal(mdctx, out, &mdoutl)))
850             goto err;
851     }
852     for (i = 0; i < 5; i++) {
853         if (!TEST_true(EVP_EncryptInit_ex(cipherctx, ciph, NULL, key, iv))
854                 || !TEST_true(EVP_EncryptUpdate(cipherctx, out, &ciphoutl,
855                                                 (unsigned char *)message,
856                                                 messlen))
857                 || !TEST_true(EVP_EncryptFinal(cipherctx, out, &ciphoutl)))
858             goto err;
859     }
860 
861     /*
862      * We want the test to run quickly - not securely.
863      * Therefore we use an insecure bit length where we can (512).
864      * In the FIPS module though we must use a longer length.
865      */
866     pkey = EVP_PKEY_Q_keygen(multi_libctx, NULL, "RSA", isfips ? 2048 : 512);
867     if (!TEST_ptr(pkey))
868         goto err;
869 
870     testresult = 1;
871  err:
872     EVP_MD_CTX_free(mdctx);
873     EVP_MD_free(md);
874     EVP_CIPHER_CTX_free(cipherctx);
875     EVP_CIPHER_free(ciph);
876     EVP_PKEY_free(pkey);
877     if (!testresult)
878         multi_set_success(0);
879 }
880 
thread_multi_simple_fetch(void)881 static void thread_multi_simple_fetch(void)
882 {
883     EVP_MD *md = EVP_MD_fetch(multi_libctx, "SHA2-256", NULL);
884 
885     if (md != NULL)
886         EVP_MD_free(md);
887     else
888         multi_set_success(0);
889 }
890 
891 static EVP_PKEY *shared_evp_pkey = NULL;
892 
thread_shared_evp_pkey(void)893 static void thread_shared_evp_pkey(void)
894 {
895     char *msg = "Hello World";
896     unsigned char ctbuf[256];
897     unsigned char ptbuf[256];
898     size_t ptlen, ctlen = sizeof(ctbuf);
899     EVP_PKEY_CTX *ctx = NULL;
900     int success = 0;
901     int i;
902 
903     for (i = 0; i < 1 + do_fips; i++) {
904         if (i > 0)
905             EVP_PKEY_CTX_free(ctx);
906         ctx = EVP_PKEY_CTX_new_from_pkey(multi_libctx, shared_evp_pkey,
907                                          i == 0 ? "provider=default"
908                                                 : "provider=fips");
909         if (!TEST_ptr(ctx))
910             goto err;
911 
912         if (!TEST_int_ge(EVP_PKEY_encrypt_init(ctx), 0)
913                 || !TEST_int_ge(EVP_PKEY_encrypt(ctx, ctbuf, &ctlen,
914                                                 (unsigned char *)msg, strlen(msg)),
915                                                 0))
916             goto err;
917 
918         EVP_PKEY_CTX_free(ctx);
919         ctx = EVP_PKEY_CTX_new_from_pkey(multi_libctx, shared_evp_pkey, NULL);
920 
921         if (!TEST_ptr(ctx))
922             goto err;
923 
924         ptlen = sizeof(ptbuf);
925         if (!TEST_int_ge(EVP_PKEY_decrypt_init(ctx), 0)
926                 || !TEST_int_gt(EVP_PKEY_decrypt(ctx, ptbuf, &ptlen, ctbuf, ctlen),
927                                                 0)
928                 || !TEST_mem_eq(msg, strlen(msg), ptbuf, ptlen))
929             goto err;
930     }
931 
932     success = 1;
933 
934  err:
935     EVP_PKEY_CTX_free(ctx);
936     if (!success)
937         multi_set_success(0);
938 }
939 
thread_provider_load_unload(void)940 static void thread_provider_load_unload(void)
941 {
942     OSSL_PROVIDER *deflt = OSSL_PROVIDER_load(multi_libctx, "default");
943 
944     if (!TEST_ptr(deflt)
945             || !TEST_true(OSSL_PROVIDER_available(multi_libctx, "default")))
946         multi_set_success(0);
947 
948     OSSL_PROVIDER_unload(deflt);
949 }
950 
test_multi_general_worker_default_provider(void)951 static int test_multi_general_worker_default_provider(void)
952 {
953     return thread_run_test(&thread_general_worker, 2, &thread_general_worker,
954                            1, default_provider);
955 }
956 
test_multi_general_worker_fips_provider(void)957 static int test_multi_general_worker_fips_provider(void)
958 {
959     if (!do_fips)
960         return TEST_skip("FIPS not supported");
961     return thread_run_test(&thread_general_worker, 2, &thread_general_worker,
962                            1, fips_provider);
963 }
964 
test_multi_fetch_worker(void)965 static int test_multi_fetch_worker(void)
966 {
967     return thread_run_test(&thread_multi_simple_fetch,
968                            2, &thread_multi_simple_fetch, 1, default_provider);
969 }
970 
test_multi_shared_pkey_common(void (* worker)(void))971 static int test_multi_shared_pkey_common(void (*worker)(void))
972 {
973     int testresult = 0;
974 
975     multi_intialise();
976     if (!thread_setup_libctx(1, do_fips ? fips_and_default_providers
977                                         : default_provider)
978             || !TEST_ptr(shared_evp_pkey = load_pkey_pem(privkey, multi_libctx))
979             || !start_threads(1, &thread_shared_evp_pkey)
980             || !start_threads(1, worker))
981         goto err;
982 
983     thread_shared_evp_pkey();
984 
985     if (!teardown_threads()
986             || !TEST_true(multi_success))
987         goto err;
988     testresult = 1;
989  err:
990     EVP_PKEY_free(shared_evp_pkey);
991     thead_teardown_libctx();
992     return testresult;
993 }
994 
995 #ifndef OPENSSL_NO_DEPRECATED_3_0
thread_downgrade_shared_evp_pkey(void)996 static void thread_downgrade_shared_evp_pkey(void)
997 {
998     /*
999      * This test is only relevant for deprecated functions that perform
1000      * downgrading
1001      */
1002     if (EVP_PKEY_get0_RSA(shared_evp_pkey) == NULL)
1003         multi_set_success(0);
1004 }
1005 
test_multi_downgrade_shared_pkey(void)1006 static int test_multi_downgrade_shared_pkey(void)
1007 {
1008     return test_multi_shared_pkey_common(&thread_downgrade_shared_evp_pkey);
1009 }
1010 #endif
1011 
test_multi_shared_pkey(void)1012 static int test_multi_shared_pkey(void)
1013 {
1014     return test_multi_shared_pkey_common(&thread_shared_evp_pkey);
1015 }
1016 
test_multi_load_unload_provider(void)1017 static int test_multi_load_unload_provider(void)
1018 {
1019     EVP_MD *sha256 = NULL;
1020     OSSL_PROVIDER *prov = NULL;
1021     int testresult = 0;
1022 
1023     multi_intialise();
1024     if (!thread_setup_libctx(1, NULL)
1025             || !TEST_ptr(prov = OSSL_PROVIDER_load(multi_libctx, "default"))
1026             || !TEST_ptr(sha256 = EVP_MD_fetch(multi_libctx, "SHA2-256", NULL))
1027             || !TEST_true(OSSL_PROVIDER_unload(prov)))
1028         goto err;
1029     prov = NULL;
1030 
1031     if (!start_threads(2, &thread_provider_load_unload))
1032         goto err;
1033 
1034     thread_provider_load_unload();
1035 
1036     if (!teardown_threads()
1037             || !TEST_true(multi_success))
1038         goto err;
1039     testresult = 1;
1040  err:
1041     OSSL_PROVIDER_unload(prov);
1042     EVP_MD_free(sha256);
1043     thead_teardown_libctx();
1044     return testresult;
1045 }
1046 
1047 static char *multi_load_provider = "legacy";
1048 /*
1049  * This test attempts to load several providers at the same time, and if
1050  * run with a thread sanitizer, should crash if the core provider code
1051  * doesn't synchronize well enough.
1052  */
test_multi_load_worker(void)1053 static void test_multi_load_worker(void)
1054 {
1055     OSSL_PROVIDER *prov;
1056 
1057     if (!TEST_ptr(prov = OSSL_PROVIDER_load(multi_libctx, multi_load_provider))
1058             || !TEST_true(OSSL_PROVIDER_unload(prov)))
1059         multi_set_success(0);
1060 }
1061 
test_multi_default(void)1062 static int test_multi_default(void)
1063 {
1064     /* Avoid running this test twice */
1065     if (multidefault_run) {
1066         TEST_skip("multi default test already run");
1067         return 1;
1068     }
1069     multidefault_run = 1;
1070 
1071     return thread_run_test(&thread_multi_simple_fetch,
1072                            2, &thread_multi_simple_fetch, 0, default_provider);
1073 }
1074 
test_multi_load(void)1075 static int test_multi_load(void)
1076 {
1077     int res = 1;
1078     OSSL_PROVIDER *prov;
1079 
1080     /* The multidefault test must run prior to this test */
1081     if (!multidefault_run) {
1082         TEST_info("Running multi default test first");
1083         res = test_multi_default();
1084     }
1085 
1086     /*
1087      * We use the legacy provider in test_multi_load_worker because it uses a
1088      * child libctx that might hit more codepaths that might be sensitive to
1089      * threading issues. But in a no-legacy build that won't be loadable so
1090      * we use the default provider instead.
1091      */
1092     prov = OSSL_PROVIDER_load(NULL, "legacy");
1093     if (prov == NULL) {
1094         TEST_info("Cannot load legacy provider - assuming this is a no-legacy build");
1095         multi_load_provider = "default";
1096     }
1097     OSSL_PROVIDER_unload(prov);
1098 
1099     return thread_run_test(NULL, MAXIMUM_THREADS, &test_multi_load_worker, 0,
1100                           NULL) && res;
1101 }
1102 
test_obj_create_one(void)1103 static void test_obj_create_one(void)
1104 {
1105     char tids[12], oid[40], sn[30], ln[30];
1106     int id = get_new_uid();
1107 
1108     BIO_snprintf(tids, sizeof(tids), "%d", id);
1109     BIO_snprintf(oid, sizeof(oid), "1.3.6.1.4.1.16604.%s", tids);
1110     BIO_snprintf(sn, sizeof(sn), "short-name-%s", tids);
1111     BIO_snprintf(ln, sizeof(ln), "long-name-%s", tids);
1112     if (!TEST_int_ne(id, 0)
1113             || !TEST_true(id = OBJ_create(oid, sn, ln))
1114             || !TEST_true(OBJ_add_sigid(id, NID_sha3_256, NID_rsa)))
1115         multi_set_success(0);
1116 }
1117 
test_obj_add(void)1118 static int test_obj_add(void)
1119 {
1120     return thread_run_test(&test_obj_create_one,
1121                            MAXIMUM_THREADS, &test_obj_create_one,
1122                            1, default_provider);
1123 }
1124 
1125 #if !defined(OPENSSL_NO_DGRAM) && !defined(OPENSSL_NO_SOCK)
1126 static BIO *multi_bio1, *multi_bio2;
1127 
test_bio_dgram_pair_worker(void)1128 static void test_bio_dgram_pair_worker(void)
1129 {
1130     ossl_unused int r;
1131     int ok = 0;
1132     uint8_t ch = 0;
1133     uint8_t scratch[64];
1134     BIO_MSG msg = {0};
1135     size_t num_processed = 0;
1136 
1137     if (!TEST_int_eq(RAND_bytes_ex(multi_libctx, &ch, 1, 64), 1))
1138         goto err;
1139 
1140     msg.data     = scratch;
1141     msg.data_len = sizeof(scratch);
1142 
1143     /*
1144      * We do not test for failure here as recvmmsg may fail if no sendmmsg
1145      * has been called yet. The purpose of this code is to exercise tsan.
1146      */
1147     if (ch & 2)
1148         r = BIO_sendmmsg(ch & 1 ? multi_bio2 : multi_bio1, &msg,
1149                          sizeof(BIO_MSG), 1, 0, &num_processed);
1150     else
1151         r = BIO_recvmmsg(ch & 1 ? multi_bio2 : multi_bio1, &msg,
1152                          sizeof(BIO_MSG), 1, 0, &num_processed);
1153 
1154     ok = 1;
1155 err:
1156     if (ok == 0)
1157         multi_set_success(0);
1158 }
1159 
test_bio_dgram_pair(void)1160 static int test_bio_dgram_pair(void)
1161 {
1162     int r;
1163     BIO *bio1 = NULL, *bio2 = NULL;
1164 
1165     r = BIO_new_bio_dgram_pair(&bio1, 0, &bio2, 0);
1166     if (!TEST_int_eq(r, 1))
1167         goto err;
1168 
1169     multi_bio1 = bio1;
1170     multi_bio2 = bio2;
1171 
1172     r  = thread_run_test(&test_bio_dgram_pair_worker,
1173                          MAXIMUM_THREADS, &test_bio_dgram_pair_worker,
1174                          1, default_provider);
1175 
1176 err:
1177     BIO_free(bio1);
1178     BIO_free(bio2);
1179     return r;
1180 }
1181 #endif
1182 
1183 static const char *pemdataraw[] = {
1184     "-----BEGIN RSA PRIVATE KEY-----\n",
1185     "MIIBOgIBAAJBAMFcGsaxxdgiuuGmCkVImy4h99CqT7jwY3pexPGcnUFtR2Fh36Bp\n",
1186     "oncwtkZ4cAgtvd4Qs8PkxUdp6p/DlUmObdkCAwEAAQJAUR44xX6zB3eaeyvTRzms\n",
1187     "kHADrPCmPWnr8dxsNwiDGHzrMKLN+i/HAam+97HxIKVWNDH2ba9Mf1SA8xu9dcHZ\n",
1188     "AQIhAOHPCLxbtQFVxlnhSyxYeb7O323c3QulPNn3bhOipElpAiEA2zZpBE8ZXVnL\n",
1189     "74QjG4zINlDfH+EOEtjJJ3RtaYDugvECIBtsQDxXytChsRgDQ1TcXdStXPcDppie\n",
1190     "dZhm8yhRTTBZAiAZjE/U9rsIDC0ebxIAZfn3iplWh84yGB3pgUI3J5WkoQIhAInE\n",
1191     "HTUY5WRj5riZtkyGnbm3DvF+1eMtO2lYV+OuLcfE\n",
1192     "-----END RSA PRIVATE KEY-----\n",
1193     NULL
1194 };
1195 
test_pem_read_one(void)1196 static void test_pem_read_one(void)
1197 {
1198     EVP_PKEY *key = NULL;
1199     BIO *pem = NULL;
1200     char *pemdata;
1201     size_t len;
1202 
1203     pemdata = glue_strings(pemdataraw, &len);
1204     if (pemdata == NULL) {
1205         multi_set_success(0);
1206         goto err;
1207     }
1208 
1209     pem = BIO_new_mem_buf(pemdata, len);
1210     if (pem == NULL) {
1211         multi_set_success(0);
1212         goto err;
1213     }
1214 
1215     key = PEM_read_bio_PrivateKey(pem, NULL, NULL, NULL);
1216     if (key == NULL)
1217         multi_set_success(0);
1218 
1219  err:
1220     EVP_PKEY_free(key);
1221     BIO_free(pem);
1222     OPENSSL_free(pemdata);
1223 }
1224 
1225 /* Test reading PEM files in multiple threads */
test_pem_read(void)1226 static int test_pem_read(void)
1227 {
1228     return thread_run_test(&test_pem_read_one, MAXIMUM_THREADS,
1229                            &test_pem_read_one, 1, default_provider);
1230 }
1231 
1232 typedef enum OPTION_choice {
1233     OPT_ERR = -1,
1234     OPT_EOF = 0,
1235     OPT_FIPS, OPT_CONFIG_FILE,
1236     OPT_TEST_ENUM
1237 } OPTION_CHOICE;
1238 
test_get_options(void)1239 const OPTIONS *test_get_options(void)
1240 {
1241     static const OPTIONS options[] = {
1242         OPT_TEST_OPTIONS_DEFAULT_USAGE,
1243         { "fips", OPT_FIPS, '-', "Test the FIPS provider" },
1244         { "config", OPT_CONFIG_FILE, '<',
1245           "The configuration file to use for the libctx" },
1246         { NULL }
1247     };
1248     return options;
1249 }
1250 
setup_tests(void)1251 int setup_tests(void)
1252 {
1253     OPTION_CHOICE o;
1254     char *datadir;
1255 
1256     while ((o = opt_next()) != OPT_EOF) {
1257         switch (o) {
1258         case OPT_FIPS:
1259             do_fips = 1;
1260             break;
1261         case OPT_CONFIG_FILE:
1262             config_file = opt_arg();
1263             break;
1264         case OPT_TEST_CASES:
1265             break;
1266         default:
1267             return 0;
1268         }
1269     }
1270 
1271     if (!TEST_ptr(datadir = test_get_argument(0)))
1272         return 0;
1273 
1274     privkey = test_mk_file_path(datadir, "rsakey.pem");
1275     if (!TEST_ptr(privkey))
1276         return 0;
1277 
1278     if (!TEST_ptr(global_lock = CRYPTO_THREAD_lock_new()))
1279         return 0;
1280 
1281 #ifdef TSAN_REQUIRES_LOCKING
1282     if (!TEST_ptr(tsan_lock = CRYPTO_THREAD_lock_new()))
1283         return 0;
1284 #endif
1285 
1286     /* Keep first to validate auto creation of default library context */
1287     ADD_TEST(test_multi_default);
1288 
1289     ADD_TEST(test_lock);
1290 #if defined(OPENSSL_THREADS)
1291     ADD_TEST(torture_rw_low);
1292     ADD_TEST(torture_rw_high);
1293 # ifndef OPENSSL_SYS_MACOSX
1294     ADD_TEST(torture_rcu_low);
1295     ADD_TEST(torture_rcu_high);
1296 # endif
1297 #endif
1298     ADD_TEST(test_once);
1299     ADD_TEST(test_thread_local);
1300     ADD_TEST(test_atomic);
1301     ADD_TEST(test_multi_load);
1302     ADD_TEST(test_multi_general_worker_default_provider);
1303     ADD_TEST(test_multi_general_worker_fips_provider);
1304     ADD_TEST(test_multi_fetch_worker);
1305     ADD_TEST(test_multi_shared_pkey);
1306 #ifndef OPENSSL_NO_DEPRECATED_3_0
1307     ADD_TEST(test_multi_downgrade_shared_pkey);
1308 #endif
1309     ADD_TEST(test_multi_load_unload_provider);
1310     ADD_TEST(test_obj_add);
1311 #if !defined(OPENSSL_NO_DGRAM) && !defined(OPENSSL_NO_SOCK)
1312     ADD_TEST(test_bio_dgram_pair);
1313 #endif
1314     ADD_TEST(test_pem_read);
1315     return 1;
1316 }
1317 
cleanup_tests(void)1318 void cleanup_tests(void)
1319 {
1320     OPENSSL_free(privkey);
1321 #ifdef TSAN_REQUIRES_LOCKING
1322     CRYPTO_THREAD_lock_free(tsan_lock);
1323 #endif
1324     CRYPTO_THREAD_lock_free(global_lock);
1325 }
1326