2 * Check decoding of ppoll syscall.
4 * Copyright (c) 2015-2017 Dmitry V. Levin <ldv@altlinux.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <asm/unistd.h>
42 static const char *errstr;
45 sys_ppoll(const kernel_ulong_t ufds,
46 const kernel_ulong_t nfds,
47 const kernel_ulong_t tsp,
48 const kernel_ulong_t sigmask,
49 const kernel_ulong_t sigsetsize)
51 long rc = syscall(__NR_ppoll, ufds, nfds, tsp, sigmask, sigsetsize);
52 errstr = sprintrc(rc);
59 static const kernel_ulong_t bogus_nfds =
60 (kernel_ulong_t) 0xdeadbeeffacefeedULL;
61 static const kernel_ulong_t bogus_sigsetsize =
62 (kernel_ulong_t) 0xdeadbeefbadc0dedULL;
63 static const char *const POLLWRNORM_str =
64 (POLLWRNORM == POLLOUT) ? "" : "|POLLWRNORM";
65 static const char *const USR2_CHLD_str =
66 (SIGUSR2 < SIGCHLD) ? "USR2 CHLD" : "CHLD USR2";
67 void *const efault = tail_alloc(1024) + 1024;
68 TAIL_ALLOC_OBJECT_CONST_PTR(struct timespec, ts);
69 const unsigned int sigset_size = get_sigset_size();
70 void *const sigmask = tail_alloc(sigset_size);
76 sys_ppoll(0, bogus_nfds, 0, 0, bogus_sigsetsize);
78 perror_msg_and_skip("ppoll");
79 printf("ppoll(NULL, %u, NULL, NULL, %llu) = %s\n",
80 (unsigned) bogus_nfds, (unsigned long long) bogus_sigsetsize,
83 sys_ppoll((unsigned long) efault, 42, (unsigned long) efault + 8,
84 (unsigned long) efault + 16, sigset_size);
85 printf("ppoll(%p, %u, %p, %p, %u) = %s\n",
86 efault, 42, efault + 8, efault + 16, sigset_size, errstr);
88 if (pipe(pipe_fd) || pipe(pipe_fd + 2))
89 perror_msg_and_fail("pipe");
92 ts->tv_nsec = 999999999;
94 const struct pollfd fds1[] = {
95 { .fd = pipe_fd[0], .events = POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND },
96 { .fd = pipe_fd[1], .events = POLLOUT | POLLWRNORM | POLLWRBAND },
97 { .fd = pipe_fd[2], .events = POLLIN | POLLPRI },
98 { .fd = pipe_fd[3], .events = POLLOUT }
100 fds = efault - sizeof(fds1);
101 memcpy(fds, fds1, sizeof(fds1));
104 sigaddset(&mask, SIGUSR2);
105 sigaddset(&mask, SIGCHLD);
106 memcpy(sigmask, &mask, sigset_size);
108 rc = sys_ppoll((unsigned long) fds,
109 F8ILL_KULONG_MASK | ARRAY_SIZE(fds1), (unsigned long) ts,
110 (unsigned long) sigmask, sigset_size);
112 perror_msg_and_fail("ppoll 1");
113 printf("ppoll([{fd=%d, events=POLLIN|POLLPRI|POLLRDNORM|POLLRDBAND}"
114 ", {fd=%d, events=POLLOUT%s|POLLWRBAND}"
116 ", {fd=%d, events=POLLIN|POLLPRI}, {fd=%d, events=POLLOUT}]"
120 ", %u, {tv_sec=42, tv_nsec=999999999}, [%s], %u) = %ld"
121 " ([{fd=%d, revents=POLLOUT%s}, {fd=%d, revents=POLLOUT}]"
122 ", left {tv_sec=%u, tv_nsec=%u})\n",
123 pipe_fd[0], pipe_fd[1], POLLWRNORM_str,
125 pipe_fd[2], pipe_fd[3],
127 (unsigned) ARRAY_SIZE(fds1), USR2_CHLD_str,
128 (unsigned) sigset_size, rc, pipe_fd[1], POLLWRNORM_str,
129 pipe_fd[3], (unsigned ) ts->tv_sec, (unsigned) ts->tv_nsec);
133 const struct pollfd fds2[] = {
134 { .fd = pipe_fd[1], .events = POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND },
135 { .fd = pipe_fd[0], .events = POLLOUT | POLLWRNORM | POLLWRBAND }
137 fds = efault - sizeof(fds2);
138 memcpy(fds, fds2, sizeof(fds2));
140 memset(&mask, -1, sizeof(mask));
141 sigdelset(&mask, SIGHUP);
142 sigdelset(&mask, SIGKILL);
143 sigdelset(&mask, SIGSTOP);
144 memcpy(sigmask, &mask, sigset_size);
146 rc = sys_ppoll((unsigned long) fds,
147 F8ILL_KULONG_MASK | ARRAY_SIZE(fds2), (unsigned long) ts,
148 (unsigned long) sigmask, sigset_size);
150 perror_msg_and_fail("ppoll 2");
151 printf("ppoll([{fd=%d, events=POLLIN|POLLPRI|POLLRDNORM|POLLRDBAND}"
152 ", {fd=%d, events=POLLOUT%s|POLLWRBAND}], %u"
153 ", {tv_sec=0, tv_nsec=999}, ~[HUP KILL STOP], %u)"
154 " = %ld (Timeout)\n",
155 pipe_fd[1], pipe_fd[0], POLLWRNORM_str,
156 (unsigned) ARRAY_SIZE(fds2), sigset_size, rc);
158 if (F8ILL_KULONG_SUPPORTED) {
159 sys_ppoll(f8ill_ptr_to_kulong(fds), ARRAY_SIZE(fds2),
160 f8ill_ptr_to_kulong(ts), f8ill_ptr_to_kulong(sigmask),
162 printf("ppoll(%#llx, %u, %#llx, %#llx, %u) = %s\n",
163 (unsigned long long) f8ill_ptr_to_kulong(fds),
164 (unsigned) ARRAY_SIZE(fds2),
165 (unsigned long long) f8ill_ptr_to_kulong(ts),
166 (unsigned long long) f8ill_ptr_to_kulong(sigmask),
167 (unsigned) sigset_size, errstr);
170 puts("+++ exited with 0 +++");
176 SKIP_MAIN_UNDEFINED("__NR_ppoll")