1*cf84ac9aSAndroid Build Coastguard Worker /*
2*cf84ac9aSAndroid Build Coastguard Worker * Test whether process_vm_readv and PTRACE_PEEKDATA work.
3*cf84ac9aSAndroid Build Coastguard Worker *
4*cf84ac9aSAndroid Build Coastguard Worker * Copyright (c) 2016-2017 Dmitry V. Levin <[email protected]>
5*cf84ac9aSAndroid Build Coastguard Worker * Copyright (c) 2017-2018 The strace developers.
6*cf84ac9aSAndroid Build Coastguard Worker * All rights reserved.
7*cf84ac9aSAndroid Build Coastguard Worker *
8*cf84ac9aSAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without
9*cf84ac9aSAndroid Build Coastguard Worker * modification, are permitted provided that the following conditions
10*cf84ac9aSAndroid Build Coastguard Worker * are met:
11*cf84ac9aSAndroid Build Coastguard Worker * 1. Redistributions of source code must retain the above copyright
12*cf84ac9aSAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer.
13*cf84ac9aSAndroid Build Coastguard Worker * 2. Redistributions in binary form must reproduce the above copyright
14*cf84ac9aSAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer in the
15*cf84ac9aSAndroid Build Coastguard Worker * documentation and/or other materials provided with the distribution.
16*cf84ac9aSAndroid Build Coastguard Worker * 3. The name of the author may not be used to endorse or promote products
17*cf84ac9aSAndroid Build Coastguard Worker * derived from this software without specific prior written permission.
18*cf84ac9aSAndroid Build Coastguard Worker *
19*cf84ac9aSAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20*cf84ac9aSAndroid Build Coastguard Worker * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21*cf84ac9aSAndroid Build Coastguard Worker * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22*cf84ac9aSAndroid Build Coastguard Worker * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23*cf84ac9aSAndroid Build Coastguard Worker * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24*cf84ac9aSAndroid Build Coastguard Worker * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25*cf84ac9aSAndroid Build Coastguard Worker * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26*cf84ac9aSAndroid Build Coastguard Worker * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27*cf84ac9aSAndroid Build Coastguard Worker * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28*cf84ac9aSAndroid Build Coastguard Worker * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*cf84ac9aSAndroid Build Coastguard Worker */
30*cf84ac9aSAndroid Build Coastguard Worker
31*cf84ac9aSAndroid Build Coastguard Worker #include "tests.h"
32*cf84ac9aSAndroid Build Coastguard Worker
33*cf84ac9aSAndroid Build Coastguard Worker #include <errno.h>
34*cf84ac9aSAndroid Build Coastguard Worker #include <sys/ptrace.h>
35*cf84ac9aSAndroid Build Coastguard Worker #include <signal.h>
36*cf84ac9aSAndroid Build Coastguard Worker #include <stdlib.h>
37*cf84ac9aSAndroid Build Coastguard Worker #include <unistd.h>
38*cf84ac9aSAndroid Build Coastguard Worker #include <sys/uio.h>
39*cf84ac9aSAndroid Build Coastguard Worker #include <sys/wait.h>
40*cf84ac9aSAndroid Build Coastguard Worker
41*cf84ac9aSAndroid Build Coastguard Worker #include "test_ucopy.h"
42*cf84ac9aSAndroid Build Coastguard Worker
43*cf84ac9aSAndroid Build Coastguard Worker #ifndef HAVE_PROCESS_VM_READV
44*cf84ac9aSAndroid Build Coastguard Worker
45*cf84ac9aSAndroid Build Coastguard Worker # include <asm/unistd.h>
46*cf84ac9aSAndroid Build Coastguard Worker # include "scno.h"
47*cf84ac9aSAndroid Build Coastguard Worker static ssize_t
strace_process_vm_readv(pid_t pid,const struct iovec * lvec,unsigned long liovcnt,const struct iovec * rvec,unsigned long riovcnt,unsigned long flags)48*cf84ac9aSAndroid Build Coastguard Worker strace_process_vm_readv(pid_t pid,
49*cf84ac9aSAndroid Build Coastguard Worker const struct iovec *lvec,
50*cf84ac9aSAndroid Build Coastguard Worker unsigned long liovcnt,
51*cf84ac9aSAndroid Build Coastguard Worker const struct iovec *rvec,
52*cf84ac9aSAndroid Build Coastguard Worker unsigned long riovcnt,
53*cf84ac9aSAndroid Build Coastguard Worker unsigned long flags)
54*cf84ac9aSAndroid Build Coastguard Worker {
55*cf84ac9aSAndroid Build Coastguard Worker return syscall(__NR_process_vm_readv,
56*cf84ac9aSAndroid Build Coastguard Worker (long) pid, lvec, liovcnt, rvec, riovcnt, flags);
57*cf84ac9aSAndroid Build Coastguard Worker }
58*cf84ac9aSAndroid Build Coastguard Worker # define process_vm_readv strace_process_vm_readv
59*cf84ac9aSAndroid Build Coastguard Worker
60*cf84ac9aSAndroid Build Coastguard Worker #endif /* !HAVE_PROCESS_VM_READV */
61*cf84ac9aSAndroid Build Coastguard Worker
62*cf84ac9aSAndroid Build Coastguard Worker static bool
call_process_vm_readv(const int pid,long * const addr)63*cf84ac9aSAndroid Build Coastguard Worker call_process_vm_readv(const int pid, long *const addr)
64*cf84ac9aSAndroid Build Coastguard Worker {
65*cf84ac9aSAndroid Build Coastguard Worker long data = 0;
66*cf84ac9aSAndroid Build Coastguard Worker
67*cf84ac9aSAndroid Build Coastguard Worker const struct iovec local = {
68*cf84ac9aSAndroid Build Coastguard Worker .iov_base = &data,
69*cf84ac9aSAndroid Build Coastguard Worker .iov_len = sizeof(data)
70*cf84ac9aSAndroid Build Coastguard Worker };
71*cf84ac9aSAndroid Build Coastguard Worker const struct iovec remote = {
72*cf84ac9aSAndroid Build Coastguard Worker .iov_base = addr,
73*cf84ac9aSAndroid Build Coastguard Worker .iov_len = sizeof(*addr)
74*cf84ac9aSAndroid Build Coastguard Worker };
75*cf84ac9aSAndroid Build Coastguard Worker
76*cf84ac9aSAndroid Build Coastguard Worker return process_vm_readv(pid, &local, 1, &remote, 1, 0) == sizeof(data)
77*cf84ac9aSAndroid Build Coastguard Worker && data == 1;
78*cf84ac9aSAndroid Build Coastguard Worker }
79*cf84ac9aSAndroid Build Coastguard Worker
80*cf84ac9aSAndroid Build Coastguard Worker static bool
call_ptrace_peekdata(const int pid,long * const addr)81*cf84ac9aSAndroid Build Coastguard Worker call_ptrace_peekdata(const int pid, long *const addr)
82*cf84ac9aSAndroid Build Coastguard Worker {
83*cf84ac9aSAndroid Build Coastguard Worker return ptrace(PTRACE_PEEKDATA, pid, addr, 0) == 1;
84*cf84ac9aSAndroid Build Coastguard Worker }
85*cf84ac9aSAndroid Build Coastguard Worker
86*cf84ac9aSAndroid Build Coastguard Worker static bool
test_ucopy(bool (* fn)(int pid,long * addr))87*cf84ac9aSAndroid Build Coastguard Worker test_ucopy(bool (*fn)(int pid, long *addr))
88*cf84ac9aSAndroid Build Coastguard Worker {
89*cf84ac9aSAndroid Build Coastguard Worker static long data;
90*cf84ac9aSAndroid Build Coastguard Worker
91*cf84ac9aSAndroid Build Coastguard Worker data = 0;
92*cf84ac9aSAndroid Build Coastguard Worker bool rc = false;
93*cf84ac9aSAndroid Build Coastguard Worker int saved = 0;
94*cf84ac9aSAndroid Build Coastguard Worker
95*cf84ac9aSAndroid Build Coastguard Worker pid_t pid = fork();
96*cf84ac9aSAndroid Build Coastguard Worker if (pid < 0)
97*cf84ac9aSAndroid Build Coastguard Worker perror_msg_and_fail("fork");
98*cf84ac9aSAndroid Build Coastguard Worker
99*cf84ac9aSAndroid Build Coastguard Worker if (!pid) {
100*cf84ac9aSAndroid Build Coastguard Worker data = 1;
101*cf84ac9aSAndroid Build Coastguard Worker if (ptrace(PTRACE_TRACEME, 0, 0, 0))
102*cf84ac9aSAndroid Build Coastguard Worker perror_msg_and_fail("PTRACE_TRACEME");
103*cf84ac9aSAndroid Build Coastguard Worker raise(SIGSTOP);
104*cf84ac9aSAndroid Build Coastguard Worker _exit(0);
105*cf84ac9aSAndroid Build Coastguard Worker }
106*cf84ac9aSAndroid Build Coastguard Worker
107*cf84ac9aSAndroid Build Coastguard Worker for (;;) {
108*cf84ac9aSAndroid Build Coastguard Worker int status, tracee;
109*cf84ac9aSAndroid Build Coastguard Worker
110*cf84ac9aSAndroid Build Coastguard Worker errno = 0;
111*cf84ac9aSAndroid Build Coastguard Worker tracee = wait(&status);
112*cf84ac9aSAndroid Build Coastguard Worker if (tracee != pid) {
113*cf84ac9aSAndroid Build Coastguard Worker if (errno == EINTR)
114*cf84ac9aSAndroid Build Coastguard Worker continue;
115*cf84ac9aSAndroid Build Coastguard Worker saved = errno;
116*cf84ac9aSAndroid Build Coastguard Worker kill(pid, SIGKILL);
117*cf84ac9aSAndroid Build Coastguard Worker errno = saved;
118*cf84ac9aSAndroid Build Coastguard Worker perror_msg_and_fail("wait");
119*cf84ac9aSAndroid Build Coastguard Worker }
120*cf84ac9aSAndroid Build Coastguard Worker if (WIFEXITED(status)) {
121*cf84ac9aSAndroid Build Coastguard Worker if (WEXITSTATUS(status) == 0)
122*cf84ac9aSAndroid Build Coastguard Worker break;
123*cf84ac9aSAndroid Build Coastguard Worker error_msg_and_fail("unexpected exit status %u",
124*cf84ac9aSAndroid Build Coastguard Worker WEXITSTATUS(status));
125*cf84ac9aSAndroid Build Coastguard Worker }
126*cf84ac9aSAndroid Build Coastguard Worker if (WIFSIGNALED(status))
127*cf84ac9aSAndroid Build Coastguard Worker error_msg_and_fail("unexpected signal %u",
128*cf84ac9aSAndroid Build Coastguard Worker WTERMSIG(status));
129*cf84ac9aSAndroid Build Coastguard Worker if (!WIFSTOPPED(status) || WSTOPSIG(status) != SIGSTOP) {
130*cf84ac9aSAndroid Build Coastguard Worker kill(pid, SIGKILL);
131*cf84ac9aSAndroid Build Coastguard Worker error_msg_and_fail("unexpected wait status %x",
132*cf84ac9aSAndroid Build Coastguard Worker status);
133*cf84ac9aSAndroid Build Coastguard Worker }
134*cf84ac9aSAndroid Build Coastguard Worker
135*cf84ac9aSAndroid Build Coastguard Worker errno = 0;
136*cf84ac9aSAndroid Build Coastguard Worker rc = fn(pid, &data);
137*cf84ac9aSAndroid Build Coastguard Worker if (!rc)
138*cf84ac9aSAndroid Build Coastguard Worker saved = errno;
139*cf84ac9aSAndroid Build Coastguard Worker
140*cf84ac9aSAndroid Build Coastguard Worker if (ptrace(PTRACE_CONT, pid, 0, 0)) {
141*cf84ac9aSAndroid Build Coastguard Worker saved = errno;
142*cf84ac9aSAndroid Build Coastguard Worker kill(pid, SIGKILL);
143*cf84ac9aSAndroid Build Coastguard Worker errno = saved;
144*cf84ac9aSAndroid Build Coastguard Worker perror_msg_and_fail("PTRACE_CONT");
145*cf84ac9aSAndroid Build Coastguard Worker }
146*cf84ac9aSAndroid Build Coastguard Worker }
147*cf84ac9aSAndroid Build Coastguard Worker
148*cf84ac9aSAndroid Build Coastguard Worker if (!rc)
149*cf84ac9aSAndroid Build Coastguard Worker errno = saved;
150*cf84ac9aSAndroid Build Coastguard Worker return rc;
151*cf84ac9aSAndroid Build Coastguard Worker }
152*cf84ac9aSAndroid Build Coastguard Worker
153*cf84ac9aSAndroid Build Coastguard Worker bool
test_process_vm_readv(void)154*cf84ac9aSAndroid Build Coastguard Worker test_process_vm_readv(void)
155*cf84ac9aSAndroid Build Coastguard Worker {
156*cf84ac9aSAndroid Build Coastguard Worker return test_ucopy(call_process_vm_readv);
157*cf84ac9aSAndroid Build Coastguard Worker }
158*cf84ac9aSAndroid Build Coastguard Worker
159*cf84ac9aSAndroid Build Coastguard Worker bool
test_ptrace_peekdata(void)160*cf84ac9aSAndroid Build Coastguard Worker test_ptrace_peekdata(void)
161*cf84ac9aSAndroid Build Coastguard Worker {
162*cf84ac9aSAndroid Build Coastguard Worker return test_ucopy(call_ptrace_peekdata);
163*cf84ac9aSAndroid Build Coastguard Worker }
164