| Pavel Labath | 1faca6c | 2016-04-21 15:13:22 +0100 | [diff] [blame] | 1 | /* | 
|  | 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> | 
| Josh Gao | bc055ca | 2017-03-29 15:01:15 -0700 | [diff] [blame] | 20 | #include <err.h> | 
| Josh Gao | 5e3fe95 | 2017-02-16 14:12:41 -0800 | [diff] [blame] | 21 | #include <fcntl.h> | 
| Pavel Labath | 1faca6c | 2016-04-21 15:13:22 +0100 | [diff] [blame] | 22 | #include <sched.h> | 
|  | 23 | #include <sys/prctl.h> | 
| Josh Gao | 5e3fe95 | 2017-02-16 14:12:41 -0800 | [diff] [blame] | 24 | #include <sys/ptrace.h> | 
| Pavel Labath | 1faca6c | 2016-04-21 15:13:22 +0100 | [diff] [blame] | 25 | #include <sys/uio.h> | 
|  | 26 | #include <sys/user.h> | 
| Josh Gao | 5e3fe95 | 2017-02-16 14:12:41 -0800 | [diff] [blame] | 27 | #include <sys/wait.h> | 
| Pavel Labath | 1faca6c | 2016-04-21 15:13:22 +0100 | [diff] [blame] | 28 | #include <unistd.h> | 
|  | 29 |  | 
| Josh Gao | bc055ca | 2017-03-29 15:01:15 -0700 | [diff] [blame] | 30 | #include <chrono> | 
|  | 31 | #include <thread> | 
|  | 32 |  | 
| Pavel Labath | 1faca6c | 2016-04-21 15:13:22 +0100 | [diff] [blame] | 33 | #include <gtest/gtest.h> | 
|  | 34 |  | 
| Pavel Labath | 3dad8d5 | 2017-02-22 18:22:46 +0000 | [diff] [blame] | 35 | #include <android-base/macros.h> | 
| Josh Gao | 5e3fe95 | 2017-02-16 14:12:41 -0800 | [diff] [blame] | 36 | #include <android-base/unique_fd.h> | 
|  | 37 |  | 
| Evgenii Stepanov | 7cc6706 | 2019-02-05 18:43:34 -0800 | [diff] [blame] | 38 | #include "utils.h" | 
|  | 39 |  | 
| Josh Gao | bc055ca | 2017-03-29 15:01:15 -0700 | [diff] [blame] | 40 | using namespace std::chrono_literals; | 
|  | 41 |  | 
| Josh Gao | 5e3fe95 | 2017-02-16 14:12:41 -0800 | [diff] [blame] | 42 | using android::base::unique_fd; | 
|  | 43 |  | 
| Pavel Labath | 1faca6c | 2016-04-21 15:13:22 +0100 | [diff] [blame] | 44 | // Host libc does not define this. | 
|  | 45 | #ifndef TRAP_HWBKPT | 
|  | 46 | #define TRAP_HWBKPT 4 | 
|  | 47 | #endif | 
|  | 48 |  | 
| Pavel Labath | 1faca6c | 2016-04-21 15:13:22 +0100 | [diff] [blame] | 49 | class ChildGuard { | 
|  | 50 | public: | 
| Chih-Hung Hsieh | 62e3a07 | 2016-05-03 12:08:05 -0700 | [diff] [blame] | 51 | explicit ChildGuard(pid_t pid) : pid(pid) {} | 
| Pavel Labath | 1faca6c | 2016-04-21 15:13:22 +0100 | [diff] [blame] | 52 |  | 
|  | 53 | ~ChildGuard() { | 
|  | 54 | kill(pid, SIGKILL); | 
|  | 55 | int status; | 
| Elliott Hughes | cabc77f | 2017-11-28 12:55:19 -0800 | [diff] [blame] | 56 | TEMP_FAILURE_RETRY(waitpid(pid, &status, 0)); | 
| Pavel Labath | 1faca6c | 2016-04-21 15:13:22 +0100 | [diff] [blame] | 57 | } | 
|  | 58 |  | 
|  | 59 | private: | 
|  | 60 | pid_t pid; | 
|  | 61 | }; | 
|  | 62 |  | 
| Pavel Labath | fb082ee | 2017-01-23 15:41:35 +0000 | [diff] [blame] | 63 | enum class HwFeature { Watchpoint, Breakpoint }; | 
|  | 64 |  | 
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 65 | static void check_hw_feature_supported(pid_t child, HwFeature feature) { | 
| Pavel Labath | 1faca6c | 2016-04-21 15:13:22 +0100 | [diff] [blame] | 66 | #if defined(__arm__) | 
|  | 67 | long capabilities; | 
|  | 68 | long result = ptrace(PTRACE_GETHBPREGS, child, 0, &capabilities); | 
|  | 69 | if (result == -1) { | 
|  | 70 | EXPECT_EQ(EIO, errno); | 
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 71 | GTEST_SKIP() << "Hardware debug support disabled at kernel configuration time"; | 
| Pavel Labath | 1faca6c | 2016-04-21 15:13:22 +0100 | [diff] [blame] | 72 | } | 
| Pavel Labath | 95d8fb1 | 2017-07-07 11:42:34 +0100 | [diff] [blame] | 73 | uint8_t hb_count = capabilities & 0xff; | 
|  | 74 | capabilities >>= 8; | 
|  | 75 | uint8_t wp_count = capabilities & 0xff; | 
|  | 76 | capabilities >>= 8; | 
|  | 77 | uint8_t max_wp_size = capabilities & 0xff; | 
|  | 78 | if (max_wp_size == 0) { | 
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 79 | GTEST_SKIP() << "Kernel reports zero maximum watchpoint size"; | 
|  | 80 | } else if (feature == HwFeature::Watchpoint && wp_count == 0) { | 
|  | 81 | GTEST_SKIP() << "Kernel reports zero hardware watchpoints"; | 
|  | 82 | } else if (feature == HwFeature::Breakpoint && hb_count == 0) { | 
|  | 83 | GTEST_SKIP() << "Kernel reports zero hardware breakpoints"; | 
| Pavel Labath | fb082ee | 2017-01-23 15:41:35 +0000 | [diff] [blame] | 84 | } | 
| Pavel Labath | 1faca6c | 2016-04-21 15:13:22 +0100 | [diff] [blame] | 85 | #elif defined(__aarch64__) | 
|  | 86 | user_hwdebug_state dreg_state; | 
|  | 87 | iovec iov; | 
|  | 88 | iov.iov_base = &dreg_state; | 
|  | 89 | iov.iov_len = sizeof(dreg_state); | 
|  | 90 |  | 
| Pavel Labath | fb082ee | 2017-01-23 15:41:35 +0000 | [diff] [blame] | 91 | long result = ptrace(PTRACE_GETREGSET, child, | 
|  | 92 | feature == HwFeature::Watchpoint ? NT_ARM_HW_WATCH : NT_ARM_HW_BREAK, &iov); | 
| Pavel Labath | 1faca6c | 2016-04-21 15:13:22 +0100 | [diff] [blame] | 93 | if (result == -1) { | 
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 94 | ASSERT_EQ(EINVAL, errno); | 
| Evgeny Eltsin | bd1c630 | 2019-12-11 15:30:16 +0100 | [diff] [blame] | 95 | GTEST_SKIP() << "Hardware support missing"; | 
|  | 96 | } else if ((dreg_state.dbg_info & 0xff) == 0) { | 
|  | 97 | if (feature == HwFeature::Watchpoint) { | 
|  | 98 | GTEST_SKIP() << "Kernel reports zero hardware watchpoints"; | 
|  | 99 | } else { | 
|  | 100 | GTEST_SKIP() << "Kernel reports zero hardware breakpoints"; | 
|  | 101 | } | 
| Pavel Labath | 1faca6c | 2016-04-21 15:13:22 +0100 | [diff] [blame] | 102 | } | 
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 103 | #else | 
| Pavel Labath | fb082ee | 2017-01-23 15:41:35 +0000 | [diff] [blame] | 104 | // We assume watchpoints and breakpoints are always supported on x86. | 
| Pavel Labath | 3dad8d5 | 2017-02-22 18:22:46 +0000 | [diff] [blame] | 105 | UNUSED(child); | 
|  | 106 | UNUSED(feature); | 
| Pavel Labath | 1faca6c | 2016-04-21 15:13:22 +0100 | [diff] [blame] | 107 | #endif | 
|  | 108 | } | 
|  | 109 |  | 
| Pavel Labath | 3dad8d5 | 2017-02-22 18:22:46 +0000 | [diff] [blame] | 110 | static void set_watchpoint(pid_t child, uintptr_t address, size_t size) { | 
|  | 111 | ASSERT_EQ(0u, address & 0x7) << "address: " << address; | 
| Pavel Labath | 1faca6c | 2016-04-21 15:13:22 +0100 | [diff] [blame] | 112 | #if defined(__arm__) || defined(__aarch64__) | 
|  | 113 | const unsigned byte_mask = (1 << size) - 1; | 
|  | 114 | const unsigned type = 2; // Write. | 
|  | 115 | const unsigned enable = 1; | 
|  | 116 | const unsigned control = byte_mask << 5 | type << 3 | enable; | 
|  | 117 |  | 
|  | 118 | #ifdef __arm__ | 
|  | 119 | ASSERT_EQ(0, ptrace(PTRACE_SETHBPREGS, child, -1, &address)) << strerror(errno); | 
|  | 120 | ASSERT_EQ(0, ptrace(PTRACE_SETHBPREGS, child, -2, &control)) << strerror(errno); | 
|  | 121 | #else // aarch64 | 
|  | 122 | user_hwdebug_state dreg_state; | 
|  | 123 | memset(&dreg_state, 0, sizeof dreg_state); | 
| Pavel Labath | 3dad8d5 | 2017-02-22 18:22:46 +0000 | [diff] [blame] | 124 | dreg_state.dbg_regs[0].addr = address; | 
| Pavel Labath | 1faca6c | 2016-04-21 15:13:22 +0100 | [diff] [blame] | 125 | dreg_state.dbg_regs[0].ctrl = control; | 
|  | 126 |  | 
|  | 127 | iovec iov; | 
|  | 128 | iov.iov_base = &dreg_state; | 
|  | 129 | iov.iov_len = offsetof(user_hwdebug_state, dbg_regs) + sizeof(dreg_state.dbg_regs[0]); | 
|  | 130 |  | 
|  | 131 | ASSERT_EQ(0, ptrace(PTRACE_SETREGSET, child, NT_ARM_HW_WATCH, &iov)) << strerror(errno); | 
|  | 132 | #endif | 
|  | 133 | #elif defined(__i386__) || defined(__x86_64__) | 
|  | 134 | ASSERT_EQ(0, ptrace(PTRACE_POKEUSER, child, offsetof(user, u_debugreg[0]), address)) << strerror(errno); | 
|  | 135 | errno = 0; | 
|  | 136 | unsigned data = ptrace(PTRACE_PEEKUSER, child, offsetof(user, u_debugreg[7]), nullptr); | 
|  | 137 | ASSERT_EQ(0, errno); | 
|  | 138 |  | 
|  | 139 | const unsigned size_flag = (size == 8) ? 2 : size - 1; | 
|  | 140 | const unsigned enable = 1; | 
|  | 141 | const unsigned type = 1; // Write. | 
|  | 142 |  | 
|  | 143 | const unsigned mask = 3 << 18 | 3 << 16 | 1; | 
|  | 144 | const unsigned value = size_flag << 18 | type << 16 | enable; | 
|  | 145 | data &= mask; | 
|  | 146 | data |= value; | 
|  | 147 | ASSERT_EQ(0, ptrace(PTRACE_POKEUSER, child, offsetof(user, u_debugreg[7]), data)) << strerror(errno); | 
|  | 148 | #else | 
| Pavel Labath | 3dad8d5 | 2017-02-22 18:22:46 +0000 | [diff] [blame] | 149 | UNUSED(child); | 
|  | 150 | UNUSED(address); | 
|  | 151 | UNUSED(size); | 
| Pavel Labath | 1faca6c | 2016-04-21 15:13:22 +0100 | [diff] [blame] | 152 | #endif | 
|  | 153 | } | 
|  | 154 |  | 
| Pavel Labath | 3dad8d5 | 2017-02-22 18:22:46 +0000 | [diff] [blame] | 155 | template <typename T> | 
|  | 156 | static void run_watchpoint_test(std::function<void(T&)> child_func, size_t offset, size_t size) { | 
|  | 157 | alignas(16) T data{}; | 
| Pavel Labath | 1faca6c | 2016-04-21 15:13:22 +0100 | [diff] [blame] | 158 |  | 
|  | 159 | pid_t child = fork(); | 
|  | 160 | ASSERT_NE(-1, child) << strerror(errno); | 
| Pavel Labath | 3dad8d5 | 2017-02-22 18:22:46 +0000 | [diff] [blame] | 161 | if (child == 0) { | 
|  | 162 | // Extra precaution: make sure we go away if anything happens to our parent. | 
|  | 163 | if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0) == -1) { | 
|  | 164 | perror("prctl(PR_SET_PDEATHSIG)"); | 
|  | 165 | _exit(1); | 
|  | 166 | } | 
|  | 167 |  | 
|  | 168 | if (ptrace(PTRACE_TRACEME, 0, nullptr, nullptr) == -1) { | 
|  | 169 | perror("ptrace(PTRACE_TRACEME)"); | 
|  | 170 | _exit(2); | 
|  | 171 | } | 
|  | 172 |  | 
|  | 173 | child_func(data); | 
|  | 174 | _exit(0); | 
|  | 175 | } | 
| Pavel Labath | 1faca6c | 2016-04-21 15:13:22 +0100 | [diff] [blame] | 176 |  | 
|  | 177 | ChildGuard guard(child); | 
|  | 178 |  | 
|  | 179 | int status; | 
| Elliott Hughes | cabc77f | 2017-11-28 12:55:19 -0800 | [diff] [blame] | 180 | ASSERT_EQ(child, TEMP_FAILURE_RETRY(waitpid(child, &status, __WALL))) << strerror(errno); | 
| Pavel Labath | 1faca6c | 2016-04-21 15:13:22 +0100 | [diff] [blame] | 181 | ASSERT_TRUE(WIFSTOPPED(status)) << "Status was: " << status; | 
|  | 182 | ASSERT_EQ(SIGSTOP, WSTOPSIG(status)) << "Status was: " << status; | 
|  | 183 |  | 
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 184 | check_hw_feature_supported(child, HwFeature::Watchpoint); | 
| Christopher Ferris | 103b998 | 2019-09-23 09:03:10 -0700 | [diff] [blame] | 185 | if (::testing::Test::IsSkipped()) { | 
|  | 186 | return; | 
|  | 187 | } | 
| Pavel Labath | 1faca6c | 2016-04-21 15:13:22 +0100 | [diff] [blame] | 188 |  | 
| Evgenii Stepanov | 7cc6706 | 2019-02-05 18:43:34 -0800 | [diff] [blame] | 189 | set_watchpoint(child, uintptr_t(untag_address(&data)) + offset, size); | 
| Pavel Labath | 1faca6c | 2016-04-21 15:13:22 +0100 | [diff] [blame] | 190 |  | 
|  | 191 | ASSERT_EQ(0, ptrace(PTRACE_CONT, child, nullptr, nullptr)) << strerror(errno); | 
| Elliott Hughes | cabc77f | 2017-11-28 12:55:19 -0800 | [diff] [blame] | 192 | ASSERT_EQ(child, TEMP_FAILURE_RETRY(waitpid(child, &status, __WALL))) << strerror(errno); | 
| Pavel Labath | 1faca6c | 2016-04-21 15:13:22 +0100 | [diff] [blame] | 193 | ASSERT_TRUE(WIFSTOPPED(status)) << "Status was: " << status; | 
|  | 194 | ASSERT_EQ(SIGTRAP, WSTOPSIG(status)) << "Status was: " << status; | 
|  | 195 |  | 
|  | 196 | siginfo_t siginfo; | 
|  | 197 | ASSERT_EQ(0, ptrace(PTRACE_GETSIGINFO, child, nullptr, &siginfo)) << strerror(errno); | 
|  | 198 | ASSERT_EQ(TRAP_HWBKPT, siginfo.si_code); | 
|  | 199 | #if defined(__arm__) || defined(__aarch64__) | 
| Pavel Labath | 3dad8d5 | 2017-02-22 18:22:46 +0000 | [diff] [blame] | 200 | ASSERT_LE(&data, siginfo.si_addr); | 
|  | 201 | ASSERT_GT((&data) + 1, siginfo.si_addr); | 
| Pavel Labath | 1faca6c | 2016-04-21 15:13:22 +0100 | [diff] [blame] | 202 | #endif | 
|  | 203 | } | 
|  | 204 |  | 
| Pavel Labath | 3dad8d5 | 2017-02-22 18:22:46 +0000 | [diff] [blame] | 205 | template <typename T> | 
|  | 206 | static void watchpoint_stress_child(unsigned cpu, T& data) { | 
|  | 207 | cpu_set_t cpus; | 
|  | 208 | CPU_ZERO(&cpus); | 
|  | 209 | CPU_SET(cpu, &cpus); | 
|  | 210 | if (sched_setaffinity(0, sizeof cpus, &cpus) == -1) { | 
|  | 211 | perror("sched_setaffinity"); | 
|  | 212 | _exit(3); | 
|  | 213 | } | 
|  | 214 | raise(SIGSTOP);  // Synchronize with the tracer, let it set the watchpoint. | 
|  | 215 |  | 
|  | 216 | data = 1;  // Now trigger the watchpoint. | 
|  | 217 | } | 
|  | 218 |  | 
|  | 219 | template <typename T> | 
|  | 220 | static void run_watchpoint_stress(size_t cpu) { | 
|  | 221 | run_watchpoint_test<T>(std::bind(watchpoint_stress_child<T>, cpu, std::placeholders::_1), 0, | 
|  | 222 | sizeof(T)); | 
| Pavel Labath | 1faca6c | 2016-04-21 15:13:22 +0100 | [diff] [blame] | 223 | } | 
|  | 224 |  | 
|  | 225 | // Test watchpoint API. The test is considered successful if our watchpoints get hit OR the | 
|  | 226 | // system reports that watchpoint support is not present. We run the test for different | 
|  | 227 | // watchpoint sizes, while pinning the process to each cpu in turn, for better coverage. | 
| Pavel Labath | fb082ee | 2017-01-23 15:41:35 +0000 | [diff] [blame] | 228 | TEST(sys_ptrace, watchpoint_stress) { | 
| Pavel Labath | 1faca6c | 2016-04-21 15:13:22 +0100 | [diff] [blame] | 229 | cpu_set_t available_cpus; | 
|  | 230 | ASSERT_EQ(0, sched_getaffinity(0, sizeof available_cpus, &available_cpus)); | 
|  | 231 |  | 
|  | 232 | for (size_t cpu = 0; cpu < CPU_SETSIZE; ++cpu) { | 
|  | 233 | if (!CPU_ISSET(cpu, &available_cpus)) continue; | 
| Pavel Labath | 3dad8d5 | 2017-02-22 18:22:46 +0000 | [diff] [blame] | 234 |  | 
|  | 235 | run_watchpoint_stress<uint8_t>(cpu); | 
| Christopher Ferris | 103b998 | 2019-09-23 09:03:10 -0700 | [diff] [blame] | 236 | if (::testing::Test::IsSkipped()) { | 
|  | 237 | // Only check first case, since all others would skip for same reason. | 
|  | 238 | return; | 
|  | 239 | } | 
| Pavel Labath | 3dad8d5 | 2017-02-22 18:22:46 +0000 | [diff] [blame] | 240 | run_watchpoint_stress<uint16_t>(cpu); | 
|  | 241 | run_watchpoint_stress<uint32_t>(cpu); | 
|  | 242 | #if defined(__LP64__) | 
|  | 243 | run_watchpoint_stress<uint64_t>(cpu); | 
|  | 244 | #endif | 
| Pavel Labath | 1faca6c | 2016-04-21 15:13:22 +0100 | [diff] [blame] | 245 | } | 
|  | 246 | } | 
| Pavel Labath | fb082ee | 2017-01-23 15:41:35 +0000 | [diff] [blame] | 247 |  | 
| Pavel Labath | 3dad8d5 | 2017-02-22 18:22:46 +0000 | [diff] [blame] | 248 | struct Uint128_t { | 
|  | 249 | uint64_t data[2]; | 
|  | 250 | }; | 
|  | 251 | static void watchpoint_imprecise_child(Uint128_t& data) { | 
|  | 252 | raise(SIGSTOP);  // Synchronize with the tracer, let it set the watchpoint. | 
|  | 253 |  | 
|  | 254 | #if defined(__i386__) || defined(__x86_64__) | 
|  | 255 | asm volatile("movdqa %%xmm0, %0" : : "m"(data)); | 
|  | 256 | #elif defined(__arm__) | 
|  | 257 | asm volatile("stm %0, { r0, r1, r2, r3 }" : : "r"(&data)); | 
|  | 258 | #elif defined(__aarch64__) | 
|  | 259 | asm volatile("stp x0, x1, %0" : : "m"(data)); | 
| Pavel Labath | 3dad8d5 | 2017-02-22 18:22:46 +0000 | [diff] [blame] | 260 | #endif | 
|  | 261 | } | 
|  | 262 |  | 
|  | 263 | // Test that the kernel is able to handle the case when the instruction writes | 
|  | 264 | // to a larger block of memory than the one we are watching. If you see this | 
|  | 265 | // test fail on arm64, you will likely need to cherry-pick fdfeff0f into your | 
|  | 266 | // kernel. | 
|  | 267 | TEST(sys_ptrace, watchpoint_imprecise) { | 
| Yabin Cui | 143b454 | 2017-11-29 10:34:24 -0800 | [diff] [blame] | 268 | // This test relies on the infrastructure to timeout if the test hangs. | 
| Pavel Labath | 4a62026 | 2017-04-26 11:30:06 +0100 | [diff] [blame] | 269 | run_watchpoint_test<Uint128_t>(watchpoint_imprecise_child, 8, sizeof(void*)); | 
| Pavel Labath | 3dad8d5 | 2017-02-22 18:22:46 +0000 | [diff] [blame] | 270 | } | 
|  | 271 |  | 
| Pavel Labath | fb082ee | 2017-01-23 15:41:35 +0000 | [diff] [blame] | 272 | static void __attribute__((noinline)) breakpoint_func() { | 
|  | 273 | asm volatile(""); | 
|  | 274 | } | 
|  | 275 |  | 
|  | 276 | static void __attribute__((noreturn)) breakpoint_fork_child() { | 
|  | 277 | // Extra precaution: make sure we go away if anything happens to our parent. | 
|  | 278 | if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0) == -1) { | 
|  | 279 | perror("prctl(PR_SET_PDEATHSIG)"); | 
|  | 280 | _exit(1); | 
|  | 281 | } | 
|  | 282 |  | 
|  | 283 | if (ptrace(PTRACE_TRACEME, 0, nullptr, nullptr) == -1) { | 
|  | 284 | perror("ptrace(PTRACE_TRACEME)"); | 
|  | 285 | _exit(2); | 
|  | 286 | } | 
|  | 287 |  | 
|  | 288 | raise(SIGSTOP);  // Synchronize with the tracer, let it set the breakpoint. | 
|  | 289 |  | 
|  | 290 | breakpoint_func();  // Now trigger the breakpoint. | 
|  | 291 |  | 
|  | 292 | _exit(0); | 
|  | 293 | } | 
|  | 294 |  | 
|  | 295 | static void set_breakpoint(pid_t child) { | 
|  | 296 | uintptr_t address = uintptr_t(breakpoint_func); | 
|  | 297 | #if defined(__arm__) || defined(__aarch64__) | 
|  | 298 | address &= ~3; | 
|  | 299 | const unsigned byte_mask = 0xf; | 
|  | 300 | const unsigned enable = 1; | 
|  | 301 | const unsigned control = byte_mask << 5 | enable; | 
|  | 302 |  | 
|  | 303 | #ifdef __arm__ | 
|  | 304 | ASSERT_EQ(0, ptrace(PTRACE_SETHBPREGS, child, 1, &address)) << strerror(errno); | 
|  | 305 | ASSERT_EQ(0, ptrace(PTRACE_SETHBPREGS, child, 2, &control)) << strerror(errno); | 
|  | 306 | #else  // aarch64 | 
|  | 307 | user_hwdebug_state dreg_state; | 
|  | 308 | memset(&dreg_state, 0, sizeof dreg_state); | 
|  | 309 | dreg_state.dbg_regs[0].addr = reinterpret_cast<uintptr_t>(address); | 
|  | 310 | dreg_state.dbg_regs[0].ctrl = control; | 
|  | 311 |  | 
|  | 312 | iovec iov; | 
|  | 313 | iov.iov_base = &dreg_state; | 
|  | 314 | iov.iov_len = offsetof(user_hwdebug_state, dbg_regs) + sizeof(dreg_state.dbg_regs[0]); | 
|  | 315 |  | 
|  | 316 | ASSERT_EQ(0, ptrace(PTRACE_SETREGSET, child, NT_ARM_HW_BREAK, &iov)) << strerror(errno); | 
|  | 317 | #endif | 
|  | 318 | #elif defined(__i386__) || defined(__x86_64__) | 
|  | 319 | ASSERT_EQ(0, ptrace(PTRACE_POKEUSER, child, offsetof(user, u_debugreg[0]), address)) | 
|  | 320 | << strerror(errno); | 
|  | 321 | errno = 0; | 
|  | 322 | unsigned data = ptrace(PTRACE_PEEKUSER, child, offsetof(user, u_debugreg[7]), nullptr); | 
|  | 323 | ASSERT_EQ(0, errno); | 
|  | 324 |  | 
|  | 325 | const unsigned size = 0; | 
|  | 326 | const unsigned enable = 1; | 
|  | 327 | const unsigned type = 0;  // Execute | 
|  | 328 |  | 
|  | 329 | const unsigned mask = 3 << 18 | 3 << 16 | 1; | 
|  | 330 | const unsigned value = size << 18 | type << 16 | enable; | 
|  | 331 | data &= mask; | 
|  | 332 | data |= value; | 
|  | 333 | ASSERT_EQ(0, ptrace(PTRACE_POKEUSER, child, offsetof(user, u_debugreg[7]), data)) | 
|  | 334 | << strerror(errno); | 
|  | 335 | #else | 
| Pavel Labath | 3dad8d5 | 2017-02-22 18:22:46 +0000 | [diff] [blame] | 336 | UNUSED(child); | 
|  | 337 | UNUSED(address); | 
| Pavel Labath | fb082ee | 2017-01-23 15:41:35 +0000 | [diff] [blame] | 338 | #endif | 
|  | 339 | } | 
|  | 340 |  | 
|  | 341 | // Test hardware breakpoint API. The test is considered successful if the breakpoints get hit OR the | 
|  | 342 | // system reports that hardware breakpoint support is not present. | 
|  | 343 | TEST(sys_ptrace, hardware_breakpoint) { | 
|  | 344 | pid_t child = fork(); | 
|  | 345 | ASSERT_NE(-1, child) << strerror(errno); | 
|  | 346 | if (child == 0) breakpoint_fork_child(); | 
|  | 347 |  | 
|  | 348 | ChildGuard guard(child); | 
|  | 349 |  | 
|  | 350 | int status; | 
| Elliott Hughes | cabc77f | 2017-11-28 12:55:19 -0800 | [diff] [blame] | 351 | ASSERT_EQ(child, TEMP_FAILURE_RETRY(waitpid(child, &status, __WALL))) << strerror(errno); | 
| Pavel Labath | fb082ee | 2017-01-23 15:41:35 +0000 | [diff] [blame] | 352 | ASSERT_TRUE(WIFSTOPPED(status)) << "Status was: " << status; | 
|  | 353 | ASSERT_EQ(SIGSTOP, WSTOPSIG(status)) << "Status was: " << status; | 
|  | 354 |  | 
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 355 | check_hw_feature_supported(child, HwFeature::Breakpoint); | 
| Christopher Ferris | 103b998 | 2019-09-23 09:03:10 -0700 | [diff] [blame] | 356 | if (::testing::Test::IsSkipped()) { | 
|  | 357 | return; | 
|  | 358 | } | 
| Pavel Labath | fb082ee | 2017-01-23 15:41:35 +0000 | [diff] [blame] | 359 |  | 
|  | 360 | set_breakpoint(child); | 
|  | 361 |  | 
|  | 362 | ASSERT_EQ(0, ptrace(PTRACE_CONT, child, nullptr, nullptr)) << strerror(errno); | 
| Elliott Hughes | cabc77f | 2017-11-28 12:55:19 -0800 | [diff] [blame] | 363 | ASSERT_EQ(child, TEMP_FAILURE_RETRY(waitpid(child, &status, __WALL))) << strerror(errno); | 
| Pavel Labath | fb082ee | 2017-01-23 15:41:35 +0000 | [diff] [blame] | 364 | ASSERT_TRUE(WIFSTOPPED(status)) << "Status was: " << status; | 
|  | 365 | ASSERT_EQ(SIGTRAP, WSTOPSIG(status)) << "Status was: " << status; | 
|  | 366 |  | 
|  | 367 | siginfo_t siginfo; | 
|  | 368 | ASSERT_EQ(0, ptrace(PTRACE_GETSIGINFO, child, nullptr, &siginfo)) << strerror(errno); | 
|  | 369 | ASSERT_EQ(TRAP_HWBKPT, siginfo.si_code); | 
|  | 370 | } | 
| Josh Gao | 5e3fe95 | 2017-02-16 14:12:41 -0800 | [diff] [blame] | 371 |  | 
|  | 372 | class PtraceResumptionTest : public ::testing::Test { | 
|  | 373 | public: | 
| Josh Gao | bc055ca | 2017-03-29 15:01:15 -0700 | [diff] [blame] | 374 | unique_fd worker_pipe_write; | 
|  | 375 |  | 
| Josh Gao | 5e3fe95 | 2017-02-16 14:12:41 -0800 | [diff] [blame] | 376 | pid_t worker = -1; | 
| Josh Gao | bc055ca | 2017-03-29 15:01:15 -0700 | [diff] [blame] | 377 | pid_t tracer = -1; | 
|  | 378 |  | 
| Josh Gao | 5e3fe95 | 2017-02-16 14:12:41 -0800 | [diff] [blame] | 379 | PtraceResumptionTest() { | 
| Josh Gao | bc055ca | 2017-03-29 15:01:15 -0700 | [diff] [blame] | 380 | unique_fd worker_pipe_read; | 
| Luis Hector Chavez | 7300d83 | 2018-04-04 10:13:25 -0700 | [diff] [blame] | 381 | if (!android::base::Pipe(&worker_pipe_read, &worker_pipe_write)) { | 
| Josh Gao | bc055ca | 2017-03-29 15:01:15 -0700 | [diff] [blame] | 382 | err(1, "failed to create pipe"); | 
|  | 383 | } | 
|  | 384 |  | 
| Luis Hector Chavez | 7300d83 | 2018-04-04 10:13:25 -0700 | [diff] [blame] | 385 | // Second pipe to synchronize the Yama ptracer setup. | 
|  | 386 | unique_fd worker_pipe_setup_read, worker_pipe_setup_write; | 
|  | 387 | if (!android::base::Pipe(&worker_pipe_setup_read, &worker_pipe_setup_write)) { | 
|  | 388 | err(1, "failed to create pipe"); | 
|  | 389 | } | 
| Josh Gao | bc055ca | 2017-03-29 15:01:15 -0700 | [diff] [blame] | 390 |  | 
|  | 391 | worker = fork(); | 
|  | 392 | if (worker == -1) { | 
|  | 393 | err(1, "failed to fork worker"); | 
|  | 394 | } else if (worker == 0) { | 
|  | 395 | char buf; | 
| Luis Hector Chavez | 7300d83 | 2018-04-04 10:13:25 -0700 | [diff] [blame] | 396 | // Allow the tracer process, which is not a direct process ancestor, to | 
|  | 397 | // be able to use ptrace(2) on this process when Yama LSM is active. | 
|  | 398 | if (prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY, 0, 0, 0) == -1) { | 
|  | 399 | // if Yama is off prctl(PR_SET_PTRACER) returns EINVAL - don't log in this | 
|  | 400 | // case since it's expected behaviour. | 
|  | 401 | if (errno != EINVAL) { | 
|  | 402 | err(1, "prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY) failed for pid %d", getpid()); | 
|  | 403 | } | 
|  | 404 | } | 
|  | 405 | worker_pipe_setup_write.reset(); | 
|  | 406 |  | 
| Josh Gao | bc055ca | 2017-03-29 15:01:15 -0700 | [diff] [blame] | 407 | worker_pipe_write.reset(); | 
|  | 408 | TEMP_FAILURE_RETRY(read(worker_pipe_read.get(), &buf, sizeof(buf))); | 
|  | 409 | exit(0); | 
| Luis Hector Chavez | 7300d83 | 2018-04-04 10:13:25 -0700 | [diff] [blame] | 410 | } else { | 
|  | 411 | // Wait until the Yama ptracer is setup. | 
|  | 412 | char buf; | 
|  | 413 | worker_pipe_setup_write.reset(); | 
|  | 414 | TEMP_FAILURE_RETRY(read(worker_pipe_setup_read.get(), &buf, sizeof(buf))); | 
| Josh Gao | bc055ca | 2017-03-29 15:01:15 -0700 | [diff] [blame] | 415 | } | 
| Josh Gao | 5e3fe95 | 2017-02-16 14:12:41 -0800 | [diff] [blame] | 416 | } | 
|  | 417 |  | 
| Yi Kong | 358603a | 2019-03-29 14:25:16 -0700 | [diff] [blame] | 418 | ~PtraceResumptionTest() override { | 
| Josh Gao | 5e3fe95 | 2017-02-16 14:12:41 -0800 | [diff] [blame] | 419 | } | 
|  | 420 |  | 
|  | 421 | void AssertDeath(int signo); | 
| Josh Gao | 5e3fe95 | 2017-02-16 14:12:41 -0800 | [diff] [blame] | 422 |  | 
| Josh Gao | bc055ca | 2017-03-29 15:01:15 -0700 | [diff] [blame] | 423 | void StartTracer(std::function<void()> f) { | 
|  | 424 | tracer = fork(); | 
| Josh Gao | 5e3fe95 | 2017-02-16 14:12:41 -0800 | [diff] [blame] | 425 | ASSERT_NE(-1, tracer); | 
|  | 426 | if (tracer == 0) { | 
|  | 427 | f(); | 
|  | 428 | if (HasFatalFailure()) { | 
|  | 429 | exit(1); | 
|  | 430 | } | 
|  | 431 | exit(0); | 
|  | 432 | } | 
| Josh Gao | bc055ca | 2017-03-29 15:01:15 -0700 | [diff] [blame] | 433 | } | 
|  | 434 |  | 
|  | 435 | bool WaitForTracer() { | 
|  | 436 | if (tracer == -1) { | 
|  | 437 | errx(1, "tracer not started"); | 
|  | 438 | } | 
| Josh Gao | 5e3fe95 | 2017-02-16 14:12:41 -0800 | [diff] [blame] | 439 |  | 
|  | 440 | int result; | 
| Elliott Hughes | cabc77f | 2017-11-28 12:55:19 -0800 | [diff] [blame] | 441 | pid_t rc = TEMP_FAILURE_RETRY(waitpid(tracer, &result, 0)); | 
| Josh Gao | bc055ca | 2017-03-29 15:01:15 -0700 | [diff] [blame] | 442 | if (rc != tracer) { | 
|  | 443 | printf("waitpid returned %d (%s)\n", rc, strerror(errno)); | 
|  | 444 | return false; | 
|  | 445 | } | 
|  | 446 |  | 
|  | 447 | if (!WIFEXITED(result) && !WIFSIGNALED(result)) { | 
|  | 448 | printf("!WIFEXITED && !WIFSIGNALED\n"); | 
|  | 449 | return false; | 
|  | 450 | } | 
|  | 451 |  | 
| Josh Gao | 5e3fe95 | 2017-02-16 14:12:41 -0800 | [diff] [blame] | 452 | if (WIFEXITED(result)) { | 
|  | 453 | if (WEXITSTATUS(result) != 0) { | 
| Josh Gao | bc055ca | 2017-03-29 15:01:15 -0700 | [diff] [blame] | 454 | printf("tracer failed\n"); | 
|  | 455 | return false; | 
| Josh Gao | 5e3fe95 | 2017-02-16 14:12:41 -0800 | [diff] [blame] | 456 | } | 
|  | 457 | } | 
|  | 458 |  | 
| Josh Gao | bc055ca | 2017-03-29 15:01:15 -0700 | [diff] [blame] | 459 | return true; | 
|  | 460 | } | 
|  | 461 |  | 
|  | 462 | bool WaitForWorker() { | 
|  | 463 | if (worker == -1) { | 
|  | 464 | errx(1, "worker not started"); | 
|  | 465 | } | 
|  | 466 |  | 
|  | 467 | int result; | 
| Elliott Hughes | cabc77f | 2017-11-28 12:55:19 -0800 | [diff] [blame] | 468 | pid_t rc = TEMP_FAILURE_RETRY(waitpid(worker, &result, WNOHANG)); | 
| Josh Gao | bc055ca | 2017-03-29 15:01:15 -0700 | [diff] [blame] | 469 | if (rc != 0) { | 
|  | 470 | printf("worker exited prematurely\n"); | 
|  | 471 | return false; | 
|  | 472 | } | 
| Josh Gao | 5e3fe95 | 2017-02-16 14:12:41 -0800 | [diff] [blame] | 473 |  | 
|  | 474 | worker_pipe_write.reset(); | 
|  | 475 |  | 
| Elliott Hughes | cabc77f | 2017-11-28 12:55:19 -0800 | [diff] [blame] | 476 | rc = TEMP_FAILURE_RETRY(waitpid(worker, &result, 0)); | 
| Josh Gao | bc055ca | 2017-03-29 15:01:15 -0700 | [diff] [blame] | 477 | if (rc != worker) { | 
|  | 478 | printf("waitpid for worker returned %d (%s)\n", rc, strerror(errno)); | 
|  | 479 | return false; | 
|  | 480 | } | 
|  | 481 |  | 
|  | 482 | if (!WIFEXITED(result)) { | 
|  | 483 | printf("worker didn't exit\n"); | 
|  | 484 | return false; | 
|  | 485 | } | 
|  | 486 |  | 
|  | 487 | if (WEXITSTATUS(result) != 0) { | 
|  | 488 | printf("worker exited with status %d\n", WEXITSTATUS(result)); | 
|  | 489 | return false; | 
|  | 490 | } | 
|  | 491 |  | 
|  | 492 | return true; | 
| Josh Gao | 5e3fe95 | 2017-02-16 14:12:41 -0800 | [diff] [blame] | 493 | } | 
|  | 494 | }; | 
|  | 495 |  | 
|  | 496 | static void wait_for_ptrace_stop(pid_t pid) { | 
|  | 497 | while (true) { | 
|  | 498 | int status; | 
|  | 499 | pid_t rc = TEMP_FAILURE_RETRY(waitpid(pid, &status, __WALL)); | 
|  | 500 | if (rc != pid) { | 
|  | 501 | abort(); | 
|  | 502 | } | 
|  | 503 | if (WIFSTOPPED(status)) { | 
|  | 504 | return; | 
|  | 505 | } | 
|  | 506 | } | 
|  | 507 | } | 
|  | 508 |  | 
| Josh Gao | bc055ca | 2017-03-29 15:01:15 -0700 | [diff] [blame] | 509 | TEST_F(PtraceResumptionTest, smoke) { | 
|  | 510 | // Make sure that the worker doesn't exit before the tracer stops tracing. | 
|  | 511 | StartTracer([this]() { | 
|  | 512 | ASSERT_EQ(0, ptrace(PTRACE_SEIZE, worker, 0, 0)) << strerror(errno); | 
|  | 513 | ASSERT_EQ(0, ptrace(PTRACE_INTERRUPT, worker, 0, 0)) << strerror(errno); | 
|  | 514 | wait_for_ptrace_stop(worker); | 
|  | 515 | std::this_thread::sleep_for(500ms); | 
|  | 516 | }); | 
|  | 517 |  | 
|  | 518 | worker_pipe_write.reset(); | 
|  | 519 | std::this_thread::sleep_for(250ms); | 
|  | 520 |  | 
|  | 521 | int result; | 
| Elliott Hughes | cabc77f | 2017-11-28 12:55:19 -0800 | [diff] [blame] | 522 | ASSERT_EQ(0, TEMP_FAILURE_RETRY(waitpid(worker, &result, WNOHANG))); | 
| Josh Gao | bc055ca | 2017-03-29 15:01:15 -0700 | [diff] [blame] | 523 | ASSERT_TRUE(WaitForTracer()); | 
| Elliott Hughes | cabc77f | 2017-11-28 12:55:19 -0800 | [diff] [blame] | 524 | ASSERT_EQ(worker, TEMP_FAILURE_RETRY(waitpid(worker, &result, 0))); | 
| Josh Gao | bc055ca | 2017-03-29 15:01:15 -0700 | [diff] [blame] | 525 | } | 
|  | 526 |  | 
| Josh Gao | 5e3fe95 | 2017-02-16 14:12:41 -0800 | [diff] [blame] | 527 | TEST_F(PtraceResumptionTest, seize) { | 
| Josh Gao | bc055ca | 2017-03-29 15:01:15 -0700 | [diff] [blame] | 528 | StartTracer([this]() { ASSERT_EQ(0, ptrace(PTRACE_SEIZE, worker, 0, 0)) << strerror(errno); }); | 
|  | 529 | ASSERT_TRUE(WaitForTracer()); | 
|  | 530 | ASSERT_TRUE(WaitForWorker()); | 
| Josh Gao | 5e3fe95 | 2017-02-16 14:12:41 -0800 | [diff] [blame] | 531 | } | 
|  | 532 |  | 
|  | 533 | TEST_F(PtraceResumptionTest, seize_interrupt) { | 
| Josh Gao | bc055ca | 2017-03-29 15:01:15 -0700 | [diff] [blame] | 534 | StartTracer([this]() { | 
| Josh Gao | 5e3fe95 | 2017-02-16 14:12:41 -0800 | [diff] [blame] | 535 | ASSERT_EQ(0, ptrace(PTRACE_SEIZE, worker, 0, 0)) << strerror(errno); | 
|  | 536 | ASSERT_EQ(0, ptrace(PTRACE_INTERRUPT, worker, 0, 0)) << strerror(errno); | 
| Josh Gao | bc055ca | 2017-03-29 15:01:15 -0700 | [diff] [blame] | 537 | wait_for_ptrace_stop(worker); | 
| Josh Gao | 5e3fe95 | 2017-02-16 14:12:41 -0800 | [diff] [blame] | 538 | }); | 
| Josh Gao | bc055ca | 2017-03-29 15:01:15 -0700 | [diff] [blame] | 539 | ASSERT_TRUE(WaitForTracer()); | 
|  | 540 | ASSERT_TRUE(WaitForWorker()); | 
| Josh Gao | 5e3fe95 | 2017-02-16 14:12:41 -0800 | [diff] [blame] | 541 | } | 
|  | 542 |  | 
|  | 543 | TEST_F(PtraceResumptionTest, seize_interrupt_cont) { | 
| Josh Gao | bc055ca | 2017-03-29 15:01:15 -0700 | [diff] [blame] | 544 | StartTracer([this]() { | 
| Josh Gao | 5e3fe95 | 2017-02-16 14:12:41 -0800 | [diff] [blame] | 545 | ASSERT_EQ(0, ptrace(PTRACE_SEIZE, worker, 0, 0)) << strerror(errno); | 
|  | 546 | ASSERT_EQ(0, ptrace(PTRACE_INTERRUPT, worker, 0, 0)) << strerror(errno); | 
|  | 547 | wait_for_ptrace_stop(worker); | 
|  | 548 | ASSERT_EQ(0, ptrace(PTRACE_CONT, worker, 0, 0)) << strerror(errno); | 
|  | 549 | }); | 
| Josh Gao | bc055ca | 2017-03-29 15:01:15 -0700 | [diff] [blame] | 550 | ASSERT_TRUE(WaitForTracer()); | 
|  | 551 | ASSERT_TRUE(WaitForWorker()); | 
|  | 552 | } | 
|  | 553 |  | 
|  | 554 | TEST_F(PtraceResumptionTest, zombie_seize) { | 
|  | 555 | StartTracer([this]() { ASSERT_EQ(0, ptrace(PTRACE_SEIZE, worker, 0, 0)) << strerror(errno); }); | 
|  | 556 | ASSERT_TRUE(WaitForWorker()); | 
|  | 557 | ASSERT_TRUE(WaitForTracer()); | 
|  | 558 | } | 
|  | 559 |  | 
|  | 560 | TEST_F(PtraceResumptionTest, zombie_seize_interrupt) { | 
|  | 561 | StartTracer([this]() { | 
|  | 562 | ASSERT_EQ(0, ptrace(PTRACE_SEIZE, worker, 0, 0)) << strerror(errno); | 
|  | 563 | ASSERT_EQ(0, ptrace(PTRACE_INTERRUPT, worker, 0, 0)) << strerror(errno); | 
|  | 564 | wait_for_ptrace_stop(worker); | 
|  | 565 | }); | 
|  | 566 | ASSERT_TRUE(WaitForWorker()); | 
|  | 567 | ASSERT_TRUE(WaitForTracer()); | 
|  | 568 | } | 
|  | 569 |  | 
|  | 570 | TEST_F(PtraceResumptionTest, zombie_seize_interrupt_cont) { | 
|  | 571 | StartTracer([this]() { | 
|  | 572 | ASSERT_EQ(0, ptrace(PTRACE_SEIZE, worker, 0, 0)) << strerror(errno); | 
|  | 573 | ASSERT_EQ(0, ptrace(PTRACE_INTERRUPT, worker, 0, 0)) << strerror(errno); | 
|  | 574 | wait_for_ptrace_stop(worker); | 
|  | 575 | ASSERT_EQ(0, ptrace(PTRACE_CONT, worker, 0, 0)) << strerror(errno); | 
|  | 576 | }); | 
|  | 577 | ASSERT_TRUE(WaitForWorker()); | 
|  | 578 | ASSERT_TRUE(WaitForTracer()); | 
| Josh Gao | 5e3fe95 | 2017-02-16 14:12:41 -0800 | [diff] [blame] | 579 | } |