1 /* -*- Mode: c; tab-width: 8; indent-tabs-mode: 1; c-basic-offset: 8; -*- */
2
3 #include <darwintest.h>
4 #include <stdio.h>
5 #include <unistd.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <sys/socket.h>
9 #include <netinet/in.h>
10 #include <arpa/inet.h>
11 #include <errno.h>
12 #include <pthread.h>
13 #include <stdbool.h>
14 #include <TargetConditionals.h>
15
16 #include "net_test_lib.h"
17
18 T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
19
20 static bool debug;
21
22 static int
sock_open_common(int pf,int type)23 sock_open_common(int pf, int type)
24 {
25 int s;
26
27 s = socket(pf, type, 0);
28 T_QUIET;
29 T_ASSERT_POSIX_SUCCESS(s, "socket(%d, %d, 0)", pf, type);
30 return s;
31 }
32
33 static int
sock_open(int type)34 sock_open(int type)
35 {
36 return sock_open_common(PF_INET, type);
37 }
38
39 static int
sock_bind(int s,int port)40 sock_bind(int s, int port)
41 {
42 struct sockaddr_in sin = {
43 .sin_len = sizeof(sin),
44 .sin_family = AF_INET,
45 };
46
47 sin.sin_port = htons(port);
48 return bind(s, (const struct sockaddr *)&sin, sizeof(sin));
49 }
50
51 static int
sockv6_open(int type)52 sockv6_open(int type)
53 {
54 return sock_open_common(PF_INET6, type);
55 }
56
57 static int
sockv6_bind(int s,int port)58 sockv6_bind(int s, int port)
59 {
60 struct sockaddr_in6 sin6 = {
61 .sin6_len = sizeof(sin6),
62 .sin6_family = AF_INET6,
63 };
64
65 sin6.sin6_port = htons(port);
66 return bind(s, (const struct sockaddr *)&sin6, sizeof(sin6));
67 }
68
69 static uint16_t
sock_get_port(int sockfd)70 sock_get_port(int sockfd)
71 {
72 int error;
73 uint16_t p;
74 union sockaddr_in_4_6 sin;
75 socklen_t sin_len;
76
77 sin_len = sizeof(sin);
78 bzero(&sin, sin_len);
79 error = getsockname(sockfd, (struct sockaddr *)&sin, &sin_len);
80 T_QUIET;
81 T_EXPECT_POSIX_ZERO(error, "getsockname(%d)", sockfd);
82 if (error != 0) {
83 return 0;
84 }
85 switch (sin.sa.sa_family) {
86 case AF_INET:
87 p = sin.sin.sin_port;
88 break;
89 case AF_INET6:
90 p = sin.sin6.sin6_port;
91 break;
92 default:
93 T_ASSERT_FAIL("unknown address family %d\n",
94 sin.sa.sa_family);
95 p = 0;
96 break;
97 }
98 return p;
99 }
100
101 typedef struct {
102 bool v6;
103 int socket_count;
104 int * socket_list;
105 } SocketInfo, * SocketInfoRef;
106
107 static void
bind_sockets(SocketInfoRef info,const char * msg)108 bind_sockets(SocketInfoRef info, const char * msg)
109 {
110 for (int i = 0; i < info->socket_count; i++) {
111 int error;
112 uint16_t port;
113
114 if (info->v6) {
115 error = sockv6_bind(info->socket_list[i], 0);
116 } else {
117 error = sock_bind(info->socket_list[i], 0);
118 }
119 port = sock_get_port(info->socket_list[i]);
120 if (debug) {
121 T_LOG( "%s: fd %d port is %d error %d",
122 msg, info->socket_list[i], ntohs(port), error);
123 }
124 }
125 return;
126 }
127
128 static void *
second_thread(void * arg)129 second_thread(void * arg)
130 {
131 SocketInfoRef info = (SocketInfoRef)arg;
132
133 bind_sockets(info, "second");
134 return NULL;
135 }
136
137 static void
multithreaded_bind_test(bool v6,int socket_count)138 multithreaded_bind_test(bool v6, int socket_count)
139 {
140 int error;
141 SocketInfo info;
142 int socket_list[socket_count];
143 pthread_t thread;
144
145 info.v6 = v6;
146 for (int i = 0; i < socket_count; i++) {
147 if (v6) {
148 socket_list[i] = sockv6_open(SOCK_STREAM);
149 } else {
150 socket_list[i] = sock_open(SOCK_STREAM);
151 }
152 }
153 info.socket_count = socket_count;
154 info.socket_list = socket_list;
155 error = pthread_create(&thread, NULL, second_thread, &info);
156 T_QUIET;
157 T_ASSERT_POSIX_ZERO(error, "pthread_create");
158
159 /* compete with second thread */
160 bind_sockets(&info, "main");
161 error = pthread_join(thread, NULL);
162 T_QUIET;
163 T_ASSERT_POSIX_ZERO(error, "pthread_join");
164
165 for (int i = 0; i < socket_count; i++) {
166 error = close(socket_list[i]);
167 T_QUIET;
168 T_ASSERT_POSIX_ZERO(error, "close socket %d", socket_list[i]);
169 }
170 }
171
172 static void
run_multithreaded_bind_test(int number_of_runs,bool v6,int socket_count)173 run_multithreaded_bind_test(int number_of_runs, bool v6, int socket_count)
174 {
175 #if TARGET_OS_BRIDGE
176 T_SKIP("Not enough memory to handle this test");
177 #else /* TARGET_OS_BRIDGE */
178 for (int i = 0; i < number_of_runs; i++) {
179 multithreaded_bind_test(v6, socket_count);
180 }
181 T_PASS("multithreaded_bind_test %s", v6 ? "IPv6" : "IPv4");
182
183 force_zone_gc();
184 #endif /* TARGET_OS_BRIDGE */
185 }
186
187 T_DECL(socket_bind_35685803,
188 "multithreaded bind IPv4 socket as root",
189 T_META_ASROOT(false),
190 T_META_CHECK_LEAKS(false),
191 T_META_TAG_VM_PREFERRED)
192 {
193 run_multithreaded_bind_test(100, false, 100);
194 }
195
196 T_DECL(socket_bind_35685803_root,
197 "multithreaded bind IPv4 socket",
198 T_META_ASROOT(true),
199 T_META_TAG_VM_PREFERRED)
200 {
201 run_multithreaded_bind_test(100, false, 100);
202 }
203
204 T_DECL(socket_bind_35685803_v6,
205 "multithreaded bind IPv6 socket as root",
206 T_META_ASROOT(false),
207 T_META_CHECK_LEAKS(false),
208 T_META_TAG_VM_PREFERRED)
209 {
210 run_multithreaded_bind_test(100, true, 100);
211 }
212
213 T_DECL(socket_bind_35685803_v6_root,
214 "multithreaded bind IPv6 socket",
215 T_META_ASROOT(true),
216 T_META_TAG_VM_PREFERRED)
217 {
218 run_multithreaded_bind_test(100, true, 100);
219 }
220