async.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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 void async_free_pool_internal(async_pool *pool);
  28. static async_ctx *async_ctx_new(void)
  29. {
  30. async_ctx *nctx = NULL;
  31. nctx = OPENSSL_malloc(sizeof(*nctx));
  32. if (nctx == NULL) {
  33. ASYNCerr(ASYNC_F_ASYNC_CTX_NEW, ERR_R_MALLOC_FAILURE);
  34. goto err;
  35. }
  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. if (!OPENSSL_init_crypto(OPENSSL_INIT_ASYNC, NULL))
  49. return NULL;
  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 = async_get_ctx();
  140. if (ctx == NULL)
  141. ctx = async_ctx_new();
  142. if (ctx == NULL) {
  143. return ASYNC_ERR;
  144. }
  145. if (*job) {
  146. ctx->currjob = *job;
  147. }
  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. }
  186. if (args != NULL) {
  187. ctx->currjob->funcargs = OPENSSL_malloc(size);
  188. if (ctx->currjob->funcargs == NULL) {
  189. ASYNCerr(ASYNC_F_ASYNC_START_JOB, ERR_R_MALLOC_FAILURE);
  190. async_release_job(ctx->currjob);
  191. ctx->currjob = NULL;
  192. return ASYNC_ERR;
  193. }
  194. memcpy(ctx->currjob->funcargs, args, size);
  195. } else {
  196. ctx->currjob->funcargs = NULL;
  197. }
  198. ctx->currjob->func = func;
  199. ctx->currjob->waitctx = wctx;
  200. if (!async_fibre_swapcontext(&ctx->dispatcher,
  201. &ctx->currjob->fibrectx, 1)) {
  202. ASYNCerr(ASYNC_F_ASYNC_START_JOB, ASYNC_R_FAILED_TO_SWAP_CONTEXT);
  203. goto err;
  204. }
  205. }
  206. err:
  207. async_release_job(ctx->currjob);
  208. ctx->currjob = NULL;
  209. *job = NULL;
  210. return ASYNC_ERR;
  211. }
  212. int ASYNC_pause_job(void)
  213. {
  214. ASYNC_JOB *job;
  215. async_ctx *ctx = async_get_ctx();
  216. if (ctx == NULL
  217. || ctx->currjob == NULL
  218. || ctx->blocked) {
  219. /*
  220. * Could be we've deliberately not been started within a job so this is
  221. * counted as success.
  222. */
  223. return 1;
  224. }
  225. job = ctx->currjob;
  226. job->status = ASYNC_JOB_PAUSING;
  227. if (!async_fibre_swapcontext(&job->fibrectx,
  228. &ctx->dispatcher, 1)) {
  229. ASYNCerr(ASYNC_F_ASYNC_PAUSE_JOB, ASYNC_R_FAILED_TO_SWAP_CONTEXT);
  230. return 0;
  231. }
  232. /* Reset counts of added and deleted fds */
  233. async_wait_ctx_reset_counts(job->waitctx);
  234. return 1;
  235. }
  236. static void async_empty_pool(async_pool *pool)
  237. {
  238. ASYNC_JOB *job;
  239. if (!pool || !pool->jobs)
  240. return;
  241. do {
  242. job = sk_ASYNC_JOB_pop(pool->jobs);
  243. async_job_free(job);
  244. } while (job);
  245. }
  246. int async_init(void)
  247. {
  248. if (!CRYPTO_THREAD_init_local(&ctxkey, NULL))
  249. return 0;
  250. if (!CRYPTO_THREAD_init_local(&poolkey, NULL)) {
  251. CRYPTO_THREAD_cleanup_local(&ctxkey);
  252. return 0;
  253. }
  254. return 1;
  255. }
  256. void async_deinit(void)
  257. {
  258. CRYPTO_THREAD_cleanup_local(&ctxkey);
  259. CRYPTO_THREAD_cleanup_local(&poolkey);
  260. }
  261. int ASYNC_init_thread(size_t max_size, size_t init_size)
  262. {
  263. async_pool *pool;
  264. size_t curr_size = 0;
  265. if (init_size > max_size) {
  266. ASYNCerr(ASYNC_F_ASYNC_INIT_THREAD, ASYNC_R_INVALID_POOL_SIZE);
  267. return 0;
  268. }
  269. if (!OPENSSL_init_crypto(OPENSSL_INIT_ASYNC, NULL)) {
  270. return 0;
  271. }
  272. if (!ossl_init_thread_start(OPENSSL_INIT_THREAD_ASYNC)) {
  273. return 0;
  274. }
  275. pool = OPENSSL_zalloc(sizeof(*pool));
  276. if (pool == NULL) {
  277. ASYNCerr(ASYNC_F_ASYNC_INIT_THREAD, ERR_R_MALLOC_FAILURE);
  278. return 0;
  279. }
  280. pool->jobs = sk_ASYNC_JOB_new_reserve(NULL, init_size);
  281. if (pool->jobs == NULL) {
  282. ASYNCerr(ASYNC_F_ASYNC_INIT_THREAD, ERR_R_MALLOC_FAILURE);
  283. OPENSSL_free(pool);
  284. return 0;
  285. }
  286. pool->max_size = max_size;
  287. /* Pre-create jobs as required */
  288. while (init_size--) {
  289. ASYNC_JOB *job;
  290. job = async_job_new();
  291. if (job == NULL || !async_fibre_makecontext(&job->fibrectx)) {
  292. /*
  293. * Not actually fatal because we already created the pool, just
  294. * skip creation of any more jobs
  295. */
  296. async_job_free(job);
  297. break;
  298. }
  299. job->funcargs = NULL;
  300. sk_ASYNC_JOB_push(pool->jobs, job); /* Cannot fail due to reserve */
  301. curr_size++;
  302. }
  303. pool->curr_size = curr_size;
  304. if (!CRYPTO_THREAD_set_local(&poolkey, pool)) {
  305. ASYNCerr(ASYNC_F_ASYNC_INIT_THREAD, ASYNC_R_FAILED_TO_SET_POOL);
  306. goto err;
  307. }
  308. return 1;
  309. err:
  310. async_free_pool_internal(pool);
  311. return 0;
  312. }
  313. static void async_free_pool_internal(async_pool *pool)
  314. {
  315. if (pool == NULL)
  316. return;
  317. async_empty_pool(pool);
  318. sk_ASYNC_JOB_free(pool->jobs);
  319. OPENSSL_free(pool);
  320. CRYPTO_THREAD_set_local(&poolkey, NULL);
  321. async_local_cleanup();
  322. async_ctx_free();
  323. }
  324. void ASYNC_cleanup_thread(void)
  325. {
  326. async_free_pool_internal((async_pool *)CRYPTO_THREAD_get_local(&poolkey));
  327. }
  328. ASYNC_JOB *ASYNC_get_current_job(void)
  329. {
  330. async_ctx *ctx;
  331. ctx = async_get_ctx();
  332. if (ctx == NULL)
  333. return NULL;
  334. return ctx->currjob;
  335. }
  336. ASYNC_WAIT_CTX *ASYNC_get_wait_ctx(ASYNC_JOB *job)
  337. {
  338. return job->waitctx;
  339. }
  340. void ASYNC_block_pause(void)
  341. {
  342. async_ctx *ctx = async_get_ctx();
  343. if (ctx == NULL || ctx->currjob == NULL) {
  344. /*
  345. * We're not in a job anyway so ignore this
  346. */
  347. return;
  348. }
  349. ctx->blocked++;
  350. }
  351. void ASYNC_unblock_pause(void)
  352. {
  353. async_ctx *ctx = async_get_ctx();
  354. if (ctx == NULL || ctx->currjob == NULL) {
  355. /*
  356. * We're not in a job anyway so ignore this
  357. */
  358. return;
  359. }
  360. if (ctx->blocked > 0)
  361. ctx->blocked--;
  362. }