Update copyright year
[openssl.git] / test / threadstest.c
1 /*
2  * Copyright 2016-2021 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 #if defined(_WIN32)
11 # include <windows.h>
12 #endif
13
14 #include <string.h>
15 #include <openssl/crypto.h>
16 #include <openssl/evp.h>
17 #include <openssl/aes.h>
18 #include <openssl/rsa.h>
19 #include "testutil.h"
20
21 static int do_fips = 0;
22
23 #if !defined(OPENSSL_THREADS) || defined(CRYPTO_TDEBUG)
24
25 typedef unsigned int thread_t;
26
27 static int run_thread(thread_t *t, void (*f)(void))
28 {
29     f();
30     return 1;
31 }
32
33 static int wait_for_thread(thread_t thread)
34 {
35     return 1;
36 }
37
38 #elif defined(OPENSSL_SYS_WINDOWS)
39
40 typedef HANDLE thread_t;
41
42 static DWORD WINAPI thread_run(LPVOID arg)
43 {
44     void (*f)(void);
45
46     *(void **) (&f) = arg;
47
48     f();
49     return 0;
50 }
51
52 static int run_thread(thread_t *t, void (*f)(void))
53 {
54     *t = CreateThread(NULL, 0, thread_run, *(void **) &f, 0, NULL);
55     return *t != NULL;
56 }
57
58 static int wait_for_thread(thread_t thread)
59 {
60     return WaitForSingleObject(thread, INFINITE) == 0;
61 }
62
63 #else
64
65 typedef pthread_t thread_t;
66
67 static void *thread_run(void *arg)
68 {
69     void (*f)(void);
70
71     *(void **) (&f) = arg;
72
73     f();
74     return NULL;
75 }
76
77 static int run_thread(thread_t *t, void (*f)(void))
78 {
79     return pthread_create(t, NULL, thread_run, *(void **) &f) == 0;
80 }
81
82 static int wait_for_thread(thread_t thread)
83 {
84     return pthread_join(thread, NULL) == 0;
85 }
86
87 #endif
88
89 static int test_lock(void)
90 {
91     CRYPTO_RWLOCK *lock = CRYPTO_THREAD_lock_new();
92
93     if (!TEST_true(CRYPTO_THREAD_read_lock(lock))
94         || !TEST_true(CRYPTO_THREAD_unlock(lock)))
95         return 0;
96
97     CRYPTO_THREAD_lock_free(lock);
98
99     return 1;
100 }
101
102 static CRYPTO_ONCE once_run = CRYPTO_ONCE_STATIC_INIT;
103 static unsigned once_run_count = 0;
104
105 static void once_do_run(void)
106 {
107     once_run_count++;
108 }
109
110 static void once_run_thread_cb(void)
111 {
112     CRYPTO_THREAD_run_once(&once_run, once_do_run);
113 }
114
115 static int test_once(void)
116 {
117     thread_t thread;
118
119     if (!TEST_true(run_thread(&thread, once_run_thread_cb))
120         || !TEST_true(wait_for_thread(thread))
121         || !CRYPTO_THREAD_run_once(&once_run, once_do_run)
122         || !TEST_int_eq(once_run_count, 1))
123         return 0;
124     return 1;
125 }
126
127 static CRYPTO_THREAD_LOCAL thread_local_key;
128 static unsigned destructor_run_count = 0;
129 static int thread_local_thread_cb_ok = 0;
130
131 static void thread_local_destructor(void *arg)
132 {
133     unsigned *count;
134
135     if (arg == NULL)
136         return;
137
138     count = arg;
139
140     (*count)++;
141 }
142
143 static void thread_local_thread_cb(void)
144 {
145     void *ptr;
146
147     ptr = CRYPTO_THREAD_get_local(&thread_local_key);
148     if (!TEST_ptr_null(ptr)
149         || !TEST_true(CRYPTO_THREAD_set_local(&thread_local_key,
150                                               &destructor_run_count)))
151         return;
152
153     ptr = CRYPTO_THREAD_get_local(&thread_local_key);
154     if (!TEST_ptr_eq(ptr, &destructor_run_count))
155         return;
156
157     thread_local_thread_cb_ok = 1;
158 }
159
160 static int test_thread_local(void)
161 {
162     thread_t thread;
163     void *ptr = NULL;
164
165     if (!TEST_true(CRYPTO_THREAD_init_local(&thread_local_key,
166                                             thread_local_destructor)))
167         return 0;
168
169     ptr = CRYPTO_THREAD_get_local(&thread_local_key);
170     if (!TEST_ptr_null(ptr)
171         || !TEST_true(run_thread(&thread, thread_local_thread_cb))
172         || !TEST_true(wait_for_thread(thread))
173         || !TEST_int_eq(thread_local_thread_cb_ok, 1))
174         return 0;
175
176 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
177
178     ptr = CRYPTO_THREAD_get_local(&thread_local_key);
179     if (!TEST_ptr_null(ptr))
180         return 0;
181
182 # if !defined(OPENSSL_SYS_WINDOWS)
183     if (!TEST_int_eq(destructor_run_count, 1))
184         return 0;
185 # endif
186 #endif
187
188     if (!TEST_true(CRYPTO_THREAD_cleanup_local(&thread_local_key)))
189         return 0;
190     return 1;
191 }
192
193 static int test_atomic(void)
194 {
195     int val = 0, ret = 0, testresult = 0;
196     uint64_t val64 = 1, ret64 = 0;
197     CRYPTO_RWLOCK *lock = CRYPTO_THREAD_lock_new();
198
199     if (!TEST_ptr(lock))
200         return 0;
201
202     if (CRYPTO_atomic_add(&val, 1, &ret, NULL)) {
203         /* This succeeds therefore we're on a platform with lockless atomics */
204         if (!TEST_int_eq(val, 1) || !TEST_int_eq(val, ret))
205             goto err;
206     } else {
207         /* This failed therefore we're on a platform without lockless atomics */
208         if (!TEST_int_eq(val, 0) || !TEST_int_eq(val, ret))
209             goto err;
210     }
211     val = 0;
212     ret = 0;
213
214     if (!TEST_true(CRYPTO_atomic_add(&val, 1, &ret, lock)))
215         goto err;
216     if (!TEST_int_eq(val, 1) || !TEST_int_eq(val, ret))
217         goto err;
218
219     if (CRYPTO_atomic_or(&val64, 2, &ret64, NULL)) {
220         /* This succeeds therefore we're on a platform with lockless atomics */
221         if (!TEST_uint_eq((unsigned int)val64, 3)
222                 || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
223             goto err;
224     } else {
225         /* This failed therefore we're on a platform without lockless atomics */
226         if (!TEST_uint_eq((unsigned int)val64, 1)
227                 || !TEST_int_eq((unsigned int)ret64, 0))
228             goto err;
229     }
230     val64 = 1;
231     ret64 = 0;
232
233     if (!TEST_true(CRYPTO_atomic_or(&val64, 2, &ret64, lock)))
234         goto err;
235
236     if (!TEST_uint_eq((unsigned int)val64, 3)
237             || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
238         goto err;
239
240     ret64 = 0;
241     if (CRYPTO_atomic_load(&val64, &ret64, NULL)) {
242         /* This succeeds therefore we're on a platform with lockless atomics */
243         if (!TEST_uint_eq((unsigned int)val64, 3)
244                 || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
245             goto err;
246     } else {
247         /* This failed therefore we're on a platform without lockless atomics */
248         if (!TEST_uint_eq((unsigned int)val64, 3)
249                 || !TEST_int_eq((unsigned int)ret64, 0))
250             goto err;
251     }
252
253     ret64 = 0;
254     if (!TEST_true(CRYPTO_atomic_load(&val64, &ret64, lock)))
255         goto err;
256
257     if (!TEST_uint_eq((unsigned int)val64, 3)
258             || !TEST_uint_eq((unsigned int)val64, (unsigned int)ret64))
259         goto err;
260
261     testresult = 1;
262  err:
263     CRYPTO_THREAD_lock_free(lock);
264     return testresult;
265 }
266
267 static OSSL_LIB_CTX *multi_libctx = NULL;
268 static int multi_success;
269
270 static void thread_general_worker(void)
271 {
272     EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
273     EVP_MD *md = EVP_MD_fetch(multi_libctx, "SHA2-256", NULL);
274     EVP_CIPHER_CTX *cipherctx = EVP_CIPHER_CTX_new();
275     EVP_CIPHER *ciph = EVP_CIPHER_fetch(multi_libctx, "AES-128-CBC", NULL);
276     const char *message = "Hello World";
277     size_t messlen = strlen(message);
278     /* Should be big enough for encryption output too */
279     unsigned char out[EVP_MAX_MD_SIZE];
280     const unsigned char key[AES_BLOCK_SIZE] = {
281         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
282         0x0c, 0x0d, 0x0e, 0x0f
283     };
284     const unsigned char iv[AES_BLOCK_SIZE] = {
285         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
286         0x0c, 0x0d, 0x0e, 0x0f
287     };
288     unsigned int mdoutl;
289     int ciphoutl;
290     EVP_PKEY_CTX *pctx = NULL;
291     EVP_PKEY *pkey = NULL;
292     int testresult = 0;
293     int i, isfips;
294
295     isfips = OSSL_PROVIDER_available(multi_libctx, "fips");
296
297     if (!TEST_ptr(mdctx)
298             || !TEST_ptr(md)
299             || !TEST_ptr(cipherctx)
300             || !TEST_ptr(ciph))
301         goto err;
302
303     /* Do some work */
304     for (i = 0; i < 5; i++) {
305         if (!TEST_true(EVP_DigestInit_ex(mdctx, md, NULL))
306                 || !TEST_true(EVP_DigestUpdate(mdctx, message, messlen))
307                 || !TEST_true(EVP_DigestFinal(mdctx, out, &mdoutl)))
308             goto err;
309     }
310     for (i = 0; i < 5; i++) {
311         if (!TEST_true(EVP_EncryptInit_ex(cipherctx, ciph, NULL, key, iv))
312                 || !TEST_true(EVP_EncryptUpdate(cipherctx, out, &ciphoutl,
313                                                 (unsigned char *)message,
314                                                 messlen))
315                 || !TEST_true(EVP_EncryptFinal(cipherctx, out, &ciphoutl)))
316             goto err;
317     }
318
319     pctx = EVP_PKEY_CTX_new_from_name(multi_libctx, "RSA", NULL);
320     if (!TEST_ptr(pctx)
321             || !TEST_int_gt(EVP_PKEY_keygen_init(pctx), 0)
322                /*
323                 * We want the test to run quickly - not securely. Therefore we
324                 * use an insecure bit length where we can (512). In the FIPS
325                 * module though we must use a longer length.
326                 */
327             || !TEST_int_gt(EVP_PKEY_CTX_set_rsa_keygen_bits(pctx,
328                                                              isfips ? 2048 : 512),
329                                                              0)
330             || !TEST_int_gt(EVP_PKEY_keygen(pctx, &pkey), 0))
331         goto err;
332
333     testresult = 1;
334  err:
335     EVP_MD_CTX_free(mdctx);
336     EVP_MD_free(md);
337     EVP_CIPHER_CTX_free(cipherctx);
338     EVP_CIPHER_free(ciph);
339     EVP_PKEY_CTX_free(pctx);
340     EVP_PKEY_free(pkey);
341     if (!testresult)
342         multi_success = 0;
343 }
344
345 static void thread_multi_simple_fetch(void)
346 {
347     EVP_MD *md = EVP_MD_fetch(NULL, "SHA2-256", NULL);
348
349     if (md != NULL)
350         EVP_MD_free(md);
351     else
352         multi_success = 0;
353 }
354
355 /*
356  * Do work in multiple worker threads at the same time.
357  * Test 0: General worker, using the default provider
358  * Test 1: General worker, using the fips provider
359  * Test 2: Simple fetch worker
360  */
361 static int test_multi(int idx)
362 {
363     thread_t thread1, thread2;
364     int testresult = 0;
365     OSSL_PROVIDER *prov = NULL;
366     void (*worker)(void);
367
368     if (idx == 1 && !do_fips)
369         return TEST_skip("FIPS not supported");
370
371     multi_success = 1;
372     multi_libctx = OSSL_LIB_CTX_new();
373     if (!TEST_ptr(multi_libctx))
374         goto err;
375     prov = OSSL_PROVIDER_load(multi_libctx, (idx == 1) ? "fips" : "default");
376     if (!TEST_ptr(prov))
377         goto err;
378
379     switch (idx) {
380     case 0:
381     case 1:
382         worker = thread_general_worker;
383         break;
384     case 2:
385         worker = thread_multi_simple_fetch;
386         break;
387     default:
388         TEST_error("Invalid test index");
389         goto err;
390     }
391
392     if (!TEST_true(run_thread(&thread1, worker))
393             || !TEST_true(run_thread(&thread2, worker)))
394         goto err;
395
396     worker();
397
398     if (!TEST_true(wait_for_thread(thread1))
399             || !TEST_true(wait_for_thread(thread2))
400             || !TEST_true(multi_success))
401         goto err;
402
403     testresult = 1;
404
405  err:
406     OSSL_PROVIDER_unload(prov);
407     OSSL_LIB_CTX_free(multi_libctx);
408     return testresult;
409 }
410
411 typedef enum OPTION_choice {
412     OPT_ERR = -1,
413     OPT_EOF = 0,
414     OPT_FIPS,
415     OPT_TEST_ENUM
416 } OPTION_CHOICE;
417
418 const OPTIONS *test_get_options(void)
419 {
420     static const OPTIONS options[] = {
421         OPT_TEST_OPTIONS_DEFAULT_USAGE,
422         { "fips", OPT_FIPS, '-', "Test the FIPS provider" },
423         { NULL }
424     };
425     return options;
426 }
427
428 int setup_tests(void)
429 {
430     OPTION_CHOICE o;
431
432     while ((o = opt_next()) != OPT_EOF) {
433         switch (o) {
434         case OPT_FIPS:
435             do_fips = 1;
436             break;
437         case OPT_TEST_CASES:
438             break;
439         default:
440             return 0;
441         }
442     }
443
444     ADD_TEST(test_lock);
445     ADD_TEST(test_once);
446     ADD_TEST(test_thread_local);
447     ADD_TEST(test_atomic);
448     ADD_ALL_TESTS(test_multi, 3);
449     return 1;
450 }