blob: f17f80cfa307fe0fb6e8b62b08aba757449481a1 [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>
Josh Gaobc055ca2017-03-29 15:01:15 -070020#include <err.h>
Josh Gao5e3fe952017-02-16 14:12:41 -080021#include <fcntl.h>
Pavel Labath1faca6c2016-04-21 15:13:22 +010022#include <sched.h>
23#include <sys/prctl.h>
Josh Gao5e3fe952017-02-16 14:12:41 -080024#include <sys/ptrace.h>
Pavel Labath1faca6c2016-04-21 15:13:22 +010025#include <sys/uio.h>
26#include <sys/user.h>
Josh Gao5e3fe952017-02-16 14:12:41 -080027#include <sys/wait.h>
Pavel Labath1faca6c2016-04-21 15:13:22 +010028#include <unistd.h>
29
Josh Gaobc055ca2017-03-29 15:01:15 -070030#include <chrono>
31#include <thread>
32
Pavel Labath1faca6c2016-04-21 15:13:22 +010033#include <gtest/gtest.h>
34
Pavel Labath3dad8d52017-02-22 18:22:46 +000035#include <android-base/macros.h>
Josh Gao5e3fe952017-02-16 14:12:41 -080036#include <android-base/unique_fd.h>
37
Evgenii Stepanov7cc67062019-02-05 18:43:34 -080038#include "utils.h"
39
Josh Gaobc055ca2017-03-29 15:01:15 -070040using namespace std::chrono_literals;
41
Josh Gao5e3fe952017-02-16 14:12:41 -080042using android::base::unique_fd;
43
Pavel Labath1faca6c2016-04-21 15:13:22 +010044// Host libc does not define this.
45#ifndef TRAP_HWBKPT
46#define TRAP_HWBKPT 4
47#endif
48
Pavel Labath1faca6c2016-04-21 15:13:22 +010049class ChildGuard {
50 public:
Chih-Hung Hsieh62e3a072016-05-03 12:08:05 -070051 explicit ChildGuard(pid_t pid) : pid(pid) {}
Pavel Labath1faca6c2016-04-21 15:13:22 +010052
53 ~ChildGuard() {
54 kill(pid, SIGKILL);
55 int status;
Elliott Hughescabc77f2017-11-28 12:55:19 -080056 TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
Pavel Labath1faca6c2016-04-21 15:13:22 +010057 }
58
59 private:
60 pid_t pid;
61};
62
Pavel Labathfb082ee2017-01-23 15:41:35 +000063enum class HwFeature { Watchpoint, Breakpoint };
64
Elliott Hughesbcaa4542019-03-08 15:20:23 -080065static void check_hw_feature_supported(pid_t child, HwFeature feature) {
Pavel Labath1faca6c2016-04-21 15:13:22 +010066#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 Hughesbcaa4542019-03-08 15:20:23 -080071 GTEST_SKIP() << "Hardware debug support disabled at kernel configuration time";
Pavel Labath1faca6c2016-04-21 15:13:22 +010072 }
Pavel Labath95d8fb12017-07-07 11:42:34 +010073 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 Hughesbcaa4542019-03-08 15:20:23 -080079 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 Labathfb082ee2017-01-23 15:41:35 +000084 }
Pavel Labath1faca6c2016-04-21 15:13:22 +010085#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 Labathfb082ee2017-01-23 15:41:35 +000091 long result = ptrace(PTRACE_GETREGSET, child,
92 feature == HwFeature::Watchpoint ? NT_ARM_HW_WATCH : NT_ARM_HW_BREAK, &iov);
Pavel Labath1faca6c2016-04-21 15:13:22 +010093 if (result == -1) {
Elliott Hughesbcaa4542019-03-08 15:20:23 -080094 ASSERT_EQ(EINVAL, errno);
Evgeny Eltsinbd1c6302019-12-11 15:30:16 +010095 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 Labath1faca6c2016-04-21 15:13:22 +0100102 }
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800103#else
Pavel Labathfb082ee2017-01-23 15:41:35 +0000104 // We assume watchpoints and breakpoints are always supported on x86.
Pavel Labath3dad8d52017-02-22 18:22:46 +0000105 UNUSED(child);
106 UNUSED(feature);
Pavel Labath1faca6c2016-04-21 15:13:22 +0100107#endif
108}
109
Pavel Labath3dad8d52017-02-22 18:22:46 +0000110static void set_watchpoint(pid_t child, uintptr_t address, size_t size) {
111 ASSERT_EQ(0u, address & 0x7) << "address: " << address;
Pavel Labath1faca6c2016-04-21 15:13:22 +0100112#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 Labath3dad8d52017-02-22 18:22:46 +0000124 dreg_state.dbg_regs[0].addr = address;
Pavel Labath1faca6c2016-04-21 15:13:22 +0100125 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 Labath3dad8d52017-02-22 18:22:46 +0000149 UNUSED(child);
150 UNUSED(address);
151 UNUSED(size);
Pavel Labath1faca6c2016-04-21 15:13:22 +0100152#endif
153}
154
Pavel Labath3dad8d52017-02-22 18:22:46 +0000155template <typename T>
156static void run_watchpoint_test(std::function<void(T&)> child_func, size_t offset, size_t size) {
157 alignas(16) T data{};
Pavel Labath1faca6c2016-04-21 15:13:22 +0100158
159 pid_t child = fork();
160 ASSERT_NE(-1, child) << strerror(errno);
Pavel Labath3dad8d52017-02-22 18:22:46 +0000161 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 Labath1faca6c2016-04-21 15:13:22 +0100176
177 ChildGuard guard(child);
178
179 int status;
Elliott Hughescabc77f2017-11-28 12:55:19 -0800180 ASSERT_EQ(child, TEMP_FAILURE_RETRY(waitpid(child, &status, __WALL))) << strerror(errno);
Pavel Labath1faca6c2016-04-21 15:13:22 +0100181 ASSERT_TRUE(WIFSTOPPED(status)) << "Status was: " << status;
182 ASSERT_EQ(SIGSTOP, WSTOPSIG(status)) << "Status was: " << status;
183
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800184 check_hw_feature_supported(child, HwFeature::Watchpoint);
Christopher Ferris103b9982019-09-23 09:03:10 -0700185 if (::testing::Test::IsSkipped()) {
186 return;
187 }
Pavel Labath1faca6c2016-04-21 15:13:22 +0100188
Evgenii Stepanov7cc67062019-02-05 18:43:34 -0800189 set_watchpoint(child, uintptr_t(untag_address(&data)) + offset, size);
Pavel Labath1faca6c2016-04-21 15:13:22 +0100190
191 ASSERT_EQ(0, ptrace(PTRACE_CONT, child, nullptr, nullptr)) << strerror(errno);
Elliott Hughescabc77f2017-11-28 12:55:19 -0800192 ASSERT_EQ(child, TEMP_FAILURE_RETRY(waitpid(child, &status, __WALL))) << strerror(errno);
Pavel Labath1faca6c2016-04-21 15:13:22 +0100193 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 Labath3dad8d52017-02-22 18:22:46 +0000200 ASSERT_LE(&data, siginfo.si_addr);
201 ASSERT_GT((&data) + 1, siginfo.si_addr);
Pavel Labath1faca6c2016-04-21 15:13:22 +0100202#endif
203}
204
Pavel Labath3dad8d52017-02-22 18:22:46 +0000205template <typename T>
206static 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
219template <typename T>
220static 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 Labath1faca6c2016-04-21 15:13:22 +0100223}
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 Labathfb082ee2017-01-23 15:41:35 +0000228TEST(sys_ptrace, watchpoint_stress) {
Pavel Labath1faca6c2016-04-21 15:13:22 +0100229 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 Labath3dad8d52017-02-22 18:22:46 +0000234
235 run_watchpoint_stress<uint8_t>(cpu);
Christopher Ferris103b9982019-09-23 09:03:10 -0700236 if (::testing::Test::IsSkipped()) {
237 // Only check first case, since all others would skip for same reason.
238 return;
239 }
Pavel Labath3dad8d52017-02-22 18:22:46 +0000240 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 Labath1faca6c2016-04-21 15:13:22 +0100245 }
246}
Pavel Labathfb082ee2017-01-23 15:41:35 +0000247
Pavel Labath3dad8d52017-02-22 18:22:46 +0000248struct Uint128_t {
249 uint64_t data[2];
250};
251static 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));
260#elif defined(__mips__)
261// TODO
Pavel Labathfb5a6392017-02-24 10:14:13 +0000262 UNUSED(data);
Pavel Labath3dad8d52017-02-22 18:22:46 +0000263#endif
264}
265
266// Test that the kernel is able to handle the case when the instruction writes
267// to a larger block of memory than the one we are watching. If you see this
268// test fail on arm64, you will likely need to cherry-pick fdfeff0f into your
269// kernel.
270TEST(sys_ptrace, watchpoint_imprecise) {
Yabin Cui143b4542017-11-29 10:34:24 -0800271 // This test relies on the infrastructure to timeout if the test hangs.
Pavel Labath4a620262017-04-26 11:30:06 +0100272 run_watchpoint_test<Uint128_t>(watchpoint_imprecise_child, 8, sizeof(void*));
Pavel Labath3dad8d52017-02-22 18:22:46 +0000273}
274
Pavel Labathfb082ee2017-01-23 15:41:35 +0000275static void __attribute__((noinline)) breakpoint_func() {
276 asm volatile("");
277}
278
279static void __attribute__((noreturn)) breakpoint_fork_child() {
280 // Extra precaution: make sure we go away if anything happens to our parent.
281 if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0) == -1) {
282 perror("prctl(PR_SET_PDEATHSIG)");
283 _exit(1);
284 }
285
286 if (ptrace(PTRACE_TRACEME, 0, nullptr, nullptr) == -1) {
287 perror("ptrace(PTRACE_TRACEME)");
288 _exit(2);
289 }
290
291 raise(SIGSTOP); // Synchronize with the tracer, let it set the breakpoint.
292
293 breakpoint_func(); // Now trigger the breakpoint.
294
295 _exit(0);
296}
297
298static void set_breakpoint(pid_t child) {
299 uintptr_t address = uintptr_t(breakpoint_func);
300#if defined(__arm__) || defined(__aarch64__)
301 address &= ~3;
302 const unsigned byte_mask = 0xf;
303 const unsigned enable = 1;
304 const unsigned control = byte_mask << 5 | enable;
305
306#ifdef __arm__
307 ASSERT_EQ(0, ptrace(PTRACE_SETHBPREGS, child, 1, &address)) << strerror(errno);
308 ASSERT_EQ(0, ptrace(PTRACE_SETHBPREGS, child, 2, &control)) << strerror(errno);
309#else // aarch64
310 user_hwdebug_state dreg_state;
311 memset(&dreg_state, 0, sizeof dreg_state);
312 dreg_state.dbg_regs[0].addr = reinterpret_cast<uintptr_t>(address);
313 dreg_state.dbg_regs[0].ctrl = control;
314
315 iovec iov;
316 iov.iov_base = &dreg_state;
317 iov.iov_len = offsetof(user_hwdebug_state, dbg_regs) + sizeof(dreg_state.dbg_regs[0]);
318
319 ASSERT_EQ(0, ptrace(PTRACE_SETREGSET, child, NT_ARM_HW_BREAK, &iov)) << strerror(errno);
320#endif
321#elif defined(__i386__) || defined(__x86_64__)
322 ASSERT_EQ(0, ptrace(PTRACE_POKEUSER, child, offsetof(user, u_debugreg[0]), address))
323 << strerror(errno);
324 errno = 0;
325 unsigned data = ptrace(PTRACE_PEEKUSER, child, offsetof(user, u_debugreg[7]), nullptr);
326 ASSERT_EQ(0, errno);
327
328 const unsigned size = 0;
329 const unsigned enable = 1;
330 const unsigned type = 0; // Execute
331
332 const unsigned mask = 3 << 18 | 3 << 16 | 1;
333 const unsigned value = size << 18 | type << 16 | enable;
334 data &= mask;
335 data |= value;
336 ASSERT_EQ(0, ptrace(PTRACE_POKEUSER, child, offsetof(user, u_debugreg[7]), data))
337 << strerror(errno);
338#else
Pavel Labath3dad8d52017-02-22 18:22:46 +0000339 UNUSED(child);
340 UNUSED(address);
Pavel Labathfb082ee2017-01-23 15:41:35 +0000341#endif
342}
343
344// Test hardware breakpoint API. The test is considered successful if the breakpoints get hit OR the
345// system reports that hardware breakpoint support is not present.
346TEST(sys_ptrace, hardware_breakpoint) {
347 pid_t child = fork();
348 ASSERT_NE(-1, child) << strerror(errno);
349 if (child == 0) breakpoint_fork_child();
350
351 ChildGuard guard(child);
352
353 int status;
Elliott Hughescabc77f2017-11-28 12:55:19 -0800354 ASSERT_EQ(child, TEMP_FAILURE_RETRY(waitpid(child, &status, __WALL))) << strerror(errno);
Pavel Labathfb082ee2017-01-23 15:41:35 +0000355 ASSERT_TRUE(WIFSTOPPED(status)) << "Status was: " << status;
356 ASSERT_EQ(SIGSTOP, WSTOPSIG(status)) << "Status was: " << status;
357
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800358 check_hw_feature_supported(child, HwFeature::Breakpoint);
Christopher Ferris103b9982019-09-23 09:03:10 -0700359 if (::testing::Test::IsSkipped()) {
360 return;
361 }
Pavel Labathfb082ee2017-01-23 15:41:35 +0000362
363 set_breakpoint(child);
364
365 ASSERT_EQ(0, ptrace(PTRACE_CONT, child, nullptr, nullptr)) << strerror(errno);
Elliott Hughescabc77f2017-11-28 12:55:19 -0800366 ASSERT_EQ(child, TEMP_FAILURE_RETRY(waitpid(child, &status, __WALL))) << strerror(errno);
Pavel Labathfb082ee2017-01-23 15:41:35 +0000367 ASSERT_TRUE(WIFSTOPPED(status)) << "Status was: " << status;
368 ASSERT_EQ(SIGTRAP, WSTOPSIG(status)) << "Status was: " << status;
369
370 siginfo_t siginfo;
371 ASSERT_EQ(0, ptrace(PTRACE_GETSIGINFO, child, nullptr, &siginfo)) << strerror(errno);
372 ASSERT_EQ(TRAP_HWBKPT, siginfo.si_code);
373}
Josh Gao5e3fe952017-02-16 14:12:41 -0800374
375class PtraceResumptionTest : public ::testing::Test {
376 public:
Josh Gaobc055ca2017-03-29 15:01:15 -0700377 unique_fd worker_pipe_write;
378
Josh Gao5e3fe952017-02-16 14:12:41 -0800379 pid_t worker = -1;
Josh Gaobc055ca2017-03-29 15:01:15 -0700380 pid_t tracer = -1;
381
Josh Gao5e3fe952017-02-16 14:12:41 -0800382 PtraceResumptionTest() {
Josh Gaobc055ca2017-03-29 15:01:15 -0700383 unique_fd worker_pipe_read;
Luis Hector Chavez7300d832018-04-04 10:13:25 -0700384 if (!android::base::Pipe(&worker_pipe_read, &worker_pipe_write)) {
Josh Gaobc055ca2017-03-29 15:01:15 -0700385 err(1, "failed to create pipe");
386 }
387
Luis Hector Chavez7300d832018-04-04 10:13:25 -0700388 // Second pipe to synchronize the Yama ptracer setup.
389 unique_fd worker_pipe_setup_read, worker_pipe_setup_write;
390 if (!android::base::Pipe(&worker_pipe_setup_read, &worker_pipe_setup_write)) {
391 err(1, "failed to create pipe");
392 }
Josh Gaobc055ca2017-03-29 15:01:15 -0700393
394 worker = fork();
395 if (worker == -1) {
396 err(1, "failed to fork worker");
397 } else if (worker == 0) {
398 char buf;
Luis Hector Chavez7300d832018-04-04 10:13:25 -0700399 // Allow the tracer process, which is not a direct process ancestor, to
400 // be able to use ptrace(2) on this process when Yama LSM is active.
401 if (prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY, 0, 0, 0) == -1) {
402 // if Yama is off prctl(PR_SET_PTRACER) returns EINVAL - don't log in this
403 // case since it's expected behaviour.
404 if (errno != EINVAL) {
405 err(1, "prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY) failed for pid %d", getpid());
406 }
407 }
408 worker_pipe_setup_write.reset();
409
Josh Gaobc055ca2017-03-29 15:01:15 -0700410 worker_pipe_write.reset();
411 TEMP_FAILURE_RETRY(read(worker_pipe_read.get(), &buf, sizeof(buf)));
412 exit(0);
Luis Hector Chavez7300d832018-04-04 10:13:25 -0700413 } else {
414 // Wait until the Yama ptracer is setup.
415 char buf;
416 worker_pipe_setup_write.reset();
417 TEMP_FAILURE_RETRY(read(worker_pipe_setup_read.get(), &buf, sizeof(buf)));
Josh Gaobc055ca2017-03-29 15:01:15 -0700418 }
Josh Gao5e3fe952017-02-16 14:12:41 -0800419 }
420
Yi Kong358603a2019-03-29 14:25:16 -0700421 ~PtraceResumptionTest() override {
Josh Gao5e3fe952017-02-16 14:12:41 -0800422 }
423
424 void AssertDeath(int signo);
Josh Gao5e3fe952017-02-16 14:12:41 -0800425
Josh Gaobc055ca2017-03-29 15:01:15 -0700426 void StartTracer(std::function<void()> f) {
427 tracer = fork();
Josh Gao5e3fe952017-02-16 14:12:41 -0800428 ASSERT_NE(-1, tracer);
429 if (tracer == 0) {
430 f();
431 if (HasFatalFailure()) {
432 exit(1);
433 }
434 exit(0);
435 }
Josh Gaobc055ca2017-03-29 15:01:15 -0700436 }
437
438 bool WaitForTracer() {
439 if (tracer == -1) {
440 errx(1, "tracer not started");
441 }
Josh Gao5e3fe952017-02-16 14:12:41 -0800442
443 int result;
Elliott Hughescabc77f2017-11-28 12:55:19 -0800444 pid_t rc = TEMP_FAILURE_RETRY(waitpid(tracer, &result, 0));
Josh Gaobc055ca2017-03-29 15:01:15 -0700445 if (rc != tracer) {
446 printf("waitpid returned %d (%s)\n", rc, strerror(errno));
447 return false;
448 }
449
450 if (!WIFEXITED(result) && !WIFSIGNALED(result)) {
451 printf("!WIFEXITED && !WIFSIGNALED\n");
452 return false;
453 }
454
Josh Gao5e3fe952017-02-16 14:12:41 -0800455 if (WIFEXITED(result)) {
456 if (WEXITSTATUS(result) != 0) {
Josh Gaobc055ca2017-03-29 15:01:15 -0700457 printf("tracer failed\n");
458 return false;
Josh Gao5e3fe952017-02-16 14:12:41 -0800459 }
460 }
461
Josh Gaobc055ca2017-03-29 15:01:15 -0700462 return true;
463 }
464
465 bool WaitForWorker() {
466 if (worker == -1) {
467 errx(1, "worker not started");
468 }
469
470 int result;
Elliott Hughescabc77f2017-11-28 12:55:19 -0800471 pid_t rc = TEMP_FAILURE_RETRY(waitpid(worker, &result, WNOHANG));
Josh Gaobc055ca2017-03-29 15:01:15 -0700472 if (rc != 0) {
473 printf("worker exited prematurely\n");
474 return false;
475 }
Josh Gao5e3fe952017-02-16 14:12:41 -0800476
477 worker_pipe_write.reset();
478
Elliott Hughescabc77f2017-11-28 12:55:19 -0800479 rc = TEMP_FAILURE_RETRY(waitpid(worker, &result, 0));
Josh Gaobc055ca2017-03-29 15:01:15 -0700480 if (rc != worker) {
481 printf("waitpid for worker returned %d (%s)\n", rc, strerror(errno));
482 return false;
483 }
484
485 if (!WIFEXITED(result)) {
486 printf("worker didn't exit\n");
487 return false;
488 }
489
490 if (WEXITSTATUS(result) != 0) {
491 printf("worker exited with status %d\n", WEXITSTATUS(result));
492 return false;
493 }
494
495 return true;
Josh Gao5e3fe952017-02-16 14:12:41 -0800496 }
497};
498
499static void wait_for_ptrace_stop(pid_t pid) {
500 while (true) {
501 int status;
502 pid_t rc = TEMP_FAILURE_RETRY(waitpid(pid, &status, __WALL));
503 if (rc != pid) {
504 abort();
505 }
506 if (WIFSTOPPED(status)) {
507 return;
508 }
509 }
510}
511
Josh Gaobc055ca2017-03-29 15:01:15 -0700512TEST_F(PtraceResumptionTest, smoke) {
513 // Make sure that the worker doesn't exit before the tracer stops tracing.
514 StartTracer([this]() {
515 ASSERT_EQ(0, ptrace(PTRACE_SEIZE, worker, 0, 0)) << strerror(errno);
516 ASSERT_EQ(0, ptrace(PTRACE_INTERRUPT, worker, 0, 0)) << strerror(errno);
517 wait_for_ptrace_stop(worker);
518 std::this_thread::sleep_for(500ms);
519 });
520
521 worker_pipe_write.reset();
522 std::this_thread::sleep_for(250ms);
523
524 int result;
Elliott Hughescabc77f2017-11-28 12:55:19 -0800525 ASSERT_EQ(0, TEMP_FAILURE_RETRY(waitpid(worker, &result, WNOHANG)));
Josh Gaobc055ca2017-03-29 15:01:15 -0700526 ASSERT_TRUE(WaitForTracer());
Elliott Hughescabc77f2017-11-28 12:55:19 -0800527 ASSERT_EQ(worker, TEMP_FAILURE_RETRY(waitpid(worker, &result, 0)));
Josh Gaobc055ca2017-03-29 15:01:15 -0700528}
529
Josh Gao5e3fe952017-02-16 14:12:41 -0800530TEST_F(PtraceResumptionTest, seize) {
Josh Gaobc055ca2017-03-29 15:01:15 -0700531 StartTracer([this]() { ASSERT_EQ(0, ptrace(PTRACE_SEIZE, worker, 0, 0)) << strerror(errno); });
532 ASSERT_TRUE(WaitForTracer());
533 ASSERT_TRUE(WaitForWorker());
Josh Gao5e3fe952017-02-16 14:12:41 -0800534}
535
536TEST_F(PtraceResumptionTest, seize_interrupt) {
Josh Gaobc055ca2017-03-29 15:01:15 -0700537 StartTracer([this]() {
Josh Gao5e3fe952017-02-16 14:12:41 -0800538 ASSERT_EQ(0, ptrace(PTRACE_SEIZE, worker, 0, 0)) << strerror(errno);
539 ASSERT_EQ(0, ptrace(PTRACE_INTERRUPT, worker, 0, 0)) << strerror(errno);
Josh Gaobc055ca2017-03-29 15:01:15 -0700540 wait_for_ptrace_stop(worker);
Josh Gao5e3fe952017-02-16 14:12:41 -0800541 });
Josh Gaobc055ca2017-03-29 15:01:15 -0700542 ASSERT_TRUE(WaitForTracer());
543 ASSERT_TRUE(WaitForWorker());
Josh Gao5e3fe952017-02-16 14:12:41 -0800544}
545
546TEST_F(PtraceResumptionTest, seize_interrupt_cont) {
Josh Gaobc055ca2017-03-29 15:01:15 -0700547 StartTracer([this]() {
Josh Gao5e3fe952017-02-16 14:12:41 -0800548 ASSERT_EQ(0, ptrace(PTRACE_SEIZE, worker, 0, 0)) << strerror(errno);
549 ASSERT_EQ(0, ptrace(PTRACE_INTERRUPT, worker, 0, 0)) << strerror(errno);
550 wait_for_ptrace_stop(worker);
551 ASSERT_EQ(0, ptrace(PTRACE_CONT, worker, 0, 0)) << strerror(errno);
552 });
Josh Gaobc055ca2017-03-29 15:01:15 -0700553 ASSERT_TRUE(WaitForTracer());
554 ASSERT_TRUE(WaitForWorker());
555}
556
557TEST_F(PtraceResumptionTest, zombie_seize) {
558 StartTracer([this]() { ASSERT_EQ(0, ptrace(PTRACE_SEIZE, worker, 0, 0)) << strerror(errno); });
559 ASSERT_TRUE(WaitForWorker());
560 ASSERT_TRUE(WaitForTracer());
561}
562
563TEST_F(PtraceResumptionTest, zombie_seize_interrupt) {
564 StartTracer([this]() {
565 ASSERT_EQ(0, ptrace(PTRACE_SEIZE, worker, 0, 0)) << strerror(errno);
566 ASSERT_EQ(0, ptrace(PTRACE_INTERRUPT, worker, 0, 0)) << strerror(errno);
567 wait_for_ptrace_stop(worker);
568 });
569 ASSERT_TRUE(WaitForWorker());
570 ASSERT_TRUE(WaitForTracer());
571}
572
573TEST_F(PtraceResumptionTest, zombie_seize_interrupt_cont) {
574 StartTracer([this]() {
575 ASSERT_EQ(0, ptrace(PTRACE_SEIZE, worker, 0, 0)) << strerror(errno);
576 ASSERT_EQ(0, ptrace(PTRACE_INTERRUPT, worker, 0, 0)) << strerror(errno);
577 wait_for_ptrace_stop(worker);
578 ASSERT_EQ(0, ptrace(PTRACE_CONT, worker, 0, 0)) << strerror(errno);
579 });
580 ASSERT_TRUE(WaitForWorker());
581 ASSERT_TRUE(WaitForTracer());
Josh Gao5e3fe952017-02-16 14:12:41 -0800582}