Debian Codegraph @

Xpjproject/pjlib/src/pjlib-test/ioq_iocp_unreg_test.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2024 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2024 jimying at github dot com.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20
21 /*
22 * IOCP crash reproduce test (issue #985).
23 * The code is taken from PR #4172 with few minor changes.
24 * Issue fix attempt is done in #4136.
25 *
26 * Note:
27 * - The crash was reproducible on Windows 10 & MSVC2005 (Win32),
28 * but not reproducible on Windows 11 & MSVC2022 (Win32 &x64).
29 * - Test can be run for any ioqueue and normally take less than one second.
30 */
31 #include <pjlib.h>
32 #include "test.h"
33
34 #define THIS_FILE "iocp_unregister_test.c"
35
36 #define CLIENT_NUM (PJ_IOQUEUE_MAX_HANDLES-1)
37
38 /**
39 * socket info
40 * has an independent memory pool, which is the key to successful
41 * reproduce crash
42 */
43 struct sock_info_t {
44 pj_pool_t *pool;
45 pj_activesock_t *asock;
46 pj_sockaddr bound_addr;
47 };
48
49 struct iocp_test_t {
50 pj_pool_t *pool;
51 pj_ioqueue_t *ioq;
52 pj_thread_t *tid;
53 pj_bool_t quit;
54 struct sock_info_t *socks[CLIENT_NUM];
55 pj_activesock_t *asock_send;
56 };
57
58
59 static int worker_thread(void *p)
60 {
61 struct iocp_test_t *test = (struct iocp_test_t *)p;
62
63 while(!test->quit) {
64 pj_time_val timeout = {0, 100};
65 pj_ioqueue_poll(test->ioq, &timeout);
66 }
67
68 return 0;
69 }
70
71 static unsigned recv_cnt;
72
73 static pj_bool_t on_data_recvfrom(pj_activesock_t *asock,
74 void *data,
75 pj_size_t size,
76 const pj_sockaddr_t *src_addr,
77 int addr_len,
78 pj_status_t status)
79 {
80 (void)asock;
81 (void)src_addr;
82 (void)addr_len;
83 PJ_LOG(4, (THIS_FILE, "on_data_recvfrom() data:%.*s, status:%d",
84 (int)size, (char *)data, status));
85 ++recv_cnt;
86 return PJ_TRUE;
87 }
88
89 int iocp_unregister_test(void)
90 {
91 struct iocp_test_t *test;
92 pj_pool_t *pool;
93 pj_status_t status;
94 unsigned i;
95 pj_activesock_cfg cfg;
96 pj_activesock_cb cb;
97 struct sock_info_t *sock_info;
98 pj_sockaddr loc_addr;
99 pj_str_t loop = {"127.0.0.1", 9};
100
101 // Let's just do it for any ioqueue.
102 //if (strcmp(pj_ioqueue_name(), "iocp")) {
103 // /* skip if ioqueue framework is not iocp */
104 // return PJ_SUCCESS;
105 //}
106
107 pool = pj_pool_create(mem, "iocp-crash-test", 500, 500, NULL);
108 test = PJ_POOL_ZALLOC_T(pool, struct iocp_test_t);
109 test->pool = pool;
110 status = pj_ioqueue_create(pool, CLIENT_NUM+1, &test->ioq);
111 if (status != PJ_SUCCESS) {
112 status = -900;
113 goto on_error;
114 }
115
116 status = pj_thread_create(pool, "iocp-crash-test", worker_thread,
117 test, 0, 0, &test->tid);
118 if (status != PJ_SUCCESS) {
119 status = -901;
120 goto on_error;
121 }
122
123 pj_activesock_cfg_default(&cfg);
124 pj_bzero(&cb, sizeof(cb));
125 cb.on_data_recvfrom = on_data_recvfrom;
126
127 /* create send socket */
128 status = pj_activesock_create_udp(pool, NULL, &cfg, test->ioq, &cb, NULL,
129 &test->asock_send, NULL);
130 if (status != PJ_SUCCESS) {
131 status = -902;
132 goto on_error;
133 }
134
135 /* create sockets to receive */
136 pj_sockaddr_init(pj_AF_INET(), &loc_addr, &loop, 0);
137 for (i = 0; i < PJ_ARRAY_SIZE(test->socks); i++) {
138 pool = pj_pool_create(mem, "sock%p", 500, 500, NULL);
139 sock_info = PJ_POOL_ZALLOC_T(pool, struct sock_info_t);
140 sock_info->pool = pool;
141
142 status = pj_activesock_create_udp(pool, &loc_addr, &cfg, test->ioq,
143 &cb, NULL, &sock_info->asock,
144 &sock_info->bound_addr);
145 if (status != PJ_SUCCESS) {
146 status = -903;
147 pj_pool_release(pool);
148 goto on_error;
149 }
150 test->socks[i] = sock_info;
151 pj_activesock_start_recvfrom(sock_info->asock, pool, 256, 0);
152 }
153
154 /* send 'hello' to every socks */
155 for (i = 0; i < PJ_ARRAY_SIZE(test->socks); i++) {
156 pj_ioqueue_op_key_t *send_key;
157 pj_str_t data;
158 pj_ssize_t sent;
159
160 sock_info = test->socks[i];
161 send_key = PJ_POOL_ZALLOC_T(test->pool, pj_ioqueue_op_key_t);
162 pj_strdup2_with_null(test->pool, &data, "hello");
163 sent = data.slen;
164 status = pj_activesock_sendto(test->asock_send, send_key, data.ptr,
165 &sent, 0, &sock_info->bound_addr,
166 pj_sockaddr_get_len(&sock_info->bound_addr));
167 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
168 char buf[80];
169 pj_sockaddr_print(&sock_info->bound_addr, buf, sizeof(buf), 3);
170 PJ_PERROR(2, (THIS_FILE, status, "send error, dest:%s", buf));
171 }
172 }
173
174 pj_thread_sleep(20);
175
176 /* close all socks */
177 for (i = 0; i < PJ_ARRAY_SIZE(test->socks); i++) {
178 sock_info = test->socks[i];
179 pj_activesock_close(sock_info->asock);
180 pj_pool_release(sock_info->pool);
181 test->socks[i] = NULL;
182 }
183
184 pj_thread_sleep(20);
185
186 /* quit */
187 test->quit = PJ_TRUE;
188 status = PJ_SUCCESS;
189
190 on_error:
191 if (test->tid)
192 pj_thread_join(test->tid);
193 for (i = 0; i < PJ_ARRAY_SIZE(test->socks); i++) {
194 sock_info = test->socks[i];
195 if (!sock_info)
196 break;
197 pj_activesock_close(sock_info->asock);
198 pj_pool_release(sock_info->pool);
199 }
200 if(test->asock_send)
201 pj_activesock_close(test->asock_send);
202 if (test->ioq)
203 pj_ioqueue_destroy(test->ioq);
204 pj_pool_release(test->pool);
205
206 PJ_LOG(3, (THIS_FILE, "Recv cnt = %u", recv_cnt));
207 return status;
208 }