]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/threadstest.c
BIO_s_dgram_pair
[thirdparty/openssl.git] / test / threadstest.c
1 /*
2 * Copyright 2016-2022 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 "internal/tsan_assist.h"
29 #include "internal/nelem.h"
30 #include "testutil.h"
31 #include "threadstest.h"
32
33 /* Limit the maximum number of threads */
34 #define MAXIMUM_THREADS 10
35
36 /* Limit the maximum number of providers loaded into a library context */
37 #define MAXIMUM_PROVIDERS 4
38
39 static int do_fips = 0;
40 static char *privkey;
41 static char *config_file = NULL;
42 static int multidefault_run = 0;
43
44 static const char *default_provider[] = { "default", NULL };
45 static const char *fips_provider[] = { "fips", NULL };
46 static const char *fips_and_default_providers[] = { "default", "fips", NULL };
47
48 static CRYPTO_RWLOCK *global_lock;
49
50 #ifdef TSAN_REQUIRES_LOCKING
51 static CRYPTO_RWLOCK *tsan_lock;
52 #endif
53
54 /* Grab a globally unique integer value, return 0 on failure */
55 static int get_new_uid(void)
56 {
57 /*
58 * Start with a nice large number to avoid potential conflicts when
59 * we generate a new OID.
60 */
61 static TSAN_QUALIFIER int current_uid = 1 << (sizeof(int) * 8 - 2);
62 #ifdef TSAN_REQUIRES_LOCKING
63 int r;
64
65 if (!TEST_true(CRYPTO_THREAD_write_lock(tsan_lock)))
66 return 0;
67 r = ++current_uid;
68 if (!TEST_true(CRYPTO_THREAD_unlock(tsan_lock)))
69 return 0;
70 return r;
71
72 #else
73 return tsan_counter(&current_uid);
74 #endif
75 }
76
77 static int test_lock(void)
78 {
79 CRYPTO_RWLOCK *lock = CRYPTO_THREAD_lock_new();
80 int res;
81
82 res = TEST_true(CRYPTO_THREAD_read_lock(lock))
83 && TEST_true(CRYPTO_THREAD_unlock(lock))
84 && TEST_true(CRYPTO_THREAD_write_lock(lock))
85 && TEST_true(CRYPTO_THREAD_unlock(lock));
86
87 CRYPTO_THREAD_lock_free(lock);
88
89 return res;
90 }
91
92 static CRYPTO_ONCE once_run = CRYPTO_ONCE_STATIC_INIT;
93 static unsigned once_run_count = 0;
94
95 static void once_do_run(void)
96 {
97 once_run_count++;
98 }
99
100 static void once_run_thread_cb(void)
101 {
102 CRYPTO_THREAD_run_once(&once_run, once_do_run);
103 }
104
105 static int test_once(void)
106 {
107 thread_t thread;
108
109 if (!TEST_true(run_thread(&thread, once_run_thread_cb))
110 || !TEST_true(wait_for_thread(thread))
111 || !CRYPTO_THREAD_run_once(&once_run, once_do_run)
112 || !TEST_int_eq(once_run_count, 1))
113 return 0;
114 return 1;
115 }
116
117 static CRYPTO_THREAD_LOCAL thread_local_key;
118 static unsigned destructor_run_count = 0;
119 static int thread_local_thread_cb_ok = 0;
120
121 static void thread_local_destructor(void *arg)
122 {
123 unsigned *count;
124
125 if (arg == NULL)
126 return;
127
128 count = arg;
129
130 (*count)++;
131 }
132
133 static void thread_local_thread_cb(void)
134 {
135 void *ptr;
136
137 ptr = CRYPTO_THREAD_get_local(&thread_local_key);
138 if (!TEST_ptr_null(ptr)
139 || !TEST_true(CRYPTO_THREAD_set_local(&thread_local_key,
140 &destructor_run_count)))
141 return;
142
143 ptr = CRYPTO_THREAD_get_local(&thread_local_key);
144 if (!TEST_ptr_eq(ptr, &destructor_run_count))
145 return;
146
147 thread_local_thread_cb_ok = 1;
148 }
149
150 static int test_thread_local(void)
151 {
152 thread_t thread;
153 void *ptr = NULL;
154
155 if (!TEST_true(CRYPTO_THREAD_init_local(&thread_local_key,
156 thread_local_destructor)))
157 return 0;
158
159 ptr = CRYPTO_THREAD_get_local(&thread_local_key);
160 if (!TEST_ptr_null(ptr)
161 || !TEST_true(run_thread(&thread, thread_local_thread_cb))
162 || !TEST_true(wait_for_thread(thread))
163 || !TEST_int_eq(thread_local_thread_cb_ok, 1))
164 return 0;
165
166 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
167
168 ptr = CRYPTO_THREAD_get_local(&thread_local_key);
169 if (!TEST_ptr_null(ptr))
170 return 0;
171
172 # if !defined(OPENSSL_SYS_WINDOWS)
173 if (!TEST_int_eq(destructor_run_count, 1))
174 return 0;
175 # endif
176 #endif
177
178 if (!TEST_true(CRYPTO_THREAD_cleanup_local(&thread_local_key)))
179 return 0;
180 return 1;
181 }
182
183 static int test_atomic(void)
184 {
185 int val = 0, ret = 0, testresult = 0;
186 uint64_t val64 = 1, ret64 = 0;
187 CRYPTO_RWLOCK *lock = CRYPTO_THREAD_lock_new();
188
189 if (!TEST_ptr(lock))
190 return 0;
191
192 if (CRYPTO_atomic_add(&val, 1, &ret, NULL)) {
193 /* This succeeds therefore we're on a platform with lockless atomics */
194 if (!TEST_int_eq(val, 1) || !TEST_int_eq(val, ret))
195 goto err;
196 } else {
197 /* This failed therefore we're on a platform without lockless atomics */
198 if (!TEST_int_eq(val, 0) || !TEST_int_eq(val, ret))
199 goto err;
200 }
201 val = 0;
202 ret = 0;
203
204 if (!TEST_true(CRYPTO_atomic_add(&val, 1, &ret, lock)))
205 goto err;
206 if (!TEST_int_eq(val, 1) || !TEST_int_eq(val, ret))
207 goto err;
208
209 if (CRYPTO_atomic_or(&val64, 2, &ret64, NULL)) {
210 /* This succeeds therefore we're on a platform with lockless atomics */
211 if (!TEST_uint_eq((unsigned int)val64, 3)
212 || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
213 goto err;
214 } else {
215 /* This failed therefore we're on a platform without lockless atomics */
216 if (!TEST_uint_eq((unsigned int)val64, 1)
217 || !TEST_int_eq((unsigned int)ret64, 0))
218 goto err;
219 }
220 val64 = 1;
221 ret64 = 0;
222
223 if (!TEST_true(CRYPTO_atomic_or(&val64, 2, &ret64, lock)))
224 goto err;
225
226 if (!TEST_uint_eq((unsigned int)val64, 3)
227 || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
228 goto err;
229
230 ret64 = 0;
231 if (CRYPTO_atomic_load(&val64, &ret64, NULL)) {
232 /* This succeeds therefore we're on a platform with lockless atomics */
233 if (!TEST_uint_eq((unsigned int)val64, 3)
234 || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
235 goto err;
236 } else {
237 /* This failed therefore we're on a platform without lockless atomics */
238 if (!TEST_uint_eq((unsigned int)val64, 3)
239 || !TEST_int_eq((unsigned int)ret64, 0))
240 goto err;
241 }
242
243 ret64 = 0;
244 if (!TEST_true(CRYPTO_atomic_load(&val64, &ret64, lock)))
245 goto err;
246
247 if (!TEST_uint_eq((unsigned int)val64, 3)
248 || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
249 goto err;
250
251 testresult = 1;
252 err:
253 CRYPTO_THREAD_lock_free(lock);
254 return testresult;
255 }
256
257 static OSSL_LIB_CTX *multi_libctx = NULL;
258 static int multi_success;
259 static OSSL_PROVIDER *multi_provider[MAXIMUM_PROVIDERS + 1];
260 static size_t multi_num_threads;
261 static thread_t multi_threads[MAXIMUM_THREADS];
262
263 static void multi_intialise(void)
264 {
265 multi_success = 1;
266 multi_libctx = NULL;
267 multi_num_threads = 0;
268 memset(multi_threads, 0, sizeof(multi_threads));
269 memset(multi_provider, 0, sizeof(multi_provider));
270 }
271
272 static void multi_set_success(int ok)
273 {
274 if (CRYPTO_THREAD_write_lock(global_lock) == 0) {
275 /* not synchronized, but better than not reporting failure */
276 multi_success = ok;
277 return;
278 }
279
280 multi_success = ok;
281
282 CRYPTO_THREAD_unlock(global_lock);
283 }
284
285 static void thead_teardown_libctx(void)
286 {
287 OSSL_PROVIDER **p;
288
289 for (p = multi_provider; *p != NULL; p++)
290 OSSL_PROVIDER_unload(*p);
291 OSSL_LIB_CTX_free(multi_libctx);
292 multi_intialise();
293 }
294
295 static int thread_setup_libctx(int libctx, const char *providers[])
296 {
297 size_t n;
298
299 if (libctx && !TEST_true(test_get_libctx(&multi_libctx, NULL, config_file,
300 NULL, NULL)))
301 return 0;
302
303 if (providers != NULL)
304 for (n = 0; providers[n] != NULL; n++)
305 if (!TEST_size_t_lt(n, MAXIMUM_PROVIDERS)
306 || !TEST_ptr(multi_provider[n] = OSSL_PROVIDER_load(multi_libctx,
307 providers[n]))) {
308 thead_teardown_libctx();
309 return 0;
310 }
311 return 1;
312 }
313
314 static int teardown_threads(void)
315 {
316 size_t i;
317
318 for (i = 0; i < multi_num_threads; i++)
319 if (!TEST_true(wait_for_thread(multi_threads[i])))
320 return 0;
321 return 1;
322 }
323
324 static int start_threads(size_t n, void (*thread_func)(void))
325 {
326 size_t i;
327
328 if (!TEST_size_t_le(multi_num_threads + n, MAXIMUM_THREADS))
329 return 0;
330
331 for (i = 0 ; i < n; i++)
332 if (!TEST_true(run_thread(multi_threads + multi_num_threads++, thread_func)))
333 return 0;
334 return 1;
335 }
336
337 /* Template multi-threaded test function */
338 static int thread_run_test(void (*main_func)(void),
339 size_t num_threads, void (*thread_func)(void),
340 int libctx, const char *providers[])
341 {
342 int testresult = 0;
343
344 multi_intialise();
345 if (!thread_setup_libctx(libctx, providers)
346 || !start_threads(num_threads, thread_func))
347 goto err;
348
349 if (main_func != NULL)
350 main_func();
351
352 if (!teardown_threads()
353 || !TEST_true(multi_success))
354 goto err;
355 testresult = 1;
356 err:
357 thead_teardown_libctx();
358 return testresult;
359 }
360
361 static void thread_general_worker(void)
362 {
363 EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
364 EVP_MD *md = EVP_MD_fetch(multi_libctx, "SHA2-256", NULL);
365 EVP_CIPHER_CTX *cipherctx = EVP_CIPHER_CTX_new();
366 EVP_CIPHER *ciph = EVP_CIPHER_fetch(multi_libctx, "AES-128-CBC", NULL);
367 const char *message = "Hello World";
368 size_t messlen = strlen(message);
369 /* Should be big enough for encryption output too */
370 unsigned char out[EVP_MAX_MD_SIZE];
371 const unsigned char key[AES_BLOCK_SIZE] = {
372 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
373 0x0c, 0x0d, 0x0e, 0x0f
374 };
375 const unsigned char iv[AES_BLOCK_SIZE] = {
376 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
377 0x0c, 0x0d, 0x0e, 0x0f
378 };
379 unsigned int mdoutl;
380 int ciphoutl;
381 EVP_PKEY *pkey = NULL;
382 int testresult = 0;
383 int i, isfips;
384
385 isfips = OSSL_PROVIDER_available(multi_libctx, "fips");
386
387 if (!TEST_ptr(mdctx)
388 || !TEST_ptr(md)
389 || !TEST_ptr(cipherctx)
390 || !TEST_ptr(ciph))
391 goto err;
392
393 /* Do some work */
394 for (i = 0; i < 5; i++) {
395 if (!TEST_true(EVP_DigestInit_ex(mdctx, md, NULL))
396 || !TEST_true(EVP_DigestUpdate(mdctx, message, messlen))
397 || !TEST_true(EVP_DigestFinal(mdctx, out, &mdoutl)))
398 goto err;
399 }
400 for (i = 0; i < 5; i++) {
401 if (!TEST_true(EVP_EncryptInit_ex(cipherctx, ciph, NULL, key, iv))
402 || !TEST_true(EVP_EncryptUpdate(cipherctx, out, &ciphoutl,
403 (unsigned char *)message,
404 messlen))
405 || !TEST_true(EVP_EncryptFinal(cipherctx, out, &ciphoutl)))
406 goto err;
407 }
408
409 /*
410 * We want the test to run quickly - not securely.
411 * Therefore we use an insecure bit length where we can (512).
412 * In the FIPS module though we must use a longer length.
413 */
414 pkey = EVP_PKEY_Q_keygen(multi_libctx, NULL, "RSA", isfips ? 2048 : 512);
415 if (!TEST_ptr(pkey))
416 goto err;
417
418 testresult = 1;
419 err:
420 EVP_MD_CTX_free(mdctx);
421 EVP_MD_free(md);
422 EVP_CIPHER_CTX_free(cipherctx);
423 EVP_CIPHER_free(ciph);
424 EVP_PKEY_free(pkey);
425 if (!testresult)
426 multi_set_success(0);
427 }
428
429 static void thread_multi_simple_fetch(void)
430 {
431 EVP_MD *md = EVP_MD_fetch(multi_libctx, "SHA2-256", NULL);
432
433 if (md != NULL)
434 EVP_MD_free(md);
435 else
436 multi_set_success(0);
437 }
438
439 static EVP_PKEY *shared_evp_pkey = NULL;
440
441 static void thread_shared_evp_pkey(void)
442 {
443 char *msg = "Hello World";
444 unsigned char ctbuf[256];
445 unsigned char ptbuf[256];
446 size_t ptlen, ctlen = sizeof(ctbuf);
447 EVP_PKEY_CTX *ctx = NULL;
448 int success = 0;
449 int i;
450
451 for (i = 0; i < 1 + do_fips; i++) {
452 if (i > 0)
453 EVP_PKEY_CTX_free(ctx);
454 ctx = EVP_PKEY_CTX_new_from_pkey(multi_libctx, shared_evp_pkey,
455 i == 0 ? "provider=default"
456 : "provider=fips");
457 if (!TEST_ptr(ctx))
458 goto err;
459
460 if (!TEST_int_ge(EVP_PKEY_encrypt_init(ctx), 0)
461 || !TEST_int_ge(EVP_PKEY_encrypt(ctx, ctbuf, &ctlen,
462 (unsigned char *)msg, strlen(msg)),
463 0))
464 goto err;
465
466 EVP_PKEY_CTX_free(ctx);
467 ctx = EVP_PKEY_CTX_new_from_pkey(multi_libctx, shared_evp_pkey, NULL);
468
469 if (!TEST_ptr(ctx))
470 goto err;
471
472 ptlen = sizeof(ptbuf);
473 if (!TEST_int_ge(EVP_PKEY_decrypt_init(ctx), 0)
474 || !TEST_int_gt(EVP_PKEY_decrypt(ctx, ptbuf, &ptlen, ctbuf, ctlen),
475 0)
476 || !TEST_mem_eq(msg, strlen(msg), ptbuf, ptlen))
477 goto err;
478 }
479
480 success = 1;
481
482 err:
483 EVP_PKEY_CTX_free(ctx);
484 if (!success)
485 multi_set_success(0);
486 }
487
488 static void thread_provider_load_unload(void)
489 {
490 OSSL_PROVIDER *deflt = OSSL_PROVIDER_load(multi_libctx, "default");
491
492 if (!TEST_ptr(deflt)
493 || !TEST_true(OSSL_PROVIDER_available(multi_libctx, "default")))
494 multi_set_success(0);
495
496 OSSL_PROVIDER_unload(deflt);
497 }
498
499 static int test_multi_general_worker_default_provider(void)
500 {
501 return thread_run_test(&thread_general_worker, 2, &thread_general_worker,
502 1, default_provider);
503 }
504
505 static int test_multi_general_worker_fips_provider(void)
506 {
507 if (!do_fips)
508 return TEST_skip("FIPS not supported");
509 return thread_run_test(&thread_general_worker, 2, &thread_general_worker,
510 1, fips_provider);
511 }
512
513 static int test_multi_fetch_worker(void)
514 {
515 return thread_run_test(&thread_multi_simple_fetch,
516 2, &thread_multi_simple_fetch, 1, default_provider);
517 }
518
519 static int test_multi_shared_pkey_common(void (*worker)(void))
520 {
521 int testresult = 0;
522
523 multi_intialise();
524 if (!thread_setup_libctx(1, do_fips ? fips_and_default_providers
525 : default_provider)
526 || !TEST_ptr(shared_evp_pkey = load_pkey_pem(privkey, multi_libctx))
527 || !start_threads(1, &thread_shared_evp_pkey)
528 || !start_threads(1, worker))
529 goto err;
530
531 thread_shared_evp_pkey();
532
533 if (!teardown_threads()
534 || !TEST_true(multi_success))
535 goto err;
536 testresult = 1;
537 err:
538 EVP_PKEY_free(shared_evp_pkey);
539 thead_teardown_libctx();
540 return testresult;
541 }
542
543 #ifndef OPENSSL_NO_DEPRECATED_3_0
544 static void thread_downgrade_shared_evp_pkey(void)
545 {
546 /*
547 * This test is only relevant for deprecated functions that perform
548 * downgrading
549 */
550 if (EVP_PKEY_get0_RSA(shared_evp_pkey) == NULL)
551 multi_set_success(0);
552 }
553
554 static int test_multi_downgrade_shared_pkey(void)
555 {
556 return test_multi_shared_pkey_common(&thread_downgrade_shared_evp_pkey);
557 }
558 #endif
559
560 static int test_multi_shared_pkey(void)
561 {
562 return test_multi_shared_pkey_common(&thread_shared_evp_pkey);
563 }
564
565 static int test_multi_load_unload_provider(void)
566 {
567 EVP_MD *sha256 = NULL;
568 OSSL_PROVIDER *prov = NULL;
569 int testresult = 0;
570
571 multi_intialise();
572 if (!thread_setup_libctx(1, NULL)
573 || !TEST_ptr(prov = OSSL_PROVIDER_load(multi_libctx, "default"))
574 || !TEST_ptr(sha256 = EVP_MD_fetch(multi_libctx, "SHA2-256", NULL))
575 || !TEST_true(OSSL_PROVIDER_unload(prov)))
576 goto err;
577 prov = NULL;
578
579 if (!start_threads(2, &thread_provider_load_unload))
580 goto err;
581
582 thread_provider_load_unload();
583
584 if (!teardown_threads()
585 || !TEST_true(multi_success))
586 goto err;
587 testresult = 1;
588 err:
589 OSSL_PROVIDER_unload(prov);
590 EVP_MD_free(sha256);
591 thead_teardown_libctx();
592 return testresult;
593 }
594
595 static char *multi_load_provider = "legacy";
596 /*
597 * This test attempts to load several providers at the same time, and if
598 * run with a thread sanitizer, should crash if the core provider code
599 * doesn't synchronize well enough.
600 */
601 static void test_multi_load_worker(void)
602 {
603 OSSL_PROVIDER *prov;
604
605 if (!TEST_ptr(prov = OSSL_PROVIDER_load(multi_libctx, multi_load_provider))
606 || !TEST_true(OSSL_PROVIDER_unload(prov)))
607 multi_set_success(0);
608 }
609
610 static int test_multi_default(void)
611 {
612 /* Avoid running this test twice */
613 if (multidefault_run) {
614 TEST_skip("multi default test already run");
615 return 1;
616 }
617 multidefault_run = 1;
618
619 return thread_run_test(&thread_multi_simple_fetch,
620 2, &thread_multi_simple_fetch, 0, default_provider);
621 }
622
623 static int test_multi_load(void)
624 {
625 int res = 1;
626 OSSL_PROVIDER *prov;
627
628 /* The multidefault test must run prior to this test */
629 if (!multidefault_run) {
630 TEST_info("Running multi default test first");
631 res = test_multi_default();
632 }
633
634 /*
635 * We use the legacy provider in test_multi_load_worker because it uses a
636 * child libctx that might hit more codepaths that might be sensitive to
637 * threading issues. But in a no-legacy build that won't be loadable so
638 * we use the default provider instead.
639 */
640 prov = OSSL_PROVIDER_load(NULL, "legacy");
641 if (prov == NULL) {
642 TEST_info("Cannot load legacy provider - assuming this is a no-legacy build");
643 multi_load_provider = "default";
644 }
645 OSSL_PROVIDER_unload(prov);
646
647 return thread_run_test(NULL, MAXIMUM_THREADS, &test_multi_load_worker, 0,
648 NULL) && res;
649 }
650
651 static void test_obj_create_one(void)
652 {
653 char tids[12], oid[40], sn[30], ln[30];
654 int id = get_new_uid();
655
656 BIO_snprintf(tids, sizeof(tids), "%d", id);
657 BIO_snprintf(oid, sizeof(oid), "1.3.6.1.4.1.16604.%s", tids);
658 BIO_snprintf(sn, sizeof(sn), "short-name-%s", tids);
659 BIO_snprintf(ln, sizeof(ln), "long-name-%s", tids);
660 if (!TEST_int_ne(id, 0)
661 || !TEST_true(id = OBJ_create(oid, sn, ln))
662 || !TEST_true(OBJ_add_sigid(id, NID_sha3_256, NID_rsa)))
663 multi_set_success(0);
664 }
665
666 static int test_obj_add(void)
667 {
668 return thread_run_test(&test_obj_create_one,
669 MAXIMUM_THREADS, &test_obj_create_one,
670 1, default_provider);
671 }
672
673 static void test_lib_ctx_load_config_worker(void)
674 {
675 if (!TEST_int_eq(OSSL_LIB_CTX_load_config(multi_libctx, config_file), 1))
676 multi_set_success(0);
677 }
678
679 static int test_lib_ctx_load_config(void)
680 {
681 return thread_run_test(&test_lib_ctx_load_config_worker,
682 MAXIMUM_THREADS, &test_lib_ctx_load_config_worker,
683 1, default_provider);
684 }
685
686 #if !defined(OPENSSL_NO_DGRAM) && !defined(OPENSSL_NO_SOCK)
687 static BIO *multi_bio1, *multi_bio2;
688
689 static void test_bio_dgram_pair_worker(void)
690 {
691 ossl_unused int r;
692 int ok = 0;
693 uint8_t ch = 0;
694 uint8_t scratch[64];
695 BIO_MSG msg = {0};
696 size_t num_processed = 0;
697
698 if (!TEST_int_eq(RAND_bytes_ex(multi_libctx, &ch, 1, 64), 1))
699 goto err;
700
701 msg.data = scratch;
702 msg.data_len = sizeof(scratch);
703
704 /*
705 * We do not test for failure here as recvmmsg may fail if no sendmmsg
706 * has been called yet. The purpose of this code is to exercise tsan.
707 */
708 if (ch & 2)
709 r = BIO_sendmmsg(ch & 1 ? multi_bio2 : multi_bio1, &msg,
710 sizeof(BIO_MSG), 1, 0, &num_processed);
711 else
712 r = BIO_recvmmsg(ch & 1 ? multi_bio2 : multi_bio1, &msg,
713 sizeof(BIO_MSG), 1, 0, &num_processed);
714
715 ok = 1;
716 err:
717 if (ok == 0)
718 multi_set_success(0);
719 }
720
721 static int test_bio_dgram_pair(void)
722 {
723 int r;
724 BIO *bio1 = NULL, *bio2 = NULL;
725
726 r = BIO_new_bio_dgram_pair(&bio1, 0, &bio2, 0);
727 if (!TEST_int_eq(r, 1))
728 goto err;
729
730 multi_bio1 = bio1;
731 multi_bio2 = bio2;
732
733 r = thread_run_test(&test_bio_dgram_pair_worker,
734 MAXIMUM_THREADS, &test_bio_dgram_pair_worker,
735 1, default_provider);
736
737 err:
738 BIO_free(bio1);
739 BIO_free(bio2);
740 return r;
741 }
742 #endif
743
744 typedef enum OPTION_choice {
745 OPT_ERR = -1,
746 OPT_EOF = 0,
747 OPT_FIPS, OPT_CONFIG_FILE,
748 OPT_TEST_ENUM
749 } OPTION_CHOICE;
750
751 const OPTIONS *test_get_options(void)
752 {
753 static const OPTIONS options[] = {
754 OPT_TEST_OPTIONS_DEFAULT_USAGE,
755 { "fips", OPT_FIPS, '-', "Test the FIPS provider" },
756 { "config", OPT_CONFIG_FILE, '<',
757 "The configuration file to use for the libctx" },
758 { NULL }
759 };
760 return options;
761 }
762
763 int setup_tests(void)
764 {
765 OPTION_CHOICE o;
766 char *datadir;
767
768 while ((o = opt_next()) != OPT_EOF) {
769 switch (o) {
770 case OPT_FIPS:
771 do_fips = 1;
772 break;
773 case OPT_CONFIG_FILE:
774 config_file = opt_arg();
775 break;
776 case OPT_TEST_CASES:
777 break;
778 default:
779 return 0;
780 }
781 }
782
783 if (!TEST_ptr(datadir = test_get_argument(0)))
784 return 0;
785
786 privkey = test_mk_file_path(datadir, "rsakey.pem");
787 if (!TEST_ptr(privkey))
788 return 0;
789
790 if (!TEST_ptr(global_lock = CRYPTO_THREAD_lock_new()))
791 return 0;
792
793 #ifdef TSAN_REQUIRES_LOCKING
794 if (!TEST_ptr(tsan_lock = CRYPTO_THREAD_lock_new()))
795 return 0;
796 #endif
797
798 /* Keep first to validate auto creation of default library context */
799 ADD_TEST(test_multi_default);
800
801 ADD_TEST(test_lock);
802 ADD_TEST(test_once);
803 ADD_TEST(test_thread_local);
804 ADD_TEST(test_atomic);
805 ADD_TEST(test_multi_load);
806 ADD_TEST(test_multi_general_worker_default_provider);
807 ADD_TEST(test_multi_general_worker_fips_provider);
808 ADD_TEST(test_multi_fetch_worker);
809 ADD_TEST(test_multi_shared_pkey);
810 #ifndef OPENSSL_NO_DEPRECATED_3_0
811 ADD_TEST(test_multi_downgrade_shared_pkey);
812 #endif
813 ADD_TEST(test_multi_load_unload_provider);
814 ADD_TEST(test_obj_add);
815 ADD_TEST(test_lib_ctx_load_config);
816 #if !defined(OPENSSL_NO_DGRAM) && !defined(OPENSSL_NO_SOCK)
817 ADD_TEST(test_bio_dgram_pair);
818 #endif
819 return 1;
820 }
821
822 void cleanup_tests(void)
823 {
824 OPENSSL_free(privkey);
825 #ifdef TSAN_REQUIRES_LOCKING
826 CRYPTO_THREAD_lock_free(tsan_lock);
827 #endif
828 CRYPTO_THREAD_lock_free(global_lock);
829 }