threadstest.c 35 KB

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