xref: /freebsd/crypto/openssl/test/radix/quic_bindings.c (revision 78e936b2d0b5e6554425009199be31e76bc67c10)
1 /*
2  * Copyright 2024-2026 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 
406 /*
407  * Free every non-listener object's SSL before cleanup_one() frees any listener.
408  *
409  * A connection's assist thread reads from its network BIO, and for a dgram BIO
410  * pair (see hf_link_dgram_pair) that BIO belongs to the linked listener. Freeing
411  * the connection joins its assist thread (see ossl_quic_free), so doing so first
412  * ensures no assist thread is still reading when the listener BIOs are freed.
413  */
cleanup_nonlistener(RADIX_OBJ * obj)414 static void cleanup_nonlistener(RADIX_OBJ *obj)
415 {
416     if (obj->ssl != NULL && !SSL_is_listener(obj->ssl)) {
417         SSL_free(obj->ssl);
418         obj->ssl = NULL;
419     }
420 }
421 
cleanup_one(RADIX_OBJ * obj)422 static void cleanup_one(RADIX_OBJ *obj)
423 {
424     obj->registered = 0;
425     RADIX_OBJ_free(obj);
426 }
427 
428 static void RADIX_THREAD_free(RADIX_THREAD *rt);
429 
RADIX_PROCESS_cleanup(RADIX_PROCESS * rp)430 static void RADIX_PROCESS_cleanup(RADIX_PROCESS *rp)
431 {
432     size_t i;
433 
434     assert(rp->done_join_all_threads);
435 
436     for (i = 0; i < (size_t)sk_RADIX_THREAD_num(rp->threads); ++i)
437         RADIX_THREAD_free(sk_RADIX_THREAD_value(rp->threads, i));
438 
439     sk_RADIX_THREAD_free(rp->threads);
440     rp->threads = NULL;
441 
442     lh_RADIX_OBJ_doall(rp->objs, cleanup_nonlistener);
443     lh_RADIX_OBJ_doall(rp->objs, cleanup_one);
444     lh_RADIX_OBJ_free(rp->objs);
445     rp->objs = NULL;
446 
447     BIO_free_all(rp->keylog_out);
448     rp->keylog_out = NULL;
449     ossl_crypto_mutex_free(&rp->gm);
450 }
451 
RADIX_PROCESS_get_obj(RADIX_PROCESS * rp,const char * name)452 static RADIX_OBJ *RADIX_PROCESS_get_obj(RADIX_PROCESS *rp, const char *name)
453 {
454     RADIX_OBJ key;
455 
456     key.name = (char *)name;
457     return lh_RADIX_OBJ_retrieve(rp->objs, &key);
458 }
459 
RADIX_PROCESS_set_obj(RADIX_PROCESS * rp,const char * name,RADIX_OBJ * obj)460 static int RADIX_PROCESS_set_obj(RADIX_PROCESS *rp,
461     const char *name, RADIX_OBJ *obj)
462 {
463     RADIX_OBJ *existing;
464 
465     if (obj != NULL && !TEST_false(obj->registered))
466         return 0;
467 
468     existing = RADIX_PROCESS_get_obj(rp, name);
469     if (existing != NULL && obj != existing) {
470         if (!TEST_true(existing->registered))
471             return 0;
472 
473         lh_RADIX_OBJ_delete(rp->objs, existing);
474         existing->registered = 0;
475         RADIX_OBJ_free(existing);
476     }
477 
478     if (obj != NULL) {
479         lh_RADIX_OBJ_insert(rp->objs, obj);
480         obj->registered = 1;
481     }
482 
483     return 1;
484 }
485 
RADIX_PROCESS_set_ssl(RADIX_PROCESS * rp,const char * name,SSL * ssl)486 static int RADIX_PROCESS_set_ssl(RADIX_PROCESS *rp, const char *name, SSL *ssl)
487 {
488     RADIX_OBJ *obj;
489 
490     if (!TEST_ptr(obj = RADIX_OBJ_new(name, ssl)))
491         return 0;
492 
493     if (!TEST_true(RADIX_PROCESS_set_obj(rp, name, obj))) {
494         RADIX_OBJ_free(obj);
495         return 0;
496     }
497 
498     return 1;
499 }
500 
RADIX_PROCESS_get_ssl(RADIX_PROCESS * rp,const char * name)501 static SSL *RADIX_PROCESS_get_ssl(RADIX_PROCESS *rp, const char *name)
502 {
503     RADIX_OBJ *obj = RADIX_PROCESS_get_obj(rp, name);
504 
505     if (obj == NULL)
506         return NULL;
507 
508     return obj->ssl;
509 }
510 
RADIX_THREAD_new(RADIX_PROCESS * rp)511 static RADIX_THREAD *RADIX_THREAD_new(RADIX_PROCESS *rp)
512 {
513     RADIX_THREAD *rt;
514 
515     if (!TEST_ptr(rp)
516         || !TEST_ptr(rt = OPENSSL_zalloc(sizeof(*rt))))
517         return 0;
518 
519     rt->rp = rp;
520 
521 #if defined(OPENSSL_THREADS)
522     if (!TEST_ptr(rt->m = ossl_crypto_mutex_new())) {
523         OPENSSL_free(rt);
524         return 0;
525     }
526 #endif
527 
528     if (!TEST_true(sk_RADIX_THREAD_push(rp->threads, rt))) {
529         OPENSSL_free(rt);
530         return 0;
531     }
532 
533     rt->thread_idx = rp->next_thread_idx++;
534     assert(rt->thread_idx + 1 == (size_t)sk_RADIX_THREAD_num(rp->threads));
535     return rt;
536 }
537 
RADIX_THREAD_free(RADIX_THREAD * rt)538 static void RADIX_THREAD_free(RADIX_THREAD *rt)
539 {
540     if (rt == NULL)
541         return;
542 
543     assert(rt->t == NULL);
544     BIO_free_all(rt->debug_bio);
545     OPENSSL_free(rt->tmp_buf);
546     ossl_crypto_mutex_free(&rt->m);
547     OPENSSL_free(rt);
548 }
549 
RADIX_THREAD_join(RADIX_THREAD * rt)550 static int RADIX_THREAD_join(RADIX_THREAD *rt)
551 {
552     CRYPTO_THREAD_RETVAL rv;
553 
554     if (rt->t != NULL)
555         ossl_crypto_thread_native_join(rt->t, &rv);
556 
557     ossl_crypto_thread_native_clean(rt->t);
558     rt->t = NULL;
559 
560     if (!TEST_true(rt->done))
561         return 0;
562 
563     return 1;
564 }
565 
566 static RADIX_PROCESS radix_process;
567 static CRYPTO_THREAD_LOCAL radix_thread;
568 
radix_thread_cleanup_tl(void * p)569 static void radix_thread_cleanup_tl(void *p)
570 {
571     /* Should already have been cleaned up. */
572     if (!TEST_ptr_null(p))
573         abort();
574 }
575 
radix_get_thread(void)576 static RADIX_THREAD *radix_get_thread(void)
577 {
578     return CRYPTO_THREAD_get_local(&radix_thread);
579 }
580 
radix_thread_init(RADIX_THREAD * rt)581 static int radix_thread_init(RADIX_THREAD *rt)
582 {
583     if (!TEST_ptr(rt)
584         || !TEST_ptr_null(CRYPTO_THREAD_get_local(&radix_thread)))
585         return 0;
586 
587     if (!TEST_true(CRYPTO_THREAD_set_local(&radix_thread, rt)))
588         return 0;
589 
590     set_override_bio_out(rt->debug_bio);
591     set_override_bio_err(rt->debug_bio);
592     return 1;
593 }
594 
radix_thread_cleanup(void)595 static void radix_thread_cleanup(void)
596 {
597     RADIX_THREAD *rt = radix_get_thread();
598 
599     if (!TEST_ptr(rt))
600         return;
601 
602     if (!TEST_true(CRYPTO_THREAD_set_local(&radix_thread, NULL)))
603         return;
604 }
605 
bindings_process_init(size_t node_idx,size_t process_idx)606 static int bindings_process_init(size_t node_idx, size_t process_idx)
607 {
608     RADIX_THREAD *rt;
609 
610     if (!TEST_true(RADIX_PROCESS_init(&radix_process, node_idx, process_idx)))
611         return 0;
612 
613     if (!TEST_true(CRYPTO_THREAD_init_local(&radix_thread,
614             radix_thread_cleanup_tl)))
615         return 0;
616 
617     if (!TEST_ptr(rt = RADIX_THREAD_new(&radix_process)))
618         return 0;
619 
620     /* Allocate structures for main thread. */
621     return radix_thread_init(rt);
622 }
623 
bindings_process_finish(int testresult_main)624 static int bindings_process_finish(int testresult_main)
625 {
626     int testresult, testresult_child;
627 
628     if (!TEST_true(RADIX_PROCESS_join_all_threads(&radix_process,
629             &testresult_child)))
630         return 0;
631 
632     testresult = testresult_main && testresult_child;
633     RADIX_PROCESS_report_state(&radix_process, bio_err,
634         /*verbose=*/!testresult);
635     radix_thread_cleanup(); /* cleanup main thread */
636     RADIX_PROCESS_cleanup(&radix_process);
637 
638     if (testresult)
639         BIO_printf(bio_err, "==> OK\n\n");
640     else
641         BIO_printf(bio_err, "==> ERROR (main=%d, children=%d)\n\n",
642             testresult_main, testresult_child);
643 
644     return testresult;
645 }
646 
647 #define RP() (&radix_process)
648 #define RT() (radix_get_thread())
649 
get_time(void * arg)650 static OSSL_TIME get_time(void *arg)
651 {
652     OSSL_TIME time_slip;
653 
654     ossl_crypto_mutex_lock(RP()->gm);
655     time_slip = RP()->time_slip;
656     ossl_crypto_mutex_unlock(RP()->gm);
657 
658     return ossl_time_add(ossl_time_now(), time_slip);
659 }
660 
radix_skip_time(OSSL_TIME t)661 ossl_unused static void radix_skip_time(OSSL_TIME t)
662 {
663     ossl_crypto_mutex_lock(RP()->gm);
664     RP()->time_slip = ossl_time_add(RP()->time_slip, t);
665     ossl_crypto_mutex_unlock(RP()->gm);
666 }
667 
per_op_tick_obj(RADIX_OBJ * obj)668 static void per_op_tick_obj(RADIX_OBJ *obj)
669 {
670     if (obj->active)
671         SSL_handle_events(obj->ssl);
672 }
673 
do_per_op(TERP * terp,void * arg)674 static int do_per_op(TERP *terp, void *arg)
675 {
676     lh_RADIX_OBJ_doall(RP()->objs, per_op_tick_obj);
677     return 1;
678 }
679 
bindings_adjust_terp_config(TERP_CONFIG * cfg)680 static int bindings_adjust_terp_config(TERP_CONFIG *cfg)
681 {
682     cfg->now_cb = get_time;
683     cfg->per_op_cb = do_per_op;
684     return 1;
685 }
686 
expect_slot_ssl(FUNC_CTX * fctx,size_t idx,SSL ** p_ssl)687 static int expect_slot_ssl(FUNC_CTX *fctx, size_t idx, SSL **p_ssl)
688 {
689     if (!TEST_size_t_lt(idx, NUM_SLOTS)
690         || !TEST_ptr(*p_ssl = RT()->ssl[idx]))
691         return 0;
692 
693     return 1;
694 }
695 
696 #define REQUIRE_SSL_N(idx, ssl)                               \
697     do {                                                      \
698         (ssl) = NULL; /* quiet uninitialized warnings */      \
699         if (!TEST_true(expect_slot_ssl(fctx, (idx), &(ssl)))) \
700             goto err;                                         \
701     } while (0)
702 #define REQUIRE_SSL(ssl) REQUIRE_SSL_N(0, (ssl))
703 
704 #define REQUIRE_SSL_2(a, b)    \
705     do {                       \
706         REQUIRE_SSL_N(0, (a)); \
707         REQUIRE_SSL_N(1, (b)); \
708     } while (0)
709 
710 #define REQUIRE_SSL_3(a, b, c) \
711     do {                       \
712         REQUIRE_SSL_N(0, (a)); \
713         REQUIRE_SSL_N(1, (b)); \
714         REQUIRE_SSL_N(2, (c)); \
715     } while (0)
716 
717 #define REQUIRE_SSL_4(a, b, c, d) \
718     do {                          \
719         REQUIRE_SSL_N(0, (a));    \
720         REQUIRE_SSL_N(1, (b));    \
721         REQUIRE_SSL_N(2, (c));    \
722         REQUIRE_SSL_N(3, (d));    \
723     } while (0)
724 
725 #define REQUIRE_SSL_5(a, b, c, d, e) \
726     do {                             \
727         REQUIRE_SSL_N(0, (a));       \
728         REQUIRE_SSL_N(1, (b));       \
729         REQUIRE_SSL_N(2, (c));       \
730         REQUIRE_SSL_N(3, (d));       \
731         REQUIRE_SSL_N(4, (e));       \
732     } while (0)
733 
734 #define C_BIDI_ID(ordinal) \
735     (((ordinal) << 2) | QUIC_STREAM_INITIATOR_CLIENT | QUIC_STREAM_DIR_BIDI)
736 #define S_BIDI_ID(ordinal) \
737     (((ordinal) << 2) | QUIC_STREAM_INITIATOR_SERVER | QUIC_STREAM_DIR_BIDI)
738 #define C_UNI_ID(ordinal) \
739     (((ordinal) << 2) | QUIC_STREAM_INITIATOR_CLIENT | QUIC_STREAM_DIR_UNI)
740 #define S_UNI_ID(ordinal) \
741     (((ordinal) << 2) | QUIC_STREAM_INITIATOR_SERVER | QUIC_STREAM_DIR_UNI)
742 
743 #if defined(OPENSSL_THREADS)
744 
RADIX_THREAD_worker_run(RADIX_THREAD * rt)745 static int RADIX_THREAD_worker_run(RADIX_THREAD *rt)
746 {
747     int ok = 0;
748     TERP_CONFIG cfg = { 0 };
749 
750     cfg.debug_bio = rt->debug_bio;
751     if (!TEST_true(bindings_adjust_terp_config(&cfg)))
752         goto err;
753 
754     if (!TERP_run(rt->child_script_info, &cfg))
755         goto err;
756 
757     ok = 1;
758 err:
759     return ok;
760 }
761 
RADIX_THREAD_worker_main(void * p)762 static unsigned int RADIX_THREAD_worker_main(void *p)
763 {
764     int testresult = 0;
765     RADIX_THREAD *rt = p;
766 
767     if (!TEST_true(radix_thread_init(rt)))
768         return 0;
769 
770     /* Wait until thread-specific init is done (e.g. setting rt->t) */
771     ossl_crypto_mutex_lock(rt->m);
772     ossl_crypto_mutex_unlock(rt->m);
773 
774     testresult = RADIX_THREAD_worker_run(rt);
775 
776     ossl_crypto_mutex_lock(rt->m);
777     rt->testresult = testresult;
778     rt->done = 1;
779     ossl_crypto_mutex_unlock(rt->m);
780 
781     radix_thread_cleanup();
782     return 1;
783 }
784 
785 #endif
786 
radix_activate_obj(RADIX_OBJ * obj)787 static void radix_activate_obj(RADIX_OBJ *obj)
788 {
789     if (obj != NULL)
790         obj->active = 1;
791 }
792 
radix_activate_slot(size_t idx)793 static void radix_activate_slot(size_t idx)
794 {
795     if (idx >= NUM_SLOTS)
796         return;
797 
798     radix_activate_obj(RT()->slot[idx]);
799 }
800 
DEF_FUNC(hf_spawn_thread)801 DEF_FUNC(hf_spawn_thread)
802 {
803     int ok = 0;
804     RADIX_THREAD *child_rt = NULL;
805     SCRIPT_INFO *script_info = NULL;
806 
807     F_POP(script_info);
808     if (!TEST_ptr(script_info))
809         goto err;
810 
811 #if !defined(OPENSSL_THREADS)
812     TEST_skip("threading not supported, skipping");
813     F_SKIP_REST();
814 #else
815     if (!TEST_ptr(child_rt = RADIX_THREAD_new(&radix_process)))
816         return 0;
817 
818     if (!TEST_ptr(child_rt->debug_bio = BIO_new(BIO_s_mem())))
819         goto err;
820 
821     child_rt->child_script_info = script_info;
822 
823     ossl_crypto_mutex_lock(child_rt->m);
824     if (!TEST_ptr(child_rt->t = ossl_crypto_thread_native_start(RADIX_THREAD_worker_main,
825                       child_rt, 1))) {
826         ossl_crypto_mutex_unlock(child_rt->m);
827         goto err;
828     }
829 
830     ossl_crypto_mutex_unlock(child_rt->m);
831     ok = 1;
832 #endif
833 err:
834     if (!ok)
835         RADIX_THREAD_free(child_rt);
836 
837     return ok;
838 }
839 
DEF_FUNC(hf_clear)840 DEF_FUNC(hf_clear)
841 {
842     RADIX_THREAD *rt = RT();
843     size_t i;
844 
845     ossl_crypto_mutex_lock(RP()->gm);
846 
847     lh_RADIX_OBJ_doall(RP()->objs, cleanup_one);
848     lh_RADIX_OBJ_flush(RP()->objs);
849 
850     for (i = 0; i < NUM_SLOTS; ++i) {
851         rt->slot[i] = NULL;
852         rt->ssl[i] = NULL;
853     }
854 
855     ossl_crypto_mutex_unlock(RP()->gm);
856     return 1;
857 }
858 
859 #define OP_SPAWN_THREAD(script_name) \
860     (OP_PUSH_P(SCRIPT(script_name)), OP_FUNC(hf_spawn_thread))
861 #define OP_CLEAR() \
862     (OP_FUNC(hf_clear))
863