blob: f5939f3bb2bad4aff6685a78bdd096c74740b63a [file] [log] [blame]
Dmitry V. Levin77a1b6b2015-09-15 02:14:38 +00001/*
Dmitry V. Levin8d2674e2016-01-05 22:55:05 +00002 * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
Elliott Hughes39bac052017-05-25 16:56:11 -07003 * Copyright (c) 2015-2017 The strace developers.
Dmitry V. Levin6c528f52015-09-18 17:05:35 +00004 * 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. Levin77a1b6b2015-09-15 02:14:38 +000030 * Based on test by Dr. David Alan Gilbert <dave@treblig.org>
31 */
Dmitry V. Levin8d2674e2016-01-05 22:55:05 +000032
33#include "tests.h"
Elliott Hughesd35df492017-02-15 15:19:05 -080034#include "nsig.h"
Dmitry V. Levin8d2674e2016-01-05 22:55:05 +000035#include <assert.h>
Dmitry V. Levin77a1b6b2015-09-15 02:14:38 +000036#include <stdio.h>
Dmitry V. Levin77a1b6b2015-09-15 02:14:38 +000037#include <unistd.h>
38#include <sys/select.h>
Dmitry V. Levin6a2f43c2016-08-09 14:38:29 +000039#include <asm/unistd.h>
Dmitry V. Levin6c528f52015-09-18 17:05:35 +000040#include <sys/time.h>
Dmitry V. Levin77a1b6b2015-09-15 02:14:38 +000041
Dmitry V. Levin5d983102015-09-18 21:20:56 +000042#ifdef __NR_pselect6
43
Dmitry V. Levin77a1b6b2015-09-15 02:14:38 +000044static fd_set set[3][0x1000000 / sizeof(fd_set)];
45
Dmitry V. Levin6c528f52015-09-18 17:05:35 +000046static void
47handler(int signo)
48{
49}
50
Dmitry V. Levin77a1b6b2015-09-15 02:14:38 +000051int main(int ac, char **av)
52{
53 int fds[2];
Dmitry V. Levin5d983102015-09-18 21:20:56 +000054 struct {
55 struct timespec ts;
56 int pad[2];
Dmitry V. Levin81ea4bf2015-09-18 21:20:56 +000057 } tm_in = {
58 .ts = { .tv_sec = 0xc0de1, .tv_nsec = 0xc0de2 },
Dmitry V. Levin5d983102015-09-18 21:20:56 +000059 .pad = { 0xdeadbeef, 0xbadc0ded }
Dmitry V. Levin81ea4bf2015-09-18 21:20:56 +000060 }, tm = tm_in;
Dmitry V. Levin77a1b6b2015-09-15 02:14:38 +000061 sigset_t mask;
Dmitry V. Levin6c528f52015-09-18 17:05:35 +000062 const struct sigaction act = { .sa_handler = handler };
63 const struct itimerval itv = { .it_value.tv_usec = 111111 };
Dmitry V. Levin77a1b6b2015-09-15 02:14:38 +000064
65 sigemptyset(&mask);
66 sigaddset(&mask, SIGHUP);
67 sigaddset(&mask, SIGCHLD);
68
Dmitry V. Levin5d983102015-09-18 21:20:56 +000069 if (pipe(fds))
Dmitry V. Levin8d2674e2016-01-05 22:55:05 +000070 perror_msg_and_fail("pipe");
Dmitry V. Levin77a1b6b2015-09-15 02:14:38 +000071
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. Levin8d2674e2016-01-05 22:55:05 +000081 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. Levin77a1b6b2015-09-15 02:14:38 +000085 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 Hughesd35df492017-02-15 15:19:05 -080089 NSIG_BYTES, fds[1]);
Dmitry V. Levin77a1b6b2015-09-15 02:14:38 +000090
91 /*
Dmitry V. Levin81ea4bf2015-09-18 21:20:56 +000092 * 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. Levin8d2674e2016-01-05 22:55:05 +000098 assert(syscall(__NR_pselect6, fds[1] + 1, NULL, set[1], NULL, &tm.ts, NULL) == 3);
Elliott Hughes39bac052017-05-25 16:56:11 -070099 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. Levin81ea4bf2015-09-18 21:20:56 +0000106
107 /*
Dmitry V. Levin77a1b6b2015-09-15 02:14:38 +0000108 * 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. Levin8d2674e2016-01-05 22:55:05 +0000113 assert(pselect(-1, NULL, set[0], NULL, NULL, &mask) == -1);
Dmitry V. Levin77a1b6b2015-09-15 02:14:38 +0000114 printf("pselect6(-1, NULL, %p, NULL, NULL, {[HUP CHLD], %u}) "
Elliott Hughesd35df492017-02-15 15:19:05 -0800115 "= -1 EINVAL (%m)\n", set[0], NSIG_BYTES);
Dmitry V. Levin77a1b6b2015-09-15 02:14:38 +0000116
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. Levin81ea4bf2015-09-18 21:20:56 +0000123 tm.ts.tv_sec = 0;
124 tm.ts.tv_nsec = 123;
Dmitry V. Levin8d2674e2016-01-05 22:55:05 +0000125 assert(pselect(FD_SETSIZE + 1, set[0], set[1], NULL, &tm.ts, &mask) == 0);
Elliott Hughes39bac052017-05-25 16:56:11 -0700126 printf("pselect6(%d, [%d], [], NULL, {tv_sec=0, tv_nsec=123}"
127 ", {[HUP CHLD], %u}) = 0 (Timeout)\n",
Elliott Hughesd35df492017-02-15 15:19:05 -0800128 FD_SETSIZE + 1, fds[0], NSIG_BYTES);
Dmitry V. Levin77a1b6b2015-09-15 02:14:38 +0000129
Dmitry V. Levin6c528f52015-09-18 17:05:35 +0000130 /*
131 * See how timeouts are decoded.
132 */
Elliott Hughes39bac052017-05-25 16:56:11 -0700133 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. Levin8d2674e2016-01-05 22:55:05 +0000151 assert(sigaction(SIGALRM, &act, NULL) == 0);
152 assert(setitimer(ITIMER_REAL, &itv, NULL) == 0);
Dmitry V. Levin6c528f52015-09-18 17:05:35 +0000153
Elliott Hughes39bac052017-05-25 16:56:11 -0700154 tm.ts.tv_sec = 0;
Dmitry V. Levin5d983102015-09-18 21:20:56 +0000155 tm.ts.tv_nsec = 222222222;
Dmitry V. Levin8d2674e2016-01-05 22:55:05 +0000156 assert(pselect(0, NULL, NULL, NULL, &tm.ts, &mask) == -1);
Elliott Hughes39bac052017-05-25 16:56:11 -0700157 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 Hughesd35df492017-02-15 15:19:05 -0800160 NSIG_BYTES);
Dmitry V. Levin6c528f52015-09-18 17:05:35 +0000161 puts("--- SIGALRM {si_signo=SIGALRM, si_code=SI_KERNEL} ---");
162
Dmitry V. Levin77a1b6b2015-09-15 02:14:38 +0000163 puts("+++ exited with 0 +++");
164 return 0;
165}
Dmitry V. Levin5d983102015-09-18 21:20:56 +0000166
167#else
168
Dmitry V. Levin8d2674e2016-01-05 22:55:05 +0000169SKIP_MAIN_UNDEFINED("__NR_pselect6")
Dmitry V. Levin5d983102015-09-18 21:20:56 +0000170
171#endif