2 * Copyright 2015-2022 The OpenSSL Project Authors. All Rights Reserved.
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
11 * Without this we start getting longjmp crashes because it thinks we're jumping
12 * up the stack when in fact we are jumping to an entirely different stack. The
13 * cost of this is not having certain buffer overrun/underrun checks etc for
14 * this source file :-(
16 #undef _FORTIFY_SOURCE
18 /* This must be the first #include file */
19 #include "async_local.h"
20 #include "internal/threads_common.h"
22 #include <openssl/err.h>
23 #include "crypto/cryptlib.h"
26 #define ASYNC_JOB_RUNNING 0
27 #define ASYNC_JOB_PAUSING 1
28 #define ASYNC_JOB_PAUSED 2
29 #define ASYNC_JOB_STOPPING 3
31 static void async_delete_thread_state(void *arg
);
33 static async_ctx
*async_ctx_new(void)
37 if (!ossl_init_thread_start(NULL
, NULL
, async_delete_thread_state
))
40 nctx
= OPENSSL_malloc(sizeof(*nctx
));
44 async_fibre_init_dispatcher(&nctx
->dispatcher
);
47 if (!CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_ASYNC_CTX_KEY
,
48 CRYPTO_THREAD_NO_CONTEXT
, nctx
))
58 async_ctx
*async_get_ctx(void)
60 return (async_ctx
*)CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_ASYNC_CTX_KEY
,
61 CRYPTO_THREAD_NO_CONTEXT
);
64 static int async_ctx_free(void)
68 ctx
= async_get_ctx();
70 if (!CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_ASYNC_CTX_KEY
,
71 CRYPTO_THREAD_NO_CONTEXT
, NULL
))
79 static ASYNC_JOB
*async_job_new(void)
81 ASYNC_JOB
*job
= NULL
;
83 job
= OPENSSL_zalloc(sizeof(*job
));
87 job
->status
= ASYNC_JOB_RUNNING
;
92 static void async_job_free(ASYNC_JOB
*job
)
95 OPENSSL_free(job
->funcargs
);
96 async_fibre_free(&job
->fibrectx
);
101 static ASYNC_JOB
*async_get_pool_job(void) {
105 pool
= (async_pool
*)CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_ASYNC_POOL_KEY
,
106 CRYPTO_THREAD_NO_CONTEXT
);
109 * Pool has not been initialised, so init with the defaults, i.e.
110 * no max size and no pre-created jobs
112 if (ASYNC_init_thread(0, 0) == 0)
114 pool
= (async_pool
*)CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_ASYNC_POOL_KEY
,
115 CRYPTO_THREAD_NO_CONTEXT
);
118 job
= sk_ASYNC_JOB_pop(pool
->jobs
);
121 if ((pool
->max_size
!= 0) && (pool
->curr_size
>= pool
->max_size
))
124 job
= async_job_new();
126 if (! async_fibre_makecontext(&job
->fibrectx
)) {
136 static void async_release_job(ASYNC_JOB
*job
) {
139 pool
= (async_pool
*)CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_ASYNC_POOL_KEY
,
140 CRYPTO_THREAD_NO_CONTEXT
);
142 ERR_raise(ERR_LIB_ASYNC
, ERR_R_INTERNAL_ERROR
);
145 OPENSSL_free(job
->funcargs
);
146 job
->funcargs
= NULL
;
147 sk_ASYNC_JOB_push(pool
->jobs
, job
);
150 void async_start_func(void)
153 async_ctx
*ctx
= async_get_ctx();
156 ERR_raise(ERR_LIB_ASYNC
, ERR_R_INTERNAL_ERROR
);
162 job
->ret
= job
->func(job
->funcargs
);
165 job
->status
= ASYNC_JOB_STOPPING
;
166 if (!async_fibre_swapcontext(&job
->fibrectx
,
167 &ctx
->dispatcher
, 1)) {
169 * Should not happen. Getting here will close the thread...can't do
172 ERR_raise(ERR_LIB_ASYNC
, ASYNC_R_FAILED_TO_SWAP_CONTEXT
);
177 int ASYNC_start_job(ASYNC_JOB
**job
, ASYNC_WAIT_CTX
*wctx
, int *ret
,
178 int (*func
)(void *), void *args
, size_t size
)
181 OSSL_LIB_CTX
*libctx
;
183 if (!OPENSSL_init_crypto(OPENSSL_INIT_ASYNC
, NULL
))
186 ctx
= async_get_ctx();
188 ctx
= async_ctx_new();
196 if (ctx
->currjob
!= NULL
) {
197 if (ctx
->currjob
->status
== ASYNC_JOB_STOPPING
) {
198 *ret
= ctx
->currjob
->ret
;
199 ctx
->currjob
->waitctx
= NULL
;
200 async_release_job(ctx
->currjob
);
206 if (ctx
->currjob
->status
== ASYNC_JOB_PAUSING
) {
208 ctx
->currjob
->status
= ASYNC_JOB_PAUSED
;
213 if (ctx
->currjob
->status
== ASYNC_JOB_PAUSED
) {
219 * Restore the default libctx to what it was the last time the
222 libctx
= OSSL_LIB_CTX_set0_default(ctx
->currjob
->libctx
);
223 if (libctx
== NULL
) {
224 /* Failed to set the default context */
225 ERR_raise(ERR_LIB_ASYNC
, ERR_R_INTERNAL_ERROR
);
228 /* Resume previous job */
229 if (!async_fibre_swapcontext(&ctx
->dispatcher
,
230 &ctx
->currjob
->fibrectx
, 1)) {
231 ctx
->currjob
->libctx
= OSSL_LIB_CTX_set0_default(libctx
);
232 ERR_raise(ERR_LIB_ASYNC
, ASYNC_R_FAILED_TO_SWAP_CONTEXT
);
236 * In case the fibre changed the default libctx we set it back
237 * again to what it was originally, and remember what it had
240 ctx
->currjob
->libctx
= OSSL_LIB_CTX_set0_default(libctx
);
244 /* Should not happen */
245 ERR_raise(ERR_LIB_ASYNC
, ERR_R_INTERNAL_ERROR
);
246 async_release_job(ctx
->currjob
);
252 /* Start a new job */
253 if ((ctx
->currjob
= async_get_pool_job()) == NULL
)
254 return ASYNC_NO_JOBS
;
257 ctx
->currjob
->funcargs
= OPENSSL_malloc(size
);
258 if (ctx
->currjob
->funcargs
== NULL
) {
259 async_release_job(ctx
->currjob
);
263 memcpy(ctx
->currjob
->funcargs
, args
, size
);
265 ctx
->currjob
->funcargs
= NULL
;
268 ctx
->currjob
->func
= func
;
269 ctx
->currjob
->waitctx
= wctx
;
270 libctx
= ossl_lib_ctx_get_concrete(NULL
);
271 if (!async_fibre_swapcontext(&ctx
->dispatcher
,
272 &ctx
->currjob
->fibrectx
, 1)) {
273 ERR_raise(ERR_LIB_ASYNC
, ASYNC_R_FAILED_TO_SWAP_CONTEXT
);
277 * In case the fibre changed the default libctx we set it back again
278 * to what it was, and remember what it had been changed to.
280 ctx
->currjob
->libctx
= OSSL_LIB_CTX_set0_default(libctx
);
284 async_release_job(ctx
->currjob
);
290 int ASYNC_pause_job(void)
293 async_ctx
*ctx
= async_get_ctx();
296 || ctx
->currjob
== NULL
299 * Could be we've deliberately not been started within a job so this is
300 * counted as success.
306 job
->status
= ASYNC_JOB_PAUSING
;
308 if (!async_fibre_swapcontext(&job
->fibrectx
,
309 &ctx
->dispatcher
, 1)) {
310 ERR_raise(ERR_LIB_ASYNC
, ASYNC_R_FAILED_TO_SWAP_CONTEXT
);
313 /* Reset counts of added and deleted fds */
314 async_wait_ctx_reset_counts(job
->waitctx
);
319 static void async_empty_pool(async_pool
*pool
)
323 if (pool
== NULL
|| pool
->jobs
== NULL
)
327 job
= sk_ASYNC_JOB_pop(pool
->jobs
);
334 return async_local_init();
337 void async_deinit(void)
339 async_local_deinit();
342 int ASYNC_init_thread(size_t max_size
, size_t init_size
)
345 size_t curr_size
= 0;
347 if (init_size
> max_size
|| max_size
> INT_MAX
) {
348 ERR_raise(ERR_LIB_ASYNC
, ASYNC_R_INVALID_POOL_SIZE
);
352 if (!OPENSSL_init_crypto(OPENSSL_INIT_ASYNC
, NULL
))
355 if (!ossl_init_thread_start(NULL
, NULL
, async_delete_thread_state
))
358 pool
= OPENSSL_zalloc(sizeof(*pool
));
362 pool
->jobs
= sk_ASYNC_JOB_new_reserve(NULL
, (int)init_size
);
363 if (pool
->jobs
== NULL
) {
364 ERR_raise(ERR_LIB_ASYNC
, ERR_R_CRYPTO_LIB
);
369 pool
->max_size
= max_size
;
371 /* Pre-create jobs as required */
372 while (init_size
--) {
374 job
= async_job_new();
375 if (job
== NULL
|| !async_fibre_makecontext(&job
->fibrectx
)) {
377 * Not actually fatal because we already created the pool, just
378 * skip creation of any more jobs
383 job
->funcargs
= NULL
;
384 sk_ASYNC_JOB_push(pool
->jobs
, job
); /* Cannot fail due to reserve */
387 pool
->curr_size
= curr_size
;
388 if (!CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_ASYNC_POOL_KEY
,
389 CRYPTO_THREAD_NO_CONTEXT
, pool
)) {
390 ERR_raise(ERR_LIB_ASYNC
, ASYNC_R_FAILED_TO_SET_POOL
);
396 async_empty_pool(pool
);
397 sk_ASYNC_JOB_free(pool
->jobs
);
402 static void async_delete_thread_state(void *arg
)
404 async_pool
*pool
= (async_pool
*)CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_ASYNC_POOL_KEY
,
405 CRYPTO_THREAD_NO_CONTEXT
);
408 async_empty_pool(pool
);
409 sk_ASYNC_JOB_free(pool
->jobs
);
411 CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_ASYNC_POOL_KEY
,
412 CRYPTO_THREAD_NO_CONTEXT
, NULL
);
414 async_local_cleanup();
418 void ASYNC_cleanup_thread(void)
420 if (!OPENSSL_init_crypto(OPENSSL_INIT_ASYNC
, NULL
))
423 async_delete_thread_state(NULL
);
426 ASYNC_JOB
*ASYNC_get_current_job(void)
430 if (!OPENSSL_init_crypto(OPENSSL_INIT_ASYNC
, NULL
))
433 ctx
= async_get_ctx();
440 ASYNC_WAIT_CTX
*ASYNC_get_wait_ctx(ASYNC_JOB
*job
)
445 void ASYNC_block_pause(void)
449 if (!OPENSSL_init_crypto(OPENSSL_INIT_ASYNC
, NULL
))
452 ctx
= async_get_ctx();
453 if (ctx
== NULL
|| ctx
->currjob
== NULL
) {
455 * We're not in a job anyway so ignore this
462 void ASYNC_unblock_pause(void)
466 if (!OPENSSL_init_crypto(OPENSSL_INIT_ASYNC
, NULL
))
469 ctx
= async_get_ctx();
470 if (ctx
== NULL
|| ctx
->currjob
== NULL
) {
472 * We're not in a job anyway so ignore this
476 if (ctx
->blocked
> 0)