5 #include <sys/syscall.h>
12 #include <sys/select.h>
13 #include <sys/epoll.h>
16 #include <sys/resource.h>
25 #define NR_ITER 1000 /* for stress-tests */
27 #define MIN_NR_FDS 5 /* the minimum number of open FDs required for the test to run */
28 #define BIG_SELECT_FD 1022
30 #define MSEC_PER_USEC 1000
31 #define MSEC_PER_NSEC (MSEC_PER_USEC * 1000)
33 static int timeout
; /* seconds, -1 to disable */
34 volatile static int stop_thread
;
37 struct ppoll_thread_data
{
42 void test_select_big(void)
44 fd_set rfds
, wfds
, exfds
;
54 fd2
= dup2(wait_fd
, BIG_SELECT_FD
);
62 tv
.tv_usec
= timeout
* MSEC_PER_USEC
;
65 ret
= select(fd2
+ 1, &rfds
, &wfds
, &exfds
, &tv
);
67 ret
= select(fd2
+ 1, &rfds
, &wfds
, &exfds
, NULL
);
73 printf("# [select] data available\n");
74 ret
= read(wait_fd
, buf
, BUF_SIZE
);
76 perror("[select] read");
79 printf("# [select] timeout\n");
82 ret
= close(BIG_SELECT_FD
);
91 void test_pselect(void)
99 FD_SET(wait_fd
, &rfds
);
102 tv
.tv_nsec
= timeout
* MSEC_PER_NSEC
;
105 ret
= pselect(1, &rfds
, NULL
, NULL
, &tv
, NULL
);
107 ret
= pselect(1, &rfds
, NULL
, NULL
, NULL
, NULL
);
113 printf("# [pselect] data available\n");
114 ret
= read(wait_fd
, buf
, BUF_SIZE
);
116 perror("[pselect] read");
119 printf("# [pselect] timeout\n");
124 void test_select(void)
132 FD_SET(wait_fd
, &rfds
);
135 tv
.tv_usec
= timeout
* MSEC_PER_USEC
;
138 ret
= select(1, &rfds
, NULL
, NULL
, &tv
);
140 ret
= select(1, &rfds
, NULL
, NULL
, NULL
);
146 printf("# [select] data available\n");
147 ret
= read(wait_fd
, buf
, BUF_SIZE
);
149 perror("[select] read");
152 printf("# [select] timeout\n");
159 struct pollfd ufds
[NB_FD
];
163 ufds
[0].fd
= wait_fd
;
164 ufds
[0].events
= POLLIN
|POLLPRI
;
166 ret
= poll(ufds
, 1, timeout
);
170 } else if (ret
> 0) {
171 printf("# [poll] data available\n");
172 ret
= read(wait_fd
, buf
, BUF_SIZE
);
174 perror("[poll] read");
177 printf("# [poll] timeout\n");
181 void test_ppoll(void)
183 struct pollfd ufds
[NB_FD
];
188 ufds
[0].fd
= wait_fd
;
189 ufds
[0].events
= POLLIN
|POLLPRI
;
193 ts
.tv_nsec
= timeout
* MSEC_PER_NSEC
;
194 ret
= ppoll(ufds
, 1, &ts
, NULL
);
196 ret
= ppoll(ufds
, 1, NULL
, NULL
);
202 } else if (ret
> 0) {
203 printf("# [ppoll] data available\n");
204 ret
= read(wait_fd
, buf
, BUF_SIZE
);
206 perror("[ppoll] read");
209 printf("# [ppoll] timeout\n");
213 void test_ppoll_big(void)
215 struct pollfd ufds
[MAX_FDS
];
217 int ret
, i
, fds
[MAX_FDS
];
219 for (i
= 0; i
< MAX_FDS
; i
++) {
220 fds
[i
] = dup(wait_fd
);
225 ufds
[i
].events
= POLLIN
|POLLPRI
;
228 ret
= ppoll(ufds
, MAX_FDS
, NULL
, NULL
);
232 } else if (ret
> 0) {
233 printf("# [ppoll] data available\n");
234 ret
= read(wait_fd
, buf
, BUF_SIZE
);
236 perror("[ppoll] read");
239 printf("# [ppoll] timeout\n");
242 for (i
= 0; i
< MAX_FDS
; i
++) {
252 void test_epoll(void)
256 struct epoll_event epoll_event
;
258 epollfd
= epoll_create(NB_FD
);
260 perror("[epoll] create");
264 epoll_event
.events
= EPOLLIN
| EPOLLPRI
| EPOLLET
;
265 epoll_event
.data
.fd
= wait_fd
;
266 ret
= epoll_ctl(epollfd
, EPOLL_CTL_ADD
, wait_fd
, &epoll_event
);
268 perror("[epoll] add");
273 ret
= epoll_wait(epollfd
, &epoll_event
, 1, timeout
);
275 ret
= epoll_wait(epollfd
, &epoll_event
, 1, -1);
279 printf("# [epoll] data available\n");
280 ret
= read(wait_fd
, buf
, BUF_SIZE
);
282 perror("[epoll] read");
284 } else if (ret
== 0) {
285 printf("# [epoll] timeout\n");
287 perror("epoll_wait");
294 void test_pepoll(void)
298 struct epoll_event epoll_event
;
300 epollfd
= epoll_create(NB_FD
);
302 perror("[eppoll] create");
306 epoll_event
.events
= EPOLLIN
| EPOLLPRI
| EPOLLET
;
307 epoll_event
.data
.fd
= wait_fd
;
308 ret
= epoll_ctl(epollfd
, EPOLL_CTL_ADD
, wait_fd
, &epoll_event
);
310 perror("[eppoll] add");
315 ret
= epoll_pwait(epollfd
, &epoll_event
, 1, timeout
, NULL
);
317 ret
= epoll_pwait(epollfd
, &epoll_event
, 1, -1, NULL
);
321 printf("# [eppoll] data available\n");
322 ret
= read(wait_fd
, buf
, BUF_SIZE
);
324 perror("[eppoll] read");
326 } else if (ret
== 0) {
327 printf("# [eppoll] timeout\n");
329 perror("epoll_pwait");
336 void run_working_cases(void)
343 * We need an input pipe for some cases and stdin might
344 * have random data, so we create a dummy pipe for this
345 * test to make sure we are running under clean conditions.
347 ret
= pipe(pipe_fds
);
352 wait_fd
= pipe_fds
[0];
363 ret
= close(pipe_fds
[0]);
367 ret
= close(pipe_fds
[1]);
378 * Ask for 100 FDs in a buffer for allocated for only 1 FD, should
379 * segfault (eventually with a "*** stack smashing detected ***" message).
380 * The event should contain an array of 100 FDs filled with garbage.
382 void ppoll_fds_buffer_overflow(void)
384 struct pollfd ufds
[NB_FD
];
388 ufds
[0].fd
= wait_fd
;
389 ufds
[0].events
= POLLIN
|POLLPRI
;
391 ret
= syscall(SYS_ppoll
, ufds
, 100, NULL
, NULL
);
395 } else if (ret
> 0) {
396 printf("# [ppoll] data available\n");
397 ret
= read(wait_fd
, buf
, BUF_SIZE
);
399 perror("[ppoll] read");
402 printf("# [ppoll] timeout\n");
409 * Ask for ULONG_MAX FDs in a buffer for allocated for only 1 FD, should
410 * cleanly fail with a "Invalid argument".
411 * The event should contain an empty array of FDs and overflow = 1.
413 void ppoll_fds_ulong_max(void)
415 struct pollfd ufds
[NB_FD
];
419 ufds
[0].fd
= wait_fd
;
420 ufds
[0].events
= POLLIN
|POLLPRI
;
422 ret
= syscall(SYS_ppoll
, ufds
, ULONG_MAX
, NULL
, NULL
);
426 } else if (ret
> 0) {
427 printf("# [ppoll] data available\n");
428 ret
= read(wait_fd
, buf
, BUF_SIZE
);
430 perror("[ppoll] read");
433 printf("# [ppoll] timeout\n");
440 * Select is limited to 1024 FDs, should output a pselect event
443 void pselect_fd_too_big(void)
451 * Test if nfds > 1024.
452 * Make sure ulimit is set correctly (ulimit -n 2048).
454 fd2
= dup2(wait_fd
, 2047);
462 ret
= syscall(SYS_pselect6
, fd2
+ 1, &rfds
, NULL
, NULL
, NULL
, NULL
);
465 perror("# pselect()");
467 printf("# [pselect] data available\n");
468 ret
= read(wait_fd
, buf
, BUF_SIZE
);
470 perror("[pselect] read");
473 printf("# [pselect] timeout\n");
479 * Invalid pointer as writefds, should output a ppoll event
482 void pselect_invalid_pointer(void)
487 void *invalid
= (void *) 0x42;
490 FD_SET(wait_fd
, &rfds
);
492 ret
= syscall(SYS_pselect6
, 1, &rfds
, (fd_set
*) invalid
, NULL
, NULL
,
496 perror("# pselect()");
498 printf("# [pselect] data available\n");
499 ret
= read(wait_fd
, buf
, BUF_SIZE
);
501 perror("[pselect] read");
504 printf("# [pselect] timeout\n");
510 * Pass an invalid pointer to epoll_pwait, should fail with
511 * "Bad address", the event returns 0 FDs.
513 void epoll_pwait_invalid_pointer(void)
517 struct epoll_event epoll_event
;
518 void *invalid
= (void *) 0x42;
520 epollfd
= epoll_create(NB_FD
);
522 perror("[eppoll] create");
526 epoll_event
.events
= EPOLLIN
| EPOLLPRI
| EPOLLET
;
527 epoll_event
.data
.fd
= wait_fd
;
528 ret
= epoll_ctl(epollfd
, EPOLL_CTL_ADD
, wait_fd
, &epoll_event
);
530 perror("[eppoll] add");
534 ret
= syscall(SYS_epoll_pwait
, epollfd
,
535 (struct epoll_event
*) invalid
, 1, -1, NULL
);
538 printf("# [eppoll] data available\n");
539 ret
= read(wait_fd
, buf
, BUF_SIZE
);
541 perror("[eppoll] read");
543 } else if (ret
== 0) {
544 printf("# [eppoll] timeout\n");
546 perror("# epoll_pwait");
554 * Set maxevents to INT_MAX, should output "Invalid argument"
555 * The event should return an empty array.
557 void epoll_pwait_int_max(void)
561 struct epoll_event epoll_event
;
563 epollfd
= epoll_create(NB_FD
);
565 perror("[eppoll] create");
569 epoll_event
.events
= EPOLLIN
| EPOLLPRI
| EPOLLET
;
570 epoll_event
.data
.fd
= wait_fd
;
571 ret
= epoll_ctl(epollfd
, EPOLL_CTL_ADD
, wait_fd
, &epoll_event
);
573 perror("[eppoll] add");
577 ret
= syscall(SYS_epoll_pwait
, epollfd
, &epoll_event
, INT_MAX
, -1,
581 printf("# [eppoll] data available\n");
582 ret
= read(wait_fd
, buf
, BUF_SIZE
);
584 perror("[eppoll] read");
586 } else if (ret
== 0) {
587 printf("# [eppoll] timeout\n");
589 perror("# epoll_pwait");
596 void *ppoll_writer(void *arg
)
598 struct ppoll_thread_data
*data
= (struct ppoll_thread_data
*) arg
;
600 while (!stop_thread
) {
601 memset(data
->ufds
, data
->value
,
602 MAX_FDS
* sizeof(struct pollfd
));
609 void do_ppoll(int *fds
, struct pollfd
*ufds
)
616 ts
.tv_nsec
= 1 * MSEC_PER_NSEC
;
618 for (i
= 0; i
< MAX_FDS
; i
++) {
620 ufds
[i
].events
= POLLIN
|POLLPRI
;
623 ret
= ppoll(ufds
, MAX_FDS
, &ts
, NULL
);
627 } else if (ret
> 0) {
628 printf("# [ppoll] data available\n");
629 ret
= read(wait_fd
, buf
, BUF_SIZE
);
631 perror("[ppoll] read");
634 printf("# [ppoll] timeout\n");
638 void stress_ppoll(int *fds
, int value
)
642 struct ppoll_thread_data thread_data
;
643 struct pollfd ufds
[MAX_FDS
];
645 thread_data
.ufds
= ufds
;
646 thread_data
.value
= value
;
649 ret
= pthread_create(&writer
, NULL
, &ppoll_writer
, (void *) &thread_data
);
651 fprintf(stderr
, "[error] pthread_create\n");
654 for (iter
= 0; iter
< NR_ITER
; iter
++) {
658 ret
= pthread_join(writer
, NULL
);
660 fprintf(stderr
, "[error] pthread_join\n");
668 * 3 rounds of NR_ITER iterations with concurrent updates of the pollfd
672 * - memset to INT_MAX
673 * Waits for input, but also set a timeout in case the input FD is overwritten
674 * before entering in the syscall. We use MAX_FDS FDs (dup of stdin), so the
675 * resulting trace is big (20MB).
677 * ppoll should work as expected and the trace should be readable at the end.
679 void ppoll_concurrent_write(void)
681 int i
, ret
, fds
[MAX_FDS
];
683 for (i
= 0; i
< MAX_FDS
; i
++) {
684 fds
[i
] = dup(wait_fd
);
690 stress_ppoll(fds
, 0);
691 stress_ppoll(fds
, 1);
692 stress_ppoll(fds
, INT_MAX
);
694 for (i
= 0; i
< MAX_FDS
; i
++) {
704 void *epoll_pwait_writer(void *addr
)
708 while (!stop_thread
) {
710 munmap(addr
, MAX_FDS
* sizeof(struct epoll_event
));
717 * epoll_pwait on MAX_FDS fds while a concurrent thread munmaps the
718 * buffer allocated for the returned data. This should randomly segfault.
719 * The trace should be readable and no kernel OOPS should occur.
721 void epoll_pwait_concurrent_munmap(void)
723 int ret
, epollfd
, i
, fds
[MAX_FDS
];
725 struct epoll_event
*epoll_event
;
730 epollfd
= epoll_create(MAX_FDS
);
732 perror("[eppoll] create");
736 epoll_event
= mmap(addr
, MAX_FDS
* sizeof(struct epoll_event
),
737 PROT_READ
| PROT_WRITE
, MAP_PRIVATE
| MAP_ANONYMOUS
,
739 if (epoll_event
== MAP_FAILED
) {
744 for (i
= 0; i
< MAX_FDS
; i
++) {
745 fds
[i
] = dup(wait_fd
);
749 epoll_event
[i
].events
= EPOLLIN
| EPOLLPRI
| EPOLLET
;
750 epoll_event
[i
].data
.fd
= fds
[i
];
751 ret
= epoll_ctl(epollfd
, EPOLL_CTL_ADD
, fds
[i
], epoll_event
);
753 perror("[eppoll] add");
758 ret
= pthread_create(&writer
, NULL
, &epoll_pwait_writer
,
759 (void *) epoll_event
);
761 fprintf(stderr
, "[error] pthread_create\n");
765 ret
= epoll_pwait(epollfd
, epoll_event
, 1, 1, NULL
);
768 printf("# [eppoll] data available\n");
769 ret
= read(wait_fd
, buf
, BUF_SIZE
);
771 perror("[eppoll] read");
773 } else if (ret
== 0) {
774 printf("# [eppoll] timeout\n");
776 perror("# epoll_pwait");
780 ret
= pthread_join(writer
, NULL
);
782 fprintf(stderr
, "[error] pthread_join\n");
786 for (i
= 0; i
< MAX_FDS
; i
++) {
793 ret
= munmap(addr
, MAX_FDS
* sizeof(struct epoll_event
));
802 void usage(poptContext optCon
, int exitcode
, char *error
, char *addl
)
804 poptPrintUsage(optCon
, stderr
, 0);
806 fprintf(stderr
, "%s: %s\n", error
, addl
);
811 void print_list(void)
813 fprintf(stderr
, "Test list (-t X):\n");
814 fprintf(stderr
, "\t1: Working cases for select, pselect6, poll, ppoll "
815 "and epoll, waiting for input\n");
816 fprintf(stderr
, "\t2: Timeout cases (1ms) for select, pselect6, poll, "
817 "ppoll and epoll\n");
818 fprintf(stderr
, "\t3: pselect with a FD > 1023\n");
819 fprintf(stderr
, "\t4: ppoll with %d FDs\n", MAX_FDS
);
820 fprintf(stderr
, "\t5: ppoll buffer overflow, should segfault, waits "
822 fprintf(stderr
, "\t6: pselect with invalid pointer, waits for "
824 fprintf(stderr
, "\t7: ppoll with ulong_max fds, waits for input\n");
825 fprintf(stderr
, "\t8: epoll_pwait with invalid pointer, waits for "
827 fprintf(stderr
, "\t9: epoll_pwait with maxevents set to INT_MAX, "
828 "waits for input\n");
829 fprintf(stderr
, "\t10: ppoll with concurrent updates of the structure "
830 "from user-space, stress test (3000 iterations), "
831 "waits for input + timeout 1ms\n");
832 fprintf(stderr
, "\t11: epoll_pwait with concurrent munmap of the buffer "
833 "from user-space, should randomly segfault, run "
834 "multiple times, waits for input + timeout 1ms\n");
837 int main(int argc
, const char **argv
)
839 int c
, ret
, test
= -1;
841 struct rlimit open_lim
;
843 struct poptOption optionsTable
[] = {
844 { "test", 't', POPT_ARG_INT
, &test
, 0,
845 "Test to run", NULL
},
846 { "list", 'l', 0, 0, 'l',
847 "List of tests (-t X)", NULL
},
849 { NULL
, 0, 0, NULL
, 0 }
852 optCon
= poptGetContext(NULL
, argc
, argv
, optionsTable
, 0);
855 poptPrintUsage(optCon
, stderr
, 0);
862 while ((c
= poptGetNextOpt(optCon
)) >= 0) {
870 open_lim
.rlim_cur
= MAX_FDS
+ MIN_NR_FDS
;
871 open_lim
.rlim_max
= MAX_FDS
+ MIN_NR_FDS
;
873 ret
= setrlimit(RLIMIT_NOFILE
, &open_lim
);
880 * Some tests might segfault, but we need the getpid() to be output
881 * for the validation, disabling the buffering on stdout works.
883 setbuf(stdout
, NULL
);
884 printf("%d\n", getpid());
886 wait_fd
= STDIN_FILENO
;
898 pselect_fd_too_big();
904 ppoll_fds_buffer_overflow();
907 pselect_invalid_pointer();
910 ppoll_fds_ulong_max();
913 epoll_pwait_invalid_pointer();
916 epoll_pwait_int_max();
919 ppoll_concurrent_write();
922 epoll_pwait_concurrent_munmap();
925 poptPrintUsage(optCon
, stderr
, 0);
931 poptFreeContext(optCon
);