github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/syscalls/linux/poll.cc (about) 1 // Copyright 2018 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #include <poll.h> 16 #include <sys/resource.h> 17 #include <sys/socket.h> 18 #include <sys/types.h> 19 20 #include <algorithm> 21 #include <iostream> 22 23 #include "gtest/gtest.h" 24 #include "absl/synchronization/notification.h" 25 #include "absl/time/clock.h" 26 #include "absl/time/time.h" 27 #include "test/syscalls/linux/base_poll_test.h" 28 #include "test/util/eventfd_util.h" 29 #include "test/util/file_descriptor.h" 30 #include "test/util/logging.h" 31 #include "test/util/test_util.h" 32 #include "test/util/thread_util.h" 33 34 namespace gvisor { 35 namespace testing { 36 namespace { 37 38 class PollTest : public BasePollTest { 39 protected: 40 void SetUp() override { BasePollTest::SetUp(); } 41 void TearDown() override { BasePollTest::TearDown(); } 42 }; 43 44 TEST_F(PollTest, InvalidFds) { 45 // fds is invalid because it's null, but we tell ppoll the length is non-zero. 46 EXPECT_THAT(poll(nullptr, 1, 1), SyscallFailsWithErrno(EFAULT)); 47 EXPECT_THAT(poll(nullptr, -1, 1), SyscallFailsWithErrno(EINVAL)); 48 } 49 50 TEST_F(PollTest, NullFds) { 51 EXPECT_THAT(poll(nullptr, 0, 10), SyscallSucceeds()); 52 } 53 54 TEST_F(PollTest, ZeroTimeout) { 55 EXPECT_THAT(poll(nullptr, 0, 0), SyscallSucceeds()); 56 } 57 58 // If random S/R interrupts the poll, SIGALRM may be delivered before poll 59 // restarts, causing the poll to hang forever. 60 TEST_F(PollTest, NegativeTimeout) { 61 // Negative timeout mean wait forever so set a timer. 62 SetTimer(absl::Milliseconds(100)); 63 EXPECT_THAT(poll(nullptr, 0, -1), SyscallFailsWithErrno(EINTR)); 64 EXPECT_TRUE(TimerFired()); 65 } 66 67 void NonBlockingReadableTest(int16_t mask) { 68 // Create a pipe. 69 int fds[2]; 70 ASSERT_THAT(pipe(fds), SyscallSucceeds()); 71 72 FileDescriptor fd0(fds[0]); 73 FileDescriptor fd1(fds[1]); 74 75 // Write some data to the pipe. 76 char s[] = "foo\n"; 77 ASSERT_THAT(WriteFd(fd1.get(), s, strlen(s) + 1), SyscallSucceeds()); 78 79 // Poll on the reader fd with POLLIN event. 80 struct pollfd poll_fd = {fd0.get(), mask, 0}; 81 EXPECT_THAT(RetryEINTR(poll)(&poll_fd, 1, 0), SyscallSucceedsWithValue(1)); 82 83 // Should trigger POLLIN event. 84 EXPECT_EQ(poll_fd.revents & mask, mask); 85 } 86 87 TEST_F(PollTest, NonBlockingEventPOLLIN) { NonBlockingReadableTest(POLLIN); } 88 89 TEST_F(PollTest, NonBlockingEventPOLLRDNORM) { 90 NonBlockingReadableTest(POLLRDNORM); 91 } 92 93 TEST_F(PollTest, NonBlockingEventPOLLRDNORM_POLLIN) { 94 NonBlockingReadableTest(POLLRDNORM | POLLIN); 95 } 96 97 void BlockingReadableTest(int16_t mask) { 98 // Create a pipe. 99 int fds[2]; 100 ASSERT_THAT(pipe(fds), SyscallSucceeds()); 101 102 FileDescriptor fd0(fds[0]); 103 FileDescriptor fd1(fds[1]); 104 105 // Start a blocking poll on the read fd. 106 absl::Notification notify; 107 ScopedThread t([&fd0, ¬ify, &mask]() { 108 notify.Notify(); 109 110 // Poll on the reader fd with readable event. 111 struct pollfd poll_fd = {fd0.get(), mask, 0}; 112 EXPECT_THAT(RetryEINTR(poll)(&poll_fd, 1, -1), SyscallSucceedsWithValue(1)); 113 114 // Should trigger readable event. 115 EXPECT_EQ(poll_fd.revents & mask, mask); 116 }); 117 118 notify.WaitForNotification(); 119 absl::SleepFor(absl::Seconds(1)); 120 121 // Write some data to the pipe. 122 char s[] = "foo\n"; 123 ASSERT_THAT(WriteFd(fd1.get(), s, strlen(s) + 1), SyscallSucceeds()); 124 } 125 126 TEST_F(PollTest, BlockingEventPOLLIN) { BlockingReadableTest(POLLIN); } 127 128 TEST_F(PollTest, BlockingEventPOLLRDNORM) { BlockingReadableTest(POLLRDNORM); } 129 130 TEST_F(PollTest, BlockingEventPOLLRDNORM_POLLIN) { 131 BlockingReadableTest(POLLRDNORM | POLLIN); 132 } 133 134 void WritableTest(int16_t mask, int timeout) { 135 // Create a pipe. 136 int fds[2]; 137 ASSERT_THAT(pipe(fds), SyscallSucceeds()); 138 139 FileDescriptor fd0(fds[0]); 140 FileDescriptor fd1(fds[1]); 141 142 // In a newly created pipe 2nd fd should be writable. 143 144 // Poll on second fd for a writable event. 145 struct pollfd poll_fd = {fd1.get(), mask, 0}; 146 EXPECT_THAT(RetryEINTR(poll)(&poll_fd, 1, timeout), 147 SyscallSucceedsWithValue(1)); 148 149 // Should trigger a writable event. 150 EXPECT_EQ(poll_fd.revents & mask, mask); 151 } 152 153 TEST_F(PollTest, NonBlockingEventPOLLOUT) { 154 WritableTest(POLLOUT, /*timeout=*/0); 155 } 156 157 TEST_F(PollTest, NonBlockingEventPOLLWRNORM) { 158 WritableTest(POLLWRNORM, /*timeout=*/0); 159 } 160 161 TEST_F(PollTest, NonBlockingEventPOLLWRNORM_POLLOUT) { 162 WritableTest(POLLWRNORM | POLLOUT, /*timeout=*/0); 163 } 164 165 TEST_F(PollTest, BlockingEventPOLLOUT) { 166 WritableTest(POLLOUT, /*timeout=*/-1); 167 } 168 169 TEST_F(PollTest, BlockingEventPOLLWRNORM) { 170 WritableTest(POLLWRNORM, /*timeout=*/-1); 171 } 172 173 TEST_F(PollTest, BlockingEventPOLLWRNORM_POLLOUT) { 174 WritableTest(POLLWRNORM | POLLOUT, /*timeout=*/-1); 175 } 176 177 TEST_F(PollTest, NonBlockingEventPOLLHUP) { 178 // Create a pipe. 179 int fds[2]; 180 ASSERT_THAT(pipe(fds), SyscallSucceeds()); 181 182 FileDescriptor fd0(fds[0]); 183 FileDescriptor fd1(fds[1]); 184 185 // Close the writer fd. 186 fd1.reset(); 187 188 // Poll on the reader fd with POLLIN event. 189 struct pollfd poll_fd = {fd0.get(), POLLIN, 0}; 190 EXPECT_THAT(RetryEINTR(poll)(&poll_fd, 1, 0), SyscallSucceedsWithValue(1)); 191 192 // Should trigger POLLHUP event. 193 EXPECT_EQ(poll_fd.revents & POLLHUP, POLLHUP); 194 195 // Should not trigger POLLIN event. 196 EXPECT_EQ(poll_fd.revents & POLLIN, 0); 197 } 198 199 TEST_F(PollTest, BlockingEventPOLLHUP) { 200 // Create a pipe. 201 int fds[2]; 202 ASSERT_THAT(pipe(fds), SyscallSucceeds()); 203 204 FileDescriptor fd0(fds[0]); 205 FileDescriptor fd1(fds[1]); 206 207 // Start a blocking poll on the read fd. 208 absl::Notification notify; 209 ScopedThread t([&fd0, ¬ify]() { 210 notify.Notify(); 211 212 // Poll on the reader fd with POLLIN event. 213 struct pollfd poll_fd = {fd0.get(), POLLIN, 0}; 214 EXPECT_THAT(RetryEINTR(poll)(&poll_fd, 1, -1), SyscallSucceedsWithValue(1)); 215 216 // Should trigger POLLHUP event. 217 EXPECT_EQ(poll_fd.revents & POLLHUP, POLLHUP); 218 219 // Should not trigger POLLIN event. 220 EXPECT_EQ(poll_fd.revents & POLLIN, 0); 221 }); 222 223 notify.WaitForNotification(); 224 absl::SleepFor(absl::Seconds(1)); 225 226 // Write some data and close the writer fd. 227 fd1.reset(); 228 } 229 230 TEST_F(PollTest, NonBlockingEventPOLLERR) { 231 // Create a pipe. 232 int fds[2]; 233 ASSERT_THAT(pipe(fds), SyscallSucceeds()); 234 235 FileDescriptor fd0(fds[0]); 236 FileDescriptor fd1(fds[1]); 237 238 // Close the reader fd. 239 fd0.reset(); 240 241 // Poll on the writer fd with POLLOUT event. 242 struct pollfd poll_fd = {fd1.get(), POLLOUT, 0}; 243 EXPECT_THAT(RetryEINTR(poll)(&poll_fd, 1, 0), SyscallSucceedsWithValue(1)); 244 245 // Should trigger POLLERR event. 246 EXPECT_EQ(poll_fd.revents & POLLERR, POLLERR); 247 248 // Should also trigger POLLOUT event. 249 EXPECT_EQ(poll_fd.revents & POLLOUT, POLLOUT); 250 } 251 252 // This test will validate that if an FD is already ready on some event, whether 253 // it's POLLIN or POLLOUT it will not immediately return unless that's actually 254 // what the caller was interested in. 255 TEST_F(PollTest, ImmediatelyReturnOnlyOnPollEvents) { 256 // Create a pipe. 257 int fds[2]; 258 ASSERT_THAT(pipe(fds), SyscallSucceeds()); 259 260 FileDescriptor fd0(fds[0]); 261 FileDescriptor fd1(fds[1]); 262 263 // Wait for read related event on the write side of the pipe, since a write 264 // is possible on fds[1] it would mean that POLLOUT would return immediately. 265 // We should make sure that we're not woken up with that state that we didn't 266 // specificially request. 267 constexpr int kTimeoutMs = 100; 268 struct pollfd poll_fd = {fd1.get(), POLLIN | POLLPRI | POLLRDHUP, 0}; 269 EXPECT_THAT(RetryEINTR(poll)(&poll_fd, 1, kTimeoutMs), 270 SyscallSucceedsWithValue(0)); // We should timeout. 271 EXPECT_EQ(poll_fd.revents, 0); // Nothing should be in returned events. 272 273 // Now let's poll on POLLOUT and we should get back 1 fd as being ready and 274 // it should contain POLLOUT in the revents. 275 poll_fd.events = POLLOUT; 276 EXPECT_THAT(RetryEINTR(poll)(&poll_fd, 1, kTimeoutMs), 277 SyscallSucceedsWithValue(1)); // 1 fd should have an event. 278 EXPECT_EQ(poll_fd.revents, POLLOUT); // POLLOUT should be in revents. 279 } 280 281 // This test validates that poll(2) while data is available immediately returns. 282 TEST_F(PollTest, PollLevelTriggered) { 283 int fds[2] = {}; 284 ASSERT_THAT(socketpair(AF_UNIX, SOCK_STREAM, /*protocol=*/0, fds), 285 SyscallSucceeds()); 286 287 FileDescriptor fd0(fds[0]); 288 FileDescriptor fd1(fds[1]); 289 290 // Write two bytes to the socket. 291 const char* kBuf = "aa"; 292 ASSERT_THAT(RetryEINTR(send)(fd0.get(), kBuf, /*len=*/2, /*flags=*/0), 293 SyscallSucceedsWithValue(2)); // 2 bytes should be written. 294 295 // Poll(2) should immediately return as there is data available to read. 296 constexpr int kInfiniteTimeout = -1; 297 struct pollfd poll_fd = {fd1.get(), POLLIN, 0}; 298 ASSERT_THAT(RetryEINTR(poll)(&poll_fd, /*nfds=*/1, kInfiniteTimeout), 299 SyscallSucceedsWithValue(1)); // 1 fd should be ready to read. 300 EXPECT_NE(poll_fd.revents & POLLIN, 0); 301 302 // Read a single byte. 303 char read_byte = 0; 304 ASSERT_THAT(RetryEINTR(recv)(fd1.get(), &read_byte, /*len=*/1, /*flags=*/0), 305 SyscallSucceedsWithValue(1)); // 1 byte should be read. 306 ASSERT_EQ(read_byte, 'a'); // We should have read a single 'a'. 307 308 // Create a separate pollfd for our second poll. 309 struct pollfd poll_fd_after = {fd1.get(), POLLIN, 0}; 310 311 // Poll(2) should again immediately return since we only read one byte. 312 ASSERT_THAT(RetryEINTR(poll)(&poll_fd_after, /*nfds=*/1, kInfiniteTimeout), 313 SyscallSucceedsWithValue(1)); // 1 fd should be ready to read. 314 EXPECT_NE(poll_fd_after.revents & POLLIN, 0); 315 } 316 317 TEST_F(PollTest, Nfds) { 318 // Stash value of RLIMIT_NOFILES. 319 struct rlimit rlim; 320 TEST_PCHECK(getrlimit(RLIMIT_NOFILE, &rlim) == 0); 321 322 // gVisor caps the number of FDs that epoll can use beyond RLIMIT_NOFILE. 323 constexpr rlim_t maxFD = 4096; 324 if (rlim.rlim_cur > maxFD) { 325 rlim.rlim_cur = maxFD; 326 TEST_PCHECK(setrlimit(RLIMIT_NOFILE, &rlim) == 0); 327 } 328 329 rlim_t max_fds = rlim.rlim_cur; 330 std::cout << "Using limit: " << max_fds << std::endl; 331 332 // Create an eventfd. Since its value is initially zero, it is writable. 333 FileDescriptor efd = ASSERT_NO_ERRNO_AND_VALUE(NewEventFD()); 334 335 // Create the biggest possible pollfd array such that each element is valid. 336 // Each entry in the 'fds' array refers to the eventfd and polls for 337 // "writable" events (events=POLLOUT). This essentially guarantees that the 338 // poll() is a no-op and allows negative testing of the 'nfds' parameter. 339 std::vector<struct pollfd> fds(max_fds + 1, 340 {.fd = efd.get(), .events = POLLOUT}); 341 342 // Verify that 'nfds' up to RLIMIT_NOFILE are allowed. 343 EXPECT_THAT(RetryEINTR(poll)(fds.data(), 1, 1), SyscallSucceedsWithValue(1)); 344 EXPECT_THAT(RetryEINTR(poll)(fds.data(), max_fds / 2, 1), 345 SyscallSucceedsWithValue(max_fds / 2)); 346 EXPECT_THAT(RetryEINTR(poll)(fds.data(), max_fds, 1), 347 SyscallSucceedsWithValue(max_fds)); 348 349 // If 'nfds' exceeds RLIMIT_NOFILE then it must fail with EINVAL. 350 EXPECT_THAT(poll(fds.data(), max_fds + 1, 1), SyscallFailsWithErrno(EINVAL)); 351 } 352 353 } // namespace 354 } // namespace testing 355 } // namespace gvisor