async.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /*
  2. * Written by Matt Caswell (matt@openssl.org) for the OpenSSL project.
  3. */
  4. /* ====================================================================
  5. * Copyright (c) 2015 The OpenSSL Project. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. All advertising materials mentioning features or use of this
  20. * software must display the following acknowledgment:
  21. * "This product includes software developed by the OpenSSL Project
  22. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  23. *
  24. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  25. * endorse or promote products derived from this software without
  26. * prior written permission. For written permission, please contact
  27. * licensing@OpenSSL.org.
  28. *
  29. * 5. Products derived from this software may not be called "OpenSSL"
  30. * nor may "OpenSSL" appear in their names without prior written
  31. * permission of the OpenSSL Project.
  32. *
  33. * 6. Redistributions of any form whatsoever must retain the following
  34. * acknowledgment:
  35. * "This product includes software developed by the OpenSSL Project
  36. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  37. *
  38. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  39. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  40. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  41. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  42. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  43. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  44. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  45. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  46. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  47. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  48. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  49. * OF THE POSSIBILITY OF SUCH DAMAGE.
  50. * ====================================================================
  51. */
  52. /*
  53. * Without this we start getting longjmp crashes because it thinks we're jumping
  54. * up the stack when in fact we are jumping to an entirely different stack. The
  55. * cost of this is not having certain buffer overrun/underrun checks etc for
  56. * this source file :-(
  57. */
  58. #undef _FORTIFY_SOURCE
  59. /* This must be the first #include file */
  60. #include "async_locl.h"
  61. #include <openssl/err.h>
  62. #include <string.h>
  63. #define ASYNC_JOB_RUNNING 0
  64. #define ASYNC_JOB_PAUSING 1
  65. #define ASYNC_JOB_PAUSED 2
  66. #define ASYNC_JOB_STOPPING 3
  67. static void async_free_pool_internal(async_pool *pool);
  68. static async_ctx *async_ctx_new(void)
  69. {
  70. async_ctx *nctx = NULL;
  71. nctx = OPENSSL_malloc(sizeof (async_ctx));
  72. if (nctx == NULL) {
  73. ASYNCerr(ASYNC_F_ASYNC_CTX_NEW, ERR_R_MALLOC_FAILURE);
  74. goto err;
  75. }
  76. async_fibre_init_dispatcher(&nctx->dispatcher);
  77. nctx->currjob = NULL;
  78. nctx->blocked = 0;
  79. if (!async_set_ctx(nctx))
  80. goto err;
  81. return nctx;
  82. err:
  83. OPENSSL_free(nctx);
  84. return NULL;
  85. }
  86. static int async_ctx_free(void)
  87. {
  88. async_ctx *ctx;
  89. ctx = async_get_ctx();
  90. if (!async_set_ctx(NULL))
  91. return 0;
  92. OPENSSL_free(ctx);
  93. return 1;
  94. }
  95. static ASYNC_JOB *async_job_new(void)
  96. {
  97. ASYNC_JOB *job = NULL;
  98. OSSL_ASYNC_FD pipefds[2];
  99. job = OPENSSL_malloc(sizeof (ASYNC_JOB));
  100. if (job == NULL) {
  101. ASYNCerr(ASYNC_F_ASYNC_JOB_NEW, ERR_R_MALLOC_FAILURE);
  102. return NULL;
  103. }
  104. if (!async_pipe(pipefds)) {
  105. OPENSSL_free(job);
  106. ASYNCerr(ASYNC_F_ASYNC_JOB_NEW, ASYNC_R_CANNOT_CREATE_WAIT_PIPE);
  107. return NULL;
  108. }
  109. job->wake_set = 0;
  110. job->wait_fd = pipefds[0];
  111. job->wake_fd = pipefds[1];
  112. job->status = ASYNC_JOB_RUNNING;
  113. job->funcargs = NULL;
  114. return job;
  115. }
  116. static void async_job_free(ASYNC_JOB *job)
  117. {
  118. if (job != NULL) {
  119. OPENSSL_free(job->funcargs);
  120. async_fibre_free(&job->fibrectx);
  121. async_close_fd(job->wait_fd);
  122. async_close_fd(job->wake_fd);
  123. OPENSSL_free(job);
  124. }
  125. }
  126. static ASYNC_JOB *async_get_pool_job(void) {
  127. ASYNC_JOB *job;
  128. async_pool *pool;
  129. pool = async_get_pool();
  130. if (pool == NULL) {
  131. /*
  132. * Pool has not been initialised, so init with the defaults, i.e.
  133. * no max size and no pre-created jobs
  134. */
  135. if (ASYNC_init_thread(0, 0) == 0)
  136. return NULL;
  137. pool = async_get_pool();
  138. }
  139. job = sk_ASYNC_JOB_pop(pool->jobs);
  140. if (job == NULL) {
  141. /* Pool is empty */
  142. if ((pool->max_size != 0) && (pool->curr_size >= pool->max_size))
  143. return NULL;
  144. job = async_job_new();
  145. if (job != NULL) {
  146. if (! async_fibre_makecontext(&job->fibrectx)) {
  147. async_job_free(job);
  148. return NULL;
  149. }
  150. pool->curr_size++;
  151. }
  152. }
  153. return job;
  154. }
  155. static void async_release_job(ASYNC_JOB *job) {
  156. async_pool *pool;
  157. pool = async_get_pool();
  158. OPENSSL_free(job->funcargs);
  159. job->funcargs = NULL;
  160. sk_ASYNC_JOB_push(pool->jobs, job);
  161. }
  162. void async_start_func(void)
  163. {
  164. ASYNC_JOB *job;
  165. while (1) {
  166. /* Run the job */
  167. job = async_get_ctx()->currjob;
  168. job->ret = job->func(job->funcargs);
  169. /* Stop the job */
  170. job->status = ASYNC_JOB_STOPPING;
  171. if (!async_fibre_swapcontext(&job->fibrectx,
  172. &async_get_ctx()->dispatcher, 1)) {
  173. /*
  174. * Should not happen. Getting here will close the thread...can't do
  175. * much about it
  176. */
  177. ASYNCerr(ASYNC_F_ASYNC_START_FUNC, ASYNC_R_FAILED_TO_SWAP_CONTEXT);
  178. }
  179. }
  180. }
  181. int ASYNC_start_job(ASYNC_JOB **job, int *ret, int (*func)(void *),
  182. void *args, size_t size)
  183. {
  184. if (async_get_ctx() == NULL && async_ctx_new() == NULL) {
  185. return ASYNC_ERR;
  186. }
  187. if (*job) {
  188. async_get_ctx()->currjob = *job;
  189. }
  190. for (;;) {
  191. if (async_get_ctx()->currjob != NULL) {
  192. if (async_get_ctx()->currjob->status == ASYNC_JOB_STOPPING) {
  193. *ret = async_get_ctx()->currjob->ret;
  194. async_release_job(async_get_ctx()->currjob);
  195. async_get_ctx()->currjob = NULL;
  196. *job = NULL;
  197. return ASYNC_FINISH;
  198. }
  199. if (async_get_ctx()->currjob->status == ASYNC_JOB_PAUSING) {
  200. *job = async_get_ctx()->currjob;
  201. async_get_ctx()->currjob->status = ASYNC_JOB_PAUSED;
  202. async_get_ctx()->currjob = NULL;
  203. return ASYNC_PAUSE;
  204. }
  205. if (async_get_ctx()->currjob->status == ASYNC_JOB_PAUSED) {
  206. async_get_ctx()->currjob = *job;
  207. /* Resume previous job */
  208. if (!async_fibre_swapcontext(&async_get_ctx()->dispatcher,
  209. &async_get_ctx()->currjob->fibrectx, 1)) {
  210. ASYNCerr(ASYNC_F_ASYNC_START_JOB,
  211. ASYNC_R_FAILED_TO_SWAP_CONTEXT);
  212. goto err;
  213. }
  214. continue;
  215. }
  216. /* Should not happen */
  217. ASYNCerr(ASYNC_F_ASYNC_START_JOB, ERR_R_INTERNAL_ERROR);
  218. async_release_job(async_get_ctx()->currjob);
  219. async_get_ctx()->currjob = NULL;
  220. *job = NULL;
  221. return ASYNC_ERR;
  222. }
  223. /* Start a new job */
  224. if ((async_get_ctx()->currjob = async_get_pool_job()) == NULL) {
  225. return ASYNC_NO_JOBS;
  226. }
  227. if (args != NULL) {
  228. async_get_ctx()->currjob->funcargs = OPENSSL_malloc(size);
  229. if (async_get_ctx()->currjob->funcargs == NULL) {
  230. ASYNCerr(ASYNC_F_ASYNC_START_JOB, ERR_R_MALLOC_FAILURE);
  231. async_release_job(async_get_ctx()->currjob);
  232. async_get_ctx()->currjob = NULL;
  233. return ASYNC_ERR;
  234. }
  235. memcpy(async_get_ctx()->currjob->funcargs, args, size);
  236. } else {
  237. async_get_ctx()->currjob->funcargs = NULL;
  238. }
  239. async_get_ctx()->currjob->func = func;
  240. if (!async_fibre_swapcontext(&async_get_ctx()->dispatcher,
  241. &async_get_ctx()->currjob->fibrectx, 1)) {
  242. ASYNCerr(ASYNC_F_ASYNC_START_JOB, ASYNC_R_FAILED_TO_SWAP_CONTEXT);
  243. goto err;
  244. }
  245. }
  246. err:
  247. async_release_job(async_get_ctx()->currjob);
  248. async_get_ctx()->currjob = NULL;
  249. *job = NULL;
  250. return ASYNC_ERR;
  251. }
  252. int ASYNC_pause_job(void)
  253. {
  254. ASYNC_JOB *job;
  255. if (async_get_ctx() == NULL
  256. || async_get_ctx()->currjob == NULL
  257. || async_get_ctx()->blocked) {
  258. /*
  259. * Could be we've deliberately not been started within a job so this is
  260. * counted as success.
  261. */
  262. return 1;
  263. }
  264. job = async_get_ctx()->currjob;
  265. job->status = ASYNC_JOB_PAUSING;
  266. if (!async_fibre_swapcontext(&job->fibrectx,
  267. &async_get_ctx()->dispatcher, 1)) {
  268. ASYNCerr(ASYNC_F_ASYNC_PAUSE_JOB, ASYNC_R_FAILED_TO_SWAP_CONTEXT);
  269. return 0;
  270. }
  271. return 1;
  272. }
  273. static void async_empty_pool(async_pool *pool)
  274. {
  275. ASYNC_JOB *job;
  276. if (!pool || !pool->jobs)
  277. return;
  278. do {
  279. job = sk_ASYNC_JOB_pop(pool->jobs);
  280. async_job_free(job);
  281. } while (job);
  282. }
  283. int ASYNC_init(int init_thread, size_t max_size, size_t init_size)
  284. {
  285. if (!async_global_init())
  286. return 0;
  287. if (init_thread)
  288. return ASYNC_init_thread(max_size, init_size);
  289. return 1;
  290. }
  291. int ASYNC_init_thread(size_t max_size, size_t init_size)
  292. {
  293. async_pool *pool;
  294. size_t curr_size = 0;
  295. if (init_size > max_size) {
  296. ASYNCerr(ASYNC_F_ASYNC_INIT_THREAD, ASYNC_R_INVALID_POOL_SIZE);
  297. return 0;
  298. }
  299. if (!async_local_init()) {
  300. ASYNCerr(ASYNC_F_ASYNC_INIT_THREAD, ASYNC_R_INIT_FAILED);
  301. return 0;
  302. }
  303. pool = OPENSSL_zalloc(sizeof *pool);
  304. if (pool == NULL) {
  305. ASYNCerr(ASYNC_F_ASYNC_INIT_THREAD, ERR_R_MALLOC_FAILURE);
  306. return 0;
  307. }
  308. pool->jobs = sk_ASYNC_JOB_new_null();
  309. if (pool->jobs == NULL) {
  310. ASYNCerr(ASYNC_F_ASYNC_INIT_THREAD, ERR_R_MALLOC_FAILURE);
  311. OPENSSL_free(pool);
  312. return 0;
  313. }
  314. pool->max_size = max_size;
  315. /* Pre-create jobs as required */
  316. while (init_size--) {
  317. ASYNC_JOB *job;
  318. job = async_job_new();
  319. if (job == NULL || !async_fibre_makecontext(&job->fibrectx)) {
  320. /*
  321. * Not actually fatal because we already created the pool, just
  322. * skip creation of any more jobs
  323. */
  324. async_job_free(job);
  325. break;
  326. }
  327. job->funcargs = NULL;
  328. sk_ASYNC_JOB_push(pool->jobs, job);
  329. curr_size++;
  330. }
  331. pool->curr_size = curr_size;
  332. if (!async_set_pool(pool)) {
  333. ASYNCerr(ASYNC_F_ASYNC_INIT_THREAD, ASYNC_R_FAILED_TO_SET_POOL);
  334. goto err;
  335. }
  336. return 1;
  337. err:
  338. async_free_pool_internal(pool);
  339. return 0;
  340. }
  341. static void async_free_pool_internal(async_pool *pool)
  342. {
  343. if (pool == NULL)
  344. return;
  345. async_empty_pool(pool);
  346. sk_ASYNC_JOB_free(pool->jobs);
  347. OPENSSL_free(pool);
  348. (void)async_set_pool(NULL);
  349. async_local_cleanup();
  350. async_ctx_free();
  351. }
  352. void ASYNC_cleanup_thread(void)
  353. {
  354. async_free_pool_internal(async_get_pool());
  355. }
  356. void ASYNC_cleanup(int cleanupthread)
  357. {
  358. /*
  359. * We don't actually have any global cleanup at the moment so just cleanup
  360. * the thread
  361. */
  362. if (cleanupthread)
  363. ASYNC_cleanup_thread();
  364. }
  365. ASYNC_JOB *ASYNC_get_current_job(void)
  366. {
  367. async_ctx *ctx;
  368. ctx = async_get_ctx();
  369. if(ctx == NULL)
  370. return NULL;
  371. return ctx->currjob;
  372. }
  373. OSSL_ASYNC_FD ASYNC_get_wait_fd(ASYNC_JOB *job)
  374. {
  375. return job->wait_fd;
  376. }
  377. void ASYNC_wake(ASYNC_JOB *job)
  378. {
  379. char dummy = 0;
  380. if (job->wake_set)
  381. return;
  382. async_write1(job->wake_fd, &dummy);
  383. job->wake_set = 1;
  384. }
  385. void ASYNC_clear_wake(ASYNC_JOB *job)
  386. {
  387. char dummy = 0;
  388. if (!job->wake_set)
  389. return;
  390. async_read1(job->wait_fd, &dummy);
  391. job->wake_set = 0;
  392. }
  393. void ASYNC_block_pause(void)
  394. {
  395. if (async_get_ctx() == NULL
  396. || async_get_ctx()->currjob == NULL) {
  397. /*
  398. * We're not in a job anyway so ignore this
  399. */
  400. return;
  401. }
  402. async_get_ctx()->blocked++;
  403. }
  404. void ASYNC_unblock_pause(void)
  405. {
  406. if (async_get_ctx() == NULL
  407. || async_get_ctx()->currjob == NULL) {
  408. /*
  409. * We're not in a job anyway so ignore this
  410. */
  411. return;
  412. }
  413. if(async_get_ctx()->blocked > 0)
  414. async_get_ctx()->blocked--;
  415. }