threadstest.c 34 KB

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