Coverage Report

Created: 2025-06-22 06:56

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