blob: bd84d307e8934b23e12463fe2f7deebb33fe84d4 [file] [log] [blame]
Pavel Labath1faca6c2016-04-21 15:13:22 +01001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <sys/ptrace.h>
18
19#include <elf.h>
20#include <sched.h>
21#include <sys/prctl.h>
22#include <sys/uio.h>
23#include <sys/user.h>
24#include <unistd.h>
25
26#include <gtest/gtest.h>
27
28// Host libc does not define this.
29#ifndef TRAP_HWBKPT
30#define TRAP_HWBKPT 4
31#endif
32
33template<typename T>
34static void __attribute__((noreturn)) fork_child(unsigned cpu, T &data) {
35 // Extra precaution: make sure we go away if anything happens to our parent.
36 if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0) == -1) {
37 perror("prctl(PR_SET_PDEATHSIG)");
38 _exit(1);
39 }
40
41 cpu_set_t cpus;
42 CPU_ZERO(&cpus);
43 CPU_SET(cpu, &cpus);
44 if (sched_setaffinity(0, sizeof cpus, &cpus) == -1) {
45 perror("sched_setaffinity");
46 _exit(2);
47 }
48 if (ptrace(PTRACE_TRACEME, 0, nullptr, nullptr) == -1) {
49 perror("ptrace(PTRACE_TRACEME)");
50 _exit(3);
51 }
52
53 raise(SIGSTOP); // Synchronize with the tracer, let it set the watchpoint.
54
55 data = 1; // Now trigger the watchpoint.
56
57 _exit(0);
58}
59
60class ChildGuard {
61 public:
62 ChildGuard(pid_t pid) : pid(pid) {}
63
64 ~ChildGuard() {
65 kill(pid, SIGKILL);
66 int status;
67 waitpid(pid, &status, 0);
68 }
69
70 private:
71 pid_t pid;
72};
73
74static bool are_watchpoints_supported(pid_t child) {
75#if defined(__arm__)
76 long capabilities;
77 long result = ptrace(PTRACE_GETHBPREGS, child, 0, &capabilities);
78 if (result == -1) {
79 EXPECT_EQ(EIO, errno);
80 return false;
81 }
82 return ((capabilities >> 8) & 0xff) > 0;
83#elif defined(__aarch64__)
84 user_hwdebug_state dreg_state;
85 iovec iov;
86 iov.iov_base = &dreg_state;
87 iov.iov_len = sizeof(dreg_state);
88
89 long result = ptrace(PTRACE_GETREGSET, child, NT_ARM_HW_WATCH, &iov);
90 if (result == -1) {
91 EXPECT_EQ(EINVAL, errno);
92 return false;
93 }
94 return (dreg_state.dbg_info & 0xff) > 0;
95#elif defined(__i386__) || defined(__x86_64__)
96 // We assume watchpoints are always supported on x86.
97 (void) child;
98 return true;
99#else
100 // TODO: mips support.
101 (void) child;
102 return false;
103#endif
104}
105
106static void set_watchpoint(pid_t child, const void *address, size_t size) {
107#if defined(__arm__) || defined(__aarch64__)
108 const unsigned byte_mask = (1 << size) - 1;
109 const unsigned type = 2; // Write.
110 const unsigned enable = 1;
111 const unsigned control = byte_mask << 5 | type << 3 | enable;
112
113#ifdef __arm__
114 ASSERT_EQ(0, ptrace(PTRACE_SETHBPREGS, child, -1, &address)) << strerror(errno);
115 ASSERT_EQ(0, ptrace(PTRACE_SETHBPREGS, child, -2, &control)) << strerror(errno);
116#else // aarch64
117 user_hwdebug_state dreg_state;
118 memset(&dreg_state, 0, sizeof dreg_state);
119 dreg_state.dbg_regs[0].addr = reinterpret_cast<uintptr_t>(address);
120 dreg_state.dbg_regs[0].ctrl = control;
121
122 iovec iov;
123 iov.iov_base = &dreg_state;
124 iov.iov_len = offsetof(user_hwdebug_state, dbg_regs) + sizeof(dreg_state.dbg_regs[0]);
125
126 ASSERT_EQ(0, ptrace(PTRACE_SETREGSET, child, NT_ARM_HW_WATCH, &iov)) << strerror(errno);
127#endif
128#elif defined(__i386__) || defined(__x86_64__)
129 ASSERT_EQ(0, ptrace(PTRACE_POKEUSER, child, offsetof(user, u_debugreg[0]), address)) << strerror(errno);
130 errno = 0;
131 unsigned data = ptrace(PTRACE_PEEKUSER, child, offsetof(user, u_debugreg[7]), nullptr);
132 ASSERT_EQ(0, errno);
133
134 const unsigned size_flag = (size == 8) ? 2 : size - 1;
135 const unsigned enable = 1;
136 const unsigned type = 1; // Write.
137
138 const unsigned mask = 3 << 18 | 3 << 16 | 1;
139 const unsigned value = size_flag << 18 | type << 16 | enable;
140 data &= mask;
141 data |= value;
142 ASSERT_EQ(0, ptrace(PTRACE_POKEUSER, child, offsetof(user, u_debugreg[7]), data)) << strerror(errno);
143#else
144 (void) child;
145 (void) address;
146 (void) size;
147#endif
148}
149
150template<typename T>
151static void run_watchpoint_test_impl(unsigned cpu) {
152 alignas(8) T data = 0;
153
154 pid_t child = fork();
155 ASSERT_NE(-1, child) << strerror(errno);
156 if (child == 0) fork_child(cpu, data);
157
158 ChildGuard guard(child);
159
160 int status;
161 ASSERT_EQ(child, waitpid(child, &status, __WALL)) << strerror(errno);
162 ASSERT_TRUE(WIFSTOPPED(status)) << "Status was: " << status;
163 ASSERT_EQ(SIGSTOP, WSTOPSIG(status)) << "Status was: " << status;
164
165 if (!are_watchpoints_supported(child)) return;
166
167 set_watchpoint(child, &data, sizeof data);
168
169 ASSERT_EQ(0, ptrace(PTRACE_CONT, child, nullptr, nullptr)) << strerror(errno);
170 ASSERT_EQ(child, waitpid(child, &status, __WALL)) << strerror(errno);
171 ASSERT_TRUE(WIFSTOPPED(status)) << "Status was: " << status;
172 ASSERT_EQ(SIGTRAP, WSTOPSIG(status)) << "Status was: " << status;
173
174 siginfo_t siginfo;
175 ASSERT_EQ(0, ptrace(PTRACE_GETSIGINFO, child, nullptr, &siginfo)) << strerror(errno);
176 ASSERT_EQ(TRAP_HWBKPT, siginfo.si_code);
177#if defined(__arm__) || defined(__aarch64__)
178 ASSERT_EQ(&data, siginfo.si_addr);
179#endif
180}
181
182static void run_watchpoint_test(unsigned cpu) {
183 run_watchpoint_test_impl<uint8_t>(cpu);
184 run_watchpoint_test_impl<uint16_t>(cpu);
185 run_watchpoint_test_impl<uint32_t>(cpu);
186#if __LP64__
187 run_watchpoint_test_impl<uint64_t>(cpu);
188#endif
189}
190
191// Test watchpoint API. The test is considered successful if our watchpoints get hit OR the
192// system reports that watchpoint support is not present. We run the test for different
193// watchpoint sizes, while pinning the process to each cpu in turn, for better coverage.
194TEST(ptrace, watchpoint_stress) {
195 cpu_set_t available_cpus;
196 ASSERT_EQ(0, sched_getaffinity(0, sizeof available_cpus, &available_cpus));
197
198 for (size_t cpu = 0; cpu < CPU_SETSIZE; ++cpu) {
199 if (!CPU_ISSET(cpu, &available_cpus)) continue;
200 run_watchpoint_test(cpu);
201 }
202}