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