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>
28 #include <common/error.h>
33 #define NR_ITER 1000 /* for stress-tests */
35 #define MIN_NR_FDS 5 /* the minimum number of open FDs required for the test to run */
36 #define BIG_SELECT_FD 1022
38 #define MSEC_PER_USEC 1000
39 #define MSEC_PER_NSEC (MSEC_PER_USEC * 1000)
41 static int timeout
; /* seconds, -1 to disable */
42 static volatile int stop_thread
;
45 /* Used by logging utils. */
46 int lttng_opt_quiet
, lttng_opt_verbose
, lttng_opt_mi
;
48 static void run_working_cases(FILE *validation_output_file
);
49 static void pselect_invalid_fd(FILE *validation_output_file
);
50 static void test_ppoll_big(FILE *validation_output_file
);
51 static void ppoll_fds_buffer_overflow(FILE *validation_output_file
);
52 static void pselect_invalid_pointer(FILE *validation_output_file
);
53 static void ppoll_fds_ulong_max(FILE *validation_output_file
);
54 static void epoll_pwait_invalid_pointer(FILE *validation_output_file
);
55 static void epoll_pwait_int_max(FILE *validation_output_file
);
56 static void ppoll_concurrent_write(FILE *validation_output_file
);
57 static void epoll_pwait_concurrent_munmap(FILE *validation_output_file
);
59 typedef void (*test_case_cb
)(FILE *output_file
);
61 static const struct test_case
{
63 bool produces_validation_info
;
67 { .run
= run_working_cases
, .produces_validation_info
= true, .timeout
= -1 },
68 { .run
= run_working_cases
, .produces_validation_info
= true, .timeout
= 1 },
69 { .run
= pselect_invalid_fd
, .produces_validation_info
= false },
70 { .run
= test_ppoll_big
, .produces_validation_info
= false },
71 { .run
= ppoll_fds_buffer_overflow
, .produces_validation_info
= false },
72 { .run
= pselect_invalid_pointer
, .produces_validation_info
= false },
73 { .run
= ppoll_fds_ulong_max
, .produces_validation_info
= false },
74 { .run
= epoll_pwait_invalid_pointer
, .produces_validation_info
= true },
75 { .run
= epoll_pwait_int_max
, .produces_validation_info
= true },
76 { .run
= ppoll_concurrent_write
, .produces_validation_info
= false },
77 { .run
= epoll_pwait_concurrent_munmap
, .produces_validation_info
= true },
80 struct ppoll_thread_data
{
86 void test_select_big(void)
88 fd_set rfds
, wfds
, exfds
;
98 fd2
= dup2(wait_fd
, BIG_SELECT_FD
);
106 tv
.tv_usec
= timeout
* MSEC_PER_USEC
;
109 ret
= select(fd2
+ 1, &rfds
, &wfds
, &exfds
, &tv
);
111 ret
= select(fd2
+ 1, &rfds
, &wfds
, &exfds
, NULL
);
117 ret
= read(wait_fd
, buf
, BUF_SIZE
);
119 PERROR("[select] read");
123 ret
= close(BIG_SELECT_FD
);
133 void test_pselect(void)
141 FD_SET(wait_fd
, &rfds
);
144 tv
.tv_nsec
= timeout
* MSEC_PER_NSEC
;
147 ret
= pselect(1, &rfds
, NULL
, NULL
, &tv
, NULL
);
149 ret
= pselect(1, &rfds
, NULL
, NULL
, NULL
, NULL
);
155 ret
= read(wait_fd
, buf
, BUF_SIZE
);
157 PERROR("[pselect] read");
163 void test_select(void)
171 FD_SET(wait_fd
, &rfds
);
174 tv
.tv_usec
= timeout
* MSEC_PER_USEC
;
177 ret
= select(1, &rfds
, NULL
, NULL
, &tv
);
179 ret
= select(1, &rfds
, NULL
, NULL
, NULL
);
185 ret
= read(wait_fd
, buf
, BUF_SIZE
);
187 PERROR("[select] read");
195 struct pollfd ufds
[NB_FD
];
199 ufds
[0].fd
= wait_fd
;
200 ufds
[0].events
= POLLIN
|POLLPRI
;
202 ret
= poll(ufds
, 1, timeout
);
206 } else if (ret
> 0) {
207 ret
= read(wait_fd
, buf
, BUF_SIZE
);
209 PERROR("[poll] read");
215 void test_ppoll(void)
217 struct pollfd ufds
[NB_FD
];
222 ufds
[0].fd
= wait_fd
;
223 ufds
[0].events
= POLLIN
|POLLPRI
;
227 ts
.tv_nsec
= timeout
* MSEC_PER_NSEC
;
228 ret
= ppoll(ufds
, 1, &ts
, NULL
);
230 ret
= ppoll(ufds
, 1, NULL
, NULL
);
236 } else if (ret
> 0) {
237 ret
= read(wait_fd
, buf
, BUF_SIZE
);
239 PERROR("[ppoll] read");
245 void test_ppoll_big(FILE *validation_output_file
)
247 struct pollfd ufds
[MAX_FDS
];
249 int ret
, i
, fds
[MAX_FDS
];
251 for (i
= 0; i
< MAX_FDS
; i
++) {
252 fds
[i
] = dup(wait_fd
);
257 ufds
[i
].events
= POLLIN
|POLLPRI
;
260 ret
= ppoll(ufds
, MAX_FDS
, NULL
, NULL
);
264 } else if (ret
> 0) {
265 ret
= read(wait_fd
, buf
, BUF_SIZE
);
267 PERROR("[ppoll] read");
271 for (i
= 0; i
< MAX_FDS
; i
++) {
282 void test_epoll(FILE *validation_output_file
)
286 struct epoll_event epoll_event
;
288 epollfd
= epoll_create(NB_FD
);
290 PERROR("[epoll] create");
294 ret
= fprintf(validation_output_file
,
295 ", \"epoll_wait_fd\": %i", epollfd
);
297 PERROR("[epoll] Failed to write test validation output");
301 epoll_event
.events
= EPOLLIN
| EPOLLPRI
| EPOLLET
;
302 epoll_event
.data
.fd
= wait_fd
;
303 ret
= epoll_ctl(epollfd
, EPOLL_CTL_ADD
, wait_fd
, &epoll_event
);
305 PERROR("[epoll] add");
310 ret
= epoll_wait(epollfd
, &epoll_event
, 1, timeout
);
312 ret
= epoll_wait(epollfd
, &epoll_event
, 1, -1);
316 ret
= read(wait_fd
, buf
, BUF_SIZE
);
318 PERROR("[epoll] read");
320 } else if (ret
!= 0) {
321 PERROR("epoll_wait");
325 ret
= close(epollfd
);
334 void test_epoll_pwait(FILE *validation_output_file
)
338 struct epoll_event epoll_event
;
340 epollfd
= epoll_create(NB_FD
);
342 PERROR("[epoll_pwait] create");
346 ret
= fprintf(validation_output_file
,
347 ", \"epoll_pwait_fd\": %i", epollfd
);
349 PERROR("[epoll_pwait] Failed to write test validation output");
353 epoll_event
.events
= EPOLLIN
| EPOLLPRI
| EPOLLET
;
354 epoll_event
.data
.fd
= wait_fd
;
355 ret
= epoll_ctl(epollfd
, EPOLL_CTL_ADD
, wait_fd
, &epoll_event
);
357 PERROR("[epoll_pwait] add");
362 ret
= epoll_pwait(epollfd
, &epoll_event
, 1, timeout
, NULL
);
364 ret
= epoll_pwait(epollfd
, &epoll_event
, 1, -1, NULL
);
368 ret
= read(wait_fd
, buf
, BUF_SIZE
);
370 PERROR("[epoll_pwait] read");
372 } else if (ret
!= 0) {
373 PERROR("epoll_pwait");
377 ret
= close(epollfd
);
386 void run_working_cases(FILE *validation_output_file
)
393 * We need an input pipe for some cases and stdin might
394 * have random data, so we create a dummy pipe for this
395 * test to make sure we are running under clean conditions.
397 ret
= pipe(pipe_fds
);
402 wait_fd
= pipe_fds
[0];
410 ret
= fprintf(validation_output_file
, "{ \"pid\": %i", getpid());
412 PERROR("Failed to write pid to test validation file");
416 test_epoll(validation_output_file
);
417 test_epoll_pwait(validation_output_file
);
420 ret
= close(pipe_fds
[0]);
424 ret
= close(pipe_fds
[1]);
430 ret
= fputs(" }", validation_output_file
);
432 PERROR("Failed to close JSON dictionary in test validation file");
441 * Ask for 100 FDs in a buffer for allocated for only 1 FD, should
442 * segfault (eventually with a "*** stack smashing detected ***" message).
443 * The event should contain an array of 100 FDs filled with garbage.
446 void ppoll_fds_buffer_overflow(FILE *validation_output_file
)
448 struct pollfd ufds
[NB_FD
];
452 ufds
[0].fd
= wait_fd
;
453 ufds
[0].events
= POLLIN
|POLLPRI
;
455 ret
= syscall(SYS_ppoll
, ufds
, 100, NULL
, NULL
);
459 } else if (ret
> 0) {
460 ret
= read(wait_fd
, buf
, BUF_SIZE
);
462 PERROR("[ppoll] read");
468 * Ask for ULONG_MAX FDs in a buffer for allocated for only 1 FD, should
469 * cleanly fail with a "Invalid argument".
470 * The event should contain an empty array of FDs and overflow = 1.
473 void ppoll_fds_ulong_max(FILE *validation_output_file
)
475 struct pollfd ufds
[NB_FD
];
479 ufds
[0].fd
= wait_fd
;
480 ufds
[0].events
= POLLIN
|POLLPRI
;
482 ret
= syscall(SYS_ppoll
, ufds
, ULONG_MAX
, NULL
, NULL
);
484 /* Expected error. */
485 } else if (ret
> 0) {
486 ret
= read(wait_fd
, buf
, BUF_SIZE
);
488 PERROR("[ppoll] read");
494 * Pass an invalid file descriptor to pselect6(). The syscall should return
495 * -EBADF. The recorded event should contain a "ret = -EBADF (-9)".
498 void pselect_invalid_fd(FILE *validation_output_file
)
506 * Open a file, close it and use the closed FD in the pselect6 call.
508 fd
= open("/dev/null", O_RDONLY
);
523 ret
= syscall(SYS_pselect6
, fd
+ 1, &rfds
, NULL
, NULL
, NULL
, NULL
);
525 /* Expected error. */
527 ret
= read(wait_fd
, buf
, BUF_SIZE
);
529 PERROR("[pselect] read");
537 * Invalid pointer as writefds, should output a ppoll event
541 void pselect_invalid_pointer(FILE *validation_output_file
)
546 void *invalid
= (void *) 0x42;
549 FD_SET(wait_fd
, &rfds
);
551 ret
= syscall(SYS_pselect6
, 1, &rfds
, (fd_set
*) invalid
, NULL
, NULL
,
554 /* Expected error. */
556 ret
= read(wait_fd
, buf
, BUF_SIZE
);
558 PERROR("[pselect] read");
564 * Pass an invalid pointer to epoll_pwait, should fail with
565 * "Bad address", the event returns 0 FDs.
568 void epoll_pwait_invalid_pointer(FILE *validation_output_file
)
572 struct epoll_event epoll_event
;
573 void *invalid
= (void *) 0x42;
575 epollfd
= epoll_create(NB_FD
);
577 PERROR("[epoll_pwait] create");
581 ret
= fprintf(validation_output_file
,
582 "{ \"epollfd\": %i, \"pid\": %i }", epollfd
,
585 PERROR("[epoll_pwait] Failed to write test validation output");
589 epoll_event
.events
= EPOLLIN
| EPOLLPRI
| EPOLLET
;
590 epoll_event
.data
.fd
= wait_fd
;
591 ret
= epoll_ctl(epollfd
, EPOLL_CTL_ADD
, wait_fd
, &epoll_event
);
593 PERROR("[epoll_pwait] add");
597 ret
= syscall(SYS_epoll_pwait
, epollfd
,
598 (struct epoll_event
*) invalid
, 1, -1, NULL
);
601 ret
= read(wait_fd
, buf
, BUF_SIZE
);
603 PERROR("[epoll_pwait] read");
605 } else if (ret
!= 0) {
606 /* Expected error. */
610 ret
= close(epollfd
);
619 * Set maxevents to INT_MAX, should output "Invalid argument"
620 * The event should return an empty array.
623 void epoll_pwait_int_max(FILE *validation_output_file
)
627 struct epoll_event epoll_event
;
629 epollfd
= epoll_create(NB_FD
);
631 PERROR("[epoll_pwait] create");
635 ret
= fprintf(validation_output_file
,
636 "{ \"epollfd\": %i, \"pid\": %i }", epollfd
,
639 PERROR("[epoll_pwait] Failed to write test validation output");
643 epoll_event
.events
= EPOLLIN
| EPOLLPRI
| EPOLLET
;
644 epoll_event
.data
.fd
= wait_fd
;
645 ret
= epoll_ctl(epollfd
, EPOLL_CTL_ADD
, wait_fd
, &epoll_event
);
647 PERROR("[epoll_pwait] add");
651 ret
= syscall(SYS_epoll_pwait
, epollfd
, &epoll_event
, INT_MAX
, -1,
655 ret
= read(wait_fd
, buf
, BUF_SIZE
);
657 PERROR("[epoll_pwait] read");
659 } else if (ret
!= 0) {
660 /* Expected error. */
664 ret
= close(epollfd
);
673 void *ppoll_writer(void *arg
)
675 struct ppoll_thread_data
*data
= (struct ppoll_thread_data
*) arg
;
677 while (!stop_thread
) {
678 memset(data
->ufds
, data
->value
,
679 MAX_FDS
* sizeof(struct pollfd
));
687 void do_ppoll(int *fds
, struct pollfd
*ufds
)
694 ts
.tv_nsec
= 1 * MSEC_PER_NSEC
;
696 for (i
= 0; i
< MAX_FDS
; i
++) {
698 ufds
[i
].events
= POLLIN
|POLLPRI
;
701 ret
= ppoll(ufds
, MAX_FDS
, &ts
, NULL
);
705 } else if (ret
> 0) {
706 ret
= read(wait_fd
, buf
, BUF_SIZE
);
708 PERROR("[ppoll] read");
714 void stress_ppoll(int *fds
, int value
)
718 struct ppoll_thread_data thread_data
;
719 struct pollfd ufds
[MAX_FDS
];
721 thread_data
.ufds
= ufds
;
722 thread_data
.value
= value
;
725 ret
= pthread_create(&writer
, NULL
, &ppoll_writer
, (void *) &thread_data
);
727 fprintf(stderr
, "[error] pthread_create\n");
730 for (iter
= 0; iter
< NR_ITER
; iter
++) {
734 ret
= pthread_join(writer
, NULL
);
736 fprintf(stderr
, "[error] pthread_join\n");
744 * 3 rounds of NR_ITER iterations with concurrent updates of the pollfd
748 * - memset to INT_MAX
749 * Waits for input, but also set a timeout in case the input FD is overwritten
750 * before entering in the syscall. We use MAX_FDS FDs (dup of stdin), so the
751 * resulting trace is big (20MB).
753 * ppoll should work as expected and the trace should be readable at the end.
756 void ppoll_concurrent_write(FILE *validation_output_file
)
758 int i
, ret
, fds
[MAX_FDS
];
760 for (i
= 0; i
< MAX_FDS
; i
++) {
761 fds
[i
] = dup(wait_fd
);
767 stress_ppoll(fds
, 0);
768 stress_ppoll(fds
, 1);
769 stress_ppoll(fds
, INT_MAX
);
771 for (i
= 0; i
< MAX_FDS
; i
++) {
782 void *epoll_pwait_writer(void *addr
)
786 while (!stop_thread
) {
788 munmap(addr
, MAX_FDS
* sizeof(struct epoll_event
));
795 * epoll_pwait on MAX_FDS fds while a concurrent thread munmaps the
796 * buffer allocated for the returned data. This should randomly segfault.
797 * The trace should be readable and no kernel OOPS should occur.
800 void epoll_pwait_concurrent_munmap(FILE *validation_output_file
)
802 int ret
, epollfd
, i
, fds
[MAX_FDS
];
804 struct epoll_event
*epoll_event
;
807 for (i
= 0; i
< MAX_FDS
; i
++) {
810 epollfd
= epoll_create(MAX_FDS
);
812 PERROR("[epoll_pwait] create");
816 ret
= fprintf(validation_output_file
,
817 "{ \"epollfd\": %i, \"pid\": %i }", epollfd
,
820 PERROR("[epoll_pwait] Failed to write test validation output");
824 epoll_event
= (struct epoll_event
*) mmap(NULL
,
825 MAX_FDS
* sizeof(struct epoll_event
),
826 PROT_READ
| PROT_WRITE
, MAP_PRIVATE
| MAP_ANONYMOUS
, -1,
828 if (epoll_event
== MAP_FAILED
) {
833 for (i
= 0; i
< MAX_FDS
; i
++) {
834 fds
[i
] = dup(wait_fd
);
838 epoll_event
[i
].events
= EPOLLIN
| EPOLLPRI
| EPOLLET
;
839 epoll_event
[i
].data
.fd
= fds
[i
];
840 ret
= epoll_ctl(epollfd
, EPOLL_CTL_ADD
, fds
[i
], epoll_event
);
842 PERROR("[epoll_pwait] add");
847 ret
= pthread_create(&writer
, NULL
, &epoll_pwait_writer
,
848 (void *) epoll_event
);
850 fprintf(stderr
, "[error] pthread_create\n");
854 ret
= epoll_pwait(epollfd
, epoll_event
, 1, 1, NULL
);
857 ret
= read(wait_fd
, buf
, BUF_SIZE
);
859 PERROR("[epoll_pwait] read");
861 } else if (ret
!= 0) {
862 /* Expected error. */
866 ret
= pthread_join(writer
, NULL
);
868 fprintf(stderr
, "[error] pthread_join\n");
872 for (i
= 0; i
< MAX_FDS
; i
++) {
879 ret
= munmap(epoll_event
, MAX_FDS
* sizeof(struct epoll_event
));
885 ret
= close(epollfd
);
894 void print_list(void)
896 fprintf(stderr
, "Test list (-t X):\n");
897 fprintf(stderr
, "\t1: Working cases for select, pselect6, poll, ppoll "
898 "and epoll, waiting for input\n");
899 fprintf(stderr
, "\t2: Timeout cases (1ms) for select, pselect6, poll, "
900 "ppoll and epoll\n");
901 fprintf(stderr
, "\t3: pselect with an invalid fd\n");
902 fprintf(stderr
, "\t4: ppoll with %d FDs\n", MAX_FDS
);
903 fprintf(stderr
, "\t5: ppoll buffer overflow, should segfault, waits "
905 fprintf(stderr
, "\t6: pselect with an invalid pointer, waits for "
907 fprintf(stderr
, "\t7: ppoll with ulong_max fds, waits for input\n");
908 fprintf(stderr
, "\t8: epoll_pwait with an invalid pointer, waits for "
910 fprintf(stderr
, "\t9: epoll_pwait with maxevents set to INT_MAX, "
911 "waits for input\n");
912 fprintf(stderr
, "\t10: ppoll with concurrent updates of the structure "
913 "from user-space, stress test (3000 iterations), "
914 "waits for input + timeout 1ms\n");
915 fprintf(stderr
, "\t11: epoll_pwait with concurrent munmap of the buffer "
916 "from user-space, should randomly segfault, run "
917 "multiple times, waits for input + timeout 1ms\n");
920 int main(int argc
, const char **argv
)
922 int c
, ret
, test
= -1;
924 struct rlimit open_lim
;
925 FILE *test_validation_output_file
= NULL
;
926 const char *test_validation_output_file_path
= NULL
;
927 struct poptOption optionsTable
[] = {
928 { "test", 't', POPT_ARG_INT
, &test
, 0,
929 "Test to run", NULL
},
930 { "list", 'l', 0, 0, 'l',
931 "List of tests (-t X)", NULL
},
932 { "validation-file", 'o', POPT_ARG_STRING
, &test_validation_output_file_path
, 0,
933 "Test case output", NULL
},
935 { NULL
, 0, 0, NULL
, 0 }
937 const struct test_case
*test_case
;
939 optCon
= poptGetContext(NULL
, argc
, argv
, optionsTable
, 0);
942 poptPrintUsage(optCon
, stderr
, 0);
949 while ((c
= poptGetNextOpt(optCon
)) >= 0) {
957 if (!test_validation_output_file_path
) {
958 fprintf(stderr
, "A test validation file path is required (--validation-file/-o)\n");
963 test_validation_output_file
= fopen(test_validation_output_file_path
, "w+");
964 if (!test_validation_output_file
) {
965 PERROR("Failed to create test validation output file at '%s'",
966 test_validation_output_file_path
);
971 open_lim
.rlim_cur
= MAX_FDS
+ MIN_NR_FDS
;
972 open_lim
.rlim_max
= MAX_FDS
+ MIN_NR_FDS
;
974 ret
= setrlimit(RLIMIT_NOFILE
, &open_lim
);
981 * Some tests might segfault, but we need the getpid() to be output
982 * for the validation, disabling the buffering on the validation file
985 setbuf(test_validation_output_file
, NULL
);
986 wait_fd
= STDIN_FILENO
;
988 /* Test case id is 1-based. */
989 if (test
< 1 || test
> ARRAY_SIZE(test_cases
)) {
990 poptPrintUsage(optCon
, stderr
, 0);
994 test_case
= &test_cases
[test
- 1];
996 timeout
= test_case
->timeout
;
997 if (!test_case
->produces_validation_info
) {
999 * All test cases need to provide, at minimum, the pid of the
1002 ret
= fprintf(test_validation_output_file
, "{ \"pid\": %i }", getpid());
1004 PERROR("Failed to write application pid to test validation file");
1009 test_case
->run(test_validation_output_file
);
1012 if (test_validation_output_file
) {
1013 const int close_ret
= fclose(test_validation_output_file
);
1016 PERROR("Failed to close test output file");
1019 poptFreeContext(optCon
);