Dmitry V. Levin | e837b14 | 2015-02-04 02:09:52 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Based on test by Dr. David Alan Gilbert <dave@treblig.org> |
| 3 | */ |
Dmitry V. Levin | 4960b2f | 2015-08-27 08:24:39 +0000 | [diff] [blame] | 4 | #include <stdio.h> |
| 5 | #include <string.h> |
Dmitry V. Levin | e837b14 | 2015-02-04 02:09:52 +0000 | [diff] [blame] | 6 | #include <unistd.h> |
| 7 | #include <sys/select.h> |
| 8 | |
| 9 | static fd_set set[0x1000000 / sizeof(fd_set)]; |
| 10 | |
Dmitry V. Levin | 4960b2f | 2015-08-27 08:24:39 +0000 | [diff] [blame] | 11 | int main(int ac, char **av) |
Dmitry V. Levin | e837b14 | 2015-02-04 02:09:52 +0000 | [diff] [blame] | 12 | { |
| 13 | int fds[2]; |
Dmitry V. Levin | 100bf7c | 2015-09-18 21:37:09 +0000 | [diff] [blame^] | 14 | struct { |
| 15 | struct timeval tv; |
| 16 | int pad[2]; |
| 17 | } tm = { |
| 18 | .tv = { .tv_usec = 123 }, |
| 19 | .pad = { 0xdeadbeef, 0xbadc0ded } |
| 20 | }; |
Dmitry V. Levin | 4960b2f | 2015-08-27 08:24:39 +0000 | [diff] [blame] | 21 | int is_select = ac < 2 || strcmp(av[1], "pselect6"); |
Dmitry V. Levin | e837b14 | 2015-02-04 02:09:52 +0000 | [diff] [blame] | 22 | |
Dmitry V. Levin | 100bf7c | 2015-09-18 21:37:09 +0000 | [diff] [blame^] | 23 | if (pipe(fds)) |
| 24 | return 77; |
Dmitry V. Levin | e837b14 | 2015-02-04 02:09:52 +0000 | [diff] [blame] | 25 | |
| 26 | /* |
| 27 | * Start with a nice simple select. |
| 28 | */ |
| 29 | FD_ZERO(set); |
Dmitry V. Levin | 4960b2f | 2015-08-27 08:24:39 +0000 | [diff] [blame] | 30 | FD_SET(fds[0], set); |
| 31 | FD_SET(fds[1], set); |
Dmitry V. Levin | 100bf7c | 2015-09-18 21:37:09 +0000 | [diff] [blame^] | 32 | if (select(fds[1] + 1, set, set, set, NULL) != 1) |
| 33 | return 77; |
Dmitry V. Levin | 4960b2f | 2015-08-27 08:24:39 +0000 | [diff] [blame] | 34 | if (is_select) |
| 35 | printf("select(%d, [%d %d], [%d %d], [%d %d], NULL) = 1 ()\n", |
| 36 | fds[1] + 1, fds[0], fds[1], |
| 37 | fds[0], fds[1], fds[0], fds[1]); |
| 38 | else |
| 39 | printf("pselect6(%d, [%d %d], [%d %d], [%d %d], NULL, NULL) " |
| 40 | "= 1 ()\n", |
| 41 | fds[1] + 1, fds[0], fds[1], |
| 42 | fds[0], fds[1], fds[0], fds[1]); |
Dmitry V. Levin | e837b14 | 2015-02-04 02:09:52 +0000 | [diff] [blame] | 43 | |
| 44 | /* |
| 45 | * Now the crash case that trinity found, negative nfds |
| 46 | * but with a pointer to a large chunk of valid memory. |
| 47 | */ |
| 48 | FD_ZERO(set); |
Dmitry V. Levin | 4960b2f | 2015-08-27 08:24:39 +0000 | [diff] [blame] | 49 | FD_SET(fds[1],set); |
Dmitry V. Levin | 100bf7c | 2015-09-18 21:37:09 +0000 | [diff] [blame^] | 50 | if (select(-1, NULL, set, NULL, NULL) != -1) |
| 51 | return 77; |
Dmitry V. Levin | 4960b2f | 2015-08-27 08:24:39 +0000 | [diff] [blame] | 52 | if (is_select) |
| 53 | printf("select(-1, NULL, %p, NULL, NULL) " |
| 54 | "= -1 EINVAL (Invalid argument)\n", set); |
| 55 | else |
| 56 | printf("pselect6(-1, NULL, %p, NULL, NULL, NULL) " |
| 57 | "= -1 EINVAL (Invalid argument)\n", set); |
Dmitry V. Levin | e837b14 | 2015-02-04 02:09:52 +0000 | [diff] [blame] | 58 | |
| 59 | /* |
| 60 | * Another variant, with nfds exceeding FD_SETSIZE limit. |
| 61 | */ |
| 62 | FD_ZERO(set); |
Dmitry V. Levin | 4960b2f | 2015-08-27 08:24:39 +0000 | [diff] [blame] | 63 | FD_SET(fds[0],set); |
Dmitry V. Levin | 100bf7c | 2015-09-18 21:37:09 +0000 | [diff] [blame^] | 64 | if (select(FD_SETSIZE + 1, set, set + 1, NULL, &tm.tv)) |
| 65 | return 77; |
Dmitry V. Levin | 4960b2f | 2015-08-27 08:24:39 +0000 | [diff] [blame] | 66 | if (is_select) |
Dmitry V. Levin | 100bf7c | 2015-09-18 21:37:09 +0000 | [diff] [blame^] | 67 | printf("select(%d, [%d], [], NULL, {0, 123}) = 0 (Timeout)\n", |
Dmitry V. Levin | 4960b2f | 2015-08-27 08:24:39 +0000 | [diff] [blame] | 68 | FD_SETSIZE + 1, fds[0]); |
| 69 | else |
Dmitry V. Levin | 100bf7c | 2015-09-18 21:37:09 +0000 | [diff] [blame^] | 70 | printf("pselect6(%d, [%d], [], NULL, {0, 123000}, NULL) " |
Dmitry V. Levin | 4960b2f | 2015-08-27 08:24:39 +0000 | [diff] [blame] | 71 | "= 0 (Timeout)\n", FD_SETSIZE + 1, fds[0]); |
Dmitry V. Levin | e837b14 | 2015-02-04 02:09:52 +0000 | [diff] [blame] | 72 | |
Dmitry V. Levin | 4960b2f | 2015-08-27 08:24:39 +0000 | [diff] [blame] | 73 | puts("+++ exited with 0 +++"); |
Dmitry V. Levin | e837b14 | 2015-02-04 02:09:52 +0000 | [diff] [blame] | 74 | return 0; |
| 75 | } |