summaryrefslogtreecommitdiff
path: root/test/threadpool_test.c
blob: 90ddaa9ce7c3ff948193eb13c3c5447b7a22df3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
 * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
 *
 * Licensed under the Apache License 2.0 (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
 */

#include <string.h>
#include <internal/cryptlib.h>
#include <internal/thread_arch.h>
#include <internal/thread.h>
#include <openssl/thread.h>
#include "testutil.h"

static int test_thread_reported_flags(void)
{
    uint32_t flags = OSSL_get_thread_support_flags();

#if !defined(OPENSSL_THREADS)
    if (!TEST_int_eq(flags, 0))
        return 0;
#endif

#if defined(OPENSSL_NO_THREAD_POOL)
    if (!TEST_int_eq(flags & OSSL_THREAD_SUPPORT_FLAG_THREAD_POOL, 0))
        return 0;
#else
    if (!TEST_int_eq(flags & OSSL_THREAD_SUPPORT_FLAG_THREAD_POOL,
                     OSSL_THREAD_SUPPORT_FLAG_THREAD_POOL))
        return 0;
#endif

#if defined(OPENSSL_NO_DEFAULT_THREAD_POOL)
    if (!TEST_int_eq(flags & OSSL_THREAD_SUPPORT_FLAG_DEFAULT_SPAWN, 0))
        return 0;
#else
    if (!TEST_int_eq(flags & OSSL_THREAD_SUPPORT_FLAG_DEFAULT_SPAWN,
                     OSSL_THREAD_SUPPORT_FLAG_DEFAULT_SPAWN))
        return 0;
#endif

    return 1;
}

#ifndef OPENSSL_NO_THREAD_POOL

# define TEST_THREAD_NATIVE_FN_SET_VALUE 1
static uint32_t test_thread_native_fn(void *data)
{
    uint32_t *ldata = (uint32_t*) data;
    *ldata = *ldata + 1;
    return *ldata - 1;
}
/* Tests of native threads */

static int test_thread_native(void)
{
    uint32_t retval;
    uint32_t local;
    CRYPTO_THREAD *t;

    /* thread spawn, join */

    local = 1;
    t = ossl_crypto_thread_native_start(test_thread_native_fn, &local, 1);
    if (!TEST_ptr(t))
        return 0;

    /*
     * pthread_join results in undefined behaviour if called on a joined
     * thread. We do not impose such restrictions, so it's up to us to
     * ensure that this does not happen (thread sanitizer will warn us
     * if we do).
     */
    if (!TEST_int_eq(ossl_crypto_thread_native_join(t, &retval), 1))
        return 0;
    if (!TEST_int_eq(ossl_crypto_thread_native_join(t, &retval), 1))
        return 0;

    if (!TEST_int_eq(retval, 1) || !TEST_int_eq(local, 2))
        return 0;

    if (!TEST_int_eq(ossl_crypto_thread_native_clean(t), 1))
        return 0;
    t = NULL;

    if (!TEST_int_eq(ossl_crypto_thread_native_clean(t), 0))
        return 0;

    return 1;
}

# if !defined(OPENSSL_NO_DEFAULT_THREAD_POOL)
static int test_thread_internal(void)
{
    uint32_t retval[3];
    uint32_t local[3] = { 0 };
    uint32_t threads_supported;
    size_t i;
    void *t[3];
    OSSL_LIB_CTX *cust_ctx = OSSL_LIB_CTX_new();

    threads_supported = OSSL_get_thread_support_flags();
    threads_supported &= OSSL_THREAD_SUPPORT_FLAG_DEFAULT_SPAWN;

    if (threads_supported == 0) {
        if (!TEST_uint64_t_eq(OSSL_get_max_threads(NULL), 0))
            return 0;
        if (!TEST_uint64_t_eq(OSSL_get_max_threads(cust_ctx), 0))
            return 0;

        if (!TEST_int_eq(OSSL_set_max_threads(NULL, 1), 0))
            return 0;
        if (!TEST_int_eq(OSSL_set_max_threads(cust_ctx, 1), 0))
            return 0;

        if (!TEST_uint64_t_eq(OSSL_get_max_threads(NULL), 0))
            return 0;
        if (!TEST_uint64_t_eq(OSSL_get_max_threads(cust_ctx), 0))
            return 0;

        t[0] = ossl_crypto_thread_start(NULL, test_thread_native_fn, &local[0]);
        if (!TEST_ptr_null(t[0]))
            return 0;

        return 1;
    }

    /* fail when not allowed to use threads */

    if (!TEST_uint64_t_eq(OSSL_get_max_threads(NULL), 0))
        return 0;
    t[0] = ossl_crypto_thread_start(NULL, test_thread_native_fn, &local[0]);
    if (!TEST_ptr_null(t[0]))
        return 0;

    /* fail when enabled on a different context */
    if (!TEST_uint64_t_eq(OSSL_get_max_threads(cust_ctx), 0))
        return 0;
    if (!TEST_int_eq(OSSL_set_max_threads(cust_ctx, 1), 1))
        return 0;
    if (!TEST_uint64_t_eq(OSSL_get_max_threads(NULL), 0))
        return 0;
    if (!TEST_uint64_t_eq(OSSL_get_max_threads(cust_ctx), 1))
        return 0;
    t[0] = ossl_crypto_thread_start(NULL, test_thread_native_fn, &local[0]);
    if (!TEST_ptr_null(t[0]))
        return 0;
    if (!TEST_int_eq(OSSL_set_max_threads(cust_ctx, 0), 1))
        return 0;

    /* sequential startup */

    if (!TEST_int_eq(OSSL_set_max_threads(NULL, 1), 1))
        return 0;
    if (!TEST_uint64_t_eq(OSSL_get_max_threads(NULL), 1))
        return 0;
    if (!TEST_uint64_t_eq(OSSL_get_max_threads(cust_ctx), 0))
        return 0;

    for (i = 0; i < OSSL_NELEM(t); ++i) {
        local[0] = i + 1;

        t[i] = ossl_crypto_thread_start(NULL, test_thread_native_fn, &local[0]);
        if (!TEST_ptr(t[i]))
            return 0;

        /*
         * pthread_join results in undefined behaviour if called on a joined
         * thread. We do not impose such restrictions, so it's up to us to
         * ensure that this does not happen (thread sanitizer will warn us
         * if we do).
         */
        if (!TEST_int_eq(ossl_crypto_thread_join(t[i], &retval[0]), 1))
            return 0;
        if (!TEST_int_eq(ossl_crypto_thread_join(t[i], &retval[0]), 1))
            return 0;

        if (!TEST_int_eq(retval[0], i + 1) || !TEST_int_eq(local[0], i + 2))
            return 0;

        if (!TEST_int_eq(ossl_crypto_thread_clean(t[i]), 1))
            return 0;
        t[i] = NULL;

        if (!TEST_int_eq(ossl_crypto_thread_clean(t[i]), 0))
            return 0;
    }

    /* parallel startup */

    if (!TEST_int_eq(OSSL_set_max_threads(NULL, OSSL_NELEM(t)), 1))
        return 0;

    for (i = 0; i < OSSL_NELEM(t); ++i) {
        local[i] = i + 1;
        t[i] = ossl_crypto_thread_start(NULL, test_thread_native_fn, &local[i]);
        if (!TEST_ptr(t[i]))
            return 0;
    }
    for (i = 0; i < OSSL_NELEM(t); ++i) {
        if (!TEST_int_eq(ossl_crypto_thread_join(t[i], &retval[i]), 1))
            return 0;
    }
    for (i = 0; i < OSSL_NELEM(t); ++i) {
        if (!TEST_int_eq(retval[i], i + 1) || !TEST_int_eq(local[i], i + 2))
            return 0;
        if (!TEST_int_eq(ossl_crypto_thread_clean(t[i]), 1))
            return 0;
    }

    /* parallel startup, bottleneck */

    if (!TEST_int_eq(OSSL_set_max_threads(NULL, OSSL_NELEM(t) - 1), 1))
        return 0;

    for (i = 0; i < OSSL_NELEM(t); ++i) {
        local[i] = i + 1;
        t[i] = ossl_crypto_thread_start(NULL, test_thread_native_fn, &local[i]);
        if (!TEST_ptr(t[i]))
            return 0;
    }
    for (i = 0; i < OSSL_NELEM(t); ++i) {
        if (!TEST_int_eq(ossl_crypto_thread_join(t[i], &retval[i]), 1))
            return 0;
    }
    for (i = 0; i < OSSL_NELEM(t); ++i) {
        if (!TEST_int_eq(retval[i], i + 1) || !TEST_int_eq(local[i], i + 2))
            return 0;
        if (!TEST_int_eq(ossl_crypto_thread_clean(t[i]), 1))
            return 0;
    }

    if (!TEST_int_eq(OSSL_set_max_threads(NULL, 0), 1))
        return 0;

    OSSL_LIB_CTX_free(cust_ctx);
    return 1;
}
# endif

static uint32_t test_thread_native_multiple_joins_fn1(void *data)
{
    return 0;
}

static uint32_t test_thread_native_multiple_joins_fn2(void *data)
{
    ossl_crypto_thread_native_join((CRYPTO_THREAD *)data, NULL);
    return 0;
}

static uint32_t test_thread_native_multiple_joins_fn3(void *data)
{
    ossl_crypto_thread_native_join((CRYPTO_THREAD *)data, NULL);
    return 0;
}

static int test_thread_native_multiple_joins(void)
{
    CRYPTO_THREAD *t, *t1, *t2;

    t = ossl_crypto_thread_native_start(test_thread_native_multiple_joins_fn1, NULL, 1);
    t1 = ossl_crypto_thread_native_start(test_thread_native_multiple_joins_fn2, t, 1);
    t2 = ossl_crypto_thread_native_start(test_thread_native_multiple_joins_fn3, t, 1);

    if (!TEST_ptr(t) || !TEST_ptr(t1) || !TEST_ptr(t2))
        return 0;

    if (!TEST_int_eq(ossl_crypto_thread_native_join(t2, NULL), 1))
        return 0;
    if (!TEST_int_eq(ossl_crypto_thread_native_join(t1, NULL), 1))
        return 0;

    if (!TEST_int_eq(ossl_crypto_thread_native_clean(t2), 1))
        return 0;

    if (!TEST_int_eq(ossl_crypto_thread_native_clean(t1), 1))
        return 0;

    if (!TEST_int_eq(ossl_crypto_thread_native_clean(t), 1))
        return 0;

    return 1;
}

#endif

int setup_tests(void)
{
    ADD_TEST(test_thread_reported_flags);
#if !defined(OPENSSL_NO_THREAD_POOL)
    ADD_TEST(test_thread_native);
    ADD_TEST(test_thread_native_multiple_joins);
# if !defined(OPENSSL_NO_DEFAULT_THREAD_POOL)
    ADD_TEST(test_thread_internal);
# endif
#endif

    return 1;
}