Coverage Report

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