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