]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/asynctest.c
Security hardening: Expose Build flags for Position Independed Execution (PIE)
[thirdparty/openssl.git] / test / asynctest.c
CommitLineData
5705e050 1/*
fecb3aae 2 * Copyright 2015-2022 The OpenSSL Project Authors. All Rights Reserved.
5705e050 3 *
909f1a2e 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
440e5d80
RS
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
5705e050
MC
8 */
9
f1f5ee17
AP
10#ifdef _WIN32
11# include <windows.h>
12#endif
13
5705e050
MC
14#include <stdio.h>
15#include <string.h>
16#include <openssl/async.h>
17#include <openssl/crypto.h>
5705e050 18
5705e050
MC
19static int ctr = 0;
20static ASYNC_JOB *currjob = NULL;
f6f56f47
ACB
21static int custom_alloc_used = 0;
22static int custom_free_used = 0;
5705e050
MC
23
24static int only_pause(void *args)
25{
26 ASYNC_pause_job();
27
28 return 1;
29}
30
31static int add_two(void *args)
32{
33 ctr++;
34 ASYNC_pause_job();
35 ctr++;
36
37 return 2;
38}
39
40static int save_current(void *args)
41{
42 currjob = ASYNC_get_current_job();
43 ASYNC_pause_job();
44
45 return 1;
46}
47
92db29e5
MC
48static int change_deflt_libctx(void *args)
49{
b4250010
DMSP
50 OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new();
51 OSSL_LIB_CTX *oldctx, *tmpctx;
92db29e5
MC
52 int ret = 0;
53
54 if (libctx == NULL)
55 return 0;
56
b4250010 57 oldctx = OSSL_LIB_CTX_set0_default(libctx);
92db29e5
MC
58 ASYNC_pause_job();
59
60 /* Check the libctx is set up as we expect */
b4250010 61 tmpctx = OSSL_LIB_CTX_set0_default(oldctx);
92db29e5
MC
62 if (tmpctx != libctx)
63 goto err;
64
65 /* Set it back again to continue to use our own libctx */
b4250010 66 oldctx = OSSL_LIB_CTX_set0_default(libctx);
92db29e5
MC
67 ASYNC_pause_job();
68
69 /* Check the libctx is set up as we expect */
b4250010 70 tmpctx = OSSL_LIB_CTX_set0_default(oldctx);
92db29e5
MC
71 if (tmpctx != libctx)
72 goto err;
73
74 ret = 1;
75 err:
b4250010 76 OSSL_LIB_CTX_free(libctx);
92db29e5
MC
77 return ret;
78}
79
80
ff75a257
MC
81#define MAGIC_WAIT_FD ((OSSL_ASYNC_FD)99)
82static int waitfd(void *args)
5705e050 83{
ff75a257
MC
84 ASYNC_JOB *job;
85 ASYNC_WAIT_CTX *waitctx;
ff75a257
MC
86 job = ASYNC_get_current_job();
87 if (job == NULL)
88 return 0;
89 waitctx = ASYNC_get_wait_ctx(job);
90 if (waitctx == NULL)
91 return 0;
f44e6364
AG
92
93 /* First case: no fd added or removed */
94 ASYNC_pause_job();
95
96 /* Second case: one fd added */
e8aa8b6c 97 if (!ASYNC_WAIT_CTX_set_wait_fd(waitctx, waitctx, MAGIC_WAIT_FD, NULL, NULL))
ff75a257 98 return 0;
5705e050 99 ASYNC_pause_job();
ff75a257 100
f44e6364
AG
101 /* Third case: all fd removed */
102 if (!ASYNC_WAIT_CTX_clear_fd(waitctx, waitctx))
103 return 0;
104 ASYNC_pause_job();
105
106 /* Last case: fd added and immediately removed */
107 if (!ASYNC_WAIT_CTX_set_wait_fd(waitctx, waitctx, MAGIC_WAIT_FD, NULL, NULL))
108 return 0;
ff75a257
MC
109 if (!ASYNC_WAIT_CTX_clear_fd(waitctx, waitctx))
110 return 0;
5705e050
MC
111
112 return 1;
113}
114
e8dfb5bf
MC
115static int blockpause(void *args)
116{
117 ASYNC_block_pause();
118 ASYNC_pause_job();
119 ASYNC_unblock_pause();
120 ASYNC_pause_job();
121
122 return 1;
123}
124
3cb7c5cf 125static int test_ASYNC_init_thread(void)
5705e050
MC
126{
127 ASYNC_JOB *job1 = NULL, *job2 = NULL, *job3 = NULL;
128 int funcret1, funcret2, funcret3;
174a74ef 129 ASYNC_WAIT_CTX *waitctx = NULL;
5705e050 130
7b9f8f7f 131 if ( !ASYNC_init_thread(2, 0)
ff75a257
MC
132 || (waitctx = ASYNC_WAIT_CTX_new()) == NULL
133 || ASYNC_start_job(&job1, waitctx, &funcret1, only_pause, NULL, 0)
5705e050 134 != ASYNC_PAUSE
ff75a257 135 || ASYNC_start_job(&job2, waitctx, &funcret2, only_pause, NULL, 0)
5705e050 136 != ASYNC_PAUSE
ff75a257 137 || ASYNC_start_job(&job3, waitctx, &funcret3, only_pause, NULL, 0)
5705e050 138 != ASYNC_NO_JOBS
ff75a257 139 || ASYNC_start_job(&job1, waitctx, &funcret1, only_pause, NULL, 0)
5705e050 140 != ASYNC_FINISH
ff75a257 141 || ASYNC_start_job(&job3, waitctx, &funcret3, only_pause, NULL, 0)
5705e050 142 != ASYNC_PAUSE
ff75a257 143 || ASYNC_start_job(&job2, waitctx, &funcret2, only_pause, NULL, 0)
5705e050 144 != ASYNC_FINISH
ff75a257 145 || ASYNC_start_job(&job3, waitctx, &funcret3, only_pause, NULL, 0)
5705e050
MC
146 != ASYNC_FINISH
147 || funcret1 != 1
148 || funcret2 != 1
149 || funcret3 != 1) {
7b9f8f7f 150 fprintf(stderr, "test_ASYNC_init_thread() failed\n");
ff75a257 151 ASYNC_WAIT_CTX_free(waitctx);
7b9f8f7f 152 ASYNC_cleanup_thread();
5705e050
MC
153 return 0;
154 }
155
ff75a257 156 ASYNC_WAIT_CTX_free(waitctx);
7b9f8f7f 157 ASYNC_cleanup_thread();
5705e050
MC
158 return 1;
159}
160
9f5a87fd
PY
161static int test_callback(void *arg)
162{
163 printf("callback test pass\n");
164 return 1;
165}
166
167static int test_ASYNC_callback_status(void)
168{
169 ASYNC_WAIT_CTX *waitctx = NULL;
170 int set_arg = 100;
171 ASYNC_callback_fn get_callback;
172 void *get_arg;
173 int set_status = 1;
174
175 if ( !ASYNC_init_thread(1, 0)
176 || (waitctx = ASYNC_WAIT_CTX_new()) == NULL
177 || ASYNC_WAIT_CTX_set_callback(waitctx, test_callback, (void*)&set_arg)
178 != 1
179 || ASYNC_WAIT_CTX_get_callback(waitctx, &get_callback, &get_arg)
180 != 1
181 || test_callback != get_callback
182 || get_arg != (void*)&set_arg
183 || (*get_callback)(get_arg) != 1
184 || ASYNC_WAIT_CTX_set_status(waitctx, set_status) != 1
185 || set_status != ASYNC_WAIT_CTX_get_status(waitctx)) {
186 fprintf(stderr, "test_ASYNC_callback_status() failed\n");
187 ASYNC_WAIT_CTX_free(waitctx);
188 ASYNC_cleanup_thread();
189 return 0;
190 }
191
192 ASYNC_WAIT_CTX_free(waitctx);
193 ASYNC_cleanup_thread();
194 return 1;
195
196}
197
3cb7c5cf 198static int test_ASYNC_start_job(void)
5705e050
MC
199{
200 ASYNC_JOB *job = NULL;
201 int funcret;
174a74ef 202 ASYNC_WAIT_CTX *waitctx = NULL;
5705e050
MC
203
204 ctr = 0;
205
7b9f8f7f 206 if ( !ASYNC_init_thread(1, 0)
ff75a257
MC
207 || (waitctx = ASYNC_WAIT_CTX_new()) == NULL
208 || ASYNC_start_job(&job, waitctx, &funcret, add_two, NULL, 0)
209 != ASYNC_PAUSE
5705e050 210 || ctr != 1
ff75a257
MC
211 || ASYNC_start_job(&job, waitctx, &funcret, add_two, NULL, 0)
212 != ASYNC_FINISH
5705e050
MC
213 || ctr != 2
214 || funcret != 2) {
215 fprintf(stderr, "test_ASYNC_start_job() failed\n");
ff75a257 216 ASYNC_WAIT_CTX_free(waitctx);
7b9f8f7f 217 ASYNC_cleanup_thread();
5705e050
MC
218 return 0;
219 }
220
ff75a257 221 ASYNC_WAIT_CTX_free(waitctx);
7b9f8f7f 222 ASYNC_cleanup_thread();
5705e050
MC
223 return 1;
224}
225
3cb7c5cf 226static int test_ASYNC_get_current_job(void)
5705e050
MC
227{
228 ASYNC_JOB *job = NULL;
229 int funcret;
174a74ef 230 ASYNC_WAIT_CTX *waitctx = NULL;
5705e050
MC
231
232 currjob = NULL;
233
7b9f8f7f 234 if ( !ASYNC_init_thread(1, 0)
ff75a257
MC
235 || (waitctx = ASYNC_WAIT_CTX_new()) == NULL
236 || ASYNC_start_job(&job, waitctx, &funcret, save_current, NULL, 0)
5705e050
MC
237 != ASYNC_PAUSE
238 || currjob != job
ff75a257 239 || ASYNC_start_job(&job, waitctx, &funcret, save_current, NULL, 0)
5705e050
MC
240 != ASYNC_FINISH
241 || funcret != 1) {
242 fprintf(stderr, "test_ASYNC_get_current_job() failed\n");
ff75a257 243 ASYNC_WAIT_CTX_free(waitctx);
7b9f8f7f 244 ASYNC_cleanup_thread();
5705e050
MC
245 return 0;
246 }
247
ff75a257 248 ASYNC_WAIT_CTX_free(waitctx);
7b9f8f7f 249 ASYNC_cleanup_thread();
5705e050
MC
250 return 1;
251}
252
3cb7c5cf 253static int test_ASYNC_WAIT_CTX_get_all_fds(void)
5705e050
MC
254{
255 ASYNC_JOB *job = NULL;
2b2c78d4 256 int funcret;
174a74ef 257 ASYNC_WAIT_CTX *waitctx = NULL;
ff75a257
MC
258 OSSL_ASYNC_FD fd = OSSL_BAD_ASYNC_FD, delfd = OSSL_BAD_ASYNC_FD;
259 size_t numfds, numdelfds;
5705e050 260
7b9f8f7f 261 if ( !ASYNC_init_thread(1, 0)
ff75a257
MC
262 || (waitctx = ASYNC_WAIT_CTX_new()) == NULL
263 /* On first run we're not expecting any wait fds */
264 || ASYNC_start_job(&job, waitctx, &funcret, waitfd, NULL, 0)
5705e050 265 != ASYNC_PAUSE
ff75a257
MC
266 || !ASYNC_WAIT_CTX_get_all_fds(waitctx, NULL, &numfds)
267 || numfds != 0
268 || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, NULL,
269 &numdelfds)
270 || numfds != 0
271 || numdelfds != 0
272 /* On second run we're expecting one added fd */
273 || ASYNC_start_job(&job, waitctx, &funcret, waitfd, NULL, 0)
5705e050 274 != ASYNC_PAUSE
ff75a257
MC
275 || !ASYNC_WAIT_CTX_get_all_fds(waitctx, NULL, &numfds)
276 || numfds != 1
277 || !ASYNC_WAIT_CTX_get_all_fds(waitctx, &fd, &numfds)
278 || fd != MAGIC_WAIT_FD
279 || (fd = OSSL_BAD_ASYNC_FD, 0) /* Assign to something else */
280 || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, NULL,
f44e6364 281 &numdelfds)
ff75a257
MC
282 || numfds != 1
283 || numdelfds != 0
284 || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, &fd, &numfds, NULL,
285 &numdelfds)
286 || fd != MAGIC_WAIT_FD
f44e6364 287 /* On third run we expect one deleted fd */
ff75a257 288 || ASYNC_start_job(&job, waitctx, &funcret, waitfd, NULL, 0)
f44e6364 289 != ASYNC_PAUSE
ff75a257
MC
290 || !ASYNC_WAIT_CTX_get_all_fds(waitctx, NULL, &numfds)
291 || numfds != 0
292 || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, NULL,
293 &numdelfds)
294 || numfds != 0
295 || numdelfds != 1
296 || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, &delfd,
297 &numdelfds)
298 || delfd != MAGIC_WAIT_FD
f44e6364
AG
299 /* On last run we are not expecting any wait fd */
300 || ASYNC_start_job(&job, waitctx, &funcret, waitfd, NULL, 0)
301 != ASYNC_FINISH
302 || !ASYNC_WAIT_CTX_get_all_fds(waitctx, NULL, &numfds)
303 || numfds != 0
304 || !ASYNC_WAIT_CTX_get_changed_fds(waitctx, NULL, &numfds, NULL,
305 &numdelfds)
306 || numfds != 0
307 || numdelfds != 0
5705e050
MC
308 || funcret != 1) {
309 fprintf(stderr, "test_ASYNC_get_wait_fd() failed\n");
ff75a257 310 ASYNC_WAIT_CTX_free(waitctx);
7b9f8f7f 311 ASYNC_cleanup_thread();
5705e050
MC
312 return 0;
313 }
314
ff75a257 315 ASYNC_WAIT_CTX_free(waitctx);
7b9f8f7f 316 ASYNC_cleanup_thread();
5705e050
MC
317 return 1;
318}
e8dfb5bf 319
3cb7c5cf 320static int test_ASYNC_block_pause(void)
e8dfb5bf
MC
321{
322 ASYNC_JOB *job = NULL;
323 int funcret;
174a74ef 324 ASYNC_WAIT_CTX *waitctx = NULL;
e8dfb5bf 325
7b9f8f7f 326 if ( !ASYNC_init_thread(1, 0)
ff75a257
MC
327 || (waitctx = ASYNC_WAIT_CTX_new()) == NULL
328 || ASYNC_start_job(&job, waitctx, &funcret, blockpause, NULL, 0)
e8dfb5bf 329 != ASYNC_PAUSE
ff75a257 330 || ASYNC_start_job(&job, waitctx, &funcret, blockpause, NULL, 0)
e8dfb5bf
MC
331 != ASYNC_FINISH
332 || funcret != 1) {
333 fprintf(stderr, "test_ASYNC_block_pause() failed\n");
ff75a257 334 ASYNC_WAIT_CTX_free(waitctx);
7b9f8f7f 335 ASYNC_cleanup_thread();
e8dfb5bf
MC
336 return 0;
337 }
338
ff75a257 339 ASYNC_WAIT_CTX_free(waitctx);
7b9f8f7f 340 ASYNC_cleanup_thread();
e8dfb5bf
MC
341 return 1;
342}
343
d8652be0 344static int test_ASYNC_start_job_ex(void)
92db29e5
MC
345{
346 ASYNC_JOB *job = NULL;
347 int funcret;
348 ASYNC_WAIT_CTX *waitctx = NULL;
b4250010
DMSP
349 OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new();
350 OSSL_LIB_CTX *oldctx, *tmpctx, *globalctx;
92db29e5
MC
351 int ret = 0;
352
353 if (libctx == NULL) {
354 fprintf(stderr,
d8652be0 355 "test_ASYNC_start_job_ex() failed to create libctx\n");
92db29e5
MC
356 goto err;
357 }
358
b4250010 359 globalctx = oldctx = OSSL_LIB_CTX_set0_default(libctx);
92db29e5
MC
360
361 if ((waitctx = ASYNC_WAIT_CTX_new()) == NULL
362 || ASYNC_start_job(&job, waitctx, &funcret, change_deflt_libctx,
363 NULL, 0)
364 != ASYNC_PAUSE) {
365 fprintf(stderr,
d8652be0 366 "test_ASYNC_start_job_ex() failed to start job\n");
92db29e5
MC
367 goto err;
368 }
369
370 /* Reset the libctx temporarily to find out what it is*/
b4250010
DMSP
371 tmpctx = OSSL_LIB_CTX_set0_default(oldctx);
372 oldctx = OSSL_LIB_CTX_set0_default(tmpctx);
92db29e5
MC
373 if (tmpctx != libctx) {
374 fprintf(stderr,
d8652be0 375 "test_ASYNC_start_job_ex() failed - unexpected libctx\n");
92db29e5
MC
376 goto err;
377 }
378
379 if (ASYNC_start_job(&job, waitctx, &funcret, change_deflt_libctx, NULL, 0)
380 != ASYNC_PAUSE) {
381 fprintf(stderr,
d8652be0 382 "test_ASYNC_start_job_ex() - restarting job failed\n");
92db29e5
MC
383 goto err;
384 }
385
386 /* Reset the libctx and continue with the global default libctx */
b4250010 387 tmpctx = OSSL_LIB_CTX_set0_default(oldctx);
92db29e5
MC
388 if (tmpctx != libctx) {
389 fprintf(stderr,
d8652be0 390 "test_ASYNC_start_job_ex() failed - unexpected libctx\n");
92db29e5
MC
391 goto err;
392 }
393
394 if (ASYNC_start_job(&job, waitctx, &funcret, change_deflt_libctx, NULL, 0)
395 != ASYNC_FINISH
396 || funcret != 1) {
397 fprintf(stderr,
d8652be0 398 "test_ASYNC_start_job_ex() - finishing job failed\n");
92db29e5
MC
399 goto err;
400 }
401
402 /* Reset the libctx temporarily to find out what it is*/
b4250010
DMSP
403 tmpctx = OSSL_LIB_CTX_set0_default(libctx);
404 OSSL_LIB_CTX_set0_default(tmpctx);
92db29e5
MC
405 if (tmpctx != globalctx) {
406 fprintf(stderr,
d8652be0 407 "test_ASYNC_start_job_ex() failed - global libctx check failed\n");
92db29e5
MC
408 goto err;
409 }
410
411 ret = 1;
412 err:
413 ASYNC_WAIT_CTX_free(waitctx);
c5d06129 414 ASYNC_cleanup_thread();
b4250010 415 OSSL_LIB_CTX_free(libctx);
92db29e5
MC
416 return ret;
417}
418
f6f56f47
ACB
419static void *test_alloc_stack(size_t *num)
420{
421 custom_alloc_used = 1;
422 return OPENSSL_malloc(*num);
423}
424
425static void test_free_stack(void *addr)
426{
427 custom_free_used = 1;
428 OPENSSL_free(addr);
429}
430
431static int test_ASYNC_set_mem_functions(void)
432{
433 ASYNC_stack_alloc_fn alloc_fn;
434 ASYNC_stack_free_fn free_fn;
435
436 /* Not all platforms support this */
437 if (ASYNC_set_mem_functions(test_alloc_stack, test_free_stack) == 0) return 1;
438
439 ASYNC_get_mem_functions(&alloc_fn, &free_fn);
440
441 if ((alloc_fn != test_alloc_stack) || (free_fn != test_free_stack)) {
442 fprintf(stderr,
443 "test_ASYNC_set_mem_functions() - setting and retrieving custom allocators failed\n");
444 return 0;
445 }
446
447 if (!ASYNC_init_thread(1, 1)) {
448 fprintf(stderr,
449 "test_ASYNC_set_mem_functions() - failed initialising ctx pool\n");
450 return 0;
451 }
452 ASYNC_cleanup_thread();
453
454 if (!custom_alloc_used || !custom_free_used) {
455 fprintf(stderr,
456 "test_ASYNC_set_mem_functions() - custom allocation functions not used\n");
457
458 return 0;
459 }
460
461 return 1;
462}
463
5705e050
MC
464int main(int argc, char **argv)
465{
c521edc3
MC
466 if (!ASYNC_is_capable()) {
467 fprintf(stderr,
468 "OpenSSL build is not ASYNC capable - skipping async tests\n");
469 } else {
742ccab3 470 if (!test_ASYNC_init_thread()
9f5a87fd 471 || !test_ASYNC_callback_status()
c521edc3
MC
472 || !test_ASYNC_start_job()
473 || !test_ASYNC_get_current_job()
474 || !test_ASYNC_WAIT_CTX_get_all_fds()
92db29e5 475 || !test_ASYNC_block_pause()
f6f56f47
ACB
476 || !test_ASYNC_start_job_ex()
477 || !test_ASYNC_set_mem_functions()) {
c521edc3
MC
478 return 1;
479 }
5705e050 480 }
5705e050
MC
481 printf("PASS\n");
482 return 0;
483}