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