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 /*
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(¤t_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 static CRYPTO_RCU_LOCK *rcu_lock = NULL;
299
300 static int writer1_done = 0;
301 static int writer2_done = 0;
302 static int reader1_iterations = 0;
303 static int reader2_iterations = 0;
304 static int writer1_iterations = 0;
305 static int writer2_iterations = 0;
306 static uint64_t *writer_ptr = NULL;
307 static uint64_t global_ctr = 0;
308 static int rcu_torture_result = 1;
free_old_rcu_data(void * data)309 static void free_old_rcu_data(void *data)
310 {
311 CRYPTO_free(data, NULL, 0);
312 }
313
writer_fn(int id,int * iterations)314 static void writer_fn(int id, int *iterations)
315 {
316 int count;
317 OSSL_TIME t1, t2;
318 uint64_t *old, *new;
319
320 t1 = ossl_time_now();
321
322 for (count = 0; ; count++) {
323 new = CRYPTO_zalloc(sizeof(uint64_t), NULL, 0);
324 if (contention == 0)
325 OSSL_sleep(1000);
326 ossl_rcu_write_lock(rcu_lock);
327 old = ossl_rcu_deref(&writer_ptr);
328 TSAN_ACQUIRE(&writer_ptr);
329 *new = global_ctr++;
330 ossl_rcu_assign_ptr(&writer_ptr, &new);
331 if (contention == 0)
332 ossl_rcu_call(rcu_lock, free_old_rcu_data, old);
333 ossl_rcu_write_unlock(rcu_lock);
334 if (contention != 0) {
335 ossl_synchronize_rcu(rcu_lock);
336 CRYPTO_free(old, NULL, 0);
337 }
338 t2 = ossl_time_now();
339 if ((ossl_time2seconds(t2) - ossl_time2seconds(t1)) >= 4)
340 break;
341 }
342 *iterations = count;
343 return;
344 }
345
writer1_fn(void)346 static void writer1_fn(void)
347 {
348 int local;
349
350 TEST_info("Starting writer1");
351 writer_fn(1, &writer1_iterations);
352 CRYPTO_atomic_add(&writer1_done, 1, &local, atomiclock);
353 }
354
writer2_fn(void)355 static void writer2_fn(void)
356 {
357 int local;
358
359 TEST_info("Starting writer2");
360 writer_fn(2, &writer2_iterations);
361 CRYPTO_atomic_add(&writer2_done, 1, &local, atomiclock);
362 }
363
reader_fn(int * iterations)364 static void reader_fn(int *iterations)
365 {
366 unsigned int count = 0;
367 uint64_t *valp;
368 uint64_t val;
369 uint64_t oldval = 0;
370 int lw1 = 0;
371 int lw2 = 0;
372
373 while (lw1 != 1 || lw2 != 1) {
374 CRYPTO_atomic_add(&writer1_done, 0, &lw1, atomiclock);
375 CRYPTO_atomic_add(&writer2_done, 0, &lw2, atomiclock);
376 count++;
377 ossl_rcu_read_lock(rcu_lock);
378 valp = ossl_rcu_deref(&writer_ptr);
379 val = (valp == NULL) ? 0 : *valp;
380
381 if (oldval > val) {
382 TEST_info("rcu torture value went backwards! %llu : %llu", (unsigned long long)oldval, (unsigned long long)val);
383 rcu_torture_result = 0;
384 }
385 oldval = val; /* just try to deref the pointer */
386 ossl_rcu_read_unlock(rcu_lock);
387 if (rcu_torture_result == 0) {
388 *iterations = count;
389 return;
390 }
391 }
392 *iterations = count;
393 }
394
reader1_fn(void)395 static void reader1_fn(void)
396 {
397 TEST_info("Starting reader 1");
398 reader_fn(&reader1_iterations);
399 }
400
reader2_fn(void)401 static void reader2_fn(void)
402 {
403 TEST_info("Starting reader 2");
404 reader_fn(&reader2_iterations);
405 }
406
407 static thread_t writer1;
408 static thread_t writer2;
409 static thread_t reader1;
410 static thread_t reader2;
411
_torture_rcu(void)412 static int _torture_rcu(void)
413 {
414 OSSL_TIME t1, t2;
415 struct timeval dtime;
416 double tottime;
417 double avr, avw;
418 int rc = 0;
419
420 atomiclock = CRYPTO_THREAD_lock_new();
421 if (!TEST_ptr(atomiclock))
422 goto out;
423
424 memset(&writer1, 0, sizeof(thread_t));
425 memset(&writer2, 0, sizeof(thread_t));
426 memset(&reader1, 0, sizeof(thread_t));
427 memset(&reader2, 0, sizeof(thread_t));
428
429 writer1_iterations = 0;
430 writer2_iterations = 0;
431 reader1_iterations = 0;
432 reader2_iterations = 0;
433 writer1_done = 0;
434 writer2_done = 0;
435 rcu_torture_result = 1;
436
437 rcu_lock = ossl_rcu_lock_new(contention == 2 ? 4 : 1, NULL);
438 if (rcu_lock == NULL)
439 goto out;
440
441 TEST_info("Staring rcu torture");
442 t1 = ossl_time_now();
443 if (!TEST_true(run_thread(&reader1, reader1_fn))
444 || !TEST_true(run_thread(&reader2, reader2_fn))
445 || !TEST_true(run_thread(&writer1, writer1_fn))
446 || !TEST_true(run_thread(&writer2, writer2_fn))
447 || !TEST_true(wait_for_thread(writer1))
448 || !TEST_true(wait_for_thread(writer2))
449 || !TEST_true(wait_for_thread(reader1))
450 || !TEST_true(wait_for_thread(reader2)))
451 goto out;
452
453 t2 = ossl_time_now();
454 dtime = ossl_time_to_timeval(ossl_time_subtract(t2, t1));
455 tottime = dtime.tv_sec + (dtime.tv_usec / 1e6);
456 TEST_info("rcu_torture_result is %d\n", rcu_torture_result);
457 TEST_info("performed %d reads and %d writes over 2 read and 2 write threads in %e seconds",
458 reader1_iterations + reader2_iterations,
459 writer1_iterations + writer2_iterations, tottime);
460 if ((reader1_iterations + reader2_iterations == 0)
461 || (writer1_iterations + writer2_iterations == 0)) {
462 TEST_info("Threads did not iterate\n");
463 goto out;
464 }
465 avr = tottime / (reader1_iterations + reader2_iterations);
466 avw = tottime / (writer1_iterations + writer2_iterations);
467 TEST_info("Average read time %e/read", avr);
468 TEST_info("Average write time %e/write", avw);
469
470 if (!TEST_int_eq(rcu_torture_result, 1))
471 goto out;
472
473 rc = 1;
474 out:
475 ossl_rcu_lock_free(rcu_lock);
476 CRYPTO_THREAD_lock_free(atomiclock);
477 if (!TEST_int_eq(rcu_torture_result, 1))
478 return 0;
479
480 return rc;
481 }
482
torture_rcu_low(void)483 static int torture_rcu_low(void)
484 {
485 contention = 0;
486 return _torture_rcu();
487 }
488
torture_rcu_high(void)489 static int torture_rcu_high(void)
490 {
491 contention = 1;
492 return _torture_rcu();
493 }
494
torture_rcu_high2(void)495 static int torture_rcu_high2(void)
496 {
497 contention = 2;
498 return _torture_rcu();
499 }
500 #endif
501
502 static CRYPTO_ONCE once_run = CRYPTO_ONCE_STATIC_INIT;
503 static unsigned once_run_count = 0;
504
once_do_run(void)505 static void once_do_run(void)
506 {
507 once_run_count++;
508 }
509
once_run_thread_cb(void)510 static void once_run_thread_cb(void)
511 {
512 CRYPTO_THREAD_run_once(&once_run, once_do_run);
513 }
514
test_once(void)515 static int test_once(void)
516 {
517 thread_t thread;
518
519 if (!TEST_true(run_thread(&thread, once_run_thread_cb))
520 || !TEST_true(wait_for_thread(thread))
521 || !CRYPTO_THREAD_run_once(&once_run, once_do_run)
522 || !TEST_int_eq(once_run_count, 1))
523 return 0;
524 return 1;
525 }
526
527 static CRYPTO_THREAD_LOCAL thread_local_key;
528 static unsigned destructor_run_count = 0;
529 static int thread_local_thread_cb_ok = 0;
530
thread_local_destructor(void * arg)531 static void thread_local_destructor(void *arg)
532 {
533 unsigned *count;
534
535 if (arg == NULL)
536 return;
537
538 count = arg;
539
540 (*count)++;
541 }
542
thread_local_thread_cb(void)543 static void thread_local_thread_cb(void)
544 {
545 void *ptr;
546
547 ptr = CRYPTO_THREAD_get_local(&thread_local_key);
548 if (!TEST_ptr_null(ptr)
549 || !TEST_true(CRYPTO_THREAD_set_local(&thread_local_key,
550 &destructor_run_count)))
551 return;
552
553 ptr = CRYPTO_THREAD_get_local(&thread_local_key);
554 if (!TEST_ptr_eq(ptr, &destructor_run_count))
555 return;
556
557 thread_local_thread_cb_ok = 1;
558 }
559
test_thread_local(void)560 static int test_thread_local(void)
561 {
562 thread_t thread;
563 void *ptr = NULL;
564
565 if (!TEST_true(CRYPTO_THREAD_init_local(&thread_local_key,
566 thread_local_destructor)))
567 return 0;
568
569 ptr = CRYPTO_THREAD_get_local(&thread_local_key);
570 if (!TEST_ptr_null(ptr)
571 || !TEST_true(run_thread(&thread, thread_local_thread_cb))
572 || !TEST_true(wait_for_thread(thread))
573 || !TEST_int_eq(thread_local_thread_cb_ok, 1))
574 return 0;
575
576 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
577
578 ptr = CRYPTO_THREAD_get_local(&thread_local_key);
579 if (!TEST_ptr_null(ptr))
580 return 0;
581
582 # if !defined(OPENSSL_SYS_WINDOWS)
583 if (!TEST_int_eq(destructor_run_count, 1))
584 return 0;
585 # endif
586 #endif
587
588 if (!TEST_true(CRYPTO_THREAD_cleanup_local(&thread_local_key)))
589 return 0;
590 return 1;
591 }
592
593 /*
594 * Basic test to ensure that we can repeatedly create and
595 * destroy local keys without leaking anything
596 */
test_thread_local_multi_key(void)597 static int test_thread_local_multi_key(void)
598 {
599 int dummy;
600 int i;
601
602 for (i = 0; i < 1000; i++) {
603 if (!TEST_true(CRYPTO_THREAD_init_local(&thread_local_key,
604 thread_local_destructor)))
605 return 0;
606
607 if (!TEST_true(CRYPTO_THREAD_set_local(&thread_local_key, &dummy)))
608 return 0;
609
610 if (!TEST_true(CRYPTO_THREAD_cleanup_local(&thread_local_key)))
611 return 0;
612 }
613 return 1;
614 }
615
test_atomic(void)616 static int test_atomic(void)
617 {
618 int val = 0, ret = 0, testresult = 0;
619 uint64_t val64 = 1, ret64 = 0;
620 CRYPTO_RWLOCK *lock = CRYPTO_THREAD_lock_new();
621
622 if (!TEST_ptr(lock))
623 return 0;
624
625 if (CRYPTO_atomic_add(&val, 1, &ret, NULL)) {
626 /* This succeeds therefore we're on a platform with lockless atomics */
627 if (!TEST_int_eq(val, 1) || !TEST_int_eq(val, ret))
628 goto err;
629 } else {
630 /* This failed therefore we're on a platform without lockless atomics */
631 if (!TEST_int_eq(val, 0) || !TEST_int_eq(val, ret))
632 goto err;
633 }
634 val = 0;
635 ret = 0;
636
637 if (!TEST_true(CRYPTO_atomic_add(&val, 1, &ret, lock)))
638 goto err;
639 if (!TEST_int_eq(val, 1) || !TEST_int_eq(val, ret))
640 goto err;
641
642 if (CRYPTO_atomic_or(&val64, 2, &ret64, NULL)) {
643 /* This succeeds therefore we're on a platform with lockless atomics */
644 if (!TEST_uint_eq((unsigned int)val64, 3)
645 || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
646 goto err;
647 } else {
648 /* This failed therefore we're on a platform without lockless atomics */
649 if (!TEST_uint_eq((unsigned int)val64, 1)
650 || !TEST_int_eq((unsigned int)ret64, 0))
651 goto err;
652 }
653 val64 = 1;
654 ret64 = 0;
655
656 if (!TEST_true(CRYPTO_atomic_or(&val64, 2, &ret64, lock)))
657 goto err;
658
659 if (!TEST_uint_eq((unsigned int)val64, 3)
660 || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
661 goto err;
662
663 ret64 = 0;
664 if (CRYPTO_atomic_load(&val64, &ret64, NULL)) {
665 /* This succeeds therefore we're on a platform with lockless atomics */
666 if (!TEST_uint_eq((unsigned int)val64, 3)
667 || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
668 goto err;
669 } else {
670 /* This failed therefore we're on a platform without lockless atomics */
671 if (!TEST_uint_eq((unsigned int)val64, 3)
672 || !TEST_int_eq((unsigned int)ret64, 0))
673 goto err;
674 }
675
676 ret64 = 0;
677 if (!TEST_true(CRYPTO_atomic_load(&val64, &ret64, lock)))
678 goto err;
679
680 if (!TEST_uint_eq((unsigned int)val64, 3)
681 || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
682 goto err;
683
684 ret64 = 0;
685
686 if (CRYPTO_atomic_and(&val64, 5, &ret64, NULL)) {
687 /* This succeeds therefore we're on a platform with lockless atomics */
688 if (!TEST_uint_eq((unsigned int)val64, 1)
689 || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
690 goto err;
691 } else {
692 /* This failed therefore we're on a platform without lockless atomics */
693 if (!TEST_uint_eq((unsigned int)val64, 3)
694 || !TEST_int_eq((unsigned int)ret64, 0))
695 goto err;
696 }
697 val64 = 3;
698 ret64 = 0;
699
700 if (!TEST_true(CRYPTO_atomic_and(&val64, 5, &ret64, lock)))
701 goto err;
702
703 if (!TEST_uint_eq((unsigned int)val64, 1)
704 || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
705 goto err;
706
707 ret64 = 0;
708
709 if (CRYPTO_atomic_add64(&val64, 2, &ret64, NULL)) {
710 /* This succeeds therefore we're on a platform with lockless atomics */
711 if (!TEST_uint_eq((unsigned int)val64, 3)
712 || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
713 goto err;
714 } else {
715 /* This failed therefore we're on a platform without lockless atomics */
716 if (!TEST_uint_eq((unsigned int)val64, 1)
717 || !TEST_int_eq((unsigned int)ret64, 0))
718 goto err;
719 }
720 val64 = 1;
721 ret64 = 0;
722
723 if (!TEST_true(CRYPTO_atomic_add64(&val64, 2, &ret64, lock)))
724 goto err;
725
726 if (!TEST_uint_eq((unsigned int)val64, 3)
727 || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
728 goto err;
729
730 testresult = 1;
731 err:
732 CRYPTO_THREAD_lock_free(lock);
733 return testresult;
734 }
735
736 static OSSL_LIB_CTX *multi_libctx = NULL;
737 static int multi_success;
738 static OSSL_PROVIDER *multi_provider[MAXIMUM_PROVIDERS + 1];
739 static size_t multi_num_threads;
740 static thread_t multi_threads[MAXIMUM_THREADS];
741
multi_intialise(void)742 static void multi_intialise(void)
743 {
744 multi_success = 1;
745 multi_libctx = NULL;
746 multi_num_threads = 0;
747 memset(multi_threads, 0, sizeof(multi_threads));
748 memset(multi_provider, 0, sizeof(multi_provider));
749 }
750
multi_set_success(int ok)751 static void multi_set_success(int ok)
752 {
753 if (CRYPTO_THREAD_write_lock(global_lock) == 0) {
754 /* not synchronized, but better than not reporting failure */
755 multi_success = ok;
756 return;
757 }
758
759 multi_success = ok;
760
761 CRYPTO_THREAD_unlock(global_lock);
762 }
763
thead_teardown_libctx(void)764 static void thead_teardown_libctx(void)
765 {
766 OSSL_PROVIDER **p;
767
768 for (p = multi_provider; *p != NULL; p++)
769 OSSL_PROVIDER_unload(*p);
770 OSSL_LIB_CTX_free(multi_libctx);
771 multi_intialise();
772 }
773
thread_setup_libctx(int libctx,const char * providers[])774 static int thread_setup_libctx(int libctx, const char *providers[])
775 {
776 size_t n;
777
778 if (libctx && !TEST_true(test_get_libctx(&multi_libctx, NULL, config_file,
779 NULL, NULL)))
780 return 0;
781
782 if (providers != NULL)
783 for (n = 0; providers[n] != NULL; n++)
784 if (!TEST_size_t_lt(n, MAXIMUM_PROVIDERS)
785 || !TEST_ptr(multi_provider[n] = OSSL_PROVIDER_load(multi_libctx,
786 providers[n]))) {
787 thead_teardown_libctx();
788 return 0;
789 }
790 return 1;
791 }
792
teardown_threads(void)793 static int teardown_threads(void)
794 {
795 size_t i;
796
797 for (i = 0; i < multi_num_threads; i++)
798 if (!TEST_true(wait_for_thread(multi_threads[i])))
799 return 0;
800 return 1;
801 }
802
start_threads(size_t n,void (* thread_func)(void))803 static int start_threads(size_t n, void (*thread_func)(void))
804 {
805 size_t i;
806
807 if (!TEST_size_t_le(multi_num_threads + n, MAXIMUM_THREADS))
808 return 0;
809
810 for (i = 0 ; i < n; i++)
811 if (!TEST_true(run_thread(multi_threads + multi_num_threads++, thread_func)))
812 return 0;
813 return 1;
814 }
815
816 /* 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[])817 static int thread_run_test(void (*main_func)(void),
818 size_t num_threads, void (*thread_func)(void),
819 int libctx, const char *providers[])
820 {
821 int testresult = 0;
822
823 multi_intialise();
824 if (!thread_setup_libctx(libctx, providers)
825 || !start_threads(num_threads, thread_func))
826 goto err;
827
828 if (main_func != NULL)
829 main_func();
830
831 if (!teardown_threads()
832 || !TEST_true(multi_success))
833 goto err;
834 testresult = 1;
835 err:
836 thead_teardown_libctx();
837 return testresult;
838 }
839
thread_general_worker(void)840 static void thread_general_worker(void)
841 {
842 EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
843 EVP_MD *md = EVP_MD_fetch(multi_libctx, "SHA2-256", NULL);
844 EVP_CIPHER_CTX *cipherctx = EVP_CIPHER_CTX_new();
845 EVP_CIPHER *ciph = EVP_CIPHER_fetch(multi_libctx, "AES-128-CBC", NULL);
846 const char *message = "Hello World";
847 size_t messlen = strlen(message);
848 /* Should be big enough for encryption output too */
849 unsigned char out[EVP_MAX_MD_SIZE];
850 const unsigned char key[AES_BLOCK_SIZE] = {
851 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
852 0x0c, 0x0d, 0x0e, 0x0f
853 };
854 const unsigned char iv[AES_BLOCK_SIZE] = {
855 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
856 0x0c, 0x0d, 0x0e, 0x0f
857 };
858 unsigned int mdoutl;
859 int ciphoutl;
860 EVP_PKEY *pkey = NULL;
861 int testresult = 0;
862 int i, isfips;
863
864 isfips = OSSL_PROVIDER_available(multi_libctx, "fips");
865
866 if (!TEST_ptr(mdctx)
867 || !TEST_ptr(md)
868 || !TEST_ptr(cipherctx)
869 || !TEST_ptr(ciph))
870 goto err;
871
872 /* Do some work */
873 for (i = 0; i < 5; i++) {
874 if (!TEST_true(EVP_DigestInit_ex(mdctx, md, NULL))
875 || !TEST_true(EVP_DigestUpdate(mdctx, message, messlen))
876 || !TEST_true(EVP_DigestFinal(mdctx, out, &mdoutl)))
877 goto err;
878 }
879 for (i = 0; i < 5; i++) {
880 if (!TEST_true(EVP_EncryptInit_ex(cipherctx, ciph, NULL, key, iv))
881 || !TEST_true(EVP_EncryptUpdate(cipherctx, out, &ciphoutl,
882 (unsigned char *)message,
883 messlen))
884 || !TEST_true(EVP_EncryptFinal(cipherctx, out, &ciphoutl)))
885 goto err;
886 }
887
888 /*
889 * We want the test to run quickly - not securely.
890 * Therefore we use an insecure bit length where we can (512).
891 * In the FIPS module though we must use a longer length.
892 */
893 pkey = EVP_PKEY_Q_keygen(multi_libctx, NULL, "RSA", (size_t)(isfips ? 2048 : 512));
894 if (!TEST_ptr(pkey))
895 goto err;
896
897 testresult = 1;
898 err:
899 EVP_MD_CTX_free(mdctx);
900 EVP_MD_free(md);
901 EVP_CIPHER_CTX_free(cipherctx);
902 EVP_CIPHER_free(ciph);
903 EVP_PKEY_free(pkey);
904 if (!testresult)
905 multi_set_success(0);
906 }
907
thread_multi_simple_fetch(void)908 static void thread_multi_simple_fetch(void)
909 {
910 EVP_MD *md = EVP_MD_fetch(multi_libctx, "SHA2-256", NULL);
911
912 if (md != NULL)
913 EVP_MD_free(md);
914 else
915 multi_set_success(0);
916 }
917
918 static EVP_PKEY *shared_evp_pkey = NULL;
919
thread_shared_evp_pkey(void)920 static void thread_shared_evp_pkey(void)
921 {
922 char *msg = "Hello World";
923 unsigned char ctbuf[256];
924 unsigned char ptbuf[256];
925 size_t ptlen, ctlen = sizeof(ctbuf);
926 EVP_PKEY_CTX *ctx = NULL;
927 int success = 0;
928 int i;
929
930 for (i = 0; i < 1 + do_fips; i++) {
931 if (i > 0)
932 EVP_PKEY_CTX_free(ctx);
933 ctx = EVP_PKEY_CTX_new_from_pkey(multi_libctx, shared_evp_pkey,
934 i == 0 ? "provider=default"
935 : "provider=fips");
936 if (!TEST_ptr(ctx))
937 goto err;
938
939 if (!TEST_int_ge(EVP_PKEY_encrypt_init(ctx), 0)
940 || !TEST_int_ge(EVP_PKEY_encrypt(ctx, ctbuf, &ctlen,
941 (unsigned char *)msg, strlen(msg)),
942 0))
943 goto err;
944
945 EVP_PKEY_CTX_free(ctx);
946 ctx = EVP_PKEY_CTX_new_from_pkey(multi_libctx, shared_evp_pkey, NULL);
947
948 if (!TEST_ptr(ctx))
949 goto err;
950
951 ptlen = sizeof(ptbuf);
952 if (!TEST_int_ge(EVP_PKEY_decrypt_init(ctx), 0)
953 || !TEST_int_gt(EVP_PKEY_decrypt(ctx, ptbuf, &ptlen, ctbuf, ctlen),
954 0)
955 || !TEST_mem_eq(msg, strlen(msg), ptbuf, ptlen))
956 goto err;
957 }
958
959 success = 1;
960
961 err:
962 EVP_PKEY_CTX_free(ctx);
963 if (!success)
964 multi_set_success(0);
965 }
966
thread_provider_load_unload(void)967 static void thread_provider_load_unload(void)
968 {
969 OSSL_PROVIDER *deflt = OSSL_PROVIDER_load(multi_libctx, "default");
970
971 if (!TEST_ptr(deflt)
972 || !TEST_true(OSSL_PROVIDER_available(multi_libctx, "default")))
973 multi_set_success(0);
974
975 OSSL_PROVIDER_unload(deflt);
976 }
977
test_multi_general_worker_default_provider(void)978 static int test_multi_general_worker_default_provider(void)
979 {
980 return thread_run_test(&thread_general_worker, 2, &thread_general_worker,
981 1, default_provider);
982 }
983
test_multi_general_worker_fips_provider(void)984 static int test_multi_general_worker_fips_provider(void)
985 {
986 if (!do_fips)
987 return TEST_skip("FIPS not supported");
988 return thread_run_test(&thread_general_worker, 2, &thread_general_worker,
989 1, fips_provider);
990 }
991
test_multi_fetch_worker(void)992 static int test_multi_fetch_worker(void)
993 {
994 return thread_run_test(&thread_multi_simple_fetch,
995 2, &thread_multi_simple_fetch, 1, default_provider);
996 }
997
test_multi_shared_pkey_common(void (* worker)(void))998 static int test_multi_shared_pkey_common(void (*worker)(void))
999 {
1000 int testresult = 0;
1001
1002 multi_intialise();
1003 if (!thread_setup_libctx(1, do_fips ? fips_and_default_providers
1004 : default_provider)
1005 || !TEST_ptr(shared_evp_pkey = load_pkey_pem(privkey, multi_libctx))
1006 || !start_threads(1, &thread_shared_evp_pkey)
1007 || !start_threads(1, worker))
1008 goto err;
1009
1010 thread_shared_evp_pkey();
1011
1012 if (!teardown_threads()
1013 || !TEST_true(multi_success))
1014 goto err;
1015 testresult = 1;
1016 err:
1017 EVP_PKEY_free(shared_evp_pkey);
1018 thead_teardown_libctx();
1019 return testresult;
1020 }
1021
1022 #ifndef OPENSSL_NO_DEPRECATED_3_0
thread_downgrade_shared_evp_pkey(void)1023 static void thread_downgrade_shared_evp_pkey(void)
1024 {
1025 /*
1026 * This test is only relevant for deprecated functions that perform
1027 * downgrading
1028 */
1029 if (EVP_PKEY_get0_RSA(shared_evp_pkey) == NULL)
1030 multi_set_success(0);
1031 }
1032
test_multi_downgrade_shared_pkey(void)1033 static int test_multi_downgrade_shared_pkey(void)
1034 {
1035 return test_multi_shared_pkey_common(&thread_downgrade_shared_evp_pkey);
1036 }
1037 #endif
1038
test_multi_shared_pkey(void)1039 static int test_multi_shared_pkey(void)
1040 {
1041 return test_multi_shared_pkey_common(&thread_shared_evp_pkey);
1042 }
1043
thread_release_shared_pkey(void)1044 static void thread_release_shared_pkey(void)
1045 {
1046 OSSL_sleep(0);
1047 EVP_PKEY_free(shared_evp_pkey);
1048 }
1049
test_multi_shared_pkey_release(void)1050 static int test_multi_shared_pkey_release(void)
1051 {
1052 int testresult = 0;
1053 size_t i = 1;
1054
1055 multi_intialise();
1056 shared_evp_pkey = NULL;
1057 if (!thread_setup_libctx(1, do_fips ? fips_and_default_providers
1058 : default_provider)
1059 || !TEST_ptr(shared_evp_pkey = load_pkey_pem(privkey, multi_libctx)))
1060 goto err;
1061 for (; i < 10; ++i) {
1062 if (!TEST_true(EVP_PKEY_up_ref(shared_evp_pkey)))
1063 goto err;
1064 }
1065
1066 if (!start_threads(10, &thread_release_shared_pkey))
1067 goto err;
1068 i = 0;
1069
1070 if (!teardown_threads()
1071 || !TEST_true(multi_success))
1072 goto err;
1073 testresult = 1;
1074 err:
1075 while (i > 0) {
1076 EVP_PKEY_free(shared_evp_pkey);
1077 --i;
1078 }
1079 thead_teardown_libctx();
1080 return testresult;
1081 }
1082
test_multi_load_unload_provider(void)1083 static int test_multi_load_unload_provider(void)
1084 {
1085 EVP_MD *sha256 = NULL;
1086 OSSL_PROVIDER *prov = NULL;
1087 int testresult = 0;
1088
1089 multi_intialise();
1090 if (!thread_setup_libctx(1, NULL)
1091 || !TEST_ptr(prov = OSSL_PROVIDER_load(multi_libctx, "default"))
1092 || !TEST_ptr(sha256 = EVP_MD_fetch(multi_libctx, "SHA2-256", NULL))
1093 || !TEST_true(OSSL_PROVIDER_unload(prov)))
1094 goto err;
1095 prov = NULL;
1096
1097 if (!start_threads(2, &thread_provider_load_unload))
1098 goto err;
1099
1100 thread_provider_load_unload();
1101
1102 if (!teardown_threads()
1103 || !TEST_true(multi_success))
1104 goto err;
1105 testresult = 1;
1106 err:
1107 OSSL_PROVIDER_unload(prov);
1108 EVP_MD_free(sha256);
1109 thead_teardown_libctx();
1110 return testresult;
1111 }
1112
1113 static char *multi_load_provider = "legacy";
1114 /*
1115 * This test attempts to load several providers at the same time, and if
1116 * run with a thread sanitizer, should crash if the core provider code
1117 * doesn't synchronize well enough.
1118 */
test_multi_load_worker(void)1119 static void test_multi_load_worker(void)
1120 {
1121 OSSL_PROVIDER *prov;
1122
1123 if (!TEST_ptr(prov = OSSL_PROVIDER_load(multi_libctx, multi_load_provider))
1124 || !TEST_true(OSSL_PROVIDER_unload(prov)))
1125 multi_set_success(0);
1126 }
1127
test_multi_default(void)1128 static int test_multi_default(void)
1129 {
1130 /* Avoid running this test twice */
1131 if (multidefault_run) {
1132 TEST_skip("multi default test already run");
1133 return 1;
1134 }
1135 multidefault_run = 1;
1136
1137 return thread_run_test(&thread_multi_simple_fetch,
1138 2, &thread_multi_simple_fetch, 0, default_provider);
1139 }
1140
test_multi_load(void)1141 static int test_multi_load(void)
1142 {
1143 int res = 1;
1144 OSSL_PROVIDER *prov;
1145
1146 /* The multidefault test must run prior to this test */
1147 if (!multidefault_run) {
1148 TEST_info("Running multi default test first");
1149 res = test_multi_default();
1150 }
1151
1152 /*
1153 * We use the legacy provider in test_multi_load_worker because it uses a
1154 * child libctx that might hit more codepaths that might be sensitive to
1155 * threading issues. But in a no-legacy build that won't be loadable so
1156 * we use the default provider instead.
1157 */
1158 prov = OSSL_PROVIDER_load(NULL, "legacy");
1159 if (prov == NULL) {
1160 TEST_info("Cannot load legacy provider - assuming this is a no-legacy build");
1161 multi_load_provider = "default";
1162 }
1163 OSSL_PROVIDER_unload(prov);
1164
1165 return thread_run_test(NULL, MAXIMUM_THREADS, &test_multi_load_worker, 0,
1166 NULL) && res;
1167 }
1168
test_obj_create_one(void)1169 static void test_obj_create_one(void)
1170 {
1171 char tids[12], oid[40], sn[30], ln[30];
1172 int id = get_new_uid();
1173
1174 BIO_snprintf(tids, sizeof(tids), "%d", id);
1175 BIO_snprintf(oid, sizeof(oid), "1.3.6.1.4.1.16604.%s", tids);
1176 BIO_snprintf(sn, sizeof(sn), "short-name-%s", tids);
1177 BIO_snprintf(ln, sizeof(ln), "long-name-%s", tids);
1178 if (!TEST_int_ne(id, 0)
1179 || !TEST_true(id = OBJ_create(oid, sn, ln))
1180 || !TEST_true(OBJ_add_sigid(id, NID_sha3_256, NID_rsa)))
1181 multi_set_success(0);
1182 }
1183
test_obj_add(void)1184 static int test_obj_add(void)
1185 {
1186 return thread_run_test(&test_obj_create_one,
1187 MAXIMUM_THREADS, &test_obj_create_one,
1188 1, default_provider);
1189 }
1190
1191 #if !defined(OPENSSL_NO_DGRAM) && !defined(OPENSSL_NO_SOCK)
1192 static BIO *multi_bio1, *multi_bio2;
1193
test_bio_dgram_pair_worker(void)1194 static void test_bio_dgram_pair_worker(void)
1195 {
1196 ossl_unused int r;
1197 int ok = 0;
1198 uint8_t ch = 0;
1199 uint8_t scratch[64];
1200 BIO_MSG msg = {0};
1201 size_t num_processed = 0;
1202
1203 if (!TEST_int_eq(RAND_bytes_ex(multi_libctx, &ch, 1, 64), 1))
1204 goto err;
1205
1206 msg.data = scratch;
1207 msg.data_len = sizeof(scratch);
1208
1209 /*
1210 * We do not test for failure here as recvmmsg may fail if no sendmmsg
1211 * has been called yet. The purpose of this code is to exercise tsan.
1212 */
1213 if (ch & 2)
1214 r = BIO_sendmmsg(ch & 1 ? multi_bio2 : multi_bio1, &msg,
1215 sizeof(BIO_MSG), 1, 0, &num_processed);
1216 else
1217 r = BIO_recvmmsg(ch & 1 ? multi_bio2 : multi_bio1, &msg,
1218 sizeof(BIO_MSG), 1, 0, &num_processed);
1219
1220 ok = 1;
1221 err:
1222 if (ok == 0)
1223 multi_set_success(0);
1224 }
1225
test_bio_dgram_pair(void)1226 static int test_bio_dgram_pair(void)
1227 {
1228 int r;
1229 BIO *bio1 = NULL, *bio2 = NULL;
1230
1231 r = BIO_new_bio_dgram_pair(&bio1, 0, &bio2, 0);
1232 if (!TEST_int_eq(r, 1))
1233 goto err;
1234
1235 multi_bio1 = bio1;
1236 multi_bio2 = bio2;
1237
1238 r = thread_run_test(&test_bio_dgram_pair_worker,
1239 MAXIMUM_THREADS, &test_bio_dgram_pair_worker,
1240 1, default_provider);
1241
1242 err:
1243 BIO_free(bio1);
1244 BIO_free(bio2);
1245 return r;
1246 }
1247 #endif
1248
1249 static const char *pemdataraw[] = {
1250 "-----BEGIN RSA PRIVATE KEY-----\n",
1251 "MIIBOgIBAAJBAMFcGsaxxdgiuuGmCkVImy4h99CqT7jwY3pexPGcnUFtR2Fh36Bp\n",
1252 "oncwtkZ4cAgtvd4Qs8PkxUdp6p/DlUmObdkCAwEAAQJAUR44xX6zB3eaeyvTRzms\n",
1253 "kHADrPCmPWnr8dxsNwiDGHzrMKLN+i/HAam+97HxIKVWNDH2ba9Mf1SA8xu9dcHZ\n",
1254 "AQIhAOHPCLxbtQFVxlnhSyxYeb7O323c3QulPNn3bhOipElpAiEA2zZpBE8ZXVnL\n",
1255 "74QjG4zINlDfH+EOEtjJJ3RtaYDugvECIBtsQDxXytChsRgDQ1TcXdStXPcDppie\n",
1256 "dZhm8yhRTTBZAiAZjE/U9rsIDC0ebxIAZfn3iplWh84yGB3pgUI3J5WkoQIhAInE\n",
1257 "HTUY5WRj5riZtkyGnbm3DvF+1eMtO2lYV+OuLcfE\n",
1258 "-----END RSA PRIVATE KEY-----\n",
1259 NULL
1260 };
1261
test_pem_read_one(void)1262 static void test_pem_read_one(void)
1263 {
1264 EVP_PKEY *key = NULL;
1265 BIO *pem = NULL;
1266 char *pemdata;
1267 size_t len;
1268
1269 pemdata = glue_strings(pemdataraw, &len);
1270 if (pemdata == NULL) {
1271 multi_set_success(0);
1272 goto err;
1273 }
1274
1275 pem = BIO_new_mem_buf(pemdata, len);
1276 if (pem == NULL) {
1277 multi_set_success(0);
1278 goto err;
1279 }
1280
1281 key = PEM_read_bio_PrivateKey(pem, NULL, NULL, NULL);
1282 if (key == NULL)
1283 multi_set_success(0);
1284
1285 err:
1286 EVP_PKEY_free(key);
1287 BIO_free(pem);
1288 OPENSSL_free(pemdata);
1289 }
1290
1291 /* Test reading PEM files in multiple threads */
test_pem_read(void)1292 static int test_pem_read(void)
1293 {
1294 return thread_run_test(&test_pem_read_one, MAXIMUM_THREADS,
1295 &test_pem_read_one, 1, default_provider);
1296 }
1297
1298 typedef enum OPTION_choice {
1299 OPT_ERR = -1,
1300 OPT_EOF = 0,
1301 OPT_FIPS, OPT_CONFIG_FILE,
1302 OPT_TEST_ENUM
1303 } OPTION_CHOICE;
1304
test_get_options(void)1305 const OPTIONS *test_get_options(void)
1306 {
1307 static const OPTIONS options[] = {
1308 OPT_TEST_OPTIONS_DEFAULT_USAGE,
1309 { "fips", OPT_FIPS, '-', "Test the FIPS provider" },
1310 { "config", OPT_CONFIG_FILE, '<',
1311 "The configuration file to use for the libctx" },
1312 { NULL }
1313 };
1314 return options;
1315 }
1316
setup_tests(void)1317 int setup_tests(void)
1318 {
1319 OPTION_CHOICE o;
1320 char *datadir;
1321
1322 while ((o = opt_next()) != OPT_EOF) {
1323 switch (o) {
1324 case OPT_FIPS:
1325 do_fips = 1;
1326 break;
1327 case OPT_CONFIG_FILE:
1328 config_file = opt_arg();
1329 break;
1330 case OPT_TEST_CASES:
1331 break;
1332 default:
1333 return 0;
1334 }
1335 }
1336
1337 if (!TEST_ptr(datadir = test_get_argument(0)))
1338 return 0;
1339
1340 privkey = test_mk_file_path(datadir, "rsakey.pem");
1341 if (!TEST_ptr(privkey))
1342 return 0;
1343
1344 if (!TEST_ptr(global_lock = CRYPTO_THREAD_lock_new()))
1345 return 0;
1346
1347 #ifdef TSAN_REQUIRES_LOCKING
1348 if (!TEST_ptr(tsan_lock = CRYPTO_THREAD_lock_new()))
1349 return 0;
1350 #endif
1351
1352 /* Keep first to validate auto creation of default library context */
1353 ADD_TEST(test_multi_default);
1354
1355 ADD_TEST(test_lock);
1356 #if defined(OPENSSL_THREADS)
1357 ADD_TEST(torture_rw_low);
1358 ADD_TEST(torture_rw_high);
1359 ADD_TEST(torture_rcu_low);
1360 ADD_TEST(torture_rcu_high);
1361 ADD_TEST(torture_rcu_high2);
1362 #endif
1363 ADD_TEST(test_once);
1364 ADD_TEST(test_thread_local);
1365 ADD_TEST(test_thread_local_multi_key);
1366 ADD_TEST(test_atomic);
1367 ADD_TEST(test_multi_load);
1368 ADD_TEST(test_multi_general_worker_default_provider);
1369 ADD_TEST(test_multi_general_worker_fips_provider);
1370 ADD_TEST(test_multi_fetch_worker);
1371 ADD_TEST(test_multi_shared_pkey);
1372 #ifndef OPENSSL_NO_DEPRECATED_3_0
1373 ADD_TEST(test_multi_downgrade_shared_pkey);
1374 #endif
1375 ADD_TEST(test_multi_shared_pkey_release);
1376 ADD_TEST(test_multi_load_unload_provider);
1377 ADD_TEST(test_obj_add);
1378 #if !defined(OPENSSL_NO_DGRAM) && !defined(OPENSSL_NO_SOCK)
1379 ADD_TEST(test_bio_dgram_pair);
1380 #endif
1381 ADD_TEST(test_pem_read);
1382 return 1;
1383 }
1384
cleanup_tests(void)1385 void cleanup_tests(void)
1386 {
1387 OPENSSL_free(privkey);
1388 #ifdef TSAN_REQUIRES_LOCKING
1389 CRYPTO_THREAD_lock_free(tsan_lock);
1390 #endif
1391 CRYPTO_THREAD_lock_free(global_lock);
1392 }
1393