2 * Written by Matt Caswell (matt@openssl.org) for the OpenSSL project.
4 /* ====================================================================
5 * Copyright (c) 2015 The OpenSSL Project. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
33 * 6. Redistributions of any form whatsoever must retain the following
35 * "This product includes software developed by the OpenSSL Project
36 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
54 * Without this we start getting longjmp crashes because it thinks we're jumping
55 * up the stack when in fact we are jumping to an entirely different stack. The
56 * cost of this is not having certain buffer overrun/underrun checks etc for
57 * this source file :-(
59 #undef _FORTIFY_SOURCE
61 /* This must be the first #include file */
62 #include "async_locl.h"
64 #include <internal/threads.h>
65 #include <openssl/err.h>
66 #include <internal/cryptlib_int.h>
69 #define ASYNC_JOB_RUNNING 0
70 #define ASYNC_JOB_PAUSING 1
71 #define ASYNC_JOB_PAUSED 2
72 #define ASYNC_JOB_STOPPING 3
74 static CRYPTO_THREAD_LOCAL ctxkey;
75 static CRYPTO_THREAD_LOCAL poolkey;
77 static void async_free_pool_internal(async_pool *pool);
79 static async_ctx *async_ctx_new(void)
81 async_ctx *nctx = NULL;
83 nctx = OPENSSL_malloc(sizeof (async_ctx));
85 ASYNCerr(ASYNC_F_ASYNC_CTX_NEW, ERR_R_MALLOC_FAILURE);
89 async_fibre_init_dispatcher(&nctx->dispatcher);
92 if (!CRYPTO_THREAD_set_local(&ctxkey, nctx))
102 async_ctx *async_get_ctx(void)
104 if (!OPENSSL_init_crypto(OPENSSL_INIT_ASYNC, NULL))
107 return (async_ctx *)CRYPTO_THREAD_get_local(&ctxkey);
110 static int async_ctx_free(void)
114 ctx = async_get_ctx();
116 if (!CRYPTO_THREAD_set_local(&ctxkey, NULL))
124 static ASYNC_JOB *async_job_new(void)
126 ASYNC_JOB *job = NULL;
128 job = OPENSSL_zalloc(sizeof (ASYNC_JOB));
130 ASYNCerr(ASYNC_F_ASYNC_JOB_NEW, ERR_R_MALLOC_FAILURE);
134 job->status = ASYNC_JOB_RUNNING;
139 static void async_job_free(ASYNC_JOB *job)
142 OPENSSL_free(job->funcargs);
143 async_fibre_free(&job->fibrectx);
148 static ASYNC_JOB *async_get_pool_job(void) {
152 pool = (async_pool *)CRYPTO_THREAD_get_local(&poolkey);
155 * Pool has not been initialised, so init with the defaults, i.e.
156 * no max size and no pre-created jobs
158 if (ASYNC_init_thread(0, 0) == 0)
160 pool = (async_pool *)CRYPTO_THREAD_get_local(&poolkey);
163 job = sk_ASYNC_JOB_pop(pool->jobs);
166 if ((pool->max_size != 0) && (pool->curr_size >= pool->max_size))
169 job = async_job_new();
171 if (! async_fibre_makecontext(&job->fibrectx)) {
181 static void async_release_job(ASYNC_JOB *job) {
184 pool = (async_pool *)CRYPTO_THREAD_get_local(&poolkey);
185 OPENSSL_free(job->funcargs);
186 job->funcargs = NULL;
187 sk_ASYNC_JOB_push(pool->jobs, job);
190 void async_start_func(void)
193 async_ctx *ctx = async_get_ctx();
198 job->ret = job->func(job->funcargs);
201 job->status = ASYNC_JOB_STOPPING;
202 if (!async_fibre_swapcontext(&job->fibrectx,
203 &ctx->dispatcher, 1)) {
205 * Should not happen. Getting here will close the thread...can't do
208 ASYNCerr(ASYNC_F_ASYNC_START_FUNC, ASYNC_R_FAILED_TO_SWAP_CONTEXT);
213 int ASYNC_start_job(ASYNC_JOB **job, ASYNC_WAIT_CTX *wctx, int *ret,
214 int (*func)(void *), void *args, size_t size)
216 async_ctx *ctx = async_get_ctx();
218 ctx = async_ctx_new();
228 if (ctx->currjob != NULL) {
229 if (ctx->currjob->status == ASYNC_JOB_STOPPING) {
230 *ret = ctx->currjob->ret;
231 ctx->currjob->waitctx = NULL;
232 async_release_job(ctx->currjob);
238 if (ctx->currjob->status == ASYNC_JOB_PAUSING) {
240 ctx->currjob->status = ASYNC_JOB_PAUSED;
245 if (ctx->currjob->status == ASYNC_JOB_PAUSED) {
247 /* Resume previous job */
248 if (!async_fibre_swapcontext(&ctx->dispatcher,
249 &ctx->currjob->fibrectx, 1)) {
250 ASYNCerr(ASYNC_F_ASYNC_START_JOB,
251 ASYNC_R_FAILED_TO_SWAP_CONTEXT);
257 /* Should not happen */
258 ASYNCerr(ASYNC_F_ASYNC_START_JOB, ERR_R_INTERNAL_ERROR);
259 async_release_job(ctx->currjob);
265 /* Start a new job */
266 if ((ctx->currjob = async_get_pool_job()) == NULL) {
267 return ASYNC_NO_JOBS;
271 ctx->currjob->funcargs = OPENSSL_malloc(size);
272 if (ctx->currjob->funcargs == NULL) {
273 ASYNCerr(ASYNC_F_ASYNC_START_JOB, ERR_R_MALLOC_FAILURE);
274 async_release_job(ctx->currjob);
278 memcpy(ctx->currjob->funcargs, args, size);
280 ctx->currjob->funcargs = NULL;
283 ctx->currjob->func = func;
284 ctx->currjob->waitctx = wctx;
285 if (!async_fibre_swapcontext(&ctx->dispatcher,
286 &ctx->currjob->fibrectx, 1)) {
287 ASYNCerr(ASYNC_F_ASYNC_START_JOB, ASYNC_R_FAILED_TO_SWAP_CONTEXT);
293 async_release_job(ctx->currjob);
299 int ASYNC_pause_job(void)
302 async_ctx *ctx = async_get_ctx();
305 || ctx->currjob == NULL
308 * Could be we've deliberately not been started within a job so this is
309 * counted as success.
315 job->status = ASYNC_JOB_PAUSING;
317 if (!async_fibre_swapcontext(&job->fibrectx,
318 &ctx->dispatcher, 1)) {
319 ASYNCerr(ASYNC_F_ASYNC_PAUSE_JOB, ASYNC_R_FAILED_TO_SWAP_CONTEXT);
322 /* Reset counts of added and deleted fds */
323 async_wait_ctx_reset_counts(job->waitctx);
328 static void async_empty_pool(async_pool *pool)
332 if (!pool || !pool->jobs)
336 job = sk_ASYNC_JOB_pop(pool->jobs);
343 if (!CRYPTO_THREAD_init_local(&ctxkey, NULL))
346 if (!CRYPTO_THREAD_init_local(&poolkey, NULL)) {
347 CRYPTO_THREAD_cleanup_local(&ctxkey);
354 void async_deinit(void)
356 CRYPTO_THREAD_cleanup_local(&ctxkey);
357 CRYPTO_THREAD_cleanup_local(&poolkey);
360 int ASYNC_init_thread(size_t max_size, size_t init_size)
363 size_t curr_size = 0;
365 if (init_size > max_size) {
366 ASYNCerr(ASYNC_F_ASYNC_INIT_THREAD, ASYNC_R_INVALID_POOL_SIZE);
370 if (!OPENSSL_init_crypto(OPENSSL_INIT_ASYNC, NULL)) {
373 if (!ossl_init_thread_start(OPENSSL_INIT_THREAD_ASYNC)) {
377 pool = OPENSSL_zalloc(sizeof *pool);
379 ASYNCerr(ASYNC_F_ASYNC_INIT_THREAD, ERR_R_MALLOC_FAILURE);
383 pool->jobs = sk_ASYNC_JOB_new_null();
384 if (pool->jobs == NULL) {
385 ASYNCerr(ASYNC_F_ASYNC_INIT_THREAD, ERR_R_MALLOC_FAILURE);
390 pool->max_size = max_size;
392 /* Pre-create jobs as required */
393 while (init_size--) {
395 job = async_job_new();
396 if (job == NULL || !async_fibre_makecontext(&job->fibrectx)) {
398 * Not actually fatal because we already created the pool, just
399 * skip creation of any more jobs
404 job->funcargs = NULL;
405 sk_ASYNC_JOB_push(pool->jobs, job);
408 pool->curr_size = curr_size;
409 if (!CRYPTO_THREAD_set_local(&poolkey, pool)) {
410 ASYNCerr(ASYNC_F_ASYNC_INIT_THREAD, ASYNC_R_FAILED_TO_SET_POOL);
416 async_free_pool_internal(pool);
420 static void async_free_pool_internal(async_pool *pool)
425 async_empty_pool(pool);
426 sk_ASYNC_JOB_free(pool->jobs);
428 CRYPTO_THREAD_set_local(&poolkey, NULL);
429 async_local_cleanup();
433 void ASYNC_cleanup_thread(void)
435 async_free_pool_internal((async_pool *)CRYPTO_THREAD_get_local(&poolkey));
438 ASYNC_JOB *ASYNC_get_current_job(void)
442 ctx = async_get_ctx();
449 ASYNC_WAIT_CTX *ASYNC_get_wait_ctx(ASYNC_JOB *job)
454 void ASYNC_block_pause(void)
456 async_ctx *ctx = async_get_ctx();
457 if (ctx == NULL || ctx->currjob == NULL) {
459 * We're not in a job anyway so ignore this
466 void ASYNC_unblock_pause(void)
468 async_ctx *ctx = async_get_ctx();
469 if (ctx == NULL || ctx->currjob == NULL) {
471 * We're not in a job anyway so ignore this