2
0

threadstest.c 34 KB

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