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>
29 #include <common/macros.h>
34 #define NR_ITER 1000 /* for stress-tests */
36 #define MIN_NR_FDS 5 /* the minimum number of open FDs required for the test to run */
37 #define BIG_SELECT_FD 1022
39 #define MSEC_PER_USEC 1000
40 #define MSEC_PER_NSEC (MSEC_PER_USEC * 1000)
42 static int timeout
; /* seconds, -1 to disable */
43 static volatile int stop_thread
;
46 /* Used by logging utils. */
47 int lttng_opt_quiet
, lttng_opt_verbose
, lttng_opt_mi
;
49 static void run_working_cases(FILE *validation_output_file
);
50 static void pselect_invalid_fd(FILE *validation_output_file
);
51 static void test_ppoll_big(FILE *validation_output_file
);
52 static void ppoll_fds_buffer_overflow(FILE *validation_output_file
);
53 static void pselect_invalid_pointer(FILE *validation_output_file
);
54 static void ppoll_fds_ulong_max(FILE *validation_output_file
);
55 static void epoll_pwait_invalid_pointer(FILE *validation_output_file
);
56 static void epoll_pwait_int_max(FILE *validation_output_file
);
57 static void ppoll_concurrent_write(FILE *validation_output_file
);
58 static void epoll_pwait_concurrent_munmap(FILE *validation_output_file
);
60 typedef void (*test_case_cb
)(FILE *output_file
);
62 static const struct test_case
{
64 bool produces_validation_info
;
68 { .run
= run_working_cases
, .produces_validation_info
= true, .timeout
= -1 },
69 { .run
= run_working_cases
, .produces_validation_info
= true, .timeout
= 1 },
70 { .run
= pselect_invalid_fd
, .produces_validation_info
= false },
71 { .run
= test_ppoll_big
, .produces_validation_info
= false },
72 { .run
= ppoll_fds_buffer_overflow
, .produces_validation_info
= false },
73 { .run
= pselect_invalid_pointer
, .produces_validation_info
= false },
74 { .run
= ppoll_fds_ulong_max
, .produces_validation_info
= false },
75 { .run
= epoll_pwait_invalid_pointer
, .produces_validation_info
= true },
76 { .run
= epoll_pwait_int_max
, .produces_validation_info
= true },
77 { .run
= ppoll_concurrent_write
, .produces_validation_info
= false },
78 { .run
= epoll_pwait_concurrent_munmap
, .produces_validation_info
= true },
81 struct ppoll_thread_data
{
87 void test_select_big(void)
89 fd_set rfds
, wfds
, exfds
;
99 fd2
= dup2(wait_fd
, BIG_SELECT_FD
);
107 tv
.tv_usec
= timeout
* MSEC_PER_USEC
;
110 ret
= select(fd2
+ 1, &rfds
, &wfds
, &exfds
, &tv
);
112 ret
= select(fd2
+ 1, &rfds
, &wfds
, &exfds
, NULL
);
118 ret
= read(wait_fd
, buf
, BUF_SIZE
);
120 PERROR("[select] read");
124 ret
= close(BIG_SELECT_FD
);
134 void test_pselect(void)
142 FD_SET(wait_fd
, &rfds
);
145 tv
.tv_nsec
= timeout
* MSEC_PER_NSEC
;
148 ret
= pselect(1, &rfds
, NULL
, NULL
, &tv
, NULL
);
150 ret
= pselect(1, &rfds
, NULL
, NULL
, NULL
, NULL
);
156 ret
= read(wait_fd
, buf
, BUF_SIZE
);
158 PERROR("[pselect] read");
164 void test_select(void)
172 FD_SET(wait_fd
, &rfds
);
175 tv
.tv_usec
= timeout
* MSEC_PER_USEC
;
178 ret
= select(1, &rfds
, NULL
, NULL
, &tv
);
180 ret
= select(1, &rfds
, NULL
, NULL
, NULL
);
186 ret
= read(wait_fd
, buf
, BUF_SIZE
);
188 PERROR("[select] read");
196 struct pollfd ufds
[NB_FD
];
200 ufds
[0].fd
= wait_fd
;
201 ufds
[0].events
= POLLIN
|POLLPRI
;
203 ret
= poll(ufds
, 1, timeout
);
207 } else if (ret
> 0) {
208 ret
= read(wait_fd
, buf
, BUF_SIZE
);
210 PERROR("[poll] read");
216 void test_ppoll(void)
218 struct pollfd ufds
[NB_FD
];
223 ufds
[0].fd
= wait_fd
;
224 ufds
[0].events
= POLLIN
|POLLPRI
;
228 ts
.tv_nsec
= timeout
* MSEC_PER_NSEC
;
229 ret
= ppoll(ufds
, 1, &ts
, NULL
);
231 ret
= ppoll(ufds
, 1, NULL
, NULL
);
237 } else if (ret
> 0) {
238 ret
= read(wait_fd
, buf
, BUF_SIZE
);
240 PERROR("[ppoll] read");
246 void test_ppoll_big(FILE *validation_output_file
)
248 struct pollfd ufds
[MAX_FDS
];
250 int ret
, i
, fds
[MAX_FDS
];
252 for (i
= 0; i
< MAX_FDS
; i
++) {
253 fds
[i
] = dup(wait_fd
);
258 ufds
[i
].events
= POLLIN
|POLLPRI
;
261 ret
= ppoll(ufds
, MAX_FDS
, NULL
, NULL
);
265 } else if (ret
> 0) {
266 ret
= read(wait_fd
, buf
, BUF_SIZE
);
268 PERROR("[ppoll] read");
272 for (i
= 0; i
< MAX_FDS
; i
++) {
283 void test_epoll(FILE *validation_output_file
)
287 struct epoll_event epoll_event
;
289 epollfd
= epoll_create(NB_FD
);
291 PERROR("[epoll] create");
295 ret
= fprintf(validation_output_file
,
296 ", \"epoll_wait_fd\": %i", epollfd
);
298 PERROR("[epoll] Failed to write test validation output");
302 epoll_event
.events
= EPOLLIN
| EPOLLPRI
| EPOLLET
;
303 epoll_event
.data
.fd
= wait_fd
;
304 ret
= epoll_ctl(epollfd
, EPOLL_CTL_ADD
, wait_fd
, &epoll_event
);
306 PERROR("[epoll] add");
311 ret
= epoll_wait(epollfd
, &epoll_event
, 1, timeout
);
313 ret
= epoll_wait(epollfd
, &epoll_event
, 1, -1);
317 ret
= read(wait_fd
, buf
, BUF_SIZE
);
319 PERROR("[epoll] read");
321 } else if (ret
!= 0) {
322 PERROR("epoll_wait");
326 ret
= close(epollfd
);
335 void test_epoll_pwait(FILE *validation_output_file
)
339 struct epoll_event epoll_event
;
341 epollfd
= epoll_create(NB_FD
);
343 PERROR("[epoll_pwait] create");
347 ret
= fprintf(validation_output_file
,
348 ", \"epoll_pwait_fd\": %i", epollfd
);
350 PERROR("[epoll_pwait] Failed to write test validation output");
354 epoll_event
.events
= EPOLLIN
| EPOLLPRI
| EPOLLET
;
355 epoll_event
.data
.fd
= wait_fd
;
356 ret
= epoll_ctl(epollfd
, EPOLL_CTL_ADD
, wait_fd
, &epoll_event
);
358 PERROR("[epoll_pwait] add");
363 ret
= epoll_pwait(epollfd
, &epoll_event
, 1, timeout
, NULL
);
365 ret
= epoll_pwait(epollfd
, &epoll_event
, 1, -1, NULL
);
369 ret
= read(wait_fd
, buf
, BUF_SIZE
);
371 PERROR("[epoll_pwait] read");
373 } else if (ret
!= 0) {
374 PERROR("epoll_pwait");
378 ret
= close(epollfd
);
387 void run_working_cases(FILE *validation_output_file
)
394 * We need an input pipe for some cases and stdin might
395 * have random data, so we create a dummy pipe for this
396 * test to make sure we are running under clean conditions.
398 ret
= pipe(pipe_fds
);
403 wait_fd
= pipe_fds
[0];
411 ret
= fprintf(validation_output_file
, "{ \"pid\": %i", getpid());
413 PERROR("Failed to write pid to test validation file");
417 test_epoll(validation_output_file
);
418 test_epoll_pwait(validation_output_file
);
421 ret
= close(pipe_fds
[0]);
425 ret
= close(pipe_fds
[1]);
431 ret
= fputs(" }", validation_output_file
);
433 PERROR("Failed to close JSON dictionary in test validation file");
442 * Ask for 100 FDs in a buffer for allocated for only 1 FD, should
443 * segfault (eventually with a "*** stack smashing detected ***" message).
444 * The event should contain an array of 100 FDs filled with garbage.
447 void ppoll_fds_buffer_overflow(FILE *validation_output_file
)
449 struct pollfd ufds
[NB_FD
];
453 ufds
[0].fd
= wait_fd
;
454 ufds
[0].events
= POLLIN
|POLLPRI
;
456 ret
= syscall(SYS_ppoll
, ufds
, 100, NULL
, NULL
);
460 } else if (ret
> 0) {
461 ret
= read(wait_fd
, buf
, BUF_SIZE
);
463 PERROR("[ppoll] read");
469 * Ask for ULONG_MAX FDs in a buffer for allocated for only 1 FD, should
470 * cleanly fail with a "Invalid argument".
471 * The event should contain an empty array of FDs and overflow = 1.
474 void ppoll_fds_ulong_max(FILE *validation_output_file
)
476 struct pollfd ufds
[NB_FD
];
480 ufds
[0].fd
= wait_fd
;
481 ufds
[0].events
= POLLIN
|POLLPRI
;
483 ret
= syscall(SYS_ppoll
, ufds
, ULONG_MAX
, NULL
, NULL
);
485 /* Expected error. */
486 } else if (ret
> 0) {
487 ret
= read(wait_fd
, buf
, BUF_SIZE
);
489 PERROR("[ppoll] read");
495 * Pass an invalid file descriptor to pselect6(). The syscall should return
496 * -EBADF. The recorded event should contain a "ret = -EBADF (-9)".
499 void pselect_invalid_fd(FILE *validation_output_file
)
507 * Open a file, close it and use the closed FD in the pselect6 call.
509 fd
= open("/dev/null", O_RDONLY
);
524 ret
= syscall(SYS_pselect6
, fd
+ 1, &rfds
, NULL
, NULL
, NULL
, NULL
);
526 /* Expected error. */
528 ret
= read(wait_fd
, buf
, BUF_SIZE
);
530 PERROR("[pselect] read");
538 * Invalid pointer as writefds, should output a ppoll event
542 void pselect_invalid_pointer(FILE *validation_output_file
)
547 void *invalid
= (void *) 0x42;
550 FD_SET(wait_fd
, &rfds
);
552 ret
= syscall(SYS_pselect6
, 1, &rfds
, (fd_set
*) invalid
, NULL
, NULL
,
555 /* Expected error. */
557 ret
= read(wait_fd
, buf
, BUF_SIZE
);
559 PERROR("[pselect] read");
565 * Pass an invalid pointer to epoll_pwait, should fail with
566 * "Bad address", the event returns 0 FDs.
569 void epoll_pwait_invalid_pointer(FILE *validation_output_file
)
573 struct epoll_event epoll_event
;
574 void *invalid
= (void *) 0x42;
576 epollfd
= epoll_create(NB_FD
);
578 PERROR("[epoll_pwait] create");
582 ret
= fprintf(validation_output_file
,
583 "{ \"epollfd\": %i, \"pid\": %i }", epollfd
,
586 PERROR("[epoll_pwait] Failed to write test validation output");
590 epoll_event
.events
= EPOLLIN
| EPOLLPRI
| EPOLLET
;
591 epoll_event
.data
.fd
= wait_fd
;
592 ret
= epoll_ctl(epollfd
, EPOLL_CTL_ADD
, wait_fd
, &epoll_event
);
594 PERROR("[epoll_pwait] add");
598 ret
= syscall(SYS_epoll_pwait
, epollfd
,
599 (struct epoll_event
*) invalid
, 1, -1, NULL
);
602 ret
= read(wait_fd
, buf
, BUF_SIZE
);
604 PERROR("[epoll_pwait] read");
606 } else if (ret
!= 0) {
607 /* Expected error. */
611 ret
= close(epollfd
);
620 * Set maxevents to INT_MAX, should output "Invalid argument"
621 * The event should return an empty array.
624 void epoll_pwait_int_max(FILE *validation_output_file
)
628 struct epoll_event epoll_event
;
630 epollfd
= epoll_create(NB_FD
);
632 PERROR("[epoll_pwait] create");
636 ret
= fprintf(validation_output_file
,
637 "{ \"epollfd\": %i, \"pid\": %i }", epollfd
,
640 PERROR("[epoll_pwait] Failed to write test validation output");
644 epoll_event
.events
= EPOLLIN
| EPOLLPRI
| EPOLLET
;
645 epoll_event
.data
.fd
= wait_fd
;
646 ret
= epoll_ctl(epollfd
, EPOLL_CTL_ADD
, wait_fd
, &epoll_event
);
648 PERROR("[epoll_pwait] add");
652 ret
= syscall(SYS_epoll_pwait
, epollfd
, &epoll_event
, INT_MAX
, -1,
656 ret
= read(wait_fd
, buf
, BUF_SIZE
);
658 PERROR("[epoll_pwait] read");
660 } else if (ret
!= 0) {
661 /* Expected error. */
665 ret
= close(epollfd
);
674 void *ppoll_writer(void *arg
)
676 struct ppoll_thread_data
*data
= (struct ppoll_thread_data
*) arg
;
678 while (!stop_thread
) {
679 memset(data
->ufds
, data
->value
,
680 MAX_FDS
* sizeof(struct pollfd
));
688 void do_ppoll(int *fds
, struct pollfd
*ufds
)
695 ts
.tv_nsec
= 1 * MSEC_PER_NSEC
;
697 for (i
= 0; i
< MAX_FDS
; i
++) {
699 ufds
[i
].events
= POLLIN
|POLLPRI
;
702 ret
= ppoll(ufds
, MAX_FDS
, &ts
, NULL
);
706 } else if (ret
> 0) {
707 ret
= read(wait_fd
, buf
, BUF_SIZE
);
709 PERROR("[ppoll] read");
715 void stress_ppoll(int *fds
, int value
)
719 struct ppoll_thread_data thread_data
;
720 struct pollfd ufds
[MAX_FDS
];
722 thread_data
.ufds
= ufds
;
723 thread_data
.value
= value
;
726 ret
= pthread_create(&writer
, NULL
, &ppoll_writer
, (void *) &thread_data
);
728 fprintf(stderr
, "[error] pthread_create\n");
731 for (iter
= 0; iter
< NR_ITER
; iter
++) {
735 ret
= pthread_join(writer
, NULL
);
737 fprintf(stderr
, "[error] pthread_join\n");
745 * 3 rounds of NR_ITER iterations with concurrent updates of the pollfd
749 * - memset to INT_MAX
750 * Waits for input, but also set a timeout in case the input FD is overwritten
751 * before entering in the syscall. We use MAX_FDS FDs (dup of stdin), so the
752 * resulting trace is big (20MB).
754 * ppoll should work as expected and the trace should be readable at the end.
757 void ppoll_concurrent_write(FILE *validation_output_file
)
759 int i
, ret
, fds
[MAX_FDS
];
761 for (i
= 0; i
< MAX_FDS
; i
++) {
762 fds
[i
] = dup(wait_fd
);
768 stress_ppoll(fds
, 0);
769 stress_ppoll(fds
, 1);
770 stress_ppoll(fds
, INT_MAX
);
772 for (i
= 0; i
< MAX_FDS
; i
++) {
783 void *epoll_pwait_writer(void *addr
)
787 while (!stop_thread
) {
789 munmap(addr
, MAX_FDS
* sizeof(struct epoll_event
));
796 * epoll_pwait on MAX_FDS fds while a concurrent thread munmaps the
797 * buffer allocated for the returned data. This should randomly segfault.
798 * The trace should be readable and no kernel OOPS should occur.
801 void epoll_pwait_concurrent_munmap(FILE *validation_output_file
)
803 int ret
, epollfd
, i
, fds
[MAX_FDS
];
805 struct epoll_event
*epoll_event
;
808 for (i
= 0; i
< MAX_FDS
; i
++) {
811 epollfd
= epoll_create(MAX_FDS
);
813 PERROR("[epoll_pwait] create");
817 ret
= fprintf(validation_output_file
,
818 "{ \"epollfd\": %i, \"pid\": %i }", epollfd
,
821 PERROR("[epoll_pwait] Failed to write test validation output");
825 epoll_event
= (struct epoll_event
*) mmap(NULL
,
826 MAX_FDS
* sizeof(struct epoll_event
),
827 PROT_READ
| PROT_WRITE
, MAP_PRIVATE
| MAP_ANONYMOUS
, -1,
829 if (epoll_event
== MAP_FAILED
) {
834 for (i
= 0; i
< MAX_FDS
; i
++) {
835 fds
[i
] = dup(wait_fd
);
839 epoll_event
[i
].events
= EPOLLIN
| EPOLLPRI
| EPOLLET
;
840 epoll_event
[i
].data
.fd
= fds
[i
];
841 ret
= epoll_ctl(epollfd
, EPOLL_CTL_ADD
, fds
[i
], epoll_event
);
843 PERROR("[epoll_pwait] add");
848 ret
= pthread_create(&writer
, NULL
, &epoll_pwait_writer
,
849 (void *) epoll_event
);
851 fprintf(stderr
, "[error] pthread_create\n");
855 ret
= epoll_pwait(epollfd
, epoll_event
, 1, 1, NULL
);
858 ret
= read(wait_fd
, buf
, BUF_SIZE
);
860 PERROR("[epoll_pwait] read");
862 } else if (ret
!= 0) {
863 /* Expected error. */
867 ret
= pthread_join(writer
, NULL
);
869 fprintf(stderr
, "[error] pthread_join\n");
873 for (i
= 0; i
< MAX_FDS
; i
++) {
880 ret
= munmap(epoll_event
, MAX_FDS
* sizeof(struct epoll_event
));
886 ret
= close(epollfd
);
895 void print_list(void)
897 fprintf(stderr
, "Test list (-t X):\n");
898 fprintf(stderr
, "\t1: Working cases for select, pselect6, poll, ppoll "
899 "and epoll, waiting for input\n");
900 fprintf(stderr
, "\t2: Timeout cases (1ms) for select, pselect6, poll, "
901 "ppoll and epoll\n");
902 fprintf(stderr
, "\t3: pselect with an invalid fd\n");
903 fprintf(stderr
, "\t4: ppoll with %d FDs\n", MAX_FDS
);
904 fprintf(stderr
, "\t5: ppoll buffer overflow, should segfault, waits "
906 fprintf(stderr
, "\t6: pselect with an invalid pointer, waits for "
908 fprintf(stderr
, "\t7: ppoll with ulong_max fds, waits for input\n");
909 fprintf(stderr
, "\t8: epoll_pwait with an invalid pointer, waits for "
911 fprintf(stderr
, "\t9: epoll_pwait with maxevents set to INT_MAX, "
912 "waits for input\n");
913 fprintf(stderr
, "\t10: ppoll with concurrent updates of the structure "
914 "from user-space, stress test (3000 iterations), "
915 "waits for input + timeout 1ms\n");
916 fprintf(stderr
, "\t11: epoll_pwait with concurrent munmap of the buffer "
917 "from user-space, should randomly segfault, run "
918 "multiple times, waits for input + timeout 1ms\n");
921 int main(int argc
, const char **argv
)
923 int c
, ret
, test
= -1;
925 struct rlimit open_lim
;
926 FILE *test_validation_output_file
= NULL
;
927 const char *test_validation_output_file_path
= NULL
;
928 struct poptOption optionsTable
[] = {
929 { "test", 't', POPT_ARG_INT
, &test
, 0,
930 "Test to run", NULL
},
931 { "list", 'l', 0, 0, 'l',
932 "List of tests (-t X)", NULL
},
933 { "validation-file", 'o', POPT_ARG_STRING
, &test_validation_output_file_path
, 0,
934 "Test case output", NULL
},
936 { NULL
, 0, 0, NULL
, 0 }
938 const struct test_case
*test_case
;
940 optCon
= poptGetContext(NULL
, argc
, argv
, optionsTable
, 0);
943 poptPrintUsage(optCon
, stderr
, 0);
950 while ((c
= poptGetNextOpt(optCon
)) >= 0) {
958 if (!test_validation_output_file_path
) {
959 fprintf(stderr
, "A test validation file path is required (--validation-file/-o)\n");
964 test_validation_output_file
= fopen(test_validation_output_file_path
, "w+");
965 if (!test_validation_output_file
) {
966 PERROR("Failed to create test validation output file at '%s'",
967 test_validation_output_file_path
);
972 open_lim
.rlim_cur
= MAX_FDS
+ MIN_NR_FDS
;
973 open_lim
.rlim_max
= MAX_FDS
+ MIN_NR_FDS
;
975 ret
= setrlimit(RLIMIT_NOFILE
, &open_lim
);
982 * Some tests might segfault, but we need the getpid() to be output
983 * for the validation, disabling the buffering on the validation file
986 setbuf(test_validation_output_file
, NULL
);
987 wait_fd
= STDIN_FILENO
;
989 /* Test case id is 1-based. */
990 if (test
< 1 || test
> ARRAY_SIZE(test_cases
)) {
991 poptPrintUsage(optCon
, stderr
, 0);
995 test_case
= &test_cases
[test
- 1];
997 timeout
= test_case
->timeout
;
998 if (!test_case
->produces_validation_info
) {
1000 * All test cases need to provide, at minimum, the pid of the
1003 ret
= fprintf(test_validation_output_file
, "{ \"pid\": %i }", getpid());
1005 PERROR("Failed to write application pid to test validation file");
1010 test_case
->run(test_validation_output_file
);
1013 if (test_validation_output_file
) {
1014 const int close_ret
= fclose(test_validation_output_file
);
1017 PERROR("Failed to close test output file");
1020 poptFreeContext(optCon
);