]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/async/async.c
Fix some conversion from size_t to const int errors
[thirdparty/openssl.git] / crypto / async / async.c
CommitLineData
a3667c31 1/*
fecb3aae 2 * Copyright 2015-2022 The OpenSSL Project Authors. All Rights Reserved.
a3667c31 3 *
f3a95349 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
62867571
RS
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
a3667c31
MC
8 */
9
4abc7681
MC
10/*
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 :-(
15 */
16#undef _FORTIFY_SOURCE
17
6e8ac508 18/* This must be the first #include file */
706457b7 19#include "async_local.h"
21980b98 20#include "internal/threads_common.h"
6e8ac508 21
079a1a90 22#include <openssl/err.h>
25f2138b 23#include "crypto/cryptlib.h"
a3667c31
MC
24#include <string.h>
25
26#define ASYNC_JOB_RUNNING 0
27#define ASYNC_JOB_PAUSING 1
28#define ASYNC_JOB_PAUSED 2
29#define ASYNC_JOB_STOPPING 3
30
da747958 31static void async_delete_thread_state(void *arg);
242f84d0 32
636ca4ff 33static async_ctx *async_ctx_new(void)
a3667c31 34{
74a8acbd
BE
35 async_ctx *nctx;
36
6913f5fe 37 if (!ossl_init_thread_start(NULL, NULL, async_delete_thread_state))
74a8acbd 38 return NULL;
a3667c31 39
cbe29648 40 nctx = OPENSSL_malloc(sizeof(*nctx));
e077455e 41 if (nctx == NULL)
a3667c31 42 goto err;
a3667c31 43
636ca4ff 44 async_fibre_init_dispatcher(&nctx->dispatcher);
a3667c31 45 nctx->currjob = NULL;
e8dfb5bf 46 nctx->blocked = 0;
24f0715e
NH
47 if (!CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_ASYNC_CTX_KEY,
48 CRYPTO_THREAD_NO_CONTEXT, nctx))
9ec1e031 49 goto err;
a3667c31
MC
50
51 return nctx;
52err:
e38565f5 53 OPENSSL_free(nctx);
a3667c31
MC
54
55 return NULL;
56}
57
224905f8 58async_ctx *async_get_ctx(void)
7b9f8f7f 59{
24f0715e
NH
60 return (async_ctx *)CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_ASYNC_CTX_KEY,
61 CRYPTO_THREAD_NO_CONTEXT);
7b9f8f7f
MC
62}
63
636ca4ff 64static int async_ctx_free(void)
a3667c31 65{
e38565f5 66 async_ctx *ctx;
a3667c31 67
e38565f5
MC
68 ctx = async_get_ctx();
69
24f0715e
NH
70 if (!CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_ASYNC_CTX_KEY,
71 CRYPTO_THREAD_NO_CONTEXT, NULL))
9ec1e031 72 return 0;
a3667c31 73
e38565f5
MC
74 OPENSSL_free(ctx);
75
a3667c31
MC
76 return 1;
77}
78
636ca4ff 79static ASYNC_JOB *async_job_new(void)
a3667c31
MC
80{
81 ASYNC_JOB *job = NULL;
a3667c31 82
cbe29648 83 job = OPENSSL_zalloc(sizeof(*job));
e077455e 84 if (job == NULL)
50108304 85 return NULL;
a3667c31 86
a3667c31 87 job->status = ASYNC_JOB_RUNNING;
a3667c31
MC
88
89 return job;
a3667c31
MC
90}
91
636ca4ff 92static void async_job_free(ASYNC_JOB *job)
a3667c31 93{
e38565f5
MC
94 if (job != NULL) {
95 OPENSSL_free(job->funcargs);
636ca4ff 96 async_fibre_free(&job->fibrectx);
a3667c31
MC
97 OPENSSL_free(job);
98 }
99}
100
252d6d3a 101static ASYNC_JOB *async_get_pool_job(void) {
a3667c31 102 ASYNC_JOB *job;
27949c35 103 async_pool *pool;
a3667c31 104
24f0715e
NH
105 pool = (async_pool *)CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_ASYNC_POOL_KEY,
106 CRYPTO_THREAD_NO_CONTEXT);
252d6d3a 107 if (pool == NULL) {
50108304 108 /*
252d6d3a 109 * Pool has not been initialised, so init with the defaults, i.e.
9f078e19 110 * no max size and no pre-created jobs
50108304 111 */
68487a9b 112 if (ASYNC_init_thread(0, 0) == 0)
252d6d3a 113 return NULL;
24f0715e
NH
114 pool = (async_pool *)CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_ASYNC_POOL_KEY,
115 CRYPTO_THREAD_NO_CONTEXT);
252d6d3a
MC
116 }
117
27949c35 118 job = sk_ASYNC_JOB_pop(pool->jobs);
252d6d3a
MC
119 if (job == NULL) {
120 /* Pool is empty */
27949c35 121 if ((pool->max_size != 0) && (pool->curr_size >= pool->max_size))
252d6d3a 122 return NULL;
0ff2b9ac 123
636ca4ff 124 job = async_job_new();
6e8ac508
VD
125 if (job != NULL) {
126 if (! async_fibre_makecontext(&job->fibrectx)) {
127 async_job_free(job);
128 return NULL;
129 }
27949c35 130 pool->curr_size++;
252d6d3a
MC
131 }
132 }
133 return job;
134}
135
136static void async_release_job(ASYNC_JOB *job) {
27949c35
MC
137 async_pool *pool;
138
24f0715e
NH
139 pool = (async_pool *)CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_ASYNC_POOL_KEY,
140 CRYPTO_THREAD_NO_CONTEXT);
ed5b26ce
P
141 if (pool == NULL) {
142 ERR_raise(ERR_LIB_ASYNC, ERR_R_INTERNAL_ERROR);
143 return;
144 }
e38565f5 145 OPENSSL_free(job->funcargs);
252d6d3a 146 job->funcargs = NULL;
27949c35 147 sk_ASYNC_JOB_push(pool->jobs, job);
252d6d3a
MC
148}
149
636ca4ff 150void async_start_func(void)
252d6d3a
MC
151{
152 ASYNC_JOB *job;
7b9f8f7f 153 async_ctx *ctx = async_get_ctx();
252d6d3a 154
ed5b26ce
P
155 if (ctx == NULL) {
156 ERR_raise(ERR_LIB_ASYNC, ERR_R_INTERNAL_ERROR);
157 return;
158 }
252d6d3a
MC
159 while (1) {
160 /* Run the job */
7b9f8f7f 161 job = ctx->currjob;
252d6d3a
MC
162 job->ret = job->func(job->funcargs);
163
164 /* Stop the job */
165 job->status = ASYNC_JOB_STOPPING;
e38565f5 166 if (!async_fibre_swapcontext(&job->fibrectx,
7b9f8f7f 167 &ctx->dispatcher, 1)) {
252d6d3a 168 /*
079a1a90
MC
169 * Should not happen. Getting here will close the thread...can't do
170 * much about it
252d6d3a 171 */
9311d0c4 172 ERR_raise(ERR_LIB_ASYNC, ASYNC_R_FAILED_TO_SWAP_CONTEXT);
252d6d3a 173 }
50108304 174 }
a3667c31
MC
175}
176
ff75a257
MC
177int ASYNC_start_job(ASYNC_JOB **job, ASYNC_WAIT_CTX *wctx, int *ret,
178 int (*func)(void *), void *args, size_t size)
a3667c31 179{
74a8acbd 180 async_ctx *ctx;
b4250010 181 OSSL_LIB_CTX *libctx;
74a8acbd
BE
182
183 if (!OPENSSL_init_crypto(OPENSSL_INIT_ASYNC, NULL))
184 return ASYNC_ERR;
185
186 ctx = async_get_ctx();
7b9f8f7f
MC
187 if (ctx == NULL)
188 ctx = async_ctx_new();
74a8acbd 189 if (ctx == NULL)
a3667c31 190 return ASYNC_ERR;
a3667c31 191
b6f0f546 192 if (*job != NULL)
7b9f8f7f 193 ctx->currjob = *job;
a3667c31 194
50108304 195 for (;;) {
7b9f8f7f
MC
196 if (ctx->currjob != NULL) {
197 if (ctx->currjob->status == ASYNC_JOB_STOPPING) {
198 *ret = ctx->currjob->ret;
ff75a257 199 ctx->currjob->waitctx = NULL;
7b9f8f7f
MC
200 async_release_job(ctx->currjob);
201 ctx->currjob = NULL;
82676094 202 *job = NULL;
50108304
MC
203 return ASYNC_FINISH;
204 }
205
7b9f8f7f
MC
206 if (ctx->currjob->status == ASYNC_JOB_PAUSING) {
207 *job = ctx->currjob;
208 ctx->currjob->status = ASYNC_JOB_PAUSED;
209 ctx->currjob = NULL;
50108304
MC
210 return ASYNC_PAUSE;
211 }
212
7b9f8f7f 213 if (ctx->currjob->status == ASYNC_JOB_PAUSED) {
b6f0f546
P
214 if (*job == NULL)
215 return ASYNC_ERR;
7b9f8f7f 216 ctx->currjob = *job;
b6f0f546 217
6c689e58
MC
218 /*
219 * Restore the default libctx to what it was the last time the
220 * fibre ran
221 */
b4250010 222 libctx = OSSL_LIB_CTX_set0_default(ctx->currjob->libctx);
03cd9d2f
TM
223 if (libctx == NULL) {
224 /* Failed to set the default context */
225 ERR_raise(ERR_LIB_ASYNC, ERR_R_INTERNAL_ERROR);
226 goto err;
227 }
50108304 228 /* Resume previous job */
7b9f8f7f
MC
229 if (!async_fibre_swapcontext(&ctx->dispatcher,
230 &ctx->currjob->fibrectx, 1)) {
03cd9d2f 231 ctx->currjob->libctx = OSSL_LIB_CTX_set0_default(libctx);
9311d0c4 232 ERR_raise(ERR_LIB_ASYNC, ASYNC_R_FAILED_TO_SWAP_CONTEXT);
50108304 233 goto err;
079a1a90 234 }
6c689e58
MC
235 /*
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
238 * been changed to.
239 */
b4250010 240 ctx->currjob->libctx = OSSL_LIB_CTX_set0_default(libctx);
50108304
MC
241 continue;
242 }
243
244 /* Should not happen */
9311d0c4 245 ERR_raise(ERR_LIB_ASYNC, ERR_R_INTERNAL_ERROR);
7b9f8f7f
MC
246 async_release_job(ctx->currjob);
247 ctx->currjob = NULL;
82676094 248 *job = NULL;
50108304 249 return ASYNC_ERR;
a3667c31
MC
250 }
251
50108304 252 /* Start a new job */
74a8acbd 253 if ((ctx->currjob = async_get_pool_job()) == NULL)
252d6d3a 254 return ASYNC_NO_JOBS;
a3667c31 255
e38565f5 256 if (args != NULL) {
7b9f8f7f
MC
257 ctx->currjob->funcargs = OPENSSL_malloc(size);
258 if (ctx->currjob->funcargs == NULL) {
7b9f8f7f
MC
259 async_release_job(ctx->currjob);
260 ctx->currjob = NULL;
50108304
MC
261 return ASYNC_ERR;
262 }
7b9f8f7f 263 memcpy(ctx->currjob->funcargs, args, size);
50108304 264 } else {
7b9f8f7f 265 ctx->currjob->funcargs = NULL;
a3667c31
MC
266 }
267
7b9f8f7f 268 ctx->currjob->func = func;
ff75a257 269 ctx->currjob->waitctx = wctx;
b4250010 270 libctx = ossl_lib_ctx_get_concrete(NULL);
7b9f8f7f
MC
271 if (!async_fibre_swapcontext(&ctx->dispatcher,
272 &ctx->currjob->fibrectx, 1)) {
9311d0c4 273 ERR_raise(ERR_LIB_ASYNC, ASYNC_R_FAILED_TO_SWAP_CONTEXT);
50108304 274 goto err;
079a1a90 275 }
6c689e58
MC
276 /*
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.
279 */
b4250010 280 ctx->currjob->libctx = OSSL_LIB_CTX_set0_default(libctx);
a3667c31
MC
281 }
282
50108304 283err:
7b9f8f7f
MC
284 async_release_job(ctx->currjob);
285 ctx->currjob = NULL;
82676094 286 *job = NULL;
a3667c31
MC
287 return ASYNC_ERR;
288}
289
a3667c31
MC
290int ASYNC_pause_job(void)
291{
292 ASYNC_JOB *job;
7b9f8f7f 293 async_ctx *ctx = async_get_ctx();
a3667c31 294
7b9f8f7f
MC
295 if (ctx == NULL
296 || ctx->currjob == NULL
297 || ctx->blocked) {
079a1a90 298 /*
05a6347f
MC
299 * Could be we've deliberately not been started within a job so this is
300 * counted as success.
079a1a90 301 */
05a6347f 302 return 1;
079a1a90 303 }
a3667c31 304
7b9f8f7f 305 job = ctx->currjob;
a3667c31
MC
306 job->status = ASYNC_JOB_PAUSING;
307
e8dfb5bf 308 if (!async_fibre_swapcontext(&job->fibrectx,
7b9f8f7f 309 &ctx->dispatcher, 1)) {
9311d0c4 310 ERR_raise(ERR_LIB_ASYNC, ASYNC_R_FAILED_TO_SWAP_CONTEXT);
a3667c31
MC
311 return 0;
312 }
ff75a257
MC
313 /* Reset counts of added and deleted fds */
314 async_wait_ctx_reset_counts(job->waitctx);
a3667c31
MC
315
316 return 1;
317}
318
27949c35 319static void async_empty_pool(async_pool *pool)
d63de0eb
MC
320{
321 ASYNC_JOB *job;
322
12a765a5 323 if (pool == NULL || pool->jobs == NULL)
27949c35
MC
324 return;
325
d63de0eb 326 do {
27949c35 327 job = sk_ASYNC_JOB_pop(pool->jobs);
636ca4ff 328 async_job_free(job);
d63de0eb
MC
329 } while (job);
330}
331
7b9f8f7f 332int async_init(void)
68487a9b 333{
43ed2429 334 return async_local_init();
68487a9b
MC
335}
336
224905f8
MC
337void async_deinit(void)
338{
43ed2429 339 async_local_deinit();
224905f8
MC
340}
341
68487a9b 342int ASYNC_init_thread(size_t max_size, size_t init_size)
252d6d3a 343{
27949c35 344 async_pool *pool;
0ff2b9ac
MC
345 size_t curr_size = 0;
346
bb86c43f 347 if (init_size > max_size || max_size > INT_MAX) {
9311d0c4 348 ERR_raise(ERR_LIB_ASYNC, ASYNC_R_INVALID_POOL_SIZE);
27949c35
MC
349 return 0;
350 }
351
74a8acbd 352 if (!OPENSSL_init_crypto(OPENSSL_INIT_ASYNC, NULL))
0fc32b07 353 return 0;
74a8acbd 354
6913f5fe 355 if (!ossl_init_thread_start(NULL, NULL, async_delete_thread_state))
22a34c2f 356 return 0;
7b9f8f7f 357
cbe29648 358 pool = OPENSSL_zalloc(sizeof(*pool));
e077455e 359 if (pool == NULL)
252d6d3a 360 return 0;
27949c35 361
bb86c43f 362 pool->jobs = sk_ASYNC_JOB_new_reserve(NULL, (int)init_size);
27949c35 363 if (pool->jobs == NULL) {
e077455e 364 ERR_raise(ERR_LIB_ASYNC, ERR_R_CRYPTO_LIB);
27949c35
MC
365 OPENSSL_free(pool);
366 return 0;
367 }
368
369 pool->max_size = max_size;
370
252d6d3a 371 /* Pre-create jobs as required */
6e8ac508 372 while (init_size--) {
252d6d3a 373 ASYNC_JOB *job;
636ca4ff 374 job = async_job_new();
6e8ac508 375 if (job == NULL || !async_fibre_makecontext(&job->fibrectx)) {
252d6d3a 376 /*
6e8ac508
VD
377 * Not actually fatal because we already created the pool, just
378 * skip creation of any more jobs
252d6d3a 379 */
6e8ac508
VD
380 async_job_free(job);
381 break;
252d6d3a 382 }
6e8ac508 383 job->funcargs = NULL;
e431363f 384 sk_ASYNC_JOB_push(pool->jobs, job); /* Cannot fail due to reserve */
6e8ac508 385 curr_size++;
252d6d3a 386 }
27949c35 387 pool->curr_size = curr_size;
24f0715e
NH
388 if (!CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_ASYNC_POOL_KEY,
389 CRYPTO_THREAD_NO_CONTEXT, pool)) {
9311d0c4 390 ERR_raise(ERR_LIB_ASYNC, ASYNC_R_FAILED_TO_SET_POOL);
27949c35 391 goto err;
d63de0eb 392 }
0ff2b9ac 393
252d6d3a 394 return 1;
27949c35 395err:
74a8acbd
BE
396 async_empty_pool(pool);
397 sk_ASYNC_JOB_free(pool->jobs);
398 OPENSSL_free(pool);
27949c35 399 return 0;
252d6d3a
MC
400}
401
da747958 402static void async_delete_thread_state(void *arg)
252d6d3a 403{
21980b98 404 async_pool *pool = (async_pool *)CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_ASYNC_POOL_KEY,
24f0715e 405 CRYPTO_THREAD_NO_CONTEXT);
d63de0eb 406
74a8acbd
BE
407 if (pool != NULL) {
408 async_empty_pool(pool);
409 sk_ASYNC_JOB_free(pool->jobs);
410 OPENSSL_free(pool);
24f0715e
NH
411 CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_ASYNC_POOL_KEY,
412 CRYPTO_THREAD_NO_CONTEXT, NULL);
74a8acbd 413 }
22a34c2f 414 async_local_cleanup();
636ca4ff 415 async_ctx_free();
252d6d3a 416}
f4da39d2 417
68487a9b 418void ASYNC_cleanup_thread(void)
27949c35 419{
74a8acbd
BE
420 if (!OPENSSL_init_crypto(OPENSSL_INIT_ASYNC, NULL))
421 return;
422
242f84d0 423 async_delete_thread_state(NULL);
27949c35
MC
424}
425
f4da39d2
MC
426ASYNC_JOB *ASYNC_get_current_job(void)
427{
636ca4ff 428 async_ctx *ctx;
e38565f5 429
74a8acbd
BE
430 if (!OPENSSL_init_crypto(OPENSSL_INIT_ASYNC, NULL))
431 return NULL;
432
e38565f5 433 ctx = async_get_ctx();
e8aa8b6c 434 if (ctx == NULL)
f4da39d2
MC
435 return NULL;
436
437 return ctx->currjob;
438}
439
ff75a257 440ASYNC_WAIT_CTX *ASYNC_get_wait_ctx(ASYNC_JOB *job)
f4da39d2 441{
ff75a257 442 return job->waitctx;
f4da39d2 443}
e8dfb5bf
MC
444
445void ASYNC_block_pause(void)
446{
74a8acbd
BE
447 async_ctx *ctx;
448
449 if (!OPENSSL_init_crypto(OPENSSL_INIT_ASYNC, NULL))
450 return;
451
452 ctx = async_get_ctx();
7b9f8f7f 453 if (ctx == NULL || ctx->currjob == NULL) {
e8dfb5bf
MC
454 /*
455 * We're not in a job anyway so ignore this
456 */
457 return;
458 }
7b9f8f7f 459 ctx->blocked++;
e8dfb5bf
MC
460}
461
462void ASYNC_unblock_pause(void)
463{
74a8acbd
BE
464 async_ctx *ctx;
465
466 if (!OPENSSL_init_crypto(OPENSSL_INIT_ASYNC, NULL))
467 return;
468
469 ctx = async_get_ctx();
7b9f8f7f 470 if (ctx == NULL || ctx->currjob == NULL) {
e8dfb5bf
MC
471 /*
472 * We're not in a job anyway so ignore this
473 */
474 return;
475 }
e8aa8b6c 476 if (ctx->blocked > 0)
7b9f8f7f 477 ctx->blocked--;
e8dfb5bf 478}