2 * Copyright (C) 2016 Julien Desfossez <jdesfossez@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
12 #include <sys/syscall.h>
13 #include <sys/types.h>
19 #include <sys/select.h>
20 #include <sys/epoll.h>
23 #include <sys/resource.h>
27 #include <common/compat/time.h>
32 #define NR_ITER 1000 /* for stress-tests */
34 #define MIN_NR_FDS 5 /* the minimum number of open FDs required for the test to run */
35 #define BIG_SELECT_FD 1022
37 #define MSEC_PER_USEC 1000
38 #define MSEC_PER_NSEC (MSEC_PER_USEC * 1000)
40 static int timeout
; /* seconds, -1 to disable */
41 static volatile int stop_thread
;
44 struct ppoll_thread_data
{
50 void test_select_big(void)
52 fd_set rfds
, wfds
, exfds
;
62 fd2
= dup2(wait_fd
, BIG_SELECT_FD
);
70 tv
.tv_usec
= timeout
* MSEC_PER_USEC
;
73 ret
= select(fd2
+ 1, &rfds
, &wfds
, &exfds
, &tv
);
75 ret
= select(fd2
+ 1, &rfds
, &wfds
, &exfds
, NULL
);
81 printf("# [select] data available\n");
82 ret
= read(wait_fd
, buf
, BUF_SIZE
);
84 perror("[select] read");
87 printf("# [select] timeout\n");
90 ret
= close(BIG_SELECT_FD
);
100 void test_pselect(void)
108 FD_SET(wait_fd
, &rfds
);
111 tv
.tv_nsec
= timeout
* MSEC_PER_NSEC
;
114 ret
= pselect(1, &rfds
, NULL
, NULL
, &tv
, NULL
);
116 ret
= pselect(1, &rfds
, NULL
, NULL
, NULL
, NULL
);
122 printf("# [pselect] data available\n");
123 ret
= read(wait_fd
, buf
, BUF_SIZE
);
125 perror("[pselect] read");
128 printf("# [pselect] timeout\n");
134 void test_select(void)
142 FD_SET(wait_fd
, &rfds
);
145 tv
.tv_usec
= timeout
* MSEC_PER_USEC
;
148 ret
= select(1, &rfds
, NULL
, NULL
, &tv
);
150 ret
= select(1, &rfds
, NULL
, NULL
, NULL
);
156 printf("# [select] data available\n");
157 ret
= read(wait_fd
, buf
, BUF_SIZE
);
159 perror("[select] read");
162 printf("# [select] timeout\n");
170 struct pollfd ufds
[NB_FD
];
174 ufds
[0].fd
= wait_fd
;
175 ufds
[0].events
= POLLIN
|POLLPRI
;
177 ret
= poll(ufds
, 1, timeout
);
181 } else if (ret
> 0) {
182 printf("# [poll] data available\n");
183 ret
= read(wait_fd
, buf
, BUF_SIZE
);
185 perror("[poll] read");
188 printf("# [poll] timeout\n");
193 void test_ppoll(void)
195 struct pollfd ufds
[NB_FD
];
200 ufds
[0].fd
= wait_fd
;
201 ufds
[0].events
= POLLIN
|POLLPRI
;
205 ts
.tv_nsec
= timeout
* MSEC_PER_NSEC
;
206 ret
= ppoll(ufds
, 1, &ts
, NULL
);
208 ret
= ppoll(ufds
, 1, NULL
, NULL
);
214 } else if (ret
> 0) {
215 printf("# [ppoll] data available\n");
216 ret
= read(wait_fd
, buf
, BUF_SIZE
);
218 perror("[ppoll] read");
221 printf("# [ppoll] timeout\n");
226 void test_ppoll_big(void)
228 struct pollfd ufds
[MAX_FDS
];
230 int ret
, i
, fds
[MAX_FDS
];
232 for (i
= 0; i
< MAX_FDS
; i
++) {
233 fds
[i
] = dup(wait_fd
);
238 ufds
[i
].events
= POLLIN
|POLLPRI
;
241 ret
= ppoll(ufds
, MAX_FDS
, NULL
, NULL
);
245 } else if (ret
> 0) {
246 printf("# [ppoll] data available\n");
247 ret
= read(wait_fd
, buf
, BUF_SIZE
);
249 perror("[ppoll] read");
252 printf("# [ppoll] timeout\n");
255 for (i
= 0; i
< MAX_FDS
; i
++) {
266 void test_epoll(void)
270 struct epoll_event epoll_event
;
272 epollfd
= epoll_create(NB_FD
);
274 perror("[epoll] create");
278 epoll_event
.events
= EPOLLIN
| EPOLLPRI
| EPOLLET
;
279 epoll_event
.data
.fd
= wait_fd
;
280 ret
= epoll_ctl(epollfd
, EPOLL_CTL_ADD
, wait_fd
, &epoll_event
);
282 perror("[epoll] add");
287 ret
= epoll_wait(epollfd
, &epoll_event
, 1, timeout
);
289 ret
= epoll_wait(epollfd
, &epoll_event
, 1, -1);
293 printf("# [epoll] data available\n");
294 ret
= read(wait_fd
, buf
, BUF_SIZE
);
296 perror("[epoll] read");
298 } else if (ret
== 0) {
299 printf("# [epoll] timeout\n");
301 perror("epoll_wait");
309 void test_pepoll(void)
313 struct epoll_event epoll_event
;
315 epollfd
= epoll_create(NB_FD
);
317 perror("[eppoll] create");
321 epoll_event
.events
= EPOLLIN
| EPOLLPRI
| EPOLLET
;
322 epoll_event
.data
.fd
= wait_fd
;
323 ret
= epoll_ctl(epollfd
, EPOLL_CTL_ADD
, wait_fd
, &epoll_event
);
325 perror("[eppoll] add");
330 ret
= epoll_pwait(epollfd
, &epoll_event
, 1, timeout
, NULL
);
332 ret
= epoll_pwait(epollfd
, &epoll_event
, 1, -1, NULL
);
336 printf("# [eppoll] data available\n");
337 ret
= read(wait_fd
, buf
, BUF_SIZE
);
339 perror("[eppoll] read");
341 } else if (ret
== 0) {
342 printf("# [eppoll] timeout\n");
344 perror("epoll_pwait");
352 void run_working_cases(void)
359 * We need an input pipe for some cases and stdin might
360 * have random data, so we create a dummy pipe for this
361 * test to make sure we are running under clean conditions.
363 ret
= pipe(pipe_fds
);
368 wait_fd
= pipe_fds
[0];
379 ret
= close(pipe_fds
[0]);
383 ret
= close(pipe_fds
[1]);
394 * Ask for 100 FDs in a buffer for allocated for only 1 FD, should
395 * segfault (eventually with a "*** stack smashing detected ***" message).
396 * The event should contain an array of 100 FDs filled with garbage.
399 void ppoll_fds_buffer_overflow(void)
401 struct pollfd ufds
[NB_FD
];
405 ufds
[0].fd
= wait_fd
;
406 ufds
[0].events
= POLLIN
|POLLPRI
;
408 ret
= syscall(SYS_ppoll
, ufds
, 100, NULL
, NULL
);
412 } else if (ret
> 0) {
413 printf("# [ppoll] data available\n");
414 ret
= read(wait_fd
, buf
, BUF_SIZE
);
416 perror("[ppoll] read");
419 printf("# [ppoll] timeout\n");
426 * Ask for ULONG_MAX FDs in a buffer for allocated for only 1 FD, should
427 * cleanly fail with a "Invalid argument".
428 * The event should contain an empty array of FDs and overflow = 1.
431 void ppoll_fds_ulong_max(void)
433 struct pollfd ufds
[NB_FD
];
437 ufds
[0].fd
= wait_fd
;
438 ufds
[0].events
= POLLIN
|POLLPRI
;
440 ret
= syscall(SYS_ppoll
, ufds
, ULONG_MAX
, NULL
, NULL
);
444 } else if (ret
> 0) {
445 printf("# [ppoll] data available\n");
446 ret
= read(wait_fd
, buf
, BUF_SIZE
);
448 perror("[ppoll] read");
451 printf("# [ppoll] timeout\n");
458 * Pass an invalid file descriptor to pselect6(). The syscall should return
459 * -EBADF. The recorded event should contain a "ret = -EBADF (-9)".
462 void pselect_invalid_fd(void)
470 * Open a file, close it and use the closed FD in the pselect6 call.
473 fd
= open("/dev/null", O_RDONLY
);
488 ret
= syscall(SYS_pselect6
, fd
+ 1, &rfds
, NULL
, NULL
, NULL
, NULL
);
490 perror("# pselect()");
492 printf("# [pselect] data available\n");
493 ret
= read(wait_fd
, buf
, BUF_SIZE
);
495 perror("[pselect] read");
498 printf("# [pselect] timeout\n");
505 * Invalid pointer as writefds, should output a ppoll event
509 void pselect_invalid_pointer(void)
514 void *invalid
= (void *) 0x42;
517 FD_SET(wait_fd
, &rfds
);
519 ret
= syscall(SYS_pselect6
, 1, &rfds
, (fd_set
*) invalid
, NULL
, NULL
,
523 perror("# pselect()");
525 printf("# [pselect] data available\n");
526 ret
= read(wait_fd
, buf
, BUF_SIZE
);
528 perror("[pselect] read");
531 printf("# [pselect] timeout\n");
537 * Pass an invalid pointer to epoll_pwait, should fail with
538 * "Bad address", the event returns 0 FDs.
541 void epoll_pwait_invalid_pointer(void)
545 struct epoll_event epoll_event
;
546 void *invalid
= (void *) 0x42;
548 epollfd
= epoll_create(NB_FD
);
550 perror("[eppoll] create");
554 epoll_event
.events
= EPOLLIN
| EPOLLPRI
| EPOLLET
;
555 epoll_event
.data
.fd
= wait_fd
;
556 ret
= epoll_ctl(epollfd
, EPOLL_CTL_ADD
, wait_fd
, &epoll_event
);
558 perror("[eppoll] add");
562 ret
= syscall(SYS_epoll_pwait
, epollfd
,
563 (struct epoll_event
*) invalid
, 1, -1, NULL
);
566 printf("# [eppoll] data available\n");
567 ret
= read(wait_fd
, buf
, BUF_SIZE
);
569 perror("[eppoll] read");
571 } else if (ret
== 0) {
572 printf("# [eppoll] timeout\n");
574 perror("# epoll_pwait");
582 * Set maxevents to INT_MAX, should output "Invalid argument"
583 * The event should return an empty array.
586 void epoll_pwait_int_max(void)
590 struct epoll_event epoll_event
;
592 epollfd
= epoll_create(NB_FD
);
594 perror("[eppoll] create");
598 epoll_event
.events
= EPOLLIN
| EPOLLPRI
| EPOLLET
;
599 epoll_event
.data
.fd
= wait_fd
;
600 ret
= epoll_ctl(epollfd
, EPOLL_CTL_ADD
, wait_fd
, &epoll_event
);
602 perror("[eppoll] add");
606 ret
= syscall(SYS_epoll_pwait
, epollfd
, &epoll_event
, INT_MAX
, -1,
610 printf("# [eppoll] data available\n");
611 ret
= read(wait_fd
, buf
, BUF_SIZE
);
613 perror("[eppoll] read");
615 } else if (ret
== 0) {
616 printf("# [eppoll] timeout\n");
618 perror("# epoll_pwait");
626 void *ppoll_writer(void *arg
)
628 struct ppoll_thread_data
*data
= (struct ppoll_thread_data
*) arg
;
630 while (!stop_thread
) {
631 memset(data
->ufds
, data
->value
,
632 MAX_FDS
* sizeof(struct pollfd
));
640 void do_ppoll(int *fds
, struct pollfd
*ufds
)
647 ts
.tv_nsec
= 1 * MSEC_PER_NSEC
;
649 for (i
= 0; i
< MAX_FDS
; i
++) {
651 ufds
[i
].events
= POLLIN
|POLLPRI
;
654 ret
= ppoll(ufds
, MAX_FDS
, &ts
, NULL
);
658 } else if (ret
> 0) {
659 printf("# [ppoll] data available\n");
660 ret
= read(wait_fd
, buf
, BUF_SIZE
);
662 perror("[ppoll] read");
665 printf("# [ppoll] timeout\n");
670 void stress_ppoll(int *fds
, int value
)
674 struct ppoll_thread_data thread_data
;
675 struct pollfd ufds
[MAX_FDS
];
677 thread_data
.ufds
= ufds
;
678 thread_data
.value
= value
;
681 ret
= pthread_create(&writer
, NULL
, &ppoll_writer
, (void *) &thread_data
);
683 fprintf(stderr
, "[error] pthread_create\n");
686 for (iter
= 0; iter
< NR_ITER
; iter
++) {
690 ret
= pthread_join(writer
, NULL
);
692 fprintf(stderr
, "[error] pthread_join\n");
700 * 3 rounds of NR_ITER iterations with concurrent updates of the pollfd
704 * - memset to INT_MAX
705 * Waits for input, but also set a timeout in case the input FD is overwritten
706 * before entering in the syscall. We use MAX_FDS FDs (dup of stdin), so the
707 * resulting trace is big (20MB).
709 * ppoll should work as expected and the trace should be readable at the end.
712 void ppoll_concurrent_write(void)
714 int i
, ret
, fds
[MAX_FDS
];
716 for (i
= 0; i
< MAX_FDS
; i
++) {
717 fds
[i
] = dup(wait_fd
);
723 stress_ppoll(fds
, 0);
724 stress_ppoll(fds
, 1);
725 stress_ppoll(fds
, INT_MAX
);
727 for (i
= 0; i
< MAX_FDS
; i
++) {
738 void *epoll_pwait_writer(void *addr
)
742 while (!stop_thread
) {
744 munmap(addr
, MAX_FDS
* sizeof(struct epoll_event
));
751 * epoll_pwait on MAX_FDS fds while a concurrent thread munmaps the
752 * buffer allocated for the returned data. This should randomly segfault.
753 * The trace should be readable and no kernel OOPS should occur.
756 void epoll_pwait_concurrent_munmap(void)
758 int ret
, epollfd
, i
, fds
[MAX_FDS
];
760 struct epoll_event
*epoll_event
;
763 for (i
= 0; i
< MAX_FDS
; i
++) {
766 epollfd
= epoll_create(MAX_FDS
);
768 perror("[eppoll] create");
772 epoll_event
= mmap(NULL
, MAX_FDS
* sizeof(struct epoll_event
),
773 PROT_READ
| PROT_WRITE
, MAP_PRIVATE
| MAP_ANONYMOUS
,
775 if (epoll_event
== MAP_FAILED
) {
780 for (i
= 0; i
< MAX_FDS
; i
++) {
781 fds
[i
] = dup(wait_fd
);
785 epoll_event
[i
].events
= EPOLLIN
| EPOLLPRI
| EPOLLET
;
786 epoll_event
[i
].data
.fd
= fds
[i
];
787 ret
= epoll_ctl(epollfd
, EPOLL_CTL_ADD
, fds
[i
], epoll_event
);
789 perror("[eppoll] add");
794 ret
= pthread_create(&writer
, NULL
, &epoll_pwait_writer
,
795 (void *) epoll_event
);
797 fprintf(stderr
, "[error] pthread_create\n");
801 ret
= epoll_pwait(epollfd
, epoll_event
, 1, 1, NULL
);
804 printf("# [eppoll] data available\n");
805 ret
= read(wait_fd
, buf
, BUF_SIZE
);
807 perror("[eppoll] read");
809 } else if (ret
== 0) {
810 printf("# [eppoll] timeout\n");
812 perror("# epoll_pwait");
816 ret
= pthread_join(writer
, NULL
);
818 fprintf(stderr
, "[error] pthread_join\n");
822 for (i
= 0; i
< MAX_FDS
; i
++) {
829 ret
= munmap(epoll_event
, MAX_FDS
* sizeof(struct epoll_event
));
839 void print_list(void)
841 fprintf(stderr
, "Test list (-t X):\n");
842 fprintf(stderr
, "\t1: Working cases for select, pselect6, poll, ppoll "
843 "and epoll, waiting for input\n");
844 fprintf(stderr
, "\t2: Timeout cases (1ms) for select, pselect6, poll, "
845 "ppoll and epoll\n");
846 fprintf(stderr
, "\t3: pselect with an invalid fd\n");
847 fprintf(stderr
, "\t4: ppoll with %d FDs\n", MAX_FDS
);
848 fprintf(stderr
, "\t5: ppoll buffer overflow, should segfault, waits "
850 fprintf(stderr
, "\t6: pselect with an invalid pointer, waits for "
852 fprintf(stderr
, "\t7: ppoll with ulong_max fds, waits for input\n");
853 fprintf(stderr
, "\t8: epoll_pwait with an invalid pointer, waits for "
855 fprintf(stderr
, "\t9: epoll_pwait with maxevents set to INT_MAX, "
856 "waits for input\n");
857 fprintf(stderr
, "\t10: ppoll with concurrent updates of the structure "
858 "from user-space, stress test (3000 iterations), "
859 "waits for input + timeout 1ms\n");
860 fprintf(stderr
, "\t11: epoll_pwait with concurrent munmap of the buffer "
861 "from user-space, should randomly segfault, run "
862 "multiple times, waits for input + timeout 1ms\n");
865 int main(int argc
, const char **argv
)
867 int c
, ret
, test
= -1;
869 struct rlimit open_lim
;
871 struct poptOption optionsTable
[] = {
872 { "test", 't', POPT_ARG_INT
, &test
, 0,
873 "Test to run", NULL
},
874 { "list", 'l', 0, 0, 'l',
875 "List of tests (-t X)", NULL
},
877 { NULL
, 0, 0, NULL
, 0 }
880 optCon
= poptGetContext(NULL
, argc
, argv
, optionsTable
, 0);
883 poptPrintUsage(optCon
, stderr
, 0);
890 while ((c
= poptGetNextOpt(optCon
)) >= 0) {
898 open_lim
.rlim_cur
= MAX_FDS
+ MIN_NR_FDS
;
899 open_lim
.rlim_max
= MAX_FDS
+ MIN_NR_FDS
;
901 ret
= setrlimit(RLIMIT_NOFILE
, &open_lim
);
908 * Some tests might segfault, but we need the getpid() to be output
909 * for the validation, disabling the buffering on stdout works.
911 setbuf(stdout
, NULL
);
912 printf("%d\n", getpid());
914 wait_fd
= STDIN_FILENO
;
926 pselect_invalid_fd();
932 ppoll_fds_buffer_overflow();
935 pselect_invalid_pointer();
938 ppoll_fds_ulong_max();
941 epoll_pwait_invalid_pointer();
944 epoll_pwait_int_max();
947 ppoll_concurrent_write();
950 epoll_pwait_concurrent_munmap();
953 poptPrintUsage(optCon
, stderr
, 0);
959 poptFreeContext(optCon
);