async.c 12 KB

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