1 /*
2 * Copyright 2024-2025 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 #include <openssl/lhash.h>
10 #include <assert.h>
11
12 #include "internal/quic_engine.h"
13 #include "internal/quic_channel.h"
14 #include "internal/quic_ssl.h"
15 #include "internal/quic_error.h"
16
17 /*
18 * RADIX 6D QUIC Test Framework
19 * =============================================================================
20 *
21 * The radix test framework is a six-dimension script-driven facility to support
22 * execution of
23 *
24 * multi-stream
25 * multi-client
26 * multi-server
27 * multi-thread
28 * multi-process
29 * multi-node
30 *
31 * test vignettes for QUIC. Unlike the older multistream test framework, it does
32 * not assume a single client and a single server. Examples of vignettes
33 * designed to be supported by the radix test framework in future include:
34 *
35 * single client <-> single server
36 * multiple clients <-> single server
37 * single client <-> multiple servers
38 * multiple clients <-> multiple servers
39 *
40 * 'Multi-process' and 'multi-node' means there has been some consideration
41 * given to support of multi-process and multi-node testing in the future,
42 * though this is not currently supported.
43 */
44
45 /*
46 * An object is something associated with a name in the process-level state. The
47 * process-level state primarily revolves around a global dictionary of SSL
48 * objects.
49 */
50 typedef struct radix_obj_st {
51 char *name; /* owned, zero-terminated */
52 SSL *ssl; /* owns one reference */
53 unsigned int registered : 1; /* in LHASH? */
54 unsigned int active : 1; /* tick? */
55 } RADIX_OBJ;
56
57 DEFINE_LHASH_OF_EX(RADIX_OBJ);
58
59 /* Process-level state (i.e. "globals" in the normal sense of the word) */
60 typedef struct radix_process_st {
61 size_t node_idx;
62 size_t process_idx;
63 size_t next_thread_idx;
64 STACK_OF(RADIX_THREAD) *threads;
65
66 /* Process-global state. */
67 CRYPTO_MUTEX *gm; /* global mutex */
68 LHASH_OF(RADIX_OBJ) *objs; /* protected by gm */
69 OSSL_TIME time_slip; /* protected by gm */
70 BIO *keylog_out; /* protected by gm */
71
72 int done_join_all_threads;
73
74 /*
75 * Valid if done_join_all threads. Logical AND of all child worker results.
76 */
77 int thread_composite_testresult;
78 } RADIX_PROCESS;
79
80 #define NUM_SLOTS 8
81
82 /* Thread-level state within a process */
83 typedef struct radix_thread_st {
84 RADIX_PROCESS *rp;
85 CRYPTO_THREAD *t;
86 unsigned char *tmp_buf;
87 size_t tmp_buf_offset;
88 size_t thread_idx; /* 0=main thread */
89 RADIX_OBJ *slot[NUM_SLOTS];
90 SSL *ssl[NUM_SLOTS];
91
92 /* child thread spawn arguments */
93 SCRIPT_INFO *child_script_info;
94 BIO *debug_bio;
95
96 /* m protects all of the below values */
97 CRYPTO_MUTEX *m;
98 int done;
99 int testresult; /* valid if done */
100
101 uint64_t scratch0;
102 } RADIX_THREAD;
103
DEFINE_STACK_OF(RADIX_THREAD)104 DEFINE_STACK_OF(RADIX_THREAD)
105
106 /* ssl reference is transferred. name is copied and is required. */
107 static RADIX_OBJ *RADIX_OBJ_new(const char *name, SSL *ssl)
108 {
109 RADIX_OBJ *obj;
110
111 if (!TEST_ptr(name) || !TEST_ptr(ssl))
112 return NULL;
113
114 if (!TEST_ptr(obj = OPENSSL_zalloc(sizeof(*obj))))
115 return NULL;
116
117 if (!TEST_ptr(obj->name = OPENSSL_strdup(name))) {
118 OPENSSL_free(obj);
119 return NULL;
120 }
121
122 obj->ssl = ssl;
123 return obj;
124 }
125
RADIX_OBJ_free(RADIX_OBJ * obj)126 static void RADIX_OBJ_free(RADIX_OBJ *obj)
127 {
128 if (obj == NULL)
129 return;
130
131 assert(!obj->registered);
132
133 SSL_free(obj->ssl);
134 OPENSSL_free(obj->name);
135 OPENSSL_free(obj);
136 }
137
RADIX_OBJ_hash(const RADIX_OBJ * obj)138 static unsigned long RADIX_OBJ_hash(const RADIX_OBJ *obj)
139 {
140 return OPENSSL_LH_strhash(obj->name);
141 }
142
RADIX_OBJ_cmp(const RADIX_OBJ * a,const RADIX_OBJ * b)143 static int RADIX_OBJ_cmp(const RADIX_OBJ *a, const RADIX_OBJ *b)
144 {
145 return strcmp(a->name, b->name);
146 }
147
RADIX_PROCESS_init(RADIX_PROCESS * rp,size_t node_idx,size_t process_idx)148 static int RADIX_PROCESS_init(RADIX_PROCESS *rp, size_t node_idx, size_t process_idx)
149 {
150 const char *keylog_path;
151
152 #if defined(OPENSSL_THREADS)
153 if (!TEST_ptr(rp->gm = ossl_crypto_mutex_new()))
154 goto err;
155 #endif
156
157 if (!TEST_ptr(rp->objs = lh_RADIX_OBJ_new(RADIX_OBJ_hash, RADIX_OBJ_cmp)))
158 goto err;
159
160 if (!TEST_ptr(rp->threads = sk_RADIX_THREAD_new(NULL)))
161 goto err;
162
163 rp->keylog_out = NULL;
164 keylog_path = ossl_safe_getenv("SSLKEYLOGFILE");
165 if (keylog_path != NULL && *keylog_path != '\0'
166 && !TEST_ptr(rp->keylog_out = BIO_new_file(keylog_path, "a")))
167 goto err;
168
169 rp->node_idx = node_idx;
170 rp->process_idx = process_idx;
171 rp->done_join_all_threads = 0;
172 rp->next_thread_idx = 0;
173 return 1;
174
175 err:
176 lh_RADIX_OBJ_free(rp->objs);
177 rp->objs = NULL;
178 ossl_crypto_mutex_free(&rp->gm);
179 return 0;
180 }
181
stream_state_to_str(int state)182 static const char *stream_state_to_str(int state)
183 {
184 switch (state) {
185 case SSL_STREAM_STATE_NONE:
186 return "none";
187 case SSL_STREAM_STATE_OK:
188 return "OK";
189 case SSL_STREAM_STATE_WRONG_DIR:
190 return "wrong-dir";
191 case SSL_STREAM_STATE_FINISHED:
192 return "finished";
193 case SSL_STREAM_STATE_RESET_LOCAL:
194 return "reset-local";
195 case SSL_STREAM_STATE_RESET_REMOTE:
196 return "reset-remote";
197 case SSL_STREAM_STATE_CONN_CLOSED:
198 return "conn-closed";
199 default:
200 return "?";
201 }
202 }
203
report_ssl_state(BIO * bio,const char * pfx,int is_write,int state,uint64_t ec)204 static void report_ssl_state(BIO *bio, const char *pfx, int is_write,
205 int state, uint64_t ec)
206 {
207 const char *state_s = stream_state_to_str(state);
208
209 BIO_printf(bio, "%s%-15s%s(%d)", pfx, is_write ? "Write state: " : "Read state: ",
210 state_s, state);
211 if (ec != UINT64_MAX)
212 BIO_printf(bio, ", %llu", (unsigned long long)ec);
213 BIO_printf(bio, "\n");
214 }
215
report_ssl(SSL * ssl,BIO * bio,const char * pfx)216 static void report_ssl(SSL *ssl, BIO *bio, const char *pfx)
217 {
218 const char *type = "SSL";
219 int is_quic = SSL_is_quic(ssl), is_conn = 0, is_listener = 0;
220 SSL_CONN_CLOSE_INFO cc_info = { 0 };
221 const char *e_str, *f_str;
222
223 if (is_quic) {
224 is_conn = SSL_is_connection(ssl);
225 is_listener = SSL_is_listener(ssl);
226
227 if (is_listener)
228 type = "QLSO";
229 else if (is_conn)
230 type = "QCSO";
231 else
232 type = "QSSO";
233 }
234
235 BIO_printf(bio, "%sType: %s\n", pfx, type);
236
237 if (is_quic && is_conn
238 && SSL_get_conn_close_info(ssl, &cc_info, sizeof(cc_info))) {
239
240 e_str = ossl_quic_err_to_string(cc_info.error_code);
241 f_str = ossl_quic_frame_type_to_string(cc_info.frame_type);
242
243 if (e_str == NULL)
244 e_str = "?";
245 if (f_str == NULL)
246 f_str = "?";
247
248 BIO_printf(bio, "%sConnection is closed: %s(%llu)/%s(%llu), "
249 "%s, %s, reason: \"%s\"\n",
250 pfx,
251 e_str,
252 (unsigned long long)cc_info.error_code,
253 f_str,
254 (unsigned long long)cc_info.frame_type,
255 (cc_info.flags & SSL_CONN_CLOSE_FLAG_LOCAL) != 0
256 ? "local"
257 : "remote",
258 (cc_info.flags & SSL_CONN_CLOSE_FLAG_TRANSPORT) != 0
259 ? "transport"
260 : "app",
261 cc_info.reason != NULL ? cc_info.reason : "-");
262 }
263
264 if (is_quic && !is_listener) {
265 uint64_t stream_id = SSL_get_stream_id(ssl), rec, wec;
266 int rstate, wstate;
267
268 if (stream_id != UINT64_MAX)
269 BIO_printf(bio, "%sStream ID: %llu\n", pfx,
270 (unsigned long long)stream_id);
271
272 rstate = SSL_get_stream_read_state(ssl);
273 wstate = SSL_get_stream_write_state(ssl);
274
275 if (SSL_get_stream_read_error_code(ssl, &rec) != 1)
276 rec = UINT64_MAX;
277
278 if (SSL_get_stream_write_error_code(ssl, &wec) != 1)
279 wec = UINT64_MAX;
280
281 report_ssl_state(bio, pfx, 0, rstate, rec);
282 report_ssl_state(bio, pfx, 1, wstate, wec);
283 }
284 }
285
report_obj(RADIX_OBJ * obj,void * arg)286 static void report_obj(RADIX_OBJ *obj, void *arg)
287 {
288 BIO *bio = arg;
289 SSL *ssl = obj->ssl;
290
291 BIO_printf(bio, " - %-16s @ %p\n", obj->name, (void *)obj->ssl);
292 ERR_set_mark();
293 report_ssl(ssl, bio, " ");
294 ERR_pop_to_mark();
295 }
296
RADIX_THREAD_report_state(RADIX_THREAD * rt,BIO * bio)297 static void RADIX_THREAD_report_state(RADIX_THREAD *rt, BIO *bio)
298 {
299 size_t i;
300
301 BIO_printf(bio, " Slots:\n");
302 for (i = 0; i < NUM_SLOTS; ++i)
303 if (rt->slot[i] == NULL)
304 BIO_printf(bio, " %3zu) <NULL>\n", i);
305 else
306 BIO_printf(bio, " %3zu) '%s' (SSL: %p)\n", i,
307 rt->slot[i]->name,
308 (void *)rt->ssl[i]);
309 }
310
RADIX_PROCESS_report_state(RADIX_PROCESS * rp,BIO * bio,int verbose)311 static void RADIX_PROCESS_report_state(RADIX_PROCESS *rp, BIO *bio,
312 int verbose)
313 {
314 BIO_printf(bio, "Final process state for node %zu, process %zu:\n",
315 rp->node_idx, rp->process_idx);
316
317 BIO_printf(bio, " Threads (incl. main): %zu\n",
318 rp->next_thread_idx);
319 BIO_printf(bio, " Time slip: %llu ms\n",
320 (unsigned long long)ossl_time2ms(rp->time_slip));
321
322 BIO_printf(bio, " Objects:\n");
323 lh_RADIX_OBJ_doall_arg(rp->objs, report_obj, bio);
324
325 if (verbose)
326 RADIX_THREAD_report_state(sk_RADIX_THREAD_value(rp->threads, 0),
327 bio_err);
328
329 BIO_printf(bio, "\n==========================================="
330 "===========================\n");
331 }
332
RADIX_PROCESS_report_thread_results(RADIX_PROCESS * rp,BIO * bio)333 static void RADIX_PROCESS_report_thread_results(RADIX_PROCESS *rp, BIO *bio)
334 {
335 size_t i;
336 RADIX_THREAD *rt;
337 char *p;
338 long l;
339 char pfx_buf[64];
340 int rt_testresult;
341
342 for (i = 1; i < (size_t)sk_RADIX_THREAD_num(rp->threads); ++i) {
343 rt = sk_RADIX_THREAD_value(rp->threads, i);
344
345 ossl_crypto_mutex_lock(rt->m);
346 assert(rt->done);
347 rt_testresult = rt->testresult;
348 ossl_crypto_mutex_unlock(rt->m);
349
350 BIO_printf(bio, "\n====(n%zu/p%zu/t%zu)============================"
351 "===========================\n"
352 "Result for child thread with index %zu:\n",
353 rp->node_idx, rp->process_idx, rt->thread_idx, rt->thread_idx);
354
355 BIO_snprintf(pfx_buf, sizeof(pfx_buf), "# -T-%2zu:\t# ", rt->thread_idx);
356 BIO_set_prefix(bio_err, pfx_buf);
357
358 l = BIO_get_mem_data(rt->debug_bio, &p);
359 BIO_write(bio, p, l);
360 BIO_printf(bio, "\n");
361 BIO_set_prefix(bio_err, "# ");
362 BIO_printf(bio, "==> Child thread with index %zu exited with %d\n",
363 rt->thread_idx, rt_testresult);
364 if (!rt_testresult)
365 RADIX_THREAD_report_state(rt, bio);
366 }
367
368 BIO_printf(bio, "\n==========================================="
369 "===========================\n");
370 }
371
372 static int RADIX_THREAD_join(RADIX_THREAD *rt);
373
RADIX_PROCESS_join_all_threads(RADIX_PROCESS * rp,int * testresult)374 static int RADIX_PROCESS_join_all_threads(RADIX_PROCESS *rp, int *testresult)
375 {
376 int ok = 1;
377 size_t i;
378 RADIX_THREAD *rt;
379 int composite_testresult = 1;
380
381 if (rp->done_join_all_threads) {
382 *testresult = rp->thread_composite_testresult;
383 return 1;
384 }
385
386 for (i = 1; i < (size_t)sk_RADIX_THREAD_num(rp->threads); ++i) {
387 rt = sk_RADIX_THREAD_value(rp->threads, i);
388
389 BIO_printf(bio_err, "==> Joining thread %zu\n", i);
390
391 if (!TEST_true(RADIX_THREAD_join(rt)))
392 ok = 0;
393
394 if (!rt->testresult)
395 composite_testresult = 0;
396 }
397
398 rp->thread_composite_testresult = composite_testresult;
399 *testresult = composite_testresult;
400 rp->done_join_all_threads = 1;
401
402 RADIX_PROCESS_report_thread_results(rp, bio_err);
403 return ok;
404 }
405
cleanup_one(RADIX_OBJ * obj)406 static void cleanup_one(RADIX_OBJ *obj)
407 {
408 obj->registered = 0;
409 RADIX_OBJ_free(obj);
410 }
411
412 static void RADIX_THREAD_free(RADIX_THREAD *rt);
413
RADIX_PROCESS_cleanup(RADIX_PROCESS * rp)414 static void RADIX_PROCESS_cleanup(RADIX_PROCESS *rp)
415 {
416 size_t i;
417
418 assert(rp->done_join_all_threads);
419
420 for (i = 0; i < (size_t)sk_RADIX_THREAD_num(rp->threads); ++i)
421 RADIX_THREAD_free(sk_RADIX_THREAD_value(rp->threads, i));
422
423 sk_RADIX_THREAD_free(rp->threads);
424 rp->threads = NULL;
425
426 lh_RADIX_OBJ_doall(rp->objs, cleanup_one);
427 lh_RADIX_OBJ_free(rp->objs);
428 rp->objs = NULL;
429
430 BIO_free_all(rp->keylog_out);
431 rp->keylog_out = NULL;
432 ossl_crypto_mutex_free(&rp->gm);
433 }
434
RADIX_PROCESS_get_obj(RADIX_PROCESS * rp,const char * name)435 static RADIX_OBJ *RADIX_PROCESS_get_obj(RADIX_PROCESS *rp, const char *name)
436 {
437 RADIX_OBJ key;
438
439 key.name = (char *)name;
440 return lh_RADIX_OBJ_retrieve(rp->objs, &key);
441 }
442
RADIX_PROCESS_set_obj(RADIX_PROCESS * rp,const char * name,RADIX_OBJ * obj)443 static int RADIX_PROCESS_set_obj(RADIX_PROCESS *rp,
444 const char *name, RADIX_OBJ *obj)
445 {
446 RADIX_OBJ *existing;
447
448 if (obj != NULL && !TEST_false(obj->registered))
449 return 0;
450
451 existing = RADIX_PROCESS_get_obj(rp, name);
452 if (existing != NULL && obj != existing) {
453 if (!TEST_true(existing->registered))
454 return 0;
455
456 lh_RADIX_OBJ_delete(rp->objs, existing);
457 existing->registered = 0;
458 RADIX_OBJ_free(existing);
459 }
460
461 if (obj != NULL) {
462 lh_RADIX_OBJ_insert(rp->objs, obj);
463 obj->registered = 1;
464 }
465
466 return 1;
467 }
468
RADIX_PROCESS_set_ssl(RADIX_PROCESS * rp,const char * name,SSL * ssl)469 static int RADIX_PROCESS_set_ssl(RADIX_PROCESS *rp, const char *name, SSL *ssl)
470 {
471 RADIX_OBJ *obj;
472
473 if (!TEST_ptr(obj = RADIX_OBJ_new(name, ssl)))
474 return 0;
475
476 if (!TEST_true(RADIX_PROCESS_set_obj(rp, name, obj))) {
477 RADIX_OBJ_free(obj);
478 return 0;
479 }
480
481 return 1;
482 }
483
RADIX_PROCESS_get_ssl(RADIX_PROCESS * rp,const char * name)484 static SSL *RADIX_PROCESS_get_ssl(RADIX_PROCESS *rp, const char *name)
485 {
486 RADIX_OBJ *obj = RADIX_PROCESS_get_obj(rp, name);
487
488 if (obj == NULL)
489 return NULL;
490
491 return obj->ssl;
492 }
493
RADIX_THREAD_new(RADIX_PROCESS * rp)494 static RADIX_THREAD *RADIX_THREAD_new(RADIX_PROCESS *rp)
495 {
496 RADIX_THREAD *rt;
497
498 if (!TEST_ptr(rp)
499 || !TEST_ptr(rt = OPENSSL_zalloc(sizeof(*rt))))
500 return 0;
501
502 rt->rp = rp;
503
504 #if defined(OPENSSL_THREADS)
505 if (!TEST_ptr(rt->m = ossl_crypto_mutex_new())) {
506 OPENSSL_free(rt);
507 return 0;
508 }
509 #endif
510
511 if (!TEST_true(sk_RADIX_THREAD_push(rp->threads, rt))) {
512 OPENSSL_free(rt);
513 return 0;
514 }
515
516 rt->thread_idx = rp->next_thread_idx++;
517 assert(rt->thread_idx + 1 == (size_t)sk_RADIX_THREAD_num(rp->threads));
518 return rt;
519 }
520
RADIX_THREAD_free(RADIX_THREAD * rt)521 static void RADIX_THREAD_free(RADIX_THREAD *rt)
522 {
523 if (rt == NULL)
524 return;
525
526 assert(rt->t == NULL);
527 BIO_free_all(rt->debug_bio);
528 OPENSSL_free(rt->tmp_buf);
529 ossl_crypto_mutex_free(&rt->m);
530 OPENSSL_free(rt);
531 }
532
RADIX_THREAD_join(RADIX_THREAD * rt)533 static int RADIX_THREAD_join(RADIX_THREAD *rt)
534 {
535 CRYPTO_THREAD_RETVAL rv;
536
537 if (rt->t != NULL)
538 ossl_crypto_thread_native_join(rt->t, &rv);
539
540 ossl_crypto_thread_native_clean(rt->t);
541 rt->t = NULL;
542
543 if (!TEST_true(rt->done))
544 return 0;
545
546 return 1;
547 }
548
549 static RADIX_PROCESS radix_process;
550 static CRYPTO_THREAD_LOCAL radix_thread;
551
radix_thread_cleanup_tl(void * p)552 static void radix_thread_cleanup_tl(void *p)
553 {
554 /* Should already have been cleaned up. */
555 if (!TEST_ptr_null(p))
556 abort();
557 }
558
radix_get_thread(void)559 static RADIX_THREAD *radix_get_thread(void)
560 {
561 return CRYPTO_THREAD_get_local(&radix_thread);
562 }
563
radix_thread_init(RADIX_THREAD * rt)564 static int radix_thread_init(RADIX_THREAD *rt)
565 {
566 if (!TEST_ptr(rt)
567 || !TEST_ptr_null(CRYPTO_THREAD_get_local(&radix_thread)))
568 return 0;
569
570 if (!TEST_true(CRYPTO_THREAD_set_local(&radix_thread, rt)))
571 return 0;
572
573 set_override_bio_out(rt->debug_bio);
574 set_override_bio_err(rt->debug_bio);
575 return 1;
576 }
577
radix_thread_cleanup(void)578 static void radix_thread_cleanup(void)
579 {
580 RADIX_THREAD *rt = radix_get_thread();
581
582 if (!TEST_ptr(rt))
583 return;
584
585 if (!TEST_true(CRYPTO_THREAD_set_local(&radix_thread, NULL)))
586 return;
587 }
588
bindings_process_init(size_t node_idx,size_t process_idx)589 static int bindings_process_init(size_t node_idx, size_t process_idx)
590 {
591 RADIX_THREAD *rt;
592
593 if (!TEST_true(RADIX_PROCESS_init(&radix_process, node_idx, process_idx)))
594 return 0;
595
596 if (!TEST_true(CRYPTO_THREAD_init_local(&radix_thread,
597 radix_thread_cleanup_tl)))
598 return 0;
599
600 if (!TEST_ptr(rt = RADIX_THREAD_new(&radix_process)))
601 return 0;
602
603 /* Allocate structures for main thread. */
604 return radix_thread_init(rt);
605 }
606
bindings_process_finish(int testresult_main)607 static int bindings_process_finish(int testresult_main)
608 {
609 int testresult, testresult_child;
610
611 if (!TEST_true(RADIX_PROCESS_join_all_threads(&radix_process,
612 &testresult_child)))
613 return 0;
614
615 testresult = testresult_main && testresult_child;
616 RADIX_PROCESS_report_state(&radix_process, bio_err,
617 /*verbose=*/!testresult);
618 radix_thread_cleanup(); /* cleanup main thread */
619 RADIX_PROCESS_cleanup(&radix_process);
620
621 if (testresult)
622 BIO_printf(bio_err, "==> OK\n\n");
623 else
624 BIO_printf(bio_err, "==> ERROR (main=%d, children=%d)\n\n",
625 testresult_main, testresult_child);
626
627 return testresult;
628 }
629
630 #define RP() (&radix_process)
631 #define RT() (radix_get_thread())
632
get_time(void * arg)633 static OSSL_TIME get_time(void *arg)
634 {
635 OSSL_TIME time_slip;
636
637 ossl_crypto_mutex_lock(RP()->gm);
638 time_slip = RP()->time_slip;
639 ossl_crypto_mutex_unlock(RP()->gm);
640
641 return ossl_time_add(ossl_time_now(), time_slip);
642 }
643
radix_skip_time(OSSL_TIME t)644 ossl_unused static void radix_skip_time(OSSL_TIME t)
645 {
646 ossl_crypto_mutex_lock(RP()->gm);
647 RP()->time_slip = ossl_time_add(RP()->time_slip, t);
648 ossl_crypto_mutex_unlock(RP()->gm);
649 }
650
per_op_tick_obj(RADIX_OBJ * obj)651 static void per_op_tick_obj(RADIX_OBJ *obj)
652 {
653 if (obj->active)
654 SSL_handle_events(obj->ssl);
655 }
656
do_per_op(TERP * terp,void * arg)657 static int do_per_op(TERP *terp, void *arg)
658 {
659 lh_RADIX_OBJ_doall(RP()->objs, per_op_tick_obj);
660 return 1;
661 }
662
bindings_adjust_terp_config(TERP_CONFIG * cfg)663 static int bindings_adjust_terp_config(TERP_CONFIG *cfg)
664 {
665 cfg->now_cb = get_time;
666 cfg->per_op_cb = do_per_op;
667 return 1;
668 }
669
expect_slot_ssl(FUNC_CTX * fctx,size_t idx,SSL ** p_ssl)670 static int expect_slot_ssl(FUNC_CTX *fctx, size_t idx, SSL **p_ssl)
671 {
672 if (!TEST_size_t_lt(idx, NUM_SLOTS)
673 || !TEST_ptr(*p_ssl = RT()->ssl[idx]))
674 return 0;
675
676 return 1;
677 }
678
679 #define REQUIRE_SSL_N(idx, ssl) \
680 do { \
681 (ssl) = NULL; /* quiet uninitialized warnings */ \
682 if (!TEST_true(expect_slot_ssl(fctx, (idx), &(ssl)))) \
683 goto err; \
684 } while (0)
685 #define REQUIRE_SSL(ssl) REQUIRE_SSL_N(0, (ssl))
686
687 #define REQUIRE_SSL_2(a, b) \
688 do { \
689 REQUIRE_SSL_N(0, (a)); \
690 REQUIRE_SSL_N(1, (b)); \
691 } while (0)
692
693 #define REQUIRE_SSL_3(a, b, c) \
694 do { \
695 REQUIRE_SSL_N(0, (a)); \
696 REQUIRE_SSL_N(1, (b)); \
697 REQUIRE_SSL_N(2, (c)); \
698 } while (0)
699
700 #define REQUIRE_SSL_4(a, b, c, d) \
701 do { \
702 REQUIRE_SSL_N(0, (a)); \
703 REQUIRE_SSL_N(1, (b)); \
704 REQUIRE_SSL_N(2, (c)); \
705 REQUIRE_SSL_N(3, (d)); \
706 } while (0)
707
708 #define REQUIRE_SSL_5(a, b, c, d, e) \
709 do { \
710 REQUIRE_SSL_N(0, (a)); \
711 REQUIRE_SSL_N(1, (b)); \
712 REQUIRE_SSL_N(2, (c)); \
713 REQUIRE_SSL_N(3, (d)); \
714 REQUIRE_SSL_N(4, (e)); \
715 } while (0)
716
717 #define C_BIDI_ID(ordinal) \
718 (((ordinal) << 2) | QUIC_STREAM_INITIATOR_CLIENT | QUIC_STREAM_DIR_BIDI)
719 #define S_BIDI_ID(ordinal) \
720 (((ordinal) << 2) | QUIC_STREAM_INITIATOR_SERVER | QUIC_STREAM_DIR_BIDI)
721 #define C_UNI_ID(ordinal) \
722 (((ordinal) << 2) | QUIC_STREAM_INITIATOR_CLIENT | QUIC_STREAM_DIR_UNI)
723 #define S_UNI_ID(ordinal) \
724 (((ordinal) << 2) | QUIC_STREAM_INITIATOR_SERVER | QUIC_STREAM_DIR_UNI)
725
726 #if defined(OPENSSL_THREADS)
727
RADIX_THREAD_worker_run(RADIX_THREAD * rt)728 static int RADIX_THREAD_worker_run(RADIX_THREAD *rt)
729 {
730 int ok = 0;
731 TERP_CONFIG cfg = { 0 };
732
733 cfg.debug_bio = rt->debug_bio;
734 if (!TEST_true(bindings_adjust_terp_config(&cfg)))
735 goto err;
736
737 if (!TERP_run(rt->child_script_info, &cfg))
738 goto err;
739
740 ok = 1;
741 err:
742 return ok;
743 }
744
RADIX_THREAD_worker_main(void * p)745 static unsigned int RADIX_THREAD_worker_main(void *p)
746 {
747 int testresult = 0;
748 RADIX_THREAD *rt = p;
749
750 if (!TEST_true(radix_thread_init(rt)))
751 return 0;
752
753 /* Wait until thread-specific init is done (e.g. setting rt->t) */
754 ossl_crypto_mutex_lock(rt->m);
755 ossl_crypto_mutex_unlock(rt->m);
756
757 testresult = RADIX_THREAD_worker_run(rt);
758
759 ossl_crypto_mutex_lock(rt->m);
760 rt->testresult = testresult;
761 rt->done = 1;
762 ossl_crypto_mutex_unlock(rt->m);
763
764 radix_thread_cleanup();
765 return 1;
766 }
767
768 #endif
769
radix_activate_obj(RADIX_OBJ * obj)770 static void radix_activate_obj(RADIX_OBJ *obj)
771 {
772 if (obj != NULL)
773 obj->active = 1;
774 }
775
radix_activate_slot(size_t idx)776 static void radix_activate_slot(size_t idx)
777 {
778 if (idx >= NUM_SLOTS)
779 return;
780
781 radix_activate_obj(RT()->slot[idx]);
782 }
783
DEF_FUNC(hf_spawn_thread)784 DEF_FUNC(hf_spawn_thread)
785 {
786 int ok = 0;
787 RADIX_THREAD *child_rt = NULL;
788 SCRIPT_INFO *script_info = NULL;
789
790 F_POP(script_info);
791 if (!TEST_ptr(script_info))
792 goto err;
793
794 #if !defined(OPENSSL_THREADS)
795 TEST_skip("threading not supported, skipping");
796 F_SKIP_REST();
797 #else
798 if (!TEST_ptr(child_rt = RADIX_THREAD_new(&radix_process)))
799 return 0;
800
801 if (!TEST_ptr(child_rt->debug_bio = BIO_new(BIO_s_mem())))
802 goto err;
803
804 child_rt->child_script_info = script_info;
805
806 ossl_crypto_mutex_lock(child_rt->m);
807 if (!TEST_ptr(child_rt->t = ossl_crypto_thread_native_start(RADIX_THREAD_worker_main,
808 child_rt, 1))) {
809 ossl_crypto_mutex_unlock(child_rt->m);
810 goto err;
811 }
812
813 ossl_crypto_mutex_unlock(child_rt->m);
814 ok = 1;
815 #endif
816 err:
817 if (!ok)
818 RADIX_THREAD_free(child_rt);
819
820 return ok;
821 }
822
DEF_FUNC(hf_clear)823 DEF_FUNC(hf_clear)
824 {
825 RADIX_THREAD *rt = RT();
826 size_t i;
827
828 ossl_crypto_mutex_lock(RP()->gm);
829
830 lh_RADIX_OBJ_doall(RP()->objs, cleanup_one);
831 lh_RADIX_OBJ_flush(RP()->objs);
832
833 for (i = 0; i < NUM_SLOTS; ++i) {
834 rt->slot[i] = NULL;
835 rt->ssl[i] = NULL;
836 }
837
838 ossl_crypto_mutex_unlock(RP()->gm);
839 return 1;
840 }
841
842 #define OP_SPAWN_THREAD(script_name) \
843 (OP_PUSH_P(SCRIPT(script_name)), OP_FUNC(hf_spawn_thread))
844 #define OP_CLEAR() \
845 (OP_FUNC(hf_clear))
846