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