async.c 12 KB

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