1*4724848cSchristos /*
2*4724848cSchristos * Copyright 2016-2017 The OpenSSL Project Authors. All Rights Reserved.
3*4724848cSchristos *
4*4724848cSchristos * Licensed under the OpenSSL license (the "License"). You may not use
5*4724848cSchristos * this file except in compliance with the License. You can obtain a copy
6*4724848cSchristos * in the file LICENSE in the source distribution or at
7*4724848cSchristos * https://www.openssl.org/source/license.html
8*4724848cSchristos */
9*4724848cSchristos
10*4724848cSchristos #if defined(_WIN32)
11*4724848cSchristos # include <windows.h>
12*4724848cSchristos #endif
13*4724848cSchristos
14*4724848cSchristos #include <openssl/crypto.h>
15*4724848cSchristos #include "testutil.h"
16*4724848cSchristos
17*4724848cSchristos #if !defined(OPENSSL_THREADS) || defined(CRYPTO_TDEBUG)
18*4724848cSchristos
19*4724848cSchristos typedef unsigned int thread_t;
20*4724848cSchristos
run_thread(thread_t * t,void (* f)(void))21*4724848cSchristos static int run_thread(thread_t *t, void (*f)(void))
22*4724848cSchristos {
23*4724848cSchristos f();
24*4724848cSchristos return 1;
25*4724848cSchristos }
26*4724848cSchristos
wait_for_thread(thread_t thread)27*4724848cSchristos static int wait_for_thread(thread_t thread)
28*4724848cSchristos {
29*4724848cSchristos return 1;
30*4724848cSchristos }
31*4724848cSchristos
32*4724848cSchristos #elif defined(OPENSSL_SYS_WINDOWS)
33*4724848cSchristos
34*4724848cSchristos typedef HANDLE thread_t;
35*4724848cSchristos
thread_run(LPVOID arg)36*4724848cSchristos static DWORD WINAPI thread_run(LPVOID arg)
37*4724848cSchristos {
38*4724848cSchristos void (*f)(void);
39*4724848cSchristos
40*4724848cSchristos *(void **) (&f) = arg;
41*4724848cSchristos
42*4724848cSchristos f();
43*4724848cSchristos return 0;
44*4724848cSchristos }
45*4724848cSchristos
run_thread(thread_t * t,void (* f)(void))46*4724848cSchristos static int run_thread(thread_t *t, void (*f)(void))
47*4724848cSchristos {
48*4724848cSchristos *t = CreateThread(NULL, 0, thread_run, *(void **) &f, 0, NULL);
49*4724848cSchristos return *t != NULL;
50*4724848cSchristos }
51*4724848cSchristos
wait_for_thread(thread_t thread)52*4724848cSchristos static int wait_for_thread(thread_t thread)
53*4724848cSchristos {
54*4724848cSchristos return WaitForSingleObject(thread, INFINITE) == 0;
55*4724848cSchristos }
56*4724848cSchristos
57*4724848cSchristos #else
58*4724848cSchristos
59*4724848cSchristos typedef pthread_t thread_t;
60*4724848cSchristos
thread_run(void * arg)61*4724848cSchristos static void *thread_run(void *arg)
62*4724848cSchristos {
63*4724848cSchristos void (*f)(void);
64*4724848cSchristos
65*4724848cSchristos *(void **) (&f) = arg;
66*4724848cSchristos
67*4724848cSchristos f();
68*4724848cSchristos return NULL;
69*4724848cSchristos }
70*4724848cSchristos
run_thread(thread_t * t,void (* f)(void))71*4724848cSchristos static int run_thread(thread_t *t, void (*f)(void))
72*4724848cSchristos {
73*4724848cSchristos return pthread_create(t, NULL, thread_run, *(void **) &f) == 0;
74*4724848cSchristos }
75*4724848cSchristos
wait_for_thread(thread_t thread)76*4724848cSchristos static int wait_for_thread(thread_t thread)
77*4724848cSchristos {
78*4724848cSchristos return pthread_join(thread, NULL) == 0;
79*4724848cSchristos }
80*4724848cSchristos
81*4724848cSchristos #endif
82*4724848cSchristos
test_lock(void)83*4724848cSchristos static int test_lock(void)
84*4724848cSchristos {
85*4724848cSchristos CRYPTO_RWLOCK *lock = CRYPTO_THREAD_lock_new();
86*4724848cSchristos
87*4724848cSchristos if (!TEST_true(CRYPTO_THREAD_read_lock(lock))
88*4724848cSchristos || !TEST_true(CRYPTO_THREAD_unlock(lock)))
89*4724848cSchristos return 0;
90*4724848cSchristos
91*4724848cSchristos CRYPTO_THREAD_lock_free(lock);
92*4724848cSchristos
93*4724848cSchristos return 1;
94*4724848cSchristos }
95*4724848cSchristos
96*4724848cSchristos static CRYPTO_ONCE once_run = CRYPTO_ONCE_STATIC_INIT;
97*4724848cSchristos static unsigned once_run_count = 0;
98*4724848cSchristos
once_do_run(void)99*4724848cSchristos static void once_do_run(void)
100*4724848cSchristos {
101*4724848cSchristos once_run_count++;
102*4724848cSchristos }
103*4724848cSchristos
once_run_thread_cb(void)104*4724848cSchristos static void once_run_thread_cb(void)
105*4724848cSchristos {
106*4724848cSchristos CRYPTO_THREAD_run_once(&once_run, once_do_run);
107*4724848cSchristos }
108*4724848cSchristos
test_once(void)109*4724848cSchristos static int test_once(void)
110*4724848cSchristos {
111*4724848cSchristos thread_t thread;
112*4724848cSchristos
113*4724848cSchristos if (!TEST_true(run_thread(&thread, once_run_thread_cb))
114*4724848cSchristos || !TEST_true(wait_for_thread(thread))
115*4724848cSchristos || !CRYPTO_THREAD_run_once(&once_run, once_do_run)
116*4724848cSchristos || !TEST_int_eq(once_run_count, 1))
117*4724848cSchristos return 0;
118*4724848cSchristos return 1;
119*4724848cSchristos }
120*4724848cSchristos
121*4724848cSchristos static CRYPTO_THREAD_LOCAL thread_local_key;
122*4724848cSchristos static unsigned destructor_run_count = 0;
123*4724848cSchristos static int thread_local_thread_cb_ok = 0;
124*4724848cSchristos
thread_local_destructor(void * arg)125*4724848cSchristos static void thread_local_destructor(void *arg)
126*4724848cSchristos {
127*4724848cSchristos unsigned *count;
128*4724848cSchristos
129*4724848cSchristos if (arg == NULL)
130*4724848cSchristos return;
131*4724848cSchristos
132*4724848cSchristos count = arg;
133*4724848cSchristos
134*4724848cSchristos (*count)++;
135*4724848cSchristos }
136*4724848cSchristos
thread_local_thread_cb(void)137*4724848cSchristos static void thread_local_thread_cb(void)
138*4724848cSchristos {
139*4724848cSchristos void *ptr;
140*4724848cSchristos
141*4724848cSchristos ptr = CRYPTO_THREAD_get_local(&thread_local_key);
142*4724848cSchristos if (!TEST_ptr_null(ptr)
143*4724848cSchristos || !TEST_true(CRYPTO_THREAD_set_local(&thread_local_key,
144*4724848cSchristos &destructor_run_count)))
145*4724848cSchristos return;
146*4724848cSchristos
147*4724848cSchristos ptr = CRYPTO_THREAD_get_local(&thread_local_key);
148*4724848cSchristos if (!TEST_ptr_eq(ptr, &destructor_run_count))
149*4724848cSchristos return;
150*4724848cSchristos
151*4724848cSchristos thread_local_thread_cb_ok = 1;
152*4724848cSchristos }
153*4724848cSchristos
test_thread_local(void)154*4724848cSchristos static int test_thread_local(void)
155*4724848cSchristos {
156*4724848cSchristos thread_t thread;
157*4724848cSchristos void *ptr = NULL;
158*4724848cSchristos
159*4724848cSchristos if (!TEST_true(CRYPTO_THREAD_init_local(&thread_local_key,
160*4724848cSchristos thread_local_destructor)))
161*4724848cSchristos return 0;
162*4724848cSchristos
163*4724848cSchristos ptr = CRYPTO_THREAD_get_local(&thread_local_key);
164*4724848cSchristos if (!TEST_ptr_null(ptr)
165*4724848cSchristos || !TEST_true(run_thread(&thread, thread_local_thread_cb))
166*4724848cSchristos || !TEST_true(wait_for_thread(thread))
167*4724848cSchristos || !TEST_int_eq(thread_local_thread_cb_ok, 1))
168*4724848cSchristos return 0;
169*4724848cSchristos
170*4724848cSchristos #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
171*4724848cSchristos
172*4724848cSchristos ptr = CRYPTO_THREAD_get_local(&thread_local_key);
173*4724848cSchristos if (!TEST_ptr_null(ptr))
174*4724848cSchristos return 0;
175*4724848cSchristos
176*4724848cSchristos # if !defined(OPENSSL_SYS_WINDOWS)
177*4724848cSchristos if (!TEST_int_eq(destructor_run_count, 1))
178*4724848cSchristos return 0;
179*4724848cSchristos # endif
180*4724848cSchristos #endif
181*4724848cSchristos
182*4724848cSchristos if (!TEST_true(CRYPTO_THREAD_cleanup_local(&thread_local_key)))
183*4724848cSchristos return 0;
184*4724848cSchristos return 1;
185*4724848cSchristos }
186*4724848cSchristos
setup_tests(void)187*4724848cSchristos int setup_tests(void)
188*4724848cSchristos {
189*4724848cSchristos ADD_TEST(test_lock);
190*4724848cSchristos ADD_TEST(test_once);
191*4724848cSchristos ADD_TEST(test_thread_local);
192*4724848cSchristos return 1;
193*4724848cSchristos }
194