xref: /xnu-10063.141.1/tests/fd.c (revision d8b80295118ef25ac3a784134bcf95cd8e88109f)
1*d8b80295SApple OSS Distributions #include <darwintest.h>
2*d8b80295SApple OSS Distributions #include <darwintest_utils.h>
3*d8b80295SApple OSS Distributions #include <pthread.h>
4*d8b80295SApple OSS Distributions #include <sys/select.h>
5*d8b80295SApple OSS Distributions #include <sys/fileport.h>
6*d8b80295SApple OSS Distributions #include <sys/fcntl.h>
7*d8b80295SApple OSS Distributions #include <mach/mach.h>
8*d8b80295SApple OSS Distributions 
9*d8b80295SApple OSS Distributions T_GLOBAL_META(
10*d8b80295SApple OSS Distributions 	T_META_NAMESPACE("xnu.fd"),
11*d8b80295SApple OSS Distributions 	T_META_RUN_CONCURRENTLY(true));
12*d8b80295SApple OSS Distributions 
13*d8b80295SApple OSS Distributions static void *
fd_select_close_helper(void * ctx)14*d8b80295SApple OSS Distributions fd_select_close_helper(void *ctx)
15*d8b80295SApple OSS Distributions {
16*d8b80295SApple OSS Distributions 	int fd = *(int *)ctx;
17*d8b80295SApple OSS Distributions 
18*d8b80295SApple OSS Distributions 	// wait for the thread to enter select
19*d8b80295SApple OSS Distributions 	usleep(500000);
20*d8b80295SApple OSS Distributions 	close(fd);
21*d8b80295SApple OSS Distributions 
22*d8b80295SApple OSS Distributions 	return NULL;
23*d8b80295SApple OSS Distributions }
24*d8b80295SApple OSS Distributions 
25*d8b80295SApple OSS Distributions T_DECL(fd_select_close, "Test for 54795873: make sure close breaks out of select")
26*d8b80295SApple OSS Distributions {
27*d8b80295SApple OSS Distributions 	fd_set read_fd;
28*d8b80295SApple OSS Distributions 	int pair[2], rc;
29*d8b80295SApple OSS Distributions 	pthread_t th;
30*d8b80295SApple OSS Distributions 
31*d8b80295SApple OSS Distributions 	rc = socketpair(PF_LOCAL, SOCK_STREAM, 0, pair);
32*d8b80295SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(rc, "socketpair");
33*d8b80295SApple OSS Distributions 
34*d8b80295SApple OSS Distributions 	pthread_create(&th, NULL, fd_select_close_helper, pair);
35*d8b80295SApple OSS Distributions 
36*d8b80295SApple OSS Distributions 	FD_ZERO(&read_fd);
37*d8b80295SApple OSS Distributions 	FD_SET(pair[0], &read_fd);
38*d8b80295SApple OSS Distributions 
39*d8b80295SApple OSS Distributions 	rc = select(pair[0] + 1, &read_fd, NULL, NULL, NULL);
40*d8b80295SApple OSS Distributions 	T_EXPECT_POSIX_FAILURE(rc, EBADF, "select broke out with EBADF");
41*d8b80295SApple OSS Distributions }
42*d8b80295SApple OSS Distributions 
43*d8b80295SApple OSS Distributions T_DECL(fd_select_ebadf, "Test that select on closed fd returns EBADF")
44*d8b80295SApple OSS Distributions {
45*d8b80295SApple OSS Distributions 	fd_set read_fd;
46*d8b80295SApple OSS Distributions 	int pair[2], rc;
47*d8b80295SApple OSS Distributions 
48*d8b80295SApple OSS Distributions 	rc = socketpair(PF_LOCAL, SOCK_STREAM, 0, pair);
49*d8b80295SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(rc, "socketpair");
50*d8b80295SApple OSS Distributions 
51*d8b80295SApple OSS Distributions 	FD_ZERO(&read_fd);
52*d8b80295SApple OSS Distributions 	FD_SET(pair[0], &read_fd);
53*d8b80295SApple OSS Distributions 
54*d8b80295SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(close(pair[0]), "close(%d)", pair[0]);
55*d8b80295SApple OSS Distributions 
56*d8b80295SApple OSS Distributions 	rc = select(pair[0] + 1, &read_fd, NULL, NULL, NULL);
57*d8b80295SApple OSS Distributions 	T_EXPECT_POSIX_FAILURE(rc, EBADF, "select returned with EBADF");
58*d8b80295SApple OSS Distributions }
59*d8b80295SApple OSS Distributions 
60*d8b80295SApple OSS Distributions static void *
fd_stress_dup2_close_fun(void * ctx)61*d8b80295SApple OSS Distributions fd_stress_dup2_close_fun(void *ctx)
62*d8b80295SApple OSS Distributions {
63*d8b80295SApple OSS Distributions 	int thno = (int)(long)ctx;
64*d8b80295SApple OSS Distributions 	int count = 10000, rc;
65*d8b80295SApple OSS Distributions 
66*d8b80295SApple OSS Distributions 	for (int i = 1; i <= count; i++) {
67*d8b80295SApple OSS Distributions 		rc = dup2(STDIN_FILENO, 42);
68*d8b80295SApple OSS Distributions 		T_QUIET; T_EXPECT_POSIX_SUCCESS(rc, "dup2(%d, 42)", STDIN_FILENO);
69*d8b80295SApple OSS Distributions 		if (thno == 3) {
70*d8b80295SApple OSS Distributions 			rc = close(42);
71*d8b80295SApple OSS Distributions 			if (rc == -1) {
72*d8b80295SApple OSS Distributions 				T_QUIET; T_EXPECT_POSIX_FAILURE(rc, EBADF, "close(42)");
73*d8b80295SApple OSS Distributions 			}
74*d8b80295SApple OSS Distributions 		}
75*d8b80295SApple OSS Distributions 		if (i % 1000 == 0) {
76*d8b80295SApple OSS Distributions 			T_LOG("thread %d: %d/%d dups\n", thno, i, count);
77*d8b80295SApple OSS Distributions 		}
78*d8b80295SApple OSS Distributions 	}
79*d8b80295SApple OSS Distributions 
80*d8b80295SApple OSS Distributions 	return NULL;
81*d8b80295SApple OSS Distributions }
82*d8b80295SApple OSS Distributions 
83*d8b80295SApple OSS Distributions T_DECL(fd_stress_dup2_close, "Stress test races between dup2 and close")
84*d8b80295SApple OSS Distributions {
85*d8b80295SApple OSS Distributions 	pthread_t th[4];
86*d8b80295SApple OSS Distributions 	int rc;
87*d8b80295SApple OSS Distributions 
88*d8b80295SApple OSS Distributions 	for (int i = 0; i < 4; i++) {
89*d8b80295SApple OSS Distributions 		rc = pthread_create(&th[i], NULL,
90*d8b80295SApple OSS Distributions 		    fd_stress_dup2_close_fun, (void *)(long)i);
91*d8b80295SApple OSS Distributions 		T_ASSERT_POSIX_ZERO(rc, "pthread_create");
92*d8b80295SApple OSS Distributions 	}
93*d8b80295SApple OSS Distributions 
94*d8b80295SApple OSS Distributions 	for (int i = 0; i < 4; i++) {
95*d8b80295SApple OSS Distributions 		pthread_join(th[i], NULL);
96*d8b80295SApple OSS Distributions 	}
97*d8b80295SApple OSS Distributions }
98*d8b80295SApple OSS Distributions 
99*d8b80295SApple OSS Distributions T_DECL(fd_dup2_erase_clofork_58446996,
100*d8b80295SApple OSS Distributions     "Make sure dup2() doesn't inherit flags from an old fd")
101*d8b80295SApple OSS Distributions {
102*d8b80295SApple OSS Distributions 	int fd1, fd2;
103*d8b80295SApple OSS Distributions 
104*d8b80295SApple OSS Distributions 	fd1 = open("/dev/null", O_RDONLY | O_CLOEXEC);
105*d8b80295SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(fd1, "open(/dev/null)");
106*d8b80295SApple OSS Distributions 
107*d8b80295SApple OSS Distributions 	fd2 = open("/dev/null", O_RDONLY | O_CLOEXEC);
108*d8b80295SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(fd2, "open(/dev/null)");
109*d8b80295SApple OSS Distributions 
110*d8b80295SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(dup2(fd1, fd2), "dup2(fd1, fd2)");
111*d8b80295SApple OSS Distributions 	T_EXPECT_EQ(fcntl(fd2, F_GETFD, 0), 0,
112*d8b80295SApple OSS Distributions 	    "neither FD_CLOEXEC nor FD_CLOFORK should be set");
113*d8b80295SApple OSS Distributions }
114*d8b80295SApple OSS Distributions 
115*d8b80295SApple OSS Distributions struct confined_race_state {
116*d8b80295SApple OSS Distributions 	int fd;
117*d8b80295SApple OSS Distributions 	bool made;
118*d8b80295SApple OSS Distributions };
119*d8b80295SApple OSS Distributions 
120*d8b80295SApple OSS Distributions static void *
confine_thread(void * data)121*d8b80295SApple OSS Distributions confine_thread(void *data)
122*d8b80295SApple OSS Distributions {
123*d8b80295SApple OSS Distributions 	volatile int *fdp = data;
124*d8b80295SApple OSS Distributions 
125*d8b80295SApple OSS Distributions 	for (;;) {
126*d8b80295SApple OSS Distributions 		fcntl(*fdp, F_SETCONFINED, 1);
127*d8b80295SApple OSS Distributions 	}
128*d8b80295SApple OSS Distributions 
129*d8b80295SApple OSS Distributions 	return NULL;
130*d8b80295SApple OSS Distributions }
131*d8b80295SApple OSS Distributions 
132*d8b80295SApple OSS Distributions T_DECL(confined_fileport_race, "test for rdar://69922255")
133*d8b80295SApple OSS Distributions {
134*d8b80295SApple OSS Distributions 	int fd = -1;
135*d8b80295SApple OSS Distributions 	pthread_t t;
136*d8b80295SApple OSS Distributions 	mach_port_t p = MACH_PORT_NULL;
137*d8b80295SApple OSS Distributions 
138*d8b80295SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(pthread_create(&t, NULL, confine_thread, &fd),
139*d8b80295SApple OSS Distributions 	    "pthread_create");
140*d8b80295SApple OSS Distributions 
141*d8b80295SApple OSS Distributions 	for (int i = 0; i < 100 * 1000; i++) {
142*d8b80295SApple OSS Distributions 		fd = open("/dev/null", O_RDONLY | 0x08000000 /* O_CLOFORK */);
143*d8b80295SApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(fd, "open(/dev/null)");
144*d8b80295SApple OSS Distributions 		if (fileport_makeport(fd, &p) == 0) {
145*d8b80295SApple OSS Distributions 			T_QUIET; T_ASSERT_EQ(fcntl(fd, F_GETCONFINED), 0,
146*d8b80295SApple OSS Distributions 			    "should never get a confined fd: %d", fd);
147*d8b80295SApple OSS Distributions 			mach_port_deallocate(mach_task_self(), p);
148*d8b80295SApple OSS Distributions 		}
149*d8b80295SApple OSS Distributions 
150*d8b80295SApple OSS Distributions 		close(fd);
151*d8b80295SApple OSS Distributions 	}
152*d8b80295SApple OSS Distributions }
153