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