Copyright consolidation 02/10
[openssl.git] / test / threadstest.c
1 /*
2  * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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 #include <stdio.h>
11
12 #include <openssl/crypto.h>
13
14 #if !defined(OPENSSL_THREADS) || defined(CRYPTO_TDEBUG)
15
16 typedef unsigned int thread_t;
17
18 static int run_thread(thread_t *t, void (*f)(void))
19 {
20     f();
21     return 1;
22 }
23
24 static int wait_for_thread(thread_t thread)
25 {
26     return 1;
27 }
28
29 #elif defined(OPENSSL_SYS_WINDOWS)
30
31 typedef HANDLE thread_t;
32
33 static DWORD WINAPI thread_run(LPVOID arg)
34 {
35     void (*f)(void);
36
37     *(void **) (&f) = arg;
38
39     f();
40     return 0;
41 }
42
43 static int run_thread(thread_t *t, void (*f)(void))
44 {
45     *t = CreateThread(NULL, 0, thread_run, *(void **) &f, 0, NULL);
46     return *t != NULL;
47 }
48
49 static int wait_for_thread(thread_t thread)
50 {
51     return WaitForSingleObject(thread, INFINITE) == 0;
52 }
53
54 #else
55
56 typedef pthread_t thread_t;
57
58 static void *thread_run(void *arg)
59 {
60     void (*f)(void);
61
62     *(void **) (&f) = arg;
63
64     f();
65     return NULL;
66 }
67
68 static int run_thread(thread_t *t, void (*f)(void))
69 {
70     return pthread_create(t, NULL, thread_run, *(void **) &f) == 0;
71 }
72
73 static int wait_for_thread(thread_t thread)
74 {
75     return pthread_join(thread, NULL) == 0;
76 }
77
78 #endif
79
80 static int test_lock(void)
81 {
82     CRYPTO_RWLOCK *lock = CRYPTO_THREAD_lock_new();
83
84     if (!CRYPTO_THREAD_read_lock(lock)) {
85         fprintf(stderr, "CRYPTO_THREAD_read_lock() failed\n");
86         return 0;
87     }
88
89     if (!CRYPTO_THREAD_unlock(lock)) {
90         fprintf(stderr, "CRYPTO_THREAD_unlock() failed\n");
91         return 0;
92     }
93
94     CRYPTO_THREAD_lock_free(lock);
95
96     return 1;
97 }
98
99 static CRYPTO_ONCE once_run = CRYPTO_ONCE_STATIC_INIT;
100 static unsigned once_run_count = 0;
101
102 static void once_do_run(void)
103 {
104     once_run_count++;
105 }
106
107 static void once_run_thread_cb(void)
108 {
109     CRYPTO_THREAD_run_once(&once_run, once_do_run);
110 }
111
112 static int test_once(void)
113 {
114     thread_t thread;
115     if (!run_thread(&thread, once_run_thread_cb) ||
116         !wait_for_thread(thread))
117     {
118         fprintf(stderr, "run_thread() failed\n");
119         return 0;
120     }
121
122     if (!CRYPTO_THREAD_run_once(&once_run, once_do_run)) {
123         fprintf(stderr, "CRYPTO_THREAD_run_once() failed\n");
124         return 0;
125     }
126
127     if (once_run_count != 1) {
128         fprintf(stderr, "once run %u times\n", once_run_count);
129         return 0;
130     }
131
132     return 1;
133 }
134
135 static CRYPTO_THREAD_LOCAL thread_local_key;
136 static unsigned destructor_run_count = 0;
137 static int thread_local_thread_cb_ok = 0;
138
139 static void thread_local_destructor(void *arg)
140 {
141     unsigned *count;
142
143     if (arg == NULL)
144         return;
145
146     count = arg;
147
148     (*count)++;
149 }
150
151 static void thread_local_thread_cb(void)
152 {
153     void *ptr;
154
155     ptr = CRYPTO_THREAD_get_local(&thread_local_key);
156     if (ptr != NULL) {
157         fprintf(stderr, "ptr not NULL\n");
158         return;
159     }
160
161     if (!CRYPTO_THREAD_set_local(&thread_local_key, &destructor_run_count)) {
162         fprintf(stderr, "CRYPTO_THREAD_set_local() failed\n");
163         return;
164     }
165
166     ptr = CRYPTO_THREAD_get_local(&thread_local_key);
167     if (ptr != &destructor_run_count) {
168         fprintf(stderr, "invalid ptr\n");
169         return;
170     }
171
172     thread_local_thread_cb_ok = 1;
173 }
174
175 static int test_thread_local(void)
176 {
177     thread_t thread;
178     void *ptr = NULL;
179
180     if (!CRYPTO_THREAD_init_local(&thread_local_key, thread_local_destructor)) {
181         fprintf(stderr, "CRYPTO_THREAD_init_local() failed\n");
182         return 0;
183     }
184
185     ptr = CRYPTO_THREAD_get_local(&thread_local_key);
186     if (ptr != NULL) {
187         fprintf(stderr, "ptr not NULL\n");
188         return 0;
189     }
190
191     if (!run_thread(&thread, thread_local_thread_cb) ||
192         !wait_for_thread(thread))
193     {
194         fprintf(stderr, "run_thread() failed\n");
195         return 0;
196     }
197
198     if (thread_local_thread_cb_ok != 1) {
199         fprintf(stderr, "thread-local thread callback failed\n");
200         return 0;
201     }
202
203 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
204
205     ptr = CRYPTO_THREAD_get_local(&thread_local_key);
206     if (ptr != NULL) {
207         fprintf(stderr, "ptr not NULL\n");
208         return 0;
209     }
210
211 # if !defined(OPENSSL_SYS_WINDOWS)
212     if (destructor_run_count != 1) {
213         fprintf(stderr, "thread-local destructor run %u times\n",
214                 destructor_run_count);
215         return 0;
216     }
217 # endif
218
219 #endif
220
221     if (!CRYPTO_THREAD_cleanup_local(&thread_local_key)) {
222         fprintf(stderr, "CRYPTO_THREAD_cleanup_local() failed\n");
223         return 0;
224     }
225
226     return 1;
227 }
228
229 int main(int argc, char **argv)
230 {
231     if (!test_lock())
232       return 1;
233
234     if (!test_once())
235       return 1;
236
237     if (!test_thread_local())
238       return 1;
239
240     printf("PASS\n");
241     return 0;
242 }