5 #include <sys/syscall.h>
12 #include <sys/select.h>
13 #include <sys/epoll.h>
16 #include <sys/resource.h>
20 #include <common/compat/time.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 * Pass an invalid file descriptor to pselect6(). The syscall should return
441 * -EBADF. The recorded event should contain a "ret = -EBADF (-9)".
443 void pselect_invalid_fd(void)
451 * Open a file, close it and use the closed FD in the pselect6 call.
454 fd
= open("/dev/null", O_RDONLY
);
469 ret
= syscall(SYS_pselect6
, fd
+ 1, &rfds
, NULL
, NULL
, NULL
, NULL
);
471 perror("# pselect()");
473 printf("# [pselect] data available\n");
474 ret
= read(wait_fd
, buf
, BUF_SIZE
);
476 perror("[pselect] read");
479 printf("# [pselect] timeout\n");
486 * Invalid pointer as writefds, should output a ppoll event
489 void pselect_invalid_pointer(void)
494 void *invalid
= (void *) 0x42;
497 FD_SET(wait_fd
, &rfds
);
499 ret
= syscall(SYS_pselect6
, 1, &rfds
, (fd_set
*) invalid
, NULL
, NULL
,
503 perror("# pselect()");
505 printf("# [pselect] data available\n");
506 ret
= read(wait_fd
, buf
, BUF_SIZE
);
508 perror("[pselect] read");
511 printf("# [pselect] timeout\n");
517 * Pass an invalid pointer to epoll_pwait, should fail with
518 * "Bad address", the event returns 0 FDs.
520 void epoll_pwait_invalid_pointer(void)
524 struct epoll_event epoll_event
;
525 void *invalid
= (void *) 0x42;
527 epollfd
= epoll_create(NB_FD
);
529 perror("[eppoll] create");
533 epoll_event
.events
= EPOLLIN
| EPOLLPRI
| EPOLLET
;
534 epoll_event
.data
.fd
= wait_fd
;
535 ret
= epoll_ctl(epollfd
, EPOLL_CTL_ADD
, wait_fd
, &epoll_event
);
537 perror("[eppoll] add");
541 ret
= syscall(SYS_epoll_pwait
, epollfd
,
542 (struct epoll_event
*) invalid
, 1, -1, NULL
);
545 printf("# [eppoll] data available\n");
546 ret
= read(wait_fd
, buf
, BUF_SIZE
);
548 perror("[eppoll] read");
550 } else if (ret
== 0) {
551 printf("# [eppoll] timeout\n");
553 perror("# epoll_pwait");
561 * Set maxevents to INT_MAX, should output "Invalid argument"
562 * The event should return an empty array.
564 void epoll_pwait_int_max(void)
568 struct epoll_event epoll_event
;
570 epollfd
= epoll_create(NB_FD
);
572 perror("[eppoll] create");
576 epoll_event
.events
= EPOLLIN
| EPOLLPRI
| EPOLLET
;
577 epoll_event
.data
.fd
= wait_fd
;
578 ret
= epoll_ctl(epollfd
, EPOLL_CTL_ADD
, wait_fd
, &epoll_event
);
580 perror("[eppoll] add");
584 ret
= syscall(SYS_epoll_pwait
, epollfd
, &epoll_event
, INT_MAX
, -1,
588 printf("# [eppoll] data available\n");
589 ret
= read(wait_fd
, buf
, BUF_SIZE
);
591 perror("[eppoll] read");
593 } else if (ret
== 0) {
594 printf("# [eppoll] timeout\n");
596 perror("# epoll_pwait");
603 void *ppoll_writer(void *arg
)
605 struct ppoll_thread_data
*data
= (struct ppoll_thread_data
*) arg
;
607 while (!stop_thread
) {
608 memset(data
->ufds
, data
->value
,
609 MAX_FDS
* sizeof(struct pollfd
));
616 void do_ppoll(int *fds
, struct pollfd
*ufds
)
623 ts
.tv_nsec
= 1 * MSEC_PER_NSEC
;
625 for (i
= 0; i
< MAX_FDS
; i
++) {
627 ufds
[i
].events
= POLLIN
|POLLPRI
;
630 ret
= ppoll(ufds
, MAX_FDS
, &ts
, NULL
);
634 } else if (ret
> 0) {
635 printf("# [ppoll] data available\n");
636 ret
= read(wait_fd
, buf
, BUF_SIZE
);
638 perror("[ppoll] read");
641 printf("# [ppoll] timeout\n");
645 void stress_ppoll(int *fds
, int value
)
649 struct ppoll_thread_data thread_data
;
650 struct pollfd ufds
[MAX_FDS
];
652 thread_data
.ufds
= ufds
;
653 thread_data
.value
= value
;
656 ret
= pthread_create(&writer
, NULL
, &ppoll_writer
, (void *) &thread_data
);
658 fprintf(stderr
, "[error] pthread_create\n");
661 for (iter
= 0; iter
< NR_ITER
; iter
++) {
665 ret
= pthread_join(writer
, NULL
);
667 fprintf(stderr
, "[error] pthread_join\n");
675 * 3 rounds of NR_ITER iterations with concurrent updates of the pollfd
679 * - memset to INT_MAX
680 * Waits for input, but also set a timeout in case the input FD is overwritten
681 * before entering in the syscall. We use MAX_FDS FDs (dup of stdin), so the
682 * resulting trace is big (20MB).
684 * ppoll should work as expected and the trace should be readable at the end.
686 void ppoll_concurrent_write(void)
688 int i
, ret
, fds
[MAX_FDS
];
690 for (i
= 0; i
< MAX_FDS
; i
++) {
691 fds
[i
] = dup(wait_fd
);
697 stress_ppoll(fds
, 0);
698 stress_ppoll(fds
, 1);
699 stress_ppoll(fds
, INT_MAX
);
701 for (i
= 0; i
< MAX_FDS
; i
++) {
711 void *epoll_pwait_writer(void *addr
)
715 while (!stop_thread
) {
717 munmap(addr
, MAX_FDS
* sizeof(struct epoll_event
));
724 * epoll_pwait on MAX_FDS fds while a concurrent thread munmaps the
725 * buffer allocated for the returned data. This should randomly segfault.
726 * The trace should be readable and no kernel OOPS should occur.
728 void epoll_pwait_concurrent_munmap(void)
730 int ret
, epollfd
, i
, fds
[MAX_FDS
];
732 struct epoll_event
*epoll_event
;
735 for (i
= 0; i
< MAX_FDS
; i
++) {
738 epollfd
= epoll_create(MAX_FDS
);
740 perror("[eppoll] create");
744 epoll_event
= mmap(NULL
, MAX_FDS
* sizeof(struct epoll_event
),
745 PROT_READ
| PROT_WRITE
, MAP_PRIVATE
| MAP_ANONYMOUS
,
747 if (epoll_event
== MAP_FAILED
) {
752 for (i
= 0; i
< MAX_FDS
; i
++) {
753 fds
[i
] = dup(wait_fd
);
757 epoll_event
[i
].events
= EPOLLIN
| EPOLLPRI
| EPOLLET
;
758 epoll_event
[i
].data
.fd
= fds
[i
];
759 ret
= epoll_ctl(epollfd
, EPOLL_CTL_ADD
, fds
[i
], epoll_event
);
761 perror("[eppoll] add");
766 ret
= pthread_create(&writer
, NULL
, &epoll_pwait_writer
,
767 (void *) epoll_event
);
769 fprintf(stderr
, "[error] pthread_create\n");
773 ret
= epoll_pwait(epollfd
, epoll_event
, 1, 1, NULL
);
776 printf("# [eppoll] data available\n");
777 ret
= read(wait_fd
, buf
, BUF_SIZE
);
779 perror("[eppoll] read");
781 } else if (ret
== 0) {
782 printf("# [eppoll] timeout\n");
784 perror("# epoll_pwait");
788 ret
= pthread_join(writer
, NULL
);
790 fprintf(stderr
, "[error] pthread_join\n");
794 for (i
= 0; i
< MAX_FDS
; i
++) {
801 ret
= munmap(epoll_event
, MAX_FDS
* sizeof(struct epoll_event
));
810 void usage(poptContext optCon
, int exitcode
, char *error
, char *addl
)
812 poptPrintUsage(optCon
, stderr
, 0);
814 fprintf(stderr
, "%s: %s\n", error
, addl
);
819 void print_list(void)
821 fprintf(stderr
, "Test list (-t X):\n");
822 fprintf(stderr
, "\t1: Working cases for select, pselect6, poll, ppoll "
823 "and epoll, waiting for input\n");
824 fprintf(stderr
, "\t2: Timeout cases (1ms) for select, pselect6, poll, "
825 "ppoll and epoll\n");
826 fprintf(stderr
, "\t3: pselect with an invalid fd\n");
827 fprintf(stderr
, "\t4: ppoll with %d FDs\n", MAX_FDS
);
828 fprintf(stderr
, "\t5: ppoll buffer overflow, should segfault, waits "
830 fprintf(stderr
, "\t6: pselect with an invalid pointer, waits for "
832 fprintf(stderr
, "\t7: ppoll with ulong_max fds, waits for input\n");
833 fprintf(stderr
, "\t8: epoll_pwait with an invalid pointer, waits for "
835 fprintf(stderr
, "\t9: epoll_pwait with maxevents set to INT_MAX, "
836 "waits for input\n");
837 fprintf(stderr
, "\t10: ppoll with concurrent updates of the structure "
838 "from user-space, stress test (3000 iterations), "
839 "waits for input + timeout 1ms\n");
840 fprintf(stderr
, "\t11: epoll_pwait with concurrent munmap of the buffer "
841 "from user-space, should randomly segfault, run "
842 "multiple times, waits for input + timeout 1ms\n");
845 int main(int argc
, const char **argv
)
847 int c
, ret
, test
= -1;
849 struct rlimit open_lim
;
851 struct poptOption optionsTable
[] = {
852 { "test", 't', POPT_ARG_INT
, &test
, 0,
853 "Test to run", NULL
},
854 { "list", 'l', 0, 0, 'l',
855 "List of tests (-t X)", NULL
},
857 { NULL
, 0, 0, NULL
, 0 }
860 optCon
= poptGetContext(NULL
, argc
, argv
, optionsTable
, 0);
863 poptPrintUsage(optCon
, stderr
, 0);
870 while ((c
= poptGetNextOpt(optCon
)) >= 0) {
878 open_lim
.rlim_cur
= MAX_FDS
+ MIN_NR_FDS
;
879 open_lim
.rlim_max
= MAX_FDS
+ MIN_NR_FDS
;
881 ret
= setrlimit(RLIMIT_NOFILE
, &open_lim
);
888 * Some tests might segfault, but we need the getpid() to be output
889 * for the validation, disabling the buffering on stdout works.
891 setbuf(stdout
, NULL
);
892 printf("%d\n", getpid());
894 wait_fd
= STDIN_FILENO
;
906 pselect_invalid_fd();
912 ppoll_fds_buffer_overflow();
915 pselect_invalid_pointer();
918 ppoll_fds_ulong_max();
921 epoll_pwait_invalid_pointer();
924 epoll_pwait_int_max();
927 ppoll_concurrent_write();
930 epoll_pwait_concurrent_munmap();
933 poptPrintUsage(optCon
, stderr
, 0);
939 poptFreeContext(optCon
);