Home | History | Annotate | Line # | Download | only in test
threadpool_test.c revision 1.1
      1  1.1  christos /*
      2  1.1  christos  * Copyright 2022-2024 The OpenSSL Project Authors. All Rights Reserved.
      3  1.1  christos  *
      4  1.1  christos  * Licensed under the Apache License 2.0 (the "License").  You may not use
      5  1.1  christos  * this file except in compliance with the License.  You can obtain a copy
      6  1.1  christos  * in the file LICENSE in the source distribution or at
      7  1.1  christos  * https://www.openssl.org/source/license.html
      8  1.1  christos  */
      9  1.1  christos 
     10  1.1  christos #include <string.h>
     11  1.1  christos #include <internal/cryptlib.h>
     12  1.1  christos #include <internal/thread_arch.h>
     13  1.1  christos #include <internal/thread.h>
     14  1.1  christos #include <openssl/thread.h>
     15  1.1  christos #include "testutil.h"
     16  1.1  christos 
     17  1.1  christos static int test_thread_reported_flags(void)
     18  1.1  christos {
     19  1.1  christos     uint32_t flags = OSSL_get_thread_support_flags();
     20  1.1  christos 
     21  1.1  christos #if !defined(OPENSSL_THREADS)
     22  1.1  christos     if (!TEST_int_eq(flags, 0))
     23  1.1  christos         return 0;
     24  1.1  christos #endif
     25  1.1  christos 
     26  1.1  christos #if defined(OPENSSL_NO_THREAD_POOL)
     27  1.1  christos     if (!TEST_int_eq(flags & OSSL_THREAD_SUPPORT_FLAG_THREAD_POOL, 0))
     28  1.1  christos         return 0;
     29  1.1  christos #else
     30  1.1  christos     if (!TEST_int_eq(flags & OSSL_THREAD_SUPPORT_FLAG_THREAD_POOL,
     31  1.1  christos                      OSSL_THREAD_SUPPORT_FLAG_THREAD_POOL))
     32  1.1  christos         return 0;
     33  1.1  christos #endif
     34  1.1  christos 
     35  1.1  christos #if defined(OPENSSL_NO_DEFAULT_THREAD_POOL)
     36  1.1  christos     if (!TEST_int_eq(flags & OSSL_THREAD_SUPPORT_FLAG_DEFAULT_SPAWN, 0))
     37  1.1  christos         return 0;
     38  1.1  christos #else
     39  1.1  christos     if (!TEST_int_eq(flags & OSSL_THREAD_SUPPORT_FLAG_DEFAULT_SPAWN,
     40  1.1  christos                      OSSL_THREAD_SUPPORT_FLAG_DEFAULT_SPAWN))
     41  1.1  christos         return 0;
     42  1.1  christos #endif
     43  1.1  christos 
     44  1.1  christos     return 1;
     45  1.1  christos }
     46  1.1  christos 
     47  1.1  christos #ifndef OPENSSL_NO_THREAD_POOL
     48  1.1  christos 
     49  1.1  christos # define TEST_THREAD_NATIVE_FN_SET_VALUE 1
     50  1.1  christos static uint32_t test_thread_native_fn(void *data)
     51  1.1  christos {
     52  1.1  christos     uint32_t *ldata = (uint32_t*) data;
     53  1.1  christos     *ldata = *ldata + 1;
     54  1.1  christos     return *ldata - 1;
     55  1.1  christos }
     56  1.1  christos /* Tests of native threads */
     57  1.1  christos 
     58  1.1  christos static int test_thread_native(void)
     59  1.1  christos {
     60  1.1  christos     uint32_t retval;
     61  1.1  christos     uint32_t local;
     62  1.1  christos     CRYPTO_THREAD *t;
     63  1.1  christos 
     64  1.1  christos     /* thread spawn, join */
     65  1.1  christos 
     66  1.1  christos     local = 1;
     67  1.1  christos     t = ossl_crypto_thread_native_start(test_thread_native_fn, &local, 1);
     68  1.1  christos     if (!TEST_ptr(t))
     69  1.1  christos         return 0;
     70  1.1  christos 
     71  1.1  christos     /*
     72  1.1  christos      * pthread_join results in undefined behaviour if called on a joined
     73  1.1  christos      * thread. We do not impose such restrictions, so it's up to us to
     74  1.1  christos      * ensure that this does not happen (thread sanitizer will warn us
     75  1.1  christos      * if we do).
     76  1.1  christos      */
     77  1.1  christos     if (!TEST_int_eq(ossl_crypto_thread_native_join(t, &retval), 1))
     78  1.1  christos         return 0;
     79  1.1  christos     if (!TEST_int_eq(ossl_crypto_thread_native_join(t, &retval), 1))
     80  1.1  christos         return 0;
     81  1.1  christos 
     82  1.1  christos     if (!TEST_int_eq(retval, 1) || !TEST_int_eq(local, 2))
     83  1.1  christos         return 0;
     84  1.1  christos 
     85  1.1  christos     if (!TEST_int_eq(ossl_crypto_thread_native_clean(t), 1))
     86  1.1  christos         return 0;
     87  1.1  christos     t = NULL;
     88  1.1  christos 
     89  1.1  christos     if (!TEST_int_eq(ossl_crypto_thread_native_clean(t), 0))
     90  1.1  christos         return 0;
     91  1.1  christos 
     92  1.1  christos     return 1;
     93  1.1  christos }
     94  1.1  christos 
     95  1.1  christos # if !defined(OPENSSL_NO_DEFAULT_THREAD_POOL)
     96  1.1  christos static int test_thread_internal(void)
     97  1.1  christos {
     98  1.1  christos     uint32_t retval[3];
     99  1.1  christos     uint32_t local[3] = { 0 };
    100  1.1  christos     uint32_t threads_supported;
    101  1.1  christos     size_t i;
    102  1.1  christos     void *t[3];
    103  1.1  christos     int status = 0;
    104  1.1  christos     OSSL_LIB_CTX *cust_ctx = OSSL_LIB_CTX_new();
    105  1.1  christos 
    106  1.1  christos     threads_supported = OSSL_get_thread_support_flags();
    107  1.1  christos     threads_supported &= OSSL_THREAD_SUPPORT_FLAG_DEFAULT_SPAWN;
    108  1.1  christos 
    109  1.1  christos     if (threads_supported == 0) {
    110  1.1  christos         if (!TEST_uint64_t_eq(OSSL_get_max_threads(NULL), 0))
    111  1.1  christos             goto cleanup;
    112  1.1  christos         if (!TEST_uint64_t_eq(OSSL_get_max_threads(cust_ctx), 0))
    113  1.1  christos             goto cleanup;
    114  1.1  christos 
    115  1.1  christos         if (!TEST_int_eq(OSSL_set_max_threads(NULL, 1), 0))
    116  1.1  christos             goto cleanup;
    117  1.1  christos         if (!TEST_int_eq(OSSL_set_max_threads(cust_ctx, 1), 0))
    118  1.1  christos             goto cleanup;
    119  1.1  christos 
    120  1.1  christos         if (!TEST_uint64_t_eq(OSSL_get_max_threads(NULL), 0))
    121  1.1  christos             goto cleanup;
    122  1.1  christos         if (!TEST_uint64_t_eq(OSSL_get_max_threads(cust_ctx), 0))
    123  1.1  christos             goto cleanup;
    124  1.1  christos 
    125  1.1  christos         t[0] = ossl_crypto_thread_start(NULL, test_thread_native_fn, &local[0]);
    126  1.1  christos         if (!TEST_ptr_null(t[0]))
    127  1.1  christos             goto cleanup;
    128  1.1  christos 
    129  1.1  christos         status = 1;
    130  1.1  christos         goto cleanup;
    131  1.1  christos     }
    132  1.1  christos 
    133  1.1  christos     /* fail when not allowed to use threads */
    134  1.1  christos 
    135  1.1  christos     if (!TEST_uint64_t_eq(OSSL_get_max_threads(NULL), 0))
    136  1.1  christos         goto cleanup;
    137  1.1  christos     t[0] = ossl_crypto_thread_start(NULL, test_thread_native_fn, &local[0]);
    138  1.1  christos     if (!TEST_ptr_null(t[0]))
    139  1.1  christos         goto cleanup;
    140  1.1  christos 
    141  1.1  christos     /* fail when enabled on a different context */
    142  1.1  christos     if (!TEST_uint64_t_eq(OSSL_get_max_threads(cust_ctx), 0))
    143  1.1  christos         goto cleanup;
    144  1.1  christos     if (!TEST_int_eq(OSSL_set_max_threads(cust_ctx, 1), 1))
    145  1.1  christos         goto cleanup;
    146  1.1  christos     if (!TEST_uint64_t_eq(OSSL_get_max_threads(NULL), 0))
    147  1.1  christos         goto cleanup;
    148  1.1  christos     if (!TEST_uint64_t_eq(OSSL_get_max_threads(cust_ctx), 1))
    149  1.1  christos         goto cleanup;
    150  1.1  christos     t[0] = ossl_crypto_thread_start(NULL, test_thread_native_fn, &local[0]);
    151  1.1  christos     if (!TEST_ptr_null(t[0]))
    152  1.1  christos         goto cleanup;
    153  1.1  christos     if (!TEST_int_eq(OSSL_set_max_threads(cust_ctx, 0), 1))
    154  1.1  christos         goto cleanup;
    155  1.1  christos 
    156  1.1  christos     /* sequential startup */
    157  1.1  christos 
    158  1.1  christos     if (!TEST_int_eq(OSSL_set_max_threads(NULL, 1), 1))
    159  1.1  christos         goto cleanup;
    160  1.1  christos     if (!TEST_uint64_t_eq(OSSL_get_max_threads(NULL), 1))
    161  1.1  christos         goto cleanup;
    162  1.1  christos     if (!TEST_uint64_t_eq(OSSL_get_max_threads(cust_ctx), 0))
    163  1.1  christos         goto cleanup;
    164  1.1  christos 
    165  1.1  christos     for (i = 0; i < OSSL_NELEM(t); ++i) {
    166  1.1  christos         local[0] = i + 1;
    167  1.1  christos 
    168  1.1  christos         t[i] = ossl_crypto_thread_start(NULL, test_thread_native_fn, &local[0]);
    169  1.1  christos         if (!TEST_ptr(t[i]))
    170  1.1  christos             goto cleanup;
    171  1.1  christos 
    172  1.1  christos         /*
    173  1.1  christos          * pthread_join results in undefined behaviour if called on a joined
    174  1.1  christos          * thread. We do not impose such restrictions, so it's up to us to
    175  1.1  christos          * ensure that this does not happen (thread sanitizer will warn us
    176  1.1  christos          * if we do).
    177  1.1  christos          */
    178  1.1  christos         if (!TEST_int_eq(ossl_crypto_thread_join(t[i], &retval[0]), 1))
    179  1.1  christos             goto cleanup;
    180  1.1  christos         if (!TEST_int_eq(ossl_crypto_thread_join(t[i], &retval[0]), 1))
    181  1.1  christos             goto cleanup;
    182  1.1  christos 
    183  1.1  christos         if (!TEST_int_eq(retval[0], i + 1) || !TEST_int_eq(local[0], i + 2))
    184  1.1  christos             goto cleanup;
    185  1.1  christos 
    186  1.1  christos         if (!TEST_int_eq(ossl_crypto_thread_clean(t[i]), 1))
    187  1.1  christos             goto cleanup;
    188  1.1  christos         t[i] = NULL;
    189  1.1  christos 
    190  1.1  christos         if (!TEST_int_eq(ossl_crypto_thread_clean(t[i]), 0))
    191  1.1  christos             goto cleanup;
    192  1.1  christos     }
    193  1.1  christos 
    194  1.1  christos     /* parallel startup */
    195  1.1  christos 
    196  1.1  christos     if (!TEST_int_eq(OSSL_set_max_threads(NULL, OSSL_NELEM(t)), 1))
    197  1.1  christos         goto cleanup;
    198  1.1  christos 
    199  1.1  christos     for (i = 0; i < OSSL_NELEM(t); ++i) {
    200  1.1  christos         local[i] = i + 1;
    201  1.1  christos         t[i] = ossl_crypto_thread_start(NULL, test_thread_native_fn, &local[i]);
    202  1.1  christos         if (!TEST_ptr(t[i]))
    203  1.1  christos             goto cleanup;
    204  1.1  christos     }
    205  1.1  christos     for (i = 0; i < OSSL_NELEM(t); ++i) {
    206  1.1  christos         if (!TEST_int_eq(ossl_crypto_thread_join(t[i], &retval[i]), 1))
    207  1.1  christos             goto cleanup;
    208  1.1  christos     }
    209  1.1  christos     for (i = 0; i < OSSL_NELEM(t); ++i) {
    210  1.1  christos         if (!TEST_int_eq(retval[i], i + 1) || !TEST_int_eq(local[i], i + 2))
    211  1.1  christos             goto cleanup;
    212  1.1  christos         if (!TEST_int_eq(ossl_crypto_thread_clean(t[i]), 1))
    213  1.1  christos             goto cleanup;
    214  1.1  christos     }
    215  1.1  christos 
    216  1.1  christos     /* parallel startup, bottleneck */
    217  1.1  christos 
    218  1.1  christos     if (!TEST_int_eq(OSSL_set_max_threads(NULL, OSSL_NELEM(t) - 1), 1))
    219  1.1  christos         goto cleanup;
    220  1.1  christos 
    221  1.1  christos     for (i = 0; i < OSSL_NELEM(t); ++i) {
    222  1.1  christos         local[i] = i + 1;
    223  1.1  christos         t[i] = ossl_crypto_thread_start(NULL, test_thread_native_fn, &local[i]);
    224  1.1  christos         if (!TEST_ptr(t[i]))
    225  1.1  christos             goto cleanup;
    226  1.1  christos     }
    227  1.1  christos     for (i = 0; i < OSSL_NELEM(t); ++i) {
    228  1.1  christos         if (!TEST_int_eq(ossl_crypto_thread_join(t[i], &retval[i]), 1))
    229  1.1  christos             goto cleanup;
    230  1.1  christos     }
    231  1.1  christos     for (i = 0; i < OSSL_NELEM(t); ++i) {
    232  1.1  christos         if (!TEST_int_eq(retval[i], i + 1) || !TEST_int_eq(local[i], i + 2))
    233  1.1  christos             goto cleanup;
    234  1.1  christos         if (!TEST_int_eq(ossl_crypto_thread_clean(t[i]), 1))
    235  1.1  christos             goto cleanup;
    236  1.1  christos     }
    237  1.1  christos 
    238  1.1  christos     if (!TEST_int_eq(OSSL_set_max_threads(NULL, 0), 1))
    239  1.1  christos         goto cleanup;
    240  1.1  christos 
    241  1.1  christos     status = 1;
    242  1.1  christos cleanup:
    243  1.1  christos     OSSL_LIB_CTX_free(cust_ctx);
    244  1.1  christos     return status;
    245  1.1  christos }
    246  1.1  christos # endif
    247  1.1  christos 
    248  1.1  christos static uint32_t test_thread_native_multiple_joins_fn1(void *data)
    249  1.1  christos {
    250  1.1  christos     return 0;
    251  1.1  christos }
    252  1.1  christos 
    253  1.1  christos static uint32_t test_thread_native_multiple_joins_fn2(void *data)
    254  1.1  christos {
    255  1.1  christos     ossl_crypto_thread_native_join((CRYPTO_THREAD *)data, NULL);
    256  1.1  christos     return 0;
    257  1.1  christos }
    258  1.1  christos 
    259  1.1  christos static uint32_t test_thread_native_multiple_joins_fn3(void *data)
    260  1.1  christos {
    261  1.1  christos     ossl_crypto_thread_native_join((CRYPTO_THREAD *)data, NULL);
    262  1.1  christos     return 0;
    263  1.1  christos }
    264  1.1  christos 
    265  1.1  christos static int test_thread_native_multiple_joins(void)
    266  1.1  christos {
    267  1.1  christos     CRYPTO_THREAD *t, *t1, *t2;
    268  1.1  christos 
    269  1.1  christos     t = ossl_crypto_thread_native_start(test_thread_native_multiple_joins_fn1, NULL, 1);
    270  1.1  christos     t1 = ossl_crypto_thread_native_start(test_thread_native_multiple_joins_fn2, t, 1);
    271  1.1  christos     t2 = ossl_crypto_thread_native_start(test_thread_native_multiple_joins_fn3, t, 1);
    272  1.1  christos 
    273  1.1  christos     if (!TEST_ptr(t) || !TEST_ptr(t1) || !TEST_ptr(t2))
    274  1.1  christos         return 0;
    275  1.1  christos 
    276  1.1  christos     if (!TEST_int_eq(ossl_crypto_thread_native_join(t2, NULL), 1))
    277  1.1  christos         return 0;
    278  1.1  christos     if (!TEST_int_eq(ossl_crypto_thread_native_join(t1, NULL), 1))
    279  1.1  christos         return 0;
    280  1.1  christos 
    281  1.1  christos     if (!TEST_int_eq(ossl_crypto_thread_native_clean(t2), 1))
    282  1.1  christos         return 0;
    283  1.1  christos 
    284  1.1  christos     if (!TEST_int_eq(ossl_crypto_thread_native_clean(t1), 1))
    285  1.1  christos         return 0;
    286  1.1  christos 
    287  1.1  christos     if (!TEST_int_eq(ossl_crypto_thread_native_clean(t), 1))
    288  1.1  christos         return 0;
    289  1.1  christos 
    290  1.1  christos     return 1;
    291  1.1  christos }
    292  1.1  christos 
    293  1.1  christos #endif
    294  1.1  christos 
    295  1.1  christos int setup_tests(void)
    296  1.1  christos {
    297  1.1  christos     ADD_TEST(test_thread_reported_flags);
    298  1.1  christos #if !defined(OPENSSL_NO_THREAD_POOL)
    299  1.1  christos     ADD_TEST(test_thread_native);
    300  1.1  christos     ADD_TEST(test_thread_native_multiple_joins);
    301  1.1  christos # if !defined(OPENSSL_NO_DEFAULT_THREAD_POOL)
    302  1.1  christos     ADD_TEST(test_thread_internal);
    303  1.1  christos # endif
    304  1.1  christos #endif
    305  1.1  christos 
    306  1.1  christos     return 1;
    307  1.1  christos }
    308