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