2 * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include <internal/cryptlib.h>
12 #include <internal/thread_arch.h>
13 #include <internal/thread.h>
14 #include <openssl/thread.h>
17 static int test_thread_reported_flags(void)
19 uint32_t flags = OSSL_get_thread_support_flags();
21 #if !defined(OPENSSL_THREADS)
22 if (!TEST_int_eq(flags, 0))
26 #if defined(OPENSSL_NO_THREAD_POOL)
27 if (!TEST_int_eq(flags & OSSL_THREAD_SUPPORT_FLAG_THREAD_POOL, 0))
30 if (!TEST_int_eq(flags & OSSL_THREAD_SUPPORT_FLAG_THREAD_POOL,
31 OSSL_THREAD_SUPPORT_FLAG_THREAD_POOL))
35 #if defined(OPENSSL_NO_DEFAULT_THREAD_POOL)
36 if (!TEST_int_eq(flags & OSSL_THREAD_SUPPORT_FLAG_DEFAULT_SPAWN, 0))
39 if (!TEST_int_eq(flags & OSSL_THREAD_SUPPORT_FLAG_DEFAULT_SPAWN,
40 OSSL_THREAD_SUPPORT_FLAG_DEFAULT_SPAWN))
47 #ifndef OPENSSL_NO_THREAD_POOL
49 # define TEST_THREAD_NATIVE_FN_SET_VALUE 1
50 static uint32_t test_thread_native_fn(void *data)
52 uint32_t *ldata = (uint32_t*) data;
56 /* Tests of native threads */
58 static int test_thread_native(void)
64 /* thread spawn, join */
67 t = ossl_crypto_thread_native_start(test_thread_native_fn, &local, 1);
72 * pthread_join results in undefined behaviour if called on a joined
73 * thread. We do not impose such restrictions, so it's up to us to
74 * ensure that this does not happen (thread sanitizer will warn us
77 if (!TEST_int_eq(ossl_crypto_thread_native_join(t, &retval), 1))
79 if (!TEST_int_eq(ossl_crypto_thread_native_join(t, &retval), 1))
82 if (!TEST_int_eq(retval, 1) || !TEST_int_eq(local, 2))
85 if (!TEST_int_eq(ossl_crypto_thread_native_clean(t), 1))
89 if (!TEST_int_eq(ossl_crypto_thread_native_clean(t), 0))
95 # if !defined(OPENSSL_NO_DEFAULT_THREAD_POOL)
96 static int test_thread_internal(void)
99 uint32_t local[3] = { 0 };
100 uint32_t threads_supported;
103 OSSL_LIB_CTX *cust_ctx = OSSL_LIB_CTX_new();
105 threads_supported = OSSL_get_thread_support_flags();
106 threads_supported &= OSSL_THREAD_SUPPORT_FLAG_DEFAULT_SPAWN;
108 if (threads_supported == 0) {
109 if (!TEST_uint64_t_eq(OSSL_get_max_threads(NULL), 0))
111 if (!TEST_uint64_t_eq(OSSL_get_max_threads(cust_ctx), 0))
114 if (!TEST_int_eq(OSSL_set_max_threads(NULL, 1), 0))
116 if (!TEST_int_eq(OSSL_set_max_threads(cust_ctx, 1), 0))
119 if (!TEST_uint64_t_eq(OSSL_get_max_threads(NULL), 0))
121 if (!TEST_uint64_t_eq(OSSL_get_max_threads(cust_ctx), 0))
124 t[0] = ossl_crypto_thread_start(NULL, test_thread_native_fn, &local[0]);
125 if (!TEST_ptr_null(t[0]))
131 /* fail when not allowed to use threads */
133 if (!TEST_uint64_t_eq(OSSL_get_max_threads(NULL), 0))
135 t[0] = ossl_crypto_thread_start(NULL, test_thread_native_fn, &local[0]);
136 if (!TEST_ptr_null(t[0]))
139 /* fail when enabled on a different context */
140 if (!TEST_uint64_t_eq(OSSL_get_max_threads(cust_ctx), 0))
142 if (!TEST_int_eq(OSSL_set_max_threads(cust_ctx, 1), 1))
144 if (!TEST_uint64_t_eq(OSSL_get_max_threads(NULL), 0))
146 if (!TEST_uint64_t_eq(OSSL_get_max_threads(cust_ctx), 1))
148 t[0] = ossl_crypto_thread_start(NULL, test_thread_native_fn, &local[0]);
149 if (!TEST_ptr_null(t[0]))
151 if (!TEST_int_eq(OSSL_set_max_threads(cust_ctx, 0), 1))
154 /* sequential startup */
156 if (!TEST_int_eq(OSSL_set_max_threads(NULL, 1), 1))
158 if (!TEST_uint64_t_eq(OSSL_get_max_threads(NULL), 1))
160 if (!TEST_uint64_t_eq(OSSL_get_max_threads(cust_ctx), 0))
163 for (i = 0; i < OSSL_NELEM(t); ++i) {
166 t[i] = ossl_crypto_thread_start(NULL, test_thread_native_fn, &local[0]);
171 * pthread_join results in undefined behaviour if called on a joined
172 * thread. We do not impose such restrictions, so it's up to us to
173 * ensure that this does not happen (thread sanitizer will warn us
176 if (!TEST_int_eq(ossl_crypto_thread_join(t[i], &retval[0]), 1))
178 if (!TEST_int_eq(ossl_crypto_thread_join(t[i], &retval[0]), 1))
181 if (!TEST_int_eq(retval[0], i + 1) || !TEST_int_eq(local[0], i + 2))
184 if (!TEST_int_eq(ossl_crypto_thread_clean(t[i]), 1))
188 if (!TEST_int_eq(ossl_crypto_thread_clean(t[i]), 0))
192 /* parallel startup */
194 if (!TEST_int_eq(OSSL_set_max_threads(NULL, OSSL_NELEM(t)), 1))
197 for (i = 0; i < OSSL_NELEM(t); ++i) {
199 t[i] = ossl_crypto_thread_start(NULL, test_thread_native_fn, &local[i]);
203 for (i = 0; i < OSSL_NELEM(t); ++i) {
204 if (!TEST_int_eq(ossl_crypto_thread_join(t[i], &retval[i]), 1))
207 for (i = 0; i < OSSL_NELEM(t); ++i) {
208 if (!TEST_int_eq(retval[i], i + 1) || !TEST_int_eq(local[i], i + 2))
210 if (!TEST_int_eq(ossl_crypto_thread_clean(t[i]), 1))
214 /* parallel startup, bottleneck */
216 if (!TEST_int_eq(OSSL_set_max_threads(NULL, OSSL_NELEM(t) - 1), 1))
219 for (i = 0; i < OSSL_NELEM(t); ++i) {
221 t[i] = ossl_crypto_thread_start(NULL, test_thread_native_fn, &local[i]);
225 for (i = 0; i < OSSL_NELEM(t); ++i) {
226 if (!TEST_int_eq(ossl_crypto_thread_join(t[i], &retval[i]), 1))
229 for (i = 0; i < OSSL_NELEM(t); ++i) {
230 if (!TEST_int_eq(retval[i], i + 1) || !TEST_int_eq(local[i], i + 2))
232 if (!TEST_int_eq(ossl_crypto_thread_clean(t[i]), 1))
236 if (!TEST_int_eq(OSSL_set_max_threads(NULL, 0), 1))
239 OSSL_LIB_CTX_free(cust_ctx);
244 static uint32_t test_thread_native_multiple_joins_fn1(void *data)
249 static uint32_t test_thread_native_multiple_joins_fn2(void *data)
251 ossl_crypto_thread_native_join((CRYPTO_THREAD *)data, NULL);
255 static uint32_t test_thread_native_multiple_joins_fn3(void *data)
257 ossl_crypto_thread_native_join((CRYPTO_THREAD *)data, NULL);
261 static int test_thread_native_multiple_joins(void)
263 CRYPTO_THREAD *t, *t1, *t2;
265 t = ossl_crypto_thread_native_start(test_thread_native_multiple_joins_fn1, NULL, 1);
266 t1 = ossl_crypto_thread_native_start(test_thread_native_multiple_joins_fn2, t, 1);
267 t2 = ossl_crypto_thread_native_start(test_thread_native_multiple_joins_fn3, t, 1);
269 if (!TEST_ptr(t) || !TEST_ptr(t1) || !TEST_ptr(t2))
272 if (!TEST_int_eq(ossl_crypto_thread_native_join(t2, NULL), 1))
274 if (!TEST_int_eq(ossl_crypto_thread_native_join(t1, NULL), 1))
277 if (!TEST_int_eq(ossl_crypto_thread_native_clean(t2), 1))
280 if (!TEST_int_eq(ossl_crypto_thread_native_clean(t1), 1))
283 if (!TEST_int_eq(ossl_crypto_thread_native_clean(t), 1))
291 int setup_tests(void)
293 ADD_TEST(test_thread_reported_flags);
294 #if !defined(OPENSSL_NO_THREAD_POOL)
295 ADD_TEST(test_thread_native);
296 ADD_TEST(test_thread_native_multiple_joins);
297 # if !defined(OPENSSL_NO_DEFAULT_THREAD_POOL)
298 ADD_TEST(test_thread_internal);