async.c 12 KB

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