async.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /*
  2. * Copyright 2015-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * Without this we start getting longjmp crashes because it thinks we're jumping
  11. * up the stack when in fact we are jumping to an entirely different stack. The
  12. * cost of this is not having certain buffer overrun/underrun checks etc for
  13. * this source file :-(
  14. */
  15. #undef _FORTIFY_SOURCE
  16. /* This must be the first #include file */
  17. #include "async_local.h"
  18. #include <openssl/err.h>
  19. #include "crypto/cryptlib.h"
  20. #include <string.h>
  21. #define ASYNC_JOB_RUNNING 0
  22. #define ASYNC_JOB_PAUSING 1
  23. #define ASYNC_JOB_PAUSED 2
  24. #define ASYNC_JOB_STOPPING 3
  25. static CRYPTO_THREAD_LOCAL ctxkey;
  26. static CRYPTO_THREAD_LOCAL poolkey;
  27. static void async_delete_thread_state(void *arg);
  28. static async_ctx *async_ctx_new(void)
  29. {
  30. async_ctx *nctx;
  31. if (!ossl_init_thread_start(NULL, NULL, async_delete_thread_state))
  32. return NULL;
  33. nctx = OPENSSL_malloc(sizeof(*nctx));
  34. if (nctx == NULL) {
  35. ERR_raise(ERR_LIB_ASYNC, ERR_R_MALLOC_FAILURE);
  36. goto err;
  37. }
  38. async_fibre_init_dispatcher(&nctx->dispatcher);
  39. nctx->currjob = NULL;
  40. nctx->blocked = 0;
  41. if (!CRYPTO_THREAD_set_local(&ctxkey, nctx))
  42. goto err;
  43. return nctx;
  44. err:
  45. OPENSSL_free(nctx);
  46. return NULL;
  47. }
  48. async_ctx *async_get_ctx(void)
  49. {
  50. return (async_ctx *)CRYPTO_THREAD_get_local(&ctxkey);
  51. }
  52. static int async_ctx_free(void)
  53. {
  54. async_ctx *ctx;
  55. ctx = async_get_ctx();
  56. if (!CRYPTO_THREAD_set_local(&ctxkey, NULL))
  57. return 0;
  58. OPENSSL_free(ctx);
  59. return 1;
  60. }
  61. static ASYNC_JOB *async_job_new(void)
  62. {
  63. ASYNC_JOB *job = NULL;
  64. job = OPENSSL_zalloc(sizeof(*job));
  65. if (job == NULL) {
  66. ERR_raise(ERR_LIB_ASYNC, ERR_R_MALLOC_FAILURE);
  67. return NULL;
  68. }
  69. job->status = ASYNC_JOB_RUNNING;
  70. return job;
  71. }
  72. static void async_job_free(ASYNC_JOB *job)
  73. {
  74. if (job != NULL) {
  75. OPENSSL_free(job->funcargs);
  76. async_fibre_free(&job->fibrectx);
  77. OPENSSL_free(job);
  78. }
  79. }
  80. static ASYNC_JOB *async_get_pool_job(void) {
  81. ASYNC_JOB *job;
  82. async_pool *pool;
  83. pool = (async_pool *)CRYPTO_THREAD_get_local(&poolkey);
  84. if (pool == NULL) {
  85. /*
  86. * Pool has not been initialised, so init with the defaults, i.e.
  87. * no max size and no pre-created jobs
  88. */
  89. if (ASYNC_init_thread(0, 0) == 0)
  90. return NULL;
  91. pool = (async_pool *)CRYPTO_THREAD_get_local(&poolkey);
  92. }
  93. job = sk_ASYNC_JOB_pop(pool->jobs);
  94. if (job == NULL) {
  95. /* Pool is empty */
  96. if ((pool->max_size != 0) && (pool->curr_size >= pool->max_size))
  97. return NULL;
  98. job = async_job_new();
  99. if (job != NULL) {
  100. if (! async_fibre_makecontext(&job->fibrectx)) {
  101. async_job_free(job);
  102. return NULL;
  103. }
  104. pool->curr_size++;
  105. }
  106. }
  107. return job;
  108. }
  109. static void async_release_job(ASYNC_JOB *job) {
  110. async_pool *pool;
  111. pool = (async_pool *)CRYPTO_THREAD_get_local(&poolkey);
  112. if (pool == NULL) {
  113. ERR_raise(ERR_LIB_ASYNC, ERR_R_INTERNAL_ERROR);
  114. return;
  115. }
  116. OPENSSL_free(job->funcargs);
  117. job->funcargs = NULL;
  118. sk_ASYNC_JOB_push(pool->jobs, job);
  119. }
  120. void async_start_func(void)
  121. {
  122. ASYNC_JOB *job;
  123. async_ctx *ctx = async_get_ctx();
  124. if (ctx == NULL) {
  125. ERR_raise(ERR_LIB_ASYNC, ERR_R_INTERNAL_ERROR);
  126. return;
  127. }
  128. while (1) {
  129. /* Run the job */
  130. job = ctx->currjob;
  131. job->ret = job->func(job->funcargs);
  132. /* Stop the job */
  133. job->status = ASYNC_JOB_STOPPING;
  134. if (!async_fibre_swapcontext(&job->fibrectx,
  135. &ctx->dispatcher, 1)) {
  136. /*
  137. * Should not happen. Getting here will close the thread...can't do
  138. * much about it
  139. */
  140. ERR_raise(ERR_LIB_ASYNC, ASYNC_R_FAILED_TO_SWAP_CONTEXT);
  141. }
  142. }
  143. }
  144. int ASYNC_start_job(ASYNC_JOB **job, ASYNC_WAIT_CTX *wctx, int *ret,
  145. int (*func)(void *), void *args, size_t size)
  146. {
  147. async_ctx *ctx;
  148. OSSL_LIB_CTX *libctx;
  149. if (!OPENSSL_init_crypto(OPENSSL_INIT_ASYNC, NULL))
  150. return ASYNC_ERR;
  151. ctx = async_get_ctx();
  152. if (ctx == NULL)
  153. ctx = async_ctx_new();
  154. if (ctx == NULL)
  155. return ASYNC_ERR;
  156. if (*job != NULL)
  157. ctx->currjob = *job;
  158. for (;;) {
  159. if (ctx->currjob != NULL) {
  160. if (ctx->currjob->status == ASYNC_JOB_STOPPING) {
  161. *ret = ctx->currjob->ret;
  162. ctx->currjob->waitctx = NULL;
  163. async_release_job(ctx->currjob);
  164. ctx->currjob = NULL;
  165. *job = NULL;
  166. return ASYNC_FINISH;
  167. }
  168. if (ctx->currjob->status == ASYNC_JOB_PAUSING) {
  169. *job = ctx->currjob;
  170. ctx->currjob->status = ASYNC_JOB_PAUSED;
  171. ctx->currjob = NULL;
  172. return ASYNC_PAUSE;
  173. }
  174. if (ctx->currjob->status == ASYNC_JOB_PAUSED) {
  175. if (*job == NULL)
  176. return ASYNC_ERR;
  177. ctx->currjob = *job;
  178. /*
  179. * Restore the default libctx to what it was the last time the
  180. * fibre ran
  181. */
  182. libctx = OSSL_LIB_CTX_set0_default(ctx->currjob->libctx);
  183. if (libctx == NULL) {
  184. /* Failed to set the default context */
  185. ERR_raise(ERR_LIB_ASYNC, ERR_R_INTERNAL_ERROR);
  186. goto err;
  187. }
  188. /* Resume previous job */
  189. if (!async_fibre_swapcontext(&ctx->dispatcher,
  190. &ctx->currjob->fibrectx, 1)) {
  191. ctx->currjob->libctx = OSSL_LIB_CTX_set0_default(libctx);
  192. ERR_raise(ERR_LIB_ASYNC, ASYNC_R_FAILED_TO_SWAP_CONTEXT);
  193. goto err;
  194. }
  195. /*
  196. * In case the fibre changed the default libctx we set it back
  197. * again to what it was originally, and remember what it had
  198. * been changed to.
  199. */
  200. ctx->currjob->libctx = OSSL_LIB_CTX_set0_default(libctx);
  201. continue;
  202. }
  203. /* Should not happen */
  204. ERR_raise(ERR_LIB_ASYNC, ERR_R_INTERNAL_ERROR);
  205. async_release_job(ctx->currjob);
  206. ctx->currjob = NULL;
  207. *job = NULL;
  208. return ASYNC_ERR;
  209. }
  210. /* Start a new job */
  211. if ((ctx->currjob = async_get_pool_job()) == NULL)
  212. return ASYNC_NO_JOBS;
  213. if (args != NULL) {
  214. ctx->currjob->funcargs = OPENSSL_malloc(size);
  215. if (ctx->currjob->funcargs == NULL) {
  216. ERR_raise(ERR_LIB_ASYNC, ERR_R_MALLOC_FAILURE);
  217. async_release_job(ctx->currjob);
  218. ctx->currjob = NULL;
  219. return ASYNC_ERR;
  220. }
  221. memcpy(ctx->currjob->funcargs, args, size);
  222. } else {
  223. ctx->currjob->funcargs = NULL;
  224. }
  225. ctx->currjob->func = func;
  226. ctx->currjob->waitctx = wctx;
  227. libctx = ossl_lib_ctx_get_concrete(NULL);
  228. if (!async_fibre_swapcontext(&ctx->dispatcher,
  229. &ctx->currjob->fibrectx, 1)) {
  230. ERR_raise(ERR_LIB_ASYNC, ASYNC_R_FAILED_TO_SWAP_CONTEXT);
  231. goto err;
  232. }
  233. /*
  234. * In case the fibre changed the default libctx we set it back again
  235. * to what it was, and remember what it had been changed to.
  236. */
  237. ctx->currjob->libctx = OSSL_LIB_CTX_set0_default(libctx);
  238. }
  239. err:
  240. async_release_job(ctx->currjob);
  241. ctx->currjob = NULL;
  242. *job = NULL;
  243. return ASYNC_ERR;
  244. }
  245. int ASYNC_pause_job(void)
  246. {
  247. ASYNC_JOB *job;
  248. async_ctx *ctx = async_get_ctx();
  249. if (ctx == NULL
  250. || ctx->currjob == NULL
  251. || ctx->blocked) {
  252. /*
  253. * Could be we've deliberately not been started within a job so this is
  254. * counted as success.
  255. */
  256. return 1;
  257. }
  258. job = ctx->currjob;
  259. job->status = ASYNC_JOB_PAUSING;
  260. if (!async_fibre_swapcontext(&job->fibrectx,
  261. &ctx->dispatcher, 1)) {
  262. ERR_raise(ERR_LIB_ASYNC, ASYNC_R_FAILED_TO_SWAP_CONTEXT);
  263. return 0;
  264. }
  265. /* Reset counts of added and deleted fds */
  266. async_wait_ctx_reset_counts(job->waitctx);
  267. return 1;
  268. }
  269. static void async_empty_pool(async_pool *pool)
  270. {
  271. ASYNC_JOB *job;
  272. if (pool == NULL || pool->jobs == NULL)
  273. return;
  274. do {
  275. job = sk_ASYNC_JOB_pop(pool->jobs);
  276. async_job_free(job);
  277. } while (job);
  278. }
  279. int async_init(void)
  280. {
  281. if (!CRYPTO_THREAD_init_local(&ctxkey, NULL))
  282. return 0;
  283. if (!CRYPTO_THREAD_init_local(&poolkey, NULL)) {
  284. CRYPTO_THREAD_cleanup_local(&ctxkey);
  285. return 0;
  286. }
  287. return async_local_init();
  288. }
  289. void async_deinit(void)
  290. {
  291. CRYPTO_THREAD_cleanup_local(&ctxkey);
  292. CRYPTO_THREAD_cleanup_local(&poolkey);
  293. async_local_deinit();
  294. }
  295. int ASYNC_init_thread(size_t max_size, size_t init_size)
  296. {
  297. async_pool *pool;
  298. size_t curr_size = 0;
  299. if (init_size > max_size) {
  300. ERR_raise(ERR_LIB_ASYNC, ASYNC_R_INVALID_POOL_SIZE);
  301. return 0;
  302. }
  303. if (!OPENSSL_init_crypto(OPENSSL_INIT_ASYNC, NULL))
  304. return 0;
  305. if (!ossl_init_thread_start(NULL, NULL, async_delete_thread_state))
  306. return 0;
  307. pool = OPENSSL_zalloc(sizeof(*pool));
  308. if (pool == NULL) {
  309. ERR_raise(ERR_LIB_ASYNC, ERR_R_MALLOC_FAILURE);
  310. return 0;
  311. }
  312. pool->jobs = sk_ASYNC_JOB_new_reserve(NULL, init_size);
  313. if (pool->jobs == NULL) {
  314. ERR_raise(ERR_LIB_ASYNC, ERR_R_MALLOC_FAILURE);
  315. OPENSSL_free(pool);
  316. return 0;
  317. }
  318. pool->max_size = max_size;
  319. /* Pre-create jobs as required */
  320. while (init_size--) {
  321. ASYNC_JOB *job;
  322. job = async_job_new();
  323. if (job == NULL || !async_fibre_makecontext(&job->fibrectx)) {
  324. /*
  325. * Not actually fatal because we already created the pool, just
  326. * skip creation of any more jobs
  327. */
  328. async_job_free(job);
  329. break;
  330. }
  331. job->funcargs = NULL;
  332. sk_ASYNC_JOB_push(pool->jobs, job); /* Cannot fail due to reserve */
  333. curr_size++;
  334. }
  335. pool->curr_size = curr_size;
  336. if (!CRYPTO_THREAD_set_local(&poolkey, pool)) {
  337. ERR_raise(ERR_LIB_ASYNC, ASYNC_R_FAILED_TO_SET_POOL);
  338. goto err;
  339. }
  340. return 1;
  341. err:
  342. async_empty_pool(pool);
  343. sk_ASYNC_JOB_free(pool->jobs);
  344. OPENSSL_free(pool);
  345. return 0;
  346. }
  347. static void async_delete_thread_state(void *arg)
  348. {
  349. async_pool *pool = (async_pool *)CRYPTO_THREAD_get_local(&poolkey);
  350. if (pool != NULL) {
  351. async_empty_pool(pool);
  352. sk_ASYNC_JOB_free(pool->jobs);
  353. OPENSSL_free(pool);
  354. CRYPTO_THREAD_set_local(&poolkey, NULL);
  355. }
  356. async_local_cleanup();
  357. async_ctx_free();
  358. }
  359. void ASYNC_cleanup_thread(void)
  360. {
  361. if (!OPENSSL_init_crypto(OPENSSL_INIT_ASYNC, NULL))
  362. return;
  363. async_delete_thread_state(NULL);
  364. }
  365. ASYNC_JOB *ASYNC_get_current_job(void)
  366. {
  367. async_ctx *ctx;
  368. if (!OPENSSL_init_crypto(OPENSSL_INIT_ASYNC, NULL))
  369. return NULL;
  370. ctx = async_get_ctx();
  371. if (ctx == NULL)
  372. return NULL;
  373. return ctx->currjob;
  374. }
  375. ASYNC_WAIT_CTX *ASYNC_get_wait_ctx(ASYNC_JOB *job)
  376. {
  377. return job->waitctx;
  378. }
  379. void ASYNC_block_pause(void)
  380. {
  381. async_ctx *ctx;
  382. if (!OPENSSL_init_crypto(OPENSSL_INIT_ASYNC, NULL))
  383. return;
  384. ctx = async_get_ctx();
  385. if (ctx == NULL || ctx->currjob == NULL) {
  386. /*
  387. * We're not in a job anyway so ignore this
  388. */
  389. return;
  390. }
  391. ctx->blocked++;
  392. }
  393. void ASYNC_unblock_pause(void)
  394. {
  395. async_ctx *ctx;
  396. if (!OPENSSL_init_crypto(OPENSSL_INIT_ASYNC, NULL))
  397. return;
  398. ctx = async_get_ctx();
  399. if (ctx == NULL || ctx->currjob == NULL) {
  400. /*
  401. * We're not in a job anyway so ignore this
  402. */
  403. return;
  404. }
  405. if (ctx->blocked > 0)
  406. ctx->blocked--;
  407. }