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