async.c 11 KB

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