2 * Copyright 2016-2023 The OpenSSL Project Authors. All Rights Reserved.
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
11 * The test_multi_downgrade_shared_pkey function tests the thread safety of a
12 * deprecated function.
14 #ifndef OPENSSL_NO_DEPRECATED_3_0
15 # define OPENSSL_SUPPRESS_DEPRECATED
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"
35 #include "threadstest.h"
37 #ifdef __SANITIZE_THREAD__
38 #include <sanitizer/tsan_interface.h>
39 #define TSAN_ACQUIRE(s) __tsan_acquire(s)
41 #define TSAN_ACQUIRE(s)
44 /* Limit the maximum number of threads */
45 #define MAXIMUM_THREADS 10
47 /* Limit the maximum number of providers loaded into a library context */
48 #define MAXIMUM_PROVIDERS 4
50 static int do_fips = 0;
52 static char *config_file = NULL;
53 static int multidefault_run = 0;
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 };
59 static CRYPTO_RWLOCK *global_lock;
61 #ifdef TSAN_REQUIRES_LOCKING
62 static CRYPTO_RWLOCK *tsan_lock;
65 /* Grab a globally unique integer value, return 0 on failure */
66 static int get_new_uid(void)
69 * Start with a nice large number to avoid potential conflicts when
70 * we generate a new OID.
72 static TSAN_QUALIFIER int current_uid = 1 << (sizeof(int) * 8 - 2);
73 #ifdef TSAN_REQUIRES_LOCKING
76 if (!TEST_true(CRYPTO_THREAD_write_lock(tsan_lock)))
79 if (!TEST_true(CRYPTO_THREAD_unlock(tsan_lock)))
84 return tsan_counter(¤t_uid);
88 static int test_lock(void)
90 CRYPTO_RWLOCK *lock = CRYPTO_THREAD_lock_new();
93 res = TEST_true(CRYPTO_THREAD_read_lock(lock))
94 && TEST_true(CRYPTO_THREAD_unlock(lock))
95 && TEST_true(CRYPTO_THREAD_write_lock(lock))
96 && TEST_true(CRYPTO_THREAD_unlock(lock));
98 CRYPTO_THREAD_lock_free(lock);
103 #if defined(OPENSSL_THREADS)
104 static int contention = 0;
105 static int rwwriter1_done = 0;
106 static int rwwriter2_done = 0;
107 static int rwreader1_iterations = 0;
108 static int rwreader2_iterations = 0;
109 static int rwwriter1_iterations = 0;
110 static int rwwriter2_iterations = 0;
111 static int *rwwriter_ptr = NULL;
112 static int rw_torture_result = 1;
113 static CRYPTO_RWLOCK *rwtorturelock = NULL;
115 static void rwwriter_fn(int id, int *iterations)
120 t1 = ossl_time_now();
122 for (count = 0; ; count++) {
123 new = CRYPTO_zalloc(sizeof (int), NULL, 0);
126 if (!CRYPTO_THREAD_write_lock(rwtorturelock))
128 if (rwwriter_ptr != NULL) {
129 *new = *rwwriter_ptr + 1;
135 if (!CRYPTO_THREAD_unlock(rwtorturelock))
138 CRYPTO_free(old, __FILE__, __LINE__);
139 t2 = ossl_time_now();
140 if ((ossl_time2seconds(t2) - ossl_time2seconds(t1)) >= 4)
147 static void rwwriter1_fn(void)
151 TEST_info("Starting writer1");
152 rwwriter_fn(1, &rwwriter1_iterations);
153 CRYPTO_atomic_add(&rwwriter1_done, 1, &local, NULL);
156 static void rwwriter2_fn(void)
160 TEST_info("Starting writer 2");
161 rwwriter_fn(2, &rwwriter2_iterations);
162 CRYPTO_atomic_add(&rwwriter2_done, 1, &local, NULL);
165 static void rwreader_fn(int *iterations)
167 unsigned int count = 0;
173 if (CRYPTO_THREAD_read_lock(rwtorturelock) == 0)
176 while (lw1 != 1 || lw2 != 1) {
177 CRYPTO_atomic_add(&rwwriter1_done, 0, &lw1, NULL);
178 CRYPTO_atomic_add(&rwwriter2_done, 0, &lw2, NULL);
181 if (rwwriter_ptr != NULL && old > *rwwriter_ptr) {
182 TEST_info("rwwriter pointer went backwards\n");
183 rw_torture_result = 0;
185 if (CRYPTO_THREAD_unlock(rwtorturelock) == 0)
188 if (rw_torture_result == 0) {
192 if (CRYPTO_THREAD_read_lock(rwtorturelock) == 0)
196 if (CRYPTO_THREAD_unlock(rwtorturelock) == 0)
200 static void rwreader1_fn(void)
202 TEST_info("Starting reader 1");
203 rwreader_fn(&rwreader1_iterations);
206 static void rwreader2_fn(void)
208 TEST_info("Starting reader 2");
209 rwreader_fn(&rwreader2_iterations);
212 static thread_t rwwriter1;
213 static thread_t rwwriter2;
214 static thread_t rwreader1;
215 static thread_t rwreader2;
217 static int _torture_rw(void)
223 struct timeval dtime;
225 rwtorturelock = CRYPTO_THREAD_lock_new();
226 rwwriter1_iterations = 0;
227 rwwriter2_iterations = 0;
228 rwreader1_iterations = 0;
229 rwreader2_iterations = 0;
232 rw_torture_result = 1;
234 memset(&rwwriter1, 0, sizeof(thread_t));
235 memset(&rwwriter2, 0, sizeof(thread_t));
236 memset(&rwreader1, 0, sizeof(thread_t));
237 memset(&rwreader2, 0, sizeof(thread_t));
239 TEST_info("Staring rw torture");
240 t1 = ossl_time_now();
241 if (!TEST_true(run_thread(&rwreader1, rwreader1_fn))
242 || !TEST_true(run_thread(&rwreader2, rwreader2_fn))
243 || !TEST_true(run_thread(&rwwriter1, rwwriter1_fn))
244 || !TEST_true(run_thread(&rwwriter2, rwwriter2_fn))
245 || !TEST_true(wait_for_thread(rwwriter1))
246 || !TEST_true(wait_for_thread(rwwriter2))
247 || !TEST_true(wait_for_thread(rwreader1))
248 || !TEST_true(wait_for_thread(rwreader2)))
251 t2 = ossl_time_now();
252 dtime = ossl_time_to_timeval(ossl_time_subtract(t2, t1));
253 tottime = dtime.tv_sec + (dtime.tv_usec / 1e6);
254 TEST_info("rw_torture_result is %d\n", rw_torture_result);
255 TEST_info("performed %d reads and %d writes over 2 read and 2 write threads in %e seconds",
256 rwreader1_iterations + rwreader2_iterations,
257 rwwriter1_iterations + rwwriter2_iterations, tottime);
258 avr = tottime / (rwreader1_iterations + rwreader2_iterations);
259 avw = (tottime / (rwwriter1_iterations + rwwriter2_iterations));
260 TEST_info("Average read time %e/read", avr);
261 TEST_info("Averate write time %e/write", avw);
263 if (TEST_int_eq(rw_torture_result, 1))
266 CRYPTO_THREAD_lock_free(rwtorturelock);
267 rwtorturelock = NULL;
271 static int torture_rw_low(void)
274 return _torture_rw();
277 static int torture_rw_high(void)
280 return _torture_rw();
284 static CRYPTO_RCU_LOCK *rcu_lock = NULL;
286 static int writer1_done = 0;
287 static int writer2_done = 0;
288 static int reader1_iterations = 0;
289 static int reader2_iterations = 0;
290 static int writer1_iterations = 0;
291 static int writer2_iterations = 0;
292 static unsigned int *writer_ptr = NULL;
293 static unsigned int global_ctr = 0;
294 static int rcu_torture_result = 1;
296 static void free_old_rcu_data(void *data)
298 CRYPTO_free(data, NULL, 0);
301 static void writer_fn(int id, int *iterations)
305 unsigned int *old, *new;
307 t1 = ossl_time_now();
309 for (count = 0; ; count++) {
310 new = CRYPTO_zalloc(sizeof(int), NULL, 0);
313 ossl_rcu_write_lock(rcu_lock);
314 old = ossl_rcu_deref(&writer_ptr);
315 TSAN_ACQUIRE(&writer_ptr);
317 ossl_rcu_assign_ptr(&writer_ptr, &new);
319 ossl_rcu_call(rcu_lock, free_old_rcu_data, old);
320 ossl_rcu_write_unlock(rcu_lock);
321 if (contention != 0) {
322 ossl_synchronize_rcu(rcu_lock);
323 CRYPTO_free(old, NULL, 0);
325 t2 = ossl_time_now();
326 if ((ossl_time2seconds(t2) - ossl_time2seconds(t1)) >= 4)
333 static void writer1_fn(void)
337 TEST_info("Starting writer1");
338 writer_fn(1, &writer1_iterations);
339 CRYPTO_atomic_add(&writer1_done, 1, &local, NULL);
342 static void writer2_fn(void)
346 TEST_info("Starting writer2");
347 writer_fn(2, &writer2_iterations);
348 CRYPTO_atomic_add(&writer2_done, 1, &local, NULL);
351 static void reader_fn(int *iterations)
353 unsigned int count = 0;
356 unsigned int oldval = 0;
360 while (lw1 != 1 || lw2 != 1) {
361 CRYPTO_atomic_add(&writer1_done, 0, &lw1, NULL);
362 CRYPTO_atomic_add(&writer2_done, 0, &lw2, NULL);
364 ossl_rcu_read_lock(rcu_lock);
365 valp = ossl_rcu_deref(&writer_ptr);
366 val = (valp == NULL) ? 0 : *valp;
368 TEST_info("rcu torture value went backwards! (%p) %x : %x\n", (void *)valp, oldval, val);
369 rcu_torture_result = 0;
371 oldval = val; /* just try to deref the pointer */
372 ossl_rcu_read_unlock(rcu_lock);
373 if (rcu_torture_result == 0) {
381 static void reader1_fn(void)
383 TEST_info("Starting reader 1");
384 reader_fn(&reader1_iterations);
387 static void reader2_fn(void)
389 TEST_info("Starting reader 2");
390 reader_fn(&reader2_iterations);
393 static thread_t writer1;
394 static thread_t writer2;
395 static thread_t reader1;
396 static thread_t reader2;
398 static int _torture_rcu(void)
401 struct timeval dtime;
405 memset(&writer1, 0, sizeof(thread_t));
406 memset(&writer2, 0, sizeof(thread_t));
407 memset(&reader1, 0, sizeof(thread_t));
408 memset(&reader2, 0, sizeof(thread_t));
410 writer1_iterations = 0;
411 writer2_iterations = 0;
412 reader1_iterations = 0;
413 reader2_iterations = 0;
416 rcu_torture_result = 1;
418 rcu_lock = ossl_rcu_lock_new(1);
420 TEST_info("Staring rcu torture");
421 t1 = ossl_time_now();
422 if (!TEST_true(run_thread(&reader1, reader1_fn))
423 || !TEST_true(run_thread(&reader2, reader2_fn))
424 || !TEST_true(run_thread(&writer1, writer1_fn))
425 || !TEST_true(run_thread(&writer2, writer2_fn))
426 || !TEST_true(wait_for_thread(writer1))
427 || !TEST_true(wait_for_thread(writer2))
428 || !TEST_true(wait_for_thread(reader1))
429 || !TEST_true(wait_for_thread(reader2)))
432 t2 = ossl_time_now();
433 dtime = ossl_time_to_timeval(ossl_time_subtract(t2, t1));
434 tottime = dtime.tv_sec + (dtime.tv_usec / 1e6);
435 TEST_info("rcu_torture_result is %d\n", rcu_torture_result);
436 TEST_info("performed %d reads and %d writes over 2 read and 2 write threads in %e seconds",
437 reader1_iterations + reader2_iterations,
438 writer1_iterations + writer2_iterations, tottime);
439 avr = tottime / (reader1_iterations + reader2_iterations);
440 avw = tottime / (writer1_iterations + writer2_iterations);
441 TEST_info("Average read time %e/read", avr);
442 TEST_info("Average write time %e/write", avw);
444 ossl_rcu_lock_free(rcu_lock);
445 if (!TEST_int_eq(rcu_torture_result, 1))
451 static int torture_rcu_low(void)
454 return _torture_rcu();
457 static int torture_rcu_high(void)
460 return _torture_rcu();
464 static CRYPTO_ONCE once_run = CRYPTO_ONCE_STATIC_INIT;
465 static unsigned once_run_count = 0;
467 static void once_do_run(void)
472 static void once_run_thread_cb(void)
474 CRYPTO_THREAD_run_once(&once_run, once_do_run);
477 static int test_once(void)
481 if (!TEST_true(run_thread(&thread, once_run_thread_cb))
482 || !TEST_true(wait_for_thread(thread))
483 || !CRYPTO_THREAD_run_once(&once_run, once_do_run)
484 || !TEST_int_eq(once_run_count, 1))
489 static CRYPTO_THREAD_LOCAL thread_local_key;
490 static unsigned destructor_run_count = 0;
491 static int thread_local_thread_cb_ok = 0;
493 static void thread_local_destructor(void *arg)
505 static void thread_local_thread_cb(void)
509 ptr = CRYPTO_THREAD_get_local(&thread_local_key);
510 if (!TEST_ptr_null(ptr)
511 || !TEST_true(CRYPTO_THREAD_set_local(&thread_local_key,
512 &destructor_run_count)))
515 ptr = CRYPTO_THREAD_get_local(&thread_local_key);
516 if (!TEST_ptr_eq(ptr, &destructor_run_count))
519 thread_local_thread_cb_ok = 1;
522 static int test_thread_local(void)
527 if (!TEST_true(CRYPTO_THREAD_init_local(&thread_local_key,
528 thread_local_destructor)))
531 ptr = CRYPTO_THREAD_get_local(&thread_local_key);
532 if (!TEST_ptr_null(ptr)
533 || !TEST_true(run_thread(&thread, thread_local_thread_cb))
534 || !TEST_true(wait_for_thread(thread))
535 || !TEST_int_eq(thread_local_thread_cb_ok, 1))
538 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
540 ptr = CRYPTO_THREAD_get_local(&thread_local_key);
541 if (!TEST_ptr_null(ptr))
544 # if !defined(OPENSSL_SYS_WINDOWS)
545 if (!TEST_int_eq(destructor_run_count, 1))
550 if (!TEST_true(CRYPTO_THREAD_cleanup_local(&thread_local_key)))
555 static int test_atomic(void)
557 int val = 0, ret = 0, testresult = 0;
558 uint64_t val64 = 1, ret64 = 0;
559 CRYPTO_RWLOCK *lock = CRYPTO_THREAD_lock_new();
564 if (CRYPTO_atomic_add(&val, 1, &ret, NULL)) {
565 /* This succeeds therefore we're on a platform with lockless atomics */
566 if (!TEST_int_eq(val, 1) || !TEST_int_eq(val, ret))
569 /* This failed therefore we're on a platform without lockless atomics */
570 if (!TEST_int_eq(val, 0) || !TEST_int_eq(val, ret))
576 if (!TEST_true(CRYPTO_atomic_add(&val, 1, &ret, lock)))
578 if (!TEST_int_eq(val, 1) || !TEST_int_eq(val, ret))
581 if (CRYPTO_atomic_or(&val64, 2, &ret64, NULL)) {
582 /* This succeeds therefore we're on a platform with lockless atomics */
583 if (!TEST_uint_eq((unsigned int)val64, 3)
584 || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
587 /* This failed therefore we're on a platform without lockless atomics */
588 if (!TEST_uint_eq((unsigned int)val64, 1)
589 || !TEST_int_eq((unsigned int)ret64, 0))
595 if (!TEST_true(CRYPTO_atomic_or(&val64, 2, &ret64, lock)))
598 if (!TEST_uint_eq((unsigned int)val64, 3)
599 || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
603 if (CRYPTO_atomic_load(&val64, &ret64, NULL)) {
604 /* This succeeds therefore we're on a platform with lockless atomics */
605 if (!TEST_uint_eq((unsigned int)val64, 3)
606 || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
609 /* This failed therefore we're on a platform without lockless atomics */
610 if (!TEST_uint_eq((unsigned int)val64, 3)
611 || !TEST_int_eq((unsigned int)ret64, 0))
616 if (!TEST_true(CRYPTO_atomic_load(&val64, &ret64, lock)))
619 if (!TEST_uint_eq((unsigned int)val64, 3)
620 || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
625 CRYPTO_THREAD_lock_free(lock);
629 static OSSL_LIB_CTX *multi_libctx = NULL;
630 static int multi_success;
631 static OSSL_PROVIDER *multi_provider[MAXIMUM_PROVIDERS + 1];
632 static size_t multi_num_threads;
633 static thread_t multi_threads[MAXIMUM_THREADS];
635 static void multi_intialise(void)
639 multi_num_threads = 0;
640 memset(multi_threads, 0, sizeof(multi_threads));
641 memset(multi_provider, 0, sizeof(multi_provider));
644 static void multi_set_success(int ok)
646 if (CRYPTO_THREAD_write_lock(global_lock) == 0) {
647 /* not synchronized, but better than not reporting failure */
654 CRYPTO_THREAD_unlock(global_lock);
657 static void thead_teardown_libctx(void)
661 for (p = multi_provider; *p != NULL; p++)
662 OSSL_PROVIDER_unload(*p);
663 OSSL_LIB_CTX_free(multi_libctx);
667 static int thread_setup_libctx(int libctx, const char *providers[])
671 if (libctx && !TEST_true(test_get_libctx(&multi_libctx, NULL, config_file,
675 if (providers != NULL)
676 for (n = 0; providers[n] != NULL; n++)
677 if (!TEST_size_t_lt(n, MAXIMUM_PROVIDERS)
678 || !TEST_ptr(multi_provider[n] = OSSL_PROVIDER_load(multi_libctx,
680 thead_teardown_libctx();
686 static int teardown_threads(void)
690 for (i = 0; i < multi_num_threads; i++)
691 if (!TEST_true(wait_for_thread(multi_threads[i])))
696 static int start_threads(size_t n, void (*thread_func)(void))
700 if (!TEST_size_t_le(multi_num_threads + n, MAXIMUM_THREADS))
703 for (i = 0 ; i < n; i++)
704 if (!TEST_true(run_thread(multi_threads + multi_num_threads++, thread_func)))
709 /* Template multi-threaded test function */
710 static int thread_run_test(void (*main_func)(void),
711 size_t num_threads, void (*thread_func)(void),
712 int libctx, const char *providers[])
717 if (!thread_setup_libctx(libctx, providers)
718 || !start_threads(num_threads, thread_func))
721 if (main_func != NULL)
724 if (!teardown_threads()
725 || !TEST_true(multi_success))
729 thead_teardown_libctx();
733 static void thread_general_worker(void)
735 EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
736 EVP_MD *md = EVP_MD_fetch(multi_libctx, "SHA2-256", NULL);
737 EVP_CIPHER_CTX *cipherctx = EVP_CIPHER_CTX_new();
738 EVP_CIPHER *ciph = EVP_CIPHER_fetch(multi_libctx, "AES-128-CBC", NULL);
739 const char *message = "Hello World";
740 size_t messlen = strlen(message);
741 /* Should be big enough for encryption output too */
742 unsigned char out[EVP_MAX_MD_SIZE];
743 const unsigned char key[AES_BLOCK_SIZE] = {
744 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
745 0x0c, 0x0d, 0x0e, 0x0f
747 const unsigned char iv[AES_BLOCK_SIZE] = {
748 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
749 0x0c, 0x0d, 0x0e, 0x0f
753 EVP_PKEY *pkey = NULL;
757 isfips = OSSL_PROVIDER_available(multi_libctx, "fips");
761 || !TEST_ptr(cipherctx)
766 for (i = 0; i < 5; i++) {
767 if (!TEST_true(EVP_DigestInit_ex(mdctx, md, NULL))
768 || !TEST_true(EVP_DigestUpdate(mdctx, message, messlen))
769 || !TEST_true(EVP_DigestFinal(mdctx, out, &mdoutl)))
772 for (i = 0; i < 5; i++) {
773 if (!TEST_true(EVP_EncryptInit_ex(cipherctx, ciph, NULL, key, iv))
774 || !TEST_true(EVP_EncryptUpdate(cipherctx, out, &ciphoutl,
775 (unsigned char *)message,
777 || !TEST_true(EVP_EncryptFinal(cipherctx, out, &ciphoutl)))
782 * We want the test to run quickly - not securely.
783 * Therefore we use an insecure bit length where we can (512).
784 * In the FIPS module though we must use a longer length.
786 pkey = EVP_PKEY_Q_keygen(multi_libctx, NULL, "RSA", isfips ? 2048 : 512);
792 EVP_MD_CTX_free(mdctx);
794 EVP_CIPHER_CTX_free(cipherctx);
795 EVP_CIPHER_free(ciph);
798 multi_set_success(0);
801 static void thread_multi_simple_fetch(void)
803 EVP_MD *md = EVP_MD_fetch(multi_libctx, "SHA2-256", NULL);
808 multi_set_success(0);
811 static EVP_PKEY *shared_evp_pkey = NULL;
813 static void thread_shared_evp_pkey(void)
815 char *msg = "Hello World";
816 unsigned char ctbuf[256];
817 unsigned char ptbuf[256];
818 size_t ptlen, ctlen = sizeof(ctbuf);
819 EVP_PKEY_CTX *ctx = NULL;
823 for (i = 0; i < 1 + do_fips; i++) {
825 EVP_PKEY_CTX_free(ctx);
826 ctx = EVP_PKEY_CTX_new_from_pkey(multi_libctx, shared_evp_pkey,
827 i == 0 ? "provider=default"
832 if (!TEST_int_ge(EVP_PKEY_encrypt_init(ctx), 0)
833 || !TEST_int_ge(EVP_PKEY_encrypt(ctx, ctbuf, &ctlen,
834 (unsigned char *)msg, strlen(msg)),
838 EVP_PKEY_CTX_free(ctx);
839 ctx = EVP_PKEY_CTX_new_from_pkey(multi_libctx, shared_evp_pkey, NULL);
844 ptlen = sizeof(ptbuf);
845 if (!TEST_int_ge(EVP_PKEY_decrypt_init(ctx), 0)
846 || !TEST_int_gt(EVP_PKEY_decrypt(ctx, ptbuf, &ptlen, ctbuf, ctlen),
848 || !TEST_mem_eq(msg, strlen(msg), ptbuf, ptlen))
855 EVP_PKEY_CTX_free(ctx);
857 multi_set_success(0);
860 static void thread_provider_load_unload(void)
862 OSSL_PROVIDER *deflt = OSSL_PROVIDER_load(multi_libctx, "default");
865 || !TEST_true(OSSL_PROVIDER_available(multi_libctx, "default")))
866 multi_set_success(0);
868 OSSL_PROVIDER_unload(deflt);
871 static int test_multi_general_worker_default_provider(void)
873 return thread_run_test(&thread_general_worker, 2, &thread_general_worker,
874 1, default_provider);
877 static int test_multi_general_worker_fips_provider(void)
880 return TEST_skip("FIPS not supported");
881 return thread_run_test(&thread_general_worker, 2, &thread_general_worker,
885 static int test_multi_fetch_worker(void)
887 return thread_run_test(&thread_multi_simple_fetch,
888 2, &thread_multi_simple_fetch, 1, default_provider);
891 static int test_multi_shared_pkey_common(void (*worker)(void))
896 if (!thread_setup_libctx(1, do_fips ? fips_and_default_providers
898 || !TEST_ptr(shared_evp_pkey = load_pkey_pem(privkey, multi_libctx))
899 || !start_threads(1, &thread_shared_evp_pkey)
900 || !start_threads(1, worker))
903 thread_shared_evp_pkey();
905 if (!teardown_threads()
906 || !TEST_true(multi_success))
910 EVP_PKEY_free(shared_evp_pkey);
911 thead_teardown_libctx();
915 #ifndef OPENSSL_NO_DEPRECATED_3_0
916 static void thread_downgrade_shared_evp_pkey(void)
919 * This test is only relevant for deprecated functions that perform
922 if (EVP_PKEY_get0_RSA(shared_evp_pkey) == NULL)
923 multi_set_success(0);
926 static int test_multi_downgrade_shared_pkey(void)
928 return test_multi_shared_pkey_common(&thread_downgrade_shared_evp_pkey);
932 static int test_multi_shared_pkey(void)
934 return test_multi_shared_pkey_common(&thread_shared_evp_pkey);
937 static int test_multi_load_unload_provider(void)
939 EVP_MD *sha256 = NULL;
940 OSSL_PROVIDER *prov = NULL;
944 if (!thread_setup_libctx(1, NULL)
945 || !TEST_ptr(prov = OSSL_PROVIDER_load(multi_libctx, "default"))
946 || !TEST_ptr(sha256 = EVP_MD_fetch(multi_libctx, "SHA2-256", NULL))
947 || !TEST_true(OSSL_PROVIDER_unload(prov)))
951 if (!start_threads(2, &thread_provider_load_unload))
954 thread_provider_load_unload();
956 if (!teardown_threads()
957 || !TEST_true(multi_success))
961 OSSL_PROVIDER_unload(prov);
963 thead_teardown_libctx();
967 static char *multi_load_provider = "legacy";
969 * This test attempts to load several providers at the same time, and if
970 * run with a thread sanitizer, should crash if the core provider code
971 * doesn't synchronize well enough.
973 static void test_multi_load_worker(void)
977 if (!TEST_ptr(prov = OSSL_PROVIDER_load(multi_libctx, multi_load_provider))
978 || !TEST_true(OSSL_PROVIDER_unload(prov)))
979 multi_set_success(0);
982 static int test_multi_default(void)
984 /* Avoid running this test twice */
985 if (multidefault_run) {
986 TEST_skip("multi default test already run");
989 multidefault_run = 1;
991 return thread_run_test(&thread_multi_simple_fetch,
992 2, &thread_multi_simple_fetch, 0, default_provider);
995 static int test_multi_load(void)
1000 /* The multidefault test must run prior to this test */
1001 if (!multidefault_run) {
1002 TEST_info("Running multi default test first");
1003 res = test_multi_default();
1007 * We use the legacy provider in test_multi_load_worker because it uses a
1008 * child libctx that might hit more codepaths that might be sensitive to
1009 * threading issues. But in a no-legacy build that won't be loadable so
1010 * we use the default provider instead.
1012 prov = OSSL_PROVIDER_load(NULL, "legacy");
1014 TEST_info("Cannot load legacy provider - assuming this is a no-legacy build");
1015 multi_load_provider = "default";
1017 OSSL_PROVIDER_unload(prov);
1019 return thread_run_test(NULL, MAXIMUM_THREADS, &test_multi_load_worker, 0,
1023 static void test_obj_create_one(void)
1025 char tids[12], oid[40], sn[30], ln[30];
1026 int id = get_new_uid();
1028 BIO_snprintf(tids, sizeof(tids), "%d", id);
1029 BIO_snprintf(oid, sizeof(oid), "1.3.6.1.4.1.16604.%s", tids);
1030 BIO_snprintf(sn, sizeof(sn), "short-name-%s", tids);
1031 BIO_snprintf(ln, sizeof(ln), "long-name-%s", tids);
1032 if (!TEST_int_ne(id, 0)
1033 || !TEST_true(id = OBJ_create(oid, sn, ln))
1034 || !TEST_true(OBJ_add_sigid(id, NID_sha3_256, NID_rsa)))
1035 multi_set_success(0);
1038 static int test_obj_add(void)
1040 return thread_run_test(&test_obj_create_one,
1041 MAXIMUM_THREADS, &test_obj_create_one,
1042 1, default_provider);
1045 static void test_lib_ctx_load_config_worker(void)
1047 if (!TEST_int_eq(OSSL_LIB_CTX_load_config(multi_libctx, config_file), 1))
1048 multi_set_success(0);
1051 static int test_lib_ctx_load_config(void)
1053 return thread_run_test(&test_lib_ctx_load_config_worker,
1054 MAXIMUM_THREADS, &test_lib_ctx_load_config_worker,
1055 1, default_provider);
1058 #if !defined(OPENSSL_NO_DGRAM) && !defined(OPENSSL_NO_SOCK)
1059 static BIO *multi_bio1, *multi_bio2;
1061 static void test_bio_dgram_pair_worker(void)
1066 uint8_t scratch[64];
1068 size_t num_processed = 0;
1070 if (!TEST_int_eq(RAND_bytes_ex(multi_libctx, &ch, 1, 64), 1))
1074 msg.data_len = sizeof(scratch);
1077 * We do not test for failure here as recvmmsg may fail if no sendmmsg
1078 * has been called yet. The purpose of this code is to exercise tsan.
1081 r = BIO_sendmmsg(ch & 1 ? multi_bio2 : multi_bio1, &msg,
1082 sizeof(BIO_MSG), 1, 0, &num_processed);
1084 r = BIO_recvmmsg(ch & 1 ? multi_bio2 : multi_bio1, &msg,
1085 sizeof(BIO_MSG), 1, 0, &num_processed);
1090 multi_set_success(0);
1093 static int test_bio_dgram_pair(void)
1096 BIO *bio1 = NULL, *bio2 = NULL;
1098 r = BIO_new_bio_dgram_pair(&bio1, 0, &bio2, 0);
1099 if (!TEST_int_eq(r, 1))
1105 r = thread_run_test(&test_bio_dgram_pair_worker,
1106 MAXIMUM_THREADS, &test_bio_dgram_pair_worker,
1107 1, default_provider);
1116 static const char *pemdataraw[] = {
1117 "-----BEGIN RSA PRIVATE KEY-----\n",
1118 "MIIBOgIBAAJBAMFcGsaxxdgiuuGmCkVImy4h99CqT7jwY3pexPGcnUFtR2Fh36Bp\n",
1119 "oncwtkZ4cAgtvd4Qs8PkxUdp6p/DlUmObdkCAwEAAQJAUR44xX6zB3eaeyvTRzms\n",
1120 "kHADrPCmPWnr8dxsNwiDGHzrMKLN+i/HAam+97HxIKVWNDH2ba9Mf1SA8xu9dcHZ\n",
1121 "AQIhAOHPCLxbtQFVxlnhSyxYeb7O323c3QulPNn3bhOipElpAiEA2zZpBE8ZXVnL\n",
1122 "74QjG4zINlDfH+EOEtjJJ3RtaYDugvECIBtsQDxXytChsRgDQ1TcXdStXPcDppie\n",
1123 "dZhm8yhRTTBZAiAZjE/U9rsIDC0ebxIAZfn3iplWh84yGB3pgUI3J5WkoQIhAInE\n",
1124 "HTUY5WRj5riZtkyGnbm3DvF+1eMtO2lYV+OuLcfE\n",
1125 "-----END RSA PRIVATE KEY-----\n",
1129 static void test_pem_read_one(void)
1131 EVP_PKEY *key = NULL;
1136 pemdata = glue_strings(pemdataraw, &len);
1137 if (pemdata == NULL) {
1138 multi_set_success(0);
1142 pem = BIO_new_mem_buf(pemdata, len);
1144 multi_set_success(0);
1148 key = PEM_read_bio_PrivateKey(pem, NULL, NULL, NULL);
1150 multi_set_success(0);
1155 OPENSSL_free(pemdata);
1158 /* Test reading PEM files in multiple threads */
1159 static int test_pem_read(void)
1161 return thread_run_test(&test_pem_read_one, MAXIMUM_THREADS,
1162 &test_pem_read_one, 1, default_provider);
1165 typedef enum OPTION_choice {
1168 OPT_FIPS, OPT_CONFIG_FILE,
1172 const OPTIONS *test_get_options(void)
1174 static const OPTIONS options[] = {
1175 OPT_TEST_OPTIONS_DEFAULT_USAGE,
1176 { "fips", OPT_FIPS, '-', "Test the FIPS provider" },
1177 { "config", OPT_CONFIG_FILE, '<',
1178 "The configuration file to use for the libctx" },
1184 int setup_tests(void)
1189 while ((o = opt_next()) != OPT_EOF) {
1194 case OPT_CONFIG_FILE:
1195 config_file = opt_arg();
1197 case OPT_TEST_CASES:
1204 if (!TEST_ptr(datadir = test_get_argument(0)))
1207 privkey = test_mk_file_path(datadir, "rsakey.pem");
1208 if (!TEST_ptr(privkey))
1211 if (!TEST_ptr(global_lock = CRYPTO_THREAD_lock_new()))
1214 #ifdef TSAN_REQUIRES_LOCKING
1215 if (!TEST_ptr(tsan_lock = CRYPTO_THREAD_lock_new()))
1219 /* Keep first to validate auto creation of default library context */
1220 ADD_TEST(test_multi_default);
1222 ADD_TEST(test_lock);
1223 #if defined(OPENSSL_THREADS)
1224 ADD_TEST(torture_rw_low);
1225 ADD_TEST(torture_rw_high);
1226 ADD_TEST(torture_rcu_low);
1227 ADD_TEST(torture_rcu_high);
1229 ADD_TEST(test_once);
1230 ADD_TEST(test_thread_local);
1231 ADD_TEST(test_atomic);
1232 ADD_TEST(test_multi_load);
1233 ADD_TEST(test_multi_general_worker_default_provider);
1234 ADD_TEST(test_multi_general_worker_fips_provider);
1235 ADD_TEST(test_multi_fetch_worker);
1236 ADD_TEST(test_multi_shared_pkey);
1237 #ifndef OPENSSL_NO_DEPRECATED_3_0
1238 ADD_TEST(test_multi_downgrade_shared_pkey);
1240 ADD_TEST(test_multi_load_unload_provider);
1241 ADD_TEST(test_obj_add);
1242 ADD_TEST(test_lib_ctx_load_config);
1243 #if !defined(OPENSSL_NO_DGRAM) && !defined(OPENSSL_NO_SOCK)
1244 ADD_TEST(test_bio_dgram_pair);
1246 ADD_TEST(test_pem_read);
1250 void cleanup_tests(void)
1252 OPENSSL_free(privkey);
1253 #ifdef TSAN_REQUIRES_LOCKING
1254 CRYPTO_THREAD_lock_free(tsan_lock);
1256 CRYPTO_THREAD_lock_free(global_lock);