Dmitry V. Levin | 77a1b6b | 2015-09-15 02:14:38 +0000 | [diff] [blame] | 1 | /* |
Dmitry V. Levin | 8d2674e | 2016-01-05 22:55:05 +0000 | [diff] [blame] | 2 | * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org> |
Elliott Hughes | 39bac05 | 2017-05-25 16:56:11 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2015-2017 The strace developers. |
Dmitry V. Levin | 6c528f5 | 2015-09-18 17:05:35 +0000 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * 3. The name of the author may not be used to endorse or promote products |
| 15 | * derived from this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | /* |
Dmitry V. Levin | 77a1b6b | 2015-09-15 02:14:38 +0000 | [diff] [blame] | 30 | * Based on test by Dr. David Alan Gilbert <dave@treblig.org> |
| 31 | */ |
Dmitry V. Levin | 8d2674e | 2016-01-05 22:55:05 +0000 | [diff] [blame] | 32 | |
| 33 | #include "tests.h" |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 34 | #include "nsig.h" |
Dmitry V. Levin | 8d2674e | 2016-01-05 22:55:05 +0000 | [diff] [blame] | 35 | #include <assert.h> |
Dmitry V. Levin | 77a1b6b | 2015-09-15 02:14:38 +0000 | [diff] [blame] | 36 | #include <stdio.h> |
Dmitry V. Levin | 77a1b6b | 2015-09-15 02:14:38 +0000 | [diff] [blame] | 37 | #include <unistd.h> |
| 38 | #include <sys/select.h> |
Dmitry V. Levin | 6a2f43c | 2016-08-09 14:38:29 +0000 | [diff] [blame] | 39 | #include <asm/unistd.h> |
Dmitry V. Levin | 6c528f5 | 2015-09-18 17:05:35 +0000 | [diff] [blame] | 40 | #include <sys/time.h> |
Dmitry V. Levin | 77a1b6b | 2015-09-15 02:14:38 +0000 | [diff] [blame] | 41 | |
Dmitry V. Levin | 5d98310 | 2015-09-18 21:20:56 +0000 | [diff] [blame] | 42 | #ifdef __NR_pselect6 |
| 43 | |
Dmitry V. Levin | 77a1b6b | 2015-09-15 02:14:38 +0000 | [diff] [blame] | 44 | static fd_set set[3][0x1000000 / sizeof(fd_set)]; |
| 45 | |
Dmitry V. Levin | 6c528f5 | 2015-09-18 17:05:35 +0000 | [diff] [blame] | 46 | static void |
| 47 | handler(int signo) |
| 48 | { |
| 49 | } |
| 50 | |
Dmitry V. Levin | 77a1b6b | 2015-09-15 02:14:38 +0000 | [diff] [blame] | 51 | int main(int ac, char **av) |
| 52 | { |
| 53 | int fds[2]; |
Dmitry V. Levin | 5d98310 | 2015-09-18 21:20:56 +0000 | [diff] [blame] | 54 | struct { |
| 55 | struct timespec ts; |
| 56 | int pad[2]; |
Dmitry V. Levin | 81ea4bf | 2015-09-18 21:20:56 +0000 | [diff] [blame] | 57 | } tm_in = { |
| 58 | .ts = { .tv_sec = 0xc0de1, .tv_nsec = 0xc0de2 }, |
Dmitry V. Levin | 5d98310 | 2015-09-18 21:20:56 +0000 | [diff] [blame] | 59 | .pad = { 0xdeadbeef, 0xbadc0ded } |
Dmitry V. Levin | 81ea4bf | 2015-09-18 21:20:56 +0000 | [diff] [blame] | 60 | }, tm = tm_in; |
Dmitry V. Levin | 77a1b6b | 2015-09-15 02:14:38 +0000 | [diff] [blame] | 61 | sigset_t mask; |
Dmitry V. Levin | 6c528f5 | 2015-09-18 17:05:35 +0000 | [diff] [blame] | 62 | const struct sigaction act = { .sa_handler = handler }; |
| 63 | const struct itimerval itv = { .it_value.tv_usec = 111111 }; |
Dmitry V. Levin | 77a1b6b | 2015-09-15 02:14:38 +0000 | [diff] [blame] | 64 | |
| 65 | sigemptyset(&mask); |
| 66 | sigaddset(&mask, SIGHUP); |
| 67 | sigaddset(&mask, SIGCHLD); |
| 68 | |
Dmitry V. Levin | 5d98310 | 2015-09-18 21:20:56 +0000 | [diff] [blame] | 69 | if (pipe(fds)) |
Dmitry V. Levin | 8d2674e | 2016-01-05 22:55:05 +0000 | [diff] [blame] | 70 | perror_msg_and_fail("pipe"); |
Dmitry V. Levin | 77a1b6b | 2015-09-15 02:14:38 +0000 | [diff] [blame] | 71 | |
| 72 | /* |
| 73 | * Start with a nice simple pselect. |
| 74 | */ |
| 75 | FD_SET(fds[0], set[0]); |
| 76 | FD_SET(fds[1], set[0]); |
| 77 | FD_SET(fds[0], set[1]); |
| 78 | FD_SET(fds[1], set[1]); |
| 79 | FD_SET(1, set[2]); |
| 80 | FD_SET(2, set[2]); |
Dmitry V. Levin | 8d2674e | 2016-01-05 22:55:05 +0000 | [diff] [blame] | 81 | int rc = pselect(fds[1] + 1, set[0], set[1], set[2], NULL, NULL); |
| 82 | if (rc < 0) |
| 83 | perror_msg_and_skip("pselect"); |
| 84 | assert(rc == 1); |
Dmitry V. Levin | 77a1b6b | 2015-09-15 02:14:38 +0000 | [diff] [blame] | 85 | printf("pselect6(%d, [%d %d], [%d %d], [1 2], NULL, {NULL, %u}) " |
| 86 | "= 1 (out [%d])\n", |
| 87 | fds[1] + 1, fds[0], fds[1], |
| 88 | fds[0], fds[1], |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 89 | NSIG_BYTES, fds[1]); |
Dmitry V. Levin | 77a1b6b | 2015-09-15 02:14:38 +0000 | [diff] [blame] | 90 | |
| 91 | /* |
Dmitry V. Levin | 81ea4bf | 2015-09-18 21:20:56 +0000 | [diff] [blame] | 92 | * Another simple one, with a timeout. |
| 93 | */ |
| 94 | FD_SET(1, set[1]); |
| 95 | FD_SET(2, set[1]); |
| 96 | FD_SET(fds[0], set[1]); |
| 97 | FD_SET(fds[1], set[1]); |
Dmitry V. Levin | 8d2674e | 2016-01-05 22:55:05 +0000 | [diff] [blame] | 98 | assert(syscall(__NR_pselect6, fds[1] + 1, NULL, set[1], NULL, &tm.ts, NULL) == 3); |
Elliott Hughes | 39bac05 | 2017-05-25 16:56:11 -0700 | [diff] [blame^] | 99 | printf("pselect6(%d, NULL, [1 2 %d %d], NULL" |
| 100 | ", {tv_sec=%lld, tv_nsec=%llu}, NULL) = 3 (out [1 2 %d]" |
| 101 | ", left {tv_sec=%lld, tv_nsec=%llu})\n", |
| 102 | fds[1] + 1, fds[0], fds[1], (long long) tm_in.ts.tv_sec, |
| 103 | zero_extend_signed_to_ull(tm_in.ts.tv_nsec), |
| 104 | fds[1], (long long) tm.ts.tv_sec, |
| 105 | zero_extend_signed_to_ull(tm.ts.tv_nsec)); |
Dmitry V. Levin | 81ea4bf | 2015-09-18 21:20:56 +0000 | [diff] [blame] | 106 | |
| 107 | /* |
Dmitry V. Levin | 77a1b6b | 2015-09-15 02:14:38 +0000 | [diff] [blame] | 108 | * Now the crash case that trinity found, negative nfds |
| 109 | * but with a pointer to a large chunk of valid memory. |
| 110 | */ |
| 111 | FD_ZERO(set[0]); |
| 112 | FD_SET(fds[1],set[0]); |
Dmitry V. Levin | 8d2674e | 2016-01-05 22:55:05 +0000 | [diff] [blame] | 113 | assert(pselect(-1, NULL, set[0], NULL, NULL, &mask) == -1); |
Dmitry V. Levin | 77a1b6b | 2015-09-15 02:14:38 +0000 | [diff] [blame] | 114 | printf("pselect6(-1, NULL, %p, NULL, NULL, {[HUP CHLD], %u}) " |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 115 | "= -1 EINVAL (%m)\n", set[0], NSIG_BYTES); |
Dmitry V. Levin | 77a1b6b | 2015-09-15 02:14:38 +0000 | [diff] [blame] | 116 | |
| 117 | /* |
| 118 | * Another variant, with nfds exceeding FD_SETSIZE limit. |
| 119 | */ |
| 120 | FD_ZERO(set[0]); |
| 121 | FD_SET(fds[0],set[0]); |
| 122 | FD_ZERO(set[1]); |
Dmitry V. Levin | 81ea4bf | 2015-09-18 21:20:56 +0000 | [diff] [blame] | 123 | tm.ts.tv_sec = 0; |
| 124 | tm.ts.tv_nsec = 123; |
Dmitry V. Levin | 8d2674e | 2016-01-05 22:55:05 +0000 | [diff] [blame] | 125 | assert(pselect(FD_SETSIZE + 1, set[0], set[1], NULL, &tm.ts, &mask) == 0); |
Elliott Hughes | 39bac05 | 2017-05-25 16:56:11 -0700 | [diff] [blame^] | 126 | printf("pselect6(%d, [%d], [], NULL, {tv_sec=0, tv_nsec=123}" |
| 127 | ", {[HUP CHLD], %u}) = 0 (Timeout)\n", |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 128 | FD_SETSIZE + 1, fds[0], NSIG_BYTES); |
Dmitry V. Levin | 77a1b6b | 2015-09-15 02:14:38 +0000 | [diff] [blame] | 129 | |
Dmitry V. Levin | 6c528f5 | 2015-09-18 17:05:35 +0000 | [diff] [blame] | 130 | /* |
| 131 | * See how timeouts are decoded. |
| 132 | */ |
Elliott Hughes | 39bac05 | 2017-05-25 16:56:11 -0700 | [diff] [blame^] | 133 | tm.ts.tv_sec = 0xdeadbeefU; |
| 134 | tm.ts.tv_nsec = 0xfacefeedU; |
| 135 | assert(pselect(0, NULL, NULL, NULL, &tm.ts, NULL) == -1); |
| 136 | printf("pselect6(0, NULL, NULL, NULL" |
| 137 | ", {tv_sec=%lld, tv_nsec=%llu}, {NULL, %u}) = %s\n", |
| 138 | (long long) tm.ts.tv_sec, |
| 139 | zero_extend_signed_to_ull(tm.ts.tv_nsec), |
| 140 | NSIG_BYTES, sprintrc(-1)); |
| 141 | |
| 142 | tm.ts.tv_sec = (time_t) 0xcafef00ddeadbeefLL; |
| 143 | tm.ts.tv_nsec = (long) 0xbadc0dedfacefeedLL; |
| 144 | assert(pselect(0, NULL, NULL, NULL, &tm.ts, NULL) == -1); |
| 145 | printf("pselect6(0, NULL, NULL, NULL" |
| 146 | ", {tv_sec=%lld, tv_nsec=%llu}, {NULL, %u}) = %s\n", |
| 147 | (long long) tm.ts.tv_sec, |
| 148 | zero_extend_signed_to_ull(tm.ts.tv_nsec), |
| 149 | NSIG_BYTES, sprintrc(-1)); |
| 150 | |
Dmitry V. Levin | 8d2674e | 2016-01-05 22:55:05 +0000 | [diff] [blame] | 151 | assert(sigaction(SIGALRM, &act, NULL) == 0); |
| 152 | assert(setitimer(ITIMER_REAL, &itv, NULL) == 0); |
Dmitry V. Levin | 6c528f5 | 2015-09-18 17:05:35 +0000 | [diff] [blame] | 153 | |
Elliott Hughes | 39bac05 | 2017-05-25 16:56:11 -0700 | [diff] [blame^] | 154 | tm.ts.tv_sec = 0; |
Dmitry V. Levin | 5d98310 | 2015-09-18 21:20:56 +0000 | [diff] [blame] | 155 | tm.ts.tv_nsec = 222222222; |
Dmitry V. Levin | 8d2674e | 2016-01-05 22:55:05 +0000 | [diff] [blame] | 156 | assert(pselect(0, NULL, NULL, NULL, &tm.ts, &mask) == -1); |
Elliott Hughes | 39bac05 | 2017-05-25 16:56:11 -0700 | [diff] [blame^] | 157 | printf("pselect6(0, NULL, NULL, NULL, {tv_sec=0, tv_nsec=222222222}" |
| 158 | ", {[HUP CHLD], %u})" |
| 159 | " = ? ERESTARTNOHAND (To be restarted if no handler)\n", |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 160 | NSIG_BYTES); |
Dmitry V. Levin | 6c528f5 | 2015-09-18 17:05:35 +0000 | [diff] [blame] | 161 | puts("--- SIGALRM {si_signo=SIGALRM, si_code=SI_KERNEL} ---"); |
| 162 | |
Dmitry V. Levin | 77a1b6b | 2015-09-15 02:14:38 +0000 | [diff] [blame] | 163 | puts("+++ exited with 0 +++"); |
| 164 | return 0; |
| 165 | } |
Dmitry V. Levin | 5d98310 | 2015-09-18 21:20:56 +0000 | [diff] [blame] | 166 | |
| 167 | #else |
| 168 | |
Dmitry V. Levin | 8d2674e | 2016-01-05 22:55:05 +0000 | [diff] [blame] | 169 | SKIP_MAIN_UNDEFINED("__NR_pselect6") |
Dmitry V. Levin | 5d98310 | 2015-09-18 21:20:56 +0000 | [diff] [blame] | 170 | |
| 171 | #endif |