1 /*
2 * Copyright (c) 2023 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #include <sys/fcntl.h>
30 #include <sys/socket.h>
31 #include <net/if.h>
32 #include <netinet/in.h>
33 #include <netinet/tcp.h>
34 #include <arpa/inet.h>
35
36 #include <darwintest.h>
37 #include <pthread.h>
38 #include <stdbool.h>
39 #include <string.h>
40 #include <unistd.h>
41
42 T_GLOBAL_META(
43 T_META_NAMESPACE("xnu.net"),
44 T_META_RADAR_COMPONENT_NAME("xnu"),
45 T_META_RADAR_COMPONENT_VERSION("networking"),
46 T_META_CHECK_LEAKS(false));
47
48 static int fd = -1;
49 static bool finished = false;
50 static bool is_tcp = false;
51
52 static void
init_sin6_address(struct sockaddr_in6 * sin6)53 init_sin6_address(struct sockaddr_in6 *sin6)
54 {
55 memset(sin6, 0, sizeof(struct sockaddr_in6));
56 sin6->sin6_len = sizeof(struct sockaddr_in6);
57 sin6->sin6_family = AF_INET6;
58 }
59
60 static void *
racer(void * arg)61 racer(void *arg)
62 {
63 #pragma unused(arg)
64 struct sockaddr_in6 sin6 = {};
65
66 init_sin6_address(&sin6);
67 T_ASSERT_EQ(inet_pton(AF_INET6, "::ffff:127.0.0.1", &sin6.sin6_addr), 1, NULL);
68
69 while (finished == false) {
70 (void)bind(fd, (struct sockaddr*)&sin6, sin6.sin6_len);
71 }
72 return NULL;
73 }
74
75 static void *
leader(void * arg)76 leader(void *arg)
77 {
78 #pragma unused(arg)
79 struct sockaddr_in6 sin6 = {};
80
81 init_sin6_address(&sin6);
82 T_ASSERT_EQ(inet_pton(AF_INET6, "::1", &sin6.sin6_addr), 1, NULL);
83
84 while (finished == false) {
85 T_QUIET; T_EXPECT_POSIX_SUCCESS(fd = socket(AF_INET6, is_tcp ? SOCK_STREAM : SOCK_DGRAM, 0), "socket");
86
87 (void)bind(fd, (struct sockaddr*)&sin6, sin6.sin6_len);
88
89 close(fd);
90 }
91 return NULL;
92 }
93
94 T_DECL(ipv6_tcp_bind_race, "race bind calls with TCP sockets")
95 {
96 pthread_t runner1;
97 if (pthread_create(&runner1, NULL, leader, NULL)) {
98 T_ASSERT_FAIL("pthread_create failed");
99 }
100
101 pthread_t runner2;
102 if (pthread_create(&runner2, NULL, racer, NULL)) {
103 T_ASSERT_FAIL("pthread_create failed");
104 }
105
106 sleep(5);
107
108 finished = true;
109
110 pthread_join(runner1, 0);
111 pthread_join(runner2, 0);
112 }
113
114 T_DECL(ipv6_udp_bind_race, "race bind calls with UDP sockets")
115 {
116 is_tcp = false;
117
118 pthread_t runner1;
119 if (pthread_create(&runner1, NULL, leader, NULL)) {
120 T_ASSERT_FAIL("pthread_create failed");
121 }
122
123 pthread_t runner2;
124 if (pthread_create(&runner2, NULL, racer, NULL)) {
125 T_ASSERT_FAIL("pthread_create failed");
126 }
127
128 sleep(5);
129
130 finished = true;
131
132 pthread_join(runner1, 0);
133 pthread_join(runner2, 0);
134 }
135