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