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 | #include <darwintest.h> #include <darwintest_utils.h> #include <pthread.h> #include <sys/select.h> #include <sys/fileport.h> #include <sys/fcntl.h> #include <mach/mach.h> T_GLOBAL_META( T_META_NAMESPACE("xnu.fd"), T_META_RUN_CONCURRENTLY(true)); static void * fd_select_close_helper(void *ctx) { int fd = *(int *)ctx; // wait for the thread to enter select usleep(500000); close(fd); return NULL; } T_DECL(fd_select_close, "Test for 54795873: make sure close breaks out of select") { fd_set read_fd; int pair[2], rc; pthread_t th; rc = socketpair(PF_LOCAL, SOCK_STREAM, 0, pair); T_ASSERT_POSIX_SUCCESS(rc, "socketpair"); pthread_create(&th, NULL, fd_select_close_helper, pair); FD_ZERO(&read_fd); FD_SET(pair[0], &read_fd); rc = select(pair[0] + 1, &read_fd, NULL, NULL, NULL); T_EXPECT_POSIX_FAILURE(rc, EBADF, "select broke out with EBADF"); } T_DECL(fd_select_ebadf, "Test that select on closed fd returns EBADF") { fd_set read_fd; int pair[2], rc; rc = socketpair(PF_LOCAL, SOCK_STREAM, 0, pair); T_ASSERT_POSIX_SUCCESS(rc, "socketpair"); FD_ZERO(&read_fd); FD_SET(pair[0], &read_fd); T_ASSERT_POSIX_SUCCESS(close(pair[0]), "close(%d)", pair[0]); rc = select(pair[0] + 1, &read_fd, NULL, NULL, NULL); T_EXPECT_POSIX_FAILURE(rc, EBADF, "select returned with EBADF"); } static void * fd_stress_dup2_close_fun(void *ctx) { int thno = (int)(long)ctx; int count = 10000, rc; for (int i = 1; i <= count; i++) { rc = dup2(STDIN_FILENO, 42); T_QUIET; T_EXPECT_POSIX_SUCCESS(rc, "dup2(%d, 42)", STDIN_FILENO); if (thno == 3) { rc = close(42); if (rc == -1) { T_QUIET; T_EXPECT_POSIX_FAILURE(rc, EBADF, "close(42)"); } } if (i % 1000 == 0) { T_LOG("thread %d: %d/%d dups\n", thno, i, count); } } return NULL; } T_DECL(fd_stress_dup2_close, "Stress test races between dup2 and close") { pthread_t th[4]; int rc; for (int i = 0; i < 4; i++) { rc = pthread_create(&th[i], NULL, fd_stress_dup2_close_fun, (void *)(long)i); T_ASSERT_POSIX_ZERO(rc, "pthread_create"); } for (int i = 0; i < 4; i++) { pthread_join(th[i], NULL); } } T_DECL(fd_dup2_erase_clofork_58446996, "Make sure dup2() doesn't inherit flags from an old fd") { int fd1, fd2; fd1 = open("/dev/null", O_RDONLY | O_CLOEXEC); T_ASSERT_POSIX_SUCCESS(fd1, "open(/dev/null)"); fd2 = open("/dev/null", O_RDONLY | O_CLOEXEC); T_ASSERT_POSIX_SUCCESS(fd2, "open(/dev/null)"); T_ASSERT_POSIX_SUCCESS(dup2(fd1, fd2), "dup2(fd1, fd2)"); T_EXPECT_EQ(fcntl(fd2, F_GETFD, 0), 0, "neither FD_CLOEXEC nor FD_CLOFORK should be set"); } struct confined_race_state { int fd; bool made; }; static void * confine_thread(void *data) { volatile int *fdp = data; for (;;) { fcntl(*fdp, F_SETCONFINED, 1); } return NULL; } T_DECL(confined_fileport_race, "test for rdar://69922255") { int fd = -1; pthread_t t; mach_port_t p = MACH_PORT_NULL; T_ASSERT_POSIX_SUCCESS(pthread_create(&t, NULL, confine_thread, &fd), "pthread_create"); for (int i = 0; i < 100 * 1000; i++) { fd = open("/dev/null", O_RDONLY | 0x08000000 /* O_CLOFORK */); T_QUIET; T_ASSERT_POSIX_SUCCESS(fd, "open(/dev/null)"); if (fileport_makeport(fd, &p) == 0) { T_QUIET; T_ASSERT_EQ(fcntl(fd, F_GETCONFINED), 0, "should never get a confined fd: %d", fd); mach_port_deallocate(mach_task_self(), p); } close(fd); } } |