xref: /xnu-12377.1.9/tests/mcast_group_race_82820812.c (revision f6217f891ac0bb64f3d375211650a4c1ff8ca1ea)
1 #include <arpa/inet.h>
2 #include <pthread.h>
3 #include <unistd.h>
4 #include <darwintest.h>
5 #include <TargetConditionals.h>
6 
7 #include <mach/mach_host.h>
8 
9 #include "net_test_lib.h"
10 
11 volatile static int lock_a;
12 volatile static int lock_b;
13 
14 static int fd;
15 static struct sockaddr_in saddr;
16 
17 static struct ip_mreq filler_group;
18 static struct ip_mreq group_a;
19 static struct ip_mreq group_b;
20 
21 #define ITERATIONS_LIMIT 1000
22 
23 static void *
thread_func(__unused void * arg)24 thread_func(__unused void* arg)
25 {
26 	lock_a = 1;
27 	while (lock_b == 0) {
28 	}
29 
30 	setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &group_a, sizeof(group_a));
31 
32 	return NULL;
33 }
34 
35 T_DECL(mcast_group_race_82820812, "Race between multicast group join operations.",
36     T_META_ASROOT(true),
37     T_META_ENABLED(!TARGET_OS_BRIDGE && !TARGET_OS_SIMULATOR))
38 {
39 	pthread_t th;
40 	uint32_t i = 0;
41 	uint32_t j = 0;
42 
43 	saddr.sin_family = AF_INET;
44 
45 	group_a.imr_multiaddr.s_addr = inet_addr("224.0.0.1");
46 	group_b.imr_multiaddr.s_addr = inet_addr("224.0.0.2");
47 
48 	for (i = 0; i < ITERATIONS_LIMIT; ++i) {
49 		T_ASSERT_POSIX_SUCCESS(fd = socket(AF_INET, SOCK_DGRAM, 0), "socket");
50 		T_ASSERT_POSIX_SUCCESS(bind(fd, (struct sockaddr *) &saddr, sizeof(saddr)), "bind");
51 
52 		for (j = 0; j < IP_MIN_MEMBERSHIPS - 1; ++j) {
53 			filler_group.imr_multiaddr.s_addr = htonl(ntohl(inet_addr("224.0.0.3")) + j);
54 			setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &filler_group, sizeof(filler_group));
55 		}
56 
57 		T_ASSERT_POSIX_ZERO(pthread_create(&th, NULL, thread_func, NULL), "pthread_create");
58 
59 		while (lock_a == 0) {
60 		}
61 		lock_b = 1;
62 
63 		setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &group_b, sizeof(group_b));
64 
65 		T_ASSERT_POSIX_ZERO(pthread_join(th, NULL), "pthread_join");
66 		T_ASSERT_POSIX_SUCCESS(close(fd), "close");
67 	}
68 
69 	force_zone_gc();
70 }
71