summaryrefslogtreecommitdiffhomepage
path: root/test/syscalls/linux/poll.cc
blob: 5ce7e8c8debc3ad831f69c296a483b0808f7c8b8 (plain)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// Copyright 2018 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <poll.h>
#include <sys/resource.h>
#include <sys/socket.h>
#include <sys/types.h>

#include <algorithm>
#include <iostream>

#include "gtest/gtest.h"
#include "absl/synchronization/notification.h"
#include "absl/time/clock.h"
#include "absl/time/time.h"
#include "test/syscalls/linux/base_poll_test.h"
#include "test/util/eventfd_util.h"
#include "test/util/file_descriptor.h"
#include "test/util/logging.h"
#include "test/util/test_util.h"
#include "test/util/thread_util.h"

namespace gvisor {
namespace testing {
namespace {

class PollTest : public BasePollTest {
 protected:
  void SetUp() override { BasePollTest::SetUp(); }
  void TearDown() override { BasePollTest::TearDown(); }
};

TEST_F(PollTest, InvalidFds) {
  // fds is invalid because it's null, but we tell ppoll the length is non-zero.
  EXPECT_THAT(poll(nullptr, 1, 1), SyscallFailsWithErrno(EFAULT));
  EXPECT_THAT(poll(nullptr, -1, 1), SyscallFailsWithErrno(EINVAL));
}

TEST_F(PollTest, NullFds) {
  EXPECT_THAT(poll(nullptr, 0, 10), SyscallSucceeds());
}

TEST_F(PollTest, ZeroTimeout) {
  EXPECT_THAT(poll(nullptr, 0, 0), SyscallSucceeds());
}

// If random S/R interrupts the poll, SIGALRM may be delivered before poll
// restarts, causing the poll to hang forever.
TEST_F(PollTest, NegativeTimeout) {
  // Negative timeout mean wait forever so set a timer.
  SetTimer(absl::Milliseconds(100));
  EXPECT_THAT(poll(nullptr, 0, -1), SyscallFailsWithErrno(EINTR));
  EXPECT_TRUE(TimerFired());
}

void NonBlockingReadableTest(int16_t mask) {
  // Create a pipe.
  int fds[2];
  ASSERT_THAT(pipe(fds), SyscallSucceeds());

  FileDescriptor fd0(fds[0]);
  FileDescriptor fd1(fds[1]);

  // Write some data to the pipe.
  char s[] = "foo\n";
  ASSERT_THAT(WriteFd(fd1.get(), s, strlen(s) + 1), SyscallSucceeds());

  // Poll on the reader fd with POLLIN event.
  struct pollfd poll_fd = {fd0.get(), mask, 0};
  EXPECT_THAT(RetryEINTR(poll)(&poll_fd, 1, 0), SyscallSucceedsWithValue(1));

  // Should trigger POLLIN event.
  EXPECT_EQ(poll_fd.revents & mask, mask);
}

TEST_F(PollTest, NonBlockingEventPOLLIN) { NonBlockingReadableTest(POLLIN); }

TEST_F(PollTest, NonBlockingEventPOLLRDNORM) {
  NonBlockingReadableTest(POLLRDNORM);
}

TEST_F(PollTest, NonBlockingEventPOLLRDNORM_POLLIN) {
  NonBlockingReadableTest(POLLRDNORM | POLLIN);
}

void BlockingReadableTest(int16_t mask) {
  // Create a pipe.
  int fds[2];
  ASSERT_THAT(pipe(fds), SyscallSucceeds());

  FileDescriptor fd0(fds[0]);
  FileDescriptor fd1(fds[1]);

  // Start a blocking poll on the read fd.
  absl::Notification notify;
  ScopedThread t([&fd0, &notify, &mask]() {
    notify.Notify();

    // Poll on the reader fd with readable event.
    struct pollfd poll_fd = {fd0.get(), mask, 0};
    EXPECT_THAT(RetryEINTR(poll)(&poll_fd, 1, -1), SyscallSucceedsWithValue(1));

    // Should trigger readable event.
    EXPECT_EQ(poll_fd.revents & mask, mask);
  });

  notify.WaitForNotification();
  absl::SleepFor(absl::Seconds(1.0));

  // Write some data to the pipe.
  char s[] = "foo\n";
  ASSERT_THAT(WriteFd(fd1.get(), s, strlen(s) + 1), SyscallSucceeds());
}

TEST_F(PollTest, BlockingEventPOLLIN) { BlockingReadableTest(POLLIN); }

TEST_F(PollTest, BlockingEventPOLLRDNORM) { BlockingReadableTest(POLLRDNORM); }

TEST_F(PollTest, BlockingEventPOLLRDNORM_POLLIN) {
  BlockingReadableTest(POLLRDNORM | POLLIN);
}

void WritableTest(int16_t mask, int timeout) {
  // Create a pipe.
  int fds[2];
  ASSERT_THAT(pipe(fds), SyscallSucceeds());

  FileDescriptor fd0(fds[0]);
  FileDescriptor fd1(fds[1]);

  // In a newly created pipe 2nd fd should be writable.

  // Poll on second fd for a writable event.
  struct pollfd poll_fd = {fd1.get(), mask, 0};
  EXPECT_THAT(RetryEINTR(poll)(&poll_fd, 1, timeout),
              SyscallSucceedsWithValue(1));

  // Should trigger a writable event.
  EXPECT_EQ(poll_fd.revents & mask, mask);
}

TEST_F(PollTest, NonBlockingEventPOLLOUT) {
  WritableTest(POLLOUT, /*timeout=*/0);
}

TEST_F(PollTest, NonBlockingEventPOLLWRNORM) {
  WritableTest(POLLWRNORM, /*timeout=*/0);
}

TEST_F(PollTest, NonBlockingEventPOLLWRNORM_POLLOUT) {
  WritableTest(POLLWRNORM | POLLOUT, /*timeout=*/0);
}

TEST_F(PollTest, BlockingEventPOLLOUT) {
  WritableTest(POLLOUT, /*timeout=*/-1);
}

TEST_F(PollTest, BlockingEventPOLLWRNORM) {
  WritableTest(POLLWRNORM, /*timeout=*/-1);
}

TEST_F(PollTest, BlockingEventPOLLWRNORM_POLLOUT) {
  WritableTest(POLLWRNORM | POLLOUT, /*timeout=*/-1);
}

TEST_F(PollTest, NonBlockingEventPOLLHUP) {
  // Create a pipe.
  int fds[2];
  ASSERT_THAT(pipe(fds), SyscallSucceeds());

  FileDescriptor fd0(fds[0]);
  FileDescriptor fd1(fds[1]);

  // Close the writer fd.
  fd1.reset();

  // Poll on the reader fd with POLLIN event.
  struct pollfd poll_fd = {fd0.get(), POLLIN, 0};
  EXPECT_THAT(RetryEINTR(poll)(&poll_fd, 1, 0), SyscallSucceedsWithValue(1));

  // Should trigger POLLHUP event.
  EXPECT_EQ(poll_fd.revents & POLLHUP, POLLHUP);

  // Should not trigger POLLIN event.
  EXPECT_EQ(poll_fd.revents & POLLIN, 0);
}

TEST_F(PollTest, BlockingEventPOLLHUP) {
  // Create a pipe.
  int fds[2];
  ASSERT_THAT(pipe(fds), SyscallSucceeds());

  FileDescriptor fd0(fds[0]);
  FileDescriptor fd1(fds[1]);

  // Start a blocking poll on the read fd.
  absl::Notification notify;
  ScopedThread t([&fd0, &notify]() {
    notify.Notify();

    // Poll on the reader fd with POLLIN event.
    struct pollfd poll_fd = {fd0.get(), POLLIN, 0};
    EXPECT_THAT(RetryEINTR(poll)(&poll_fd, 1, -1), SyscallSucceedsWithValue(1));

    // Should trigger POLLHUP event.
    EXPECT_EQ(poll_fd.revents & POLLHUP, POLLHUP);

    // Should not trigger POLLIN event.
    EXPECT_EQ(poll_fd.revents & POLLIN, 0);
  });

  notify.WaitForNotification();
  absl::SleepFor(absl::Seconds(1.0));

  // Write some data and close the writer fd.
  fd1.reset();
}

TEST_F(PollTest, NonBlockingEventPOLLERR) {
  // Create a pipe.
  int fds[2];
  ASSERT_THAT(pipe(fds), SyscallSucceeds());

  FileDescriptor fd0(fds[0]);
  FileDescriptor fd1(fds[1]);

  // Close the reader fd.
  fd0.reset();

  // Poll on the writer fd with POLLOUT event.
  struct pollfd poll_fd = {fd1.get(), POLLOUT, 0};
  EXPECT_THAT(RetryEINTR(poll)(&poll_fd, 1, 0), SyscallSucceedsWithValue(1));

  // Should trigger POLLERR event.
  EXPECT_EQ(poll_fd.revents & POLLERR, POLLERR);

  // Should also trigger POLLOUT event.
  EXPECT_EQ(poll_fd.revents & POLLOUT, POLLOUT);
}

// This test will validate that if an FD is already ready on some event, whether
// it's POLLIN or POLLOUT it will not immediately return unless that's actually
// what the caller was interested in.
TEST_F(PollTest, ImmediatelyReturnOnlyOnPollEvents) {
  // Create a pipe.
  int fds[2];
  ASSERT_THAT(pipe(fds), SyscallSucceeds());

  FileDescriptor fd0(fds[0]);
  FileDescriptor fd1(fds[1]);

  // Wait for read related event on the write side of the pipe, since a write
  // is possible on fds[1] it would mean that POLLOUT would return immediately.
  // We should make sure that we're not woken up with that state that we didn't
  // specificially request.
  constexpr int kTimeoutMs = 100;
  struct pollfd poll_fd = {fd1.get(), POLLIN | POLLPRI | POLLRDHUP, 0};
  EXPECT_THAT(RetryEINTR(poll)(&poll_fd, 1, kTimeoutMs),
              SyscallSucceedsWithValue(0));  // We should timeout.
  EXPECT_EQ(poll_fd.revents, 0);  // Nothing should be in returned events.

  // Now let's poll on POLLOUT and we should get back 1 fd as being ready and
  // it should contain POLLOUT in the revents.
  poll_fd.events = POLLOUT;
  EXPECT_THAT(RetryEINTR(poll)(&poll_fd, 1, kTimeoutMs),
              SyscallSucceedsWithValue(1));  // 1 fd should have an event.
  EXPECT_EQ(poll_fd.revents, POLLOUT);       // POLLOUT should be in revents.
}

// This test validates that poll(2) while data is available immediately returns.
TEST_F(PollTest, PollLevelTriggered) {
  int fds[2] = {};
  ASSERT_THAT(socketpair(AF_UNIX, SOCK_STREAM, /*protocol=*/0, fds),
              SyscallSucceeds());

  FileDescriptor fd0(fds[0]);
  FileDescriptor fd1(fds[1]);

  // Write two bytes to the socket.
  const char* kBuf = "aa";
  ASSERT_THAT(RetryEINTR(send)(fd0.get(), kBuf, /*len=*/2, /*flags=*/0),
              SyscallSucceedsWithValue(2));  // 2 bytes should be written.

  // Poll(2) should immediately return as there is data available to read.
  constexpr int kInfiniteTimeout = -1;
  struct pollfd poll_fd = {fd1.get(), POLLIN, 0};
  ASSERT_THAT(RetryEINTR(poll)(&poll_fd, /*nfds=*/1, kInfiniteTimeout),
              SyscallSucceedsWithValue(1));  // 1 fd should be ready to read.
  EXPECT_NE(poll_fd.revents & POLLIN, 0);

  // Read a single byte.
  char read_byte = 0;
  ASSERT_THAT(RetryEINTR(recv)(fd1.get(), &read_byte, /*len=*/1, /*flags=*/0),
              SyscallSucceedsWithValue(1));  // 1 byte should be read.
  ASSERT_EQ(read_byte, 'a');  // We should have read a single 'a'.

  // Create a separate pollfd for our second poll.
  struct pollfd poll_fd_after = {fd1.get(), POLLIN, 0};

  // Poll(2) should again immediately return since we only read one byte.
  ASSERT_THAT(RetryEINTR(poll)(&poll_fd_after, /*nfds=*/1, kInfiniteTimeout),
              SyscallSucceedsWithValue(1));  // 1 fd should be ready to read.
  EXPECT_NE(poll_fd_after.revents & POLLIN, 0);
}

TEST_F(PollTest, Nfds) {
  // Stash value of RLIMIT_NOFILES.
  struct rlimit rlim;
  TEST_PCHECK(getrlimit(RLIMIT_NOFILE, &rlim) == 0);

  // gVisor caps the number of FDs that epoll can use beyond RLIMIT_NOFILE.
  constexpr rlim_t maxFD = 4096;
  if (rlim.rlim_cur > maxFD) {
    rlim.rlim_cur = maxFD;
    TEST_PCHECK(setrlimit(RLIMIT_NOFILE, &rlim) == 0);
  }

  rlim_t max_fds = rlim.rlim_cur;
  std::cout << "Using limit: " << max_fds << std::endl;

  // Create an eventfd. Since its value is initially zero, it is writable.
  FileDescriptor efd = ASSERT_NO_ERRNO_AND_VALUE(NewEventFD());

  // Create the biggest possible pollfd array such that each element is valid.
  // Each entry in the 'fds' array refers to the eventfd and polls for
  // "writable" events (events=POLLOUT). This essentially guarantees that the
  // poll() is a no-op and allows negative testing of the 'nfds' parameter.
  std::vector<struct pollfd> fds(max_fds + 1,
                                 {.fd = efd.get(), .events = POLLOUT});

  // Verify that 'nfds' up to RLIMIT_NOFILE are allowed.
  EXPECT_THAT(RetryEINTR(poll)(fds.data(), 1, 1), SyscallSucceedsWithValue(1));
  EXPECT_THAT(RetryEINTR(poll)(fds.data(), max_fds / 2, 1),
              SyscallSucceedsWithValue(max_fds / 2));
  EXPECT_THAT(RetryEINTR(poll)(fds.data(), max_fds, 1),
              SyscallSucceedsWithValue(max_fds));

  // If 'nfds' exceeds RLIMIT_NOFILE then it must fail with EINVAL.
  EXPECT_THAT(poll(fds.data(), max_fds + 1, 1), SyscallFailsWithErrno(EINVAL));
}

}  // namespace
}  // namespace testing
}  // namespace gvisor