blob: 15e9a2491ca89580860ad5cc427e9924d29b7d87 [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);
Pavel Labath1faca6c2016-04-21 15:13:22 +010095 }
Elliott Hughesbcaa4542019-03-08 15:20:23 -080096 if ((dreg_state.dbg_info & 0xff) == 0) GTEST_SKIP() << "hardware support missing";
97#else
Pavel Labathfb082ee2017-01-23 15:41:35 +000098 // We assume watchpoints and breakpoints are always supported on x86.
Pavel Labath3dad8d52017-02-22 18:22:46 +000099 UNUSED(child);
100 UNUSED(feature);
Pavel Labath1faca6c2016-04-21 15:13:22 +0100101#endif
102}
103
Pavel Labath3dad8d52017-02-22 18:22:46 +0000104static void set_watchpoint(pid_t child, uintptr_t address, size_t size) {
105 ASSERT_EQ(0u, address & 0x7) << "address: " << address;
Pavel Labath1faca6c2016-04-21 15:13:22 +0100106#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 Labath3dad8d52017-02-22 18:22:46 +0000118 dreg_state.dbg_regs[0].addr = address;
Pavel Labath1faca6c2016-04-21 15:13:22 +0100119 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 Labath3dad8d52017-02-22 18:22:46 +0000143 UNUSED(child);
144 UNUSED(address);
145 UNUSED(size);
Pavel Labath1faca6c2016-04-21 15:13:22 +0100146#endif
147}
148
Pavel Labath3dad8d52017-02-22 18:22:46 +0000149template <typename T>
150static void run_watchpoint_test(std::function<void(T&)> child_func, size_t offset, size_t size) {
151 alignas(16) T data{};
Pavel Labath1faca6c2016-04-21 15:13:22 +0100152
153 pid_t child = fork();
154 ASSERT_NE(-1, child) << strerror(errno);
Pavel Labath3dad8d52017-02-22 18:22:46 +0000155 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 Labath1faca6c2016-04-21 15:13:22 +0100170
171 ChildGuard guard(child);
172
173 int status;
Elliott Hughescabc77f2017-11-28 12:55:19 -0800174 ASSERT_EQ(child, TEMP_FAILURE_RETRY(waitpid(child, &status, __WALL))) << strerror(errno);
Pavel Labath1faca6c2016-04-21 15:13:22 +0100175 ASSERT_TRUE(WIFSTOPPED(status)) << "Status was: " << status;
176 ASSERT_EQ(SIGSTOP, WSTOPSIG(status)) << "Status was: " << status;
177
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800178 check_hw_feature_supported(child, HwFeature::Watchpoint);
Christopher Ferris103b9982019-09-23 09:03:10 -0700179 if (::testing::Test::IsSkipped()) {
180 return;
181 }
Pavel Labath1faca6c2016-04-21 15:13:22 +0100182
Evgenii Stepanov7cc67062019-02-05 18:43:34 -0800183 set_watchpoint(child, uintptr_t(untag_address(&data)) + offset, size);
Pavel Labath1faca6c2016-04-21 15:13:22 +0100184
185 ASSERT_EQ(0, ptrace(PTRACE_CONT, child, nullptr, nullptr)) << strerror(errno);
Elliott Hughescabc77f2017-11-28 12:55:19 -0800186 ASSERT_EQ(child, TEMP_FAILURE_RETRY(waitpid(child, &status, __WALL))) << strerror(errno);
Pavel Labath1faca6c2016-04-21 15:13:22 +0100187 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 Labath3dad8d52017-02-22 18:22:46 +0000194 ASSERT_LE(&data, siginfo.si_addr);
195 ASSERT_GT((&data) + 1, siginfo.si_addr);
Pavel Labath1faca6c2016-04-21 15:13:22 +0100196#endif
197}
198
Pavel Labath3dad8d52017-02-22 18:22:46 +0000199template <typename T>
200static 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
213template <typename T>
214static 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 Labath1faca6c2016-04-21 15:13:22 +0100217}
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 Labathfb082ee2017-01-23 15:41:35 +0000222TEST(sys_ptrace, watchpoint_stress) {
Pavel Labath1faca6c2016-04-21 15:13:22 +0100223 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 Labath3dad8d52017-02-22 18:22:46 +0000228
229 run_watchpoint_stress<uint8_t>(cpu);
Christopher Ferris103b9982019-09-23 09:03:10 -0700230 if (::testing::Test::IsSkipped()) {
231 // Only check first case, since all others would skip for same reason.
232 return;
233 }
Pavel Labath3dad8d52017-02-22 18:22:46 +0000234 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 Labath1faca6c2016-04-21 15:13:22 +0100239 }
240}
Pavel Labathfb082ee2017-01-23 15:41:35 +0000241
Pavel Labath3dad8d52017-02-22 18:22:46 +0000242struct Uint128_t {
243 uint64_t data[2];
244};
245static 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 Labathfb5a6392017-02-24 10:14:13 +0000256 UNUSED(data);
Pavel Labath3dad8d52017-02-22 18:22:46 +0000257#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.
264TEST(sys_ptrace, watchpoint_imprecise) {
Yabin Cui143b4542017-11-29 10:34:24 -0800265 // This test relies on the infrastructure to timeout if the test hangs.
Pavel Labath4a620262017-04-26 11:30:06 +0100266 run_watchpoint_test<Uint128_t>(watchpoint_imprecise_child, 8, sizeof(void*));
Pavel Labath3dad8d52017-02-22 18:22:46 +0000267}
268
Pavel Labathfb082ee2017-01-23 15:41:35 +0000269static void __attribute__((noinline)) breakpoint_func() {
270 asm volatile("");
271}
272
273static 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
292static 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 Labath3dad8d52017-02-22 18:22:46 +0000333 UNUSED(child);
334 UNUSED(address);
Pavel Labathfb082ee2017-01-23 15:41:35 +0000335#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.
340TEST(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 Hughescabc77f2017-11-28 12:55:19 -0800348 ASSERT_EQ(child, TEMP_FAILURE_RETRY(waitpid(child, &status, __WALL))) << strerror(errno);
Pavel Labathfb082ee2017-01-23 15:41:35 +0000349 ASSERT_TRUE(WIFSTOPPED(status)) << "Status was: " << status;
350 ASSERT_EQ(SIGSTOP, WSTOPSIG(status)) << "Status was: " << status;
351
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800352 check_hw_feature_supported(child, HwFeature::Breakpoint);
Christopher Ferris103b9982019-09-23 09:03:10 -0700353 if (::testing::Test::IsSkipped()) {
354 return;
355 }
Pavel Labathfb082ee2017-01-23 15:41:35 +0000356
357 set_breakpoint(child);
358
359 ASSERT_EQ(0, ptrace(PTRACE_CONT, child, nullptr, nullptr)) << strerror(errno);
Elliott Hughescabc77f2017-11-28 12:55:19 -0800360 ASSERT_EQ(child, TEMP_FAILURE_RETRY(waitpid(child, &status, __WALL))) << strerror(errno);
Pavel Labathfb082ee2017-01-23 15:41:35 +0000361 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 Gao5e3fe952017-02-16 14:12:41 -0800368
369class PtraceResumptionTest : public ::testing::Test {
370 public:
Josh Gaobc055ca2017-03-29 15:01:15 -0700371 unique_fd worker_pipe_write;
372
Josh Gao5e3fe952017-02-16 14:12:41 -0800373 pid_t worker = -1;
Josh Gaobc055ca2017-03-29 15:01:15 -0700374 pid_t tracer = -1;
375
Josh Gao5e3fe952017-02-16 14:12:41 -0800376 PtraceResumptionTest() {
Josh Gaobc055ca2017-03-29 15:01:15 -0700377 unique_fd worker_pipe_read;
Luis Hector Chavez7300d832018-04-04 10:13:25 -0700378 if (!android::base::Pipe(&worker_pipe_read, &worker_pipe_write)) {
Josh Gaobc055ca2017-03-29 15:01:15 -0700379 err(1, "failed to create pipe");
380 }
381
Luis Hector Chavez7300d832018-04-04 10:13:25 -0700382 // 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 Gaobc055ca2017-03-29 15:01:15 -0700387
388 worker = fork();
389 if (worker == -1) {
390 err(1, "failed to fork worker");
391 } else if (worker == 0) {
392 char buf;
Luis Hector Chavez7300d832018-04-04 10:13:25 -0700393 // 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 Gaobc055ca2017-03-29 15:01:15 -0700404 worker_pipe_write.reset();
405 TEMP_FAILURE_RETRY(read(worker_pipe_read.get(), &buf, sizeof(buf)));
406 exit(0);
Luis Hector Chavez7300d832018-04-04 10:13:25 -0700407 } 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 Gaobc055ca2017-03-29 15:01:15 -0700412 }
Josh Gao5e3fe952017-02-16 14:12:41 -0800413 }
414
Yi Kong358603a2019-03-29 14:25:16 -0700415 ~PtraceResumptionTest() override {
Josh Gao5e3fe952017-02-16 14:12:41 -0800416 }
417
418 void AssertDeath(int signo);
Josh Gao5e3fe952017-02-16 14:12:41 -0800419
Josh Gaobc055ca2017-03-29 15:01:15 -0700420 void StartTracer(std::function<void()> f) {
421 tracer = fork();
Josh Gao5e3fe952017-02-16 14:12:41 -0800422 ASSERT_NE(-1, tracer);
423 if (tracer == 0) {
424 f();
425 if (HasFatalFailure()) {
426 exit(1);
427 }
428 exit(0);
429 }
Josh Gaobc055ca2017-03-29 15:01:15 -0700430 }
431
432 bool WaitForTracer() {
433 if (tracer == -1) {
434 errx(1, "tracer not started");
435 }
Josh Gao5e3fe952017-02-16 14:12:41 -0800436
437 int result;
Elliott Hughescabc77f2017-11-28 12:55:19 -0800438 pid_t rc = TEMP_FAILURE_RETRY(waitpid(tracer, &result, 0));
Josh Gaobc055ca2017-03-29 15:01:15 -0700439 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 Gao5e3fe952017-02-16 14:12:41 -0800449 if (WIFEXITED(result)) {
450 if (WEXITSTATUS(result) != 0) {
Josh Gaobc055ca2017-03-29 15:01:15 -0700451 printf("tracer failed\n");
452 return false;
Josh Gao5e3fe952017-02-16 14:12:41 -0800453 }
454 }
455
Josh Gaobc055ca2017-03-29 15:01:15 -0700456 return true;
457 }
458
459 bool WaitForWorker() {
460 if (worker == -1) {
461 errx(1, "worker not started");
462 }
463
464 int result;
Elliott Hughescabc77f2017-11-28 12:55:19 -0800465 pid_t rc = TEMP_FAILURE_RETRY(waitpid(worker, &result, WNOHANG));
Josh Gaobc055ca2017-03-29 15:01:15 -0700466 if (rc != 0) {
467 printf("worker exited prematurely\n");
468 return false;
469 }
Josh Gao5e3fe952017-02-16 14:12:41 -0800470
471 worker_pipe_write.reset();
472
Elliott Hughescabc77f2017-11-28 12:55:19 -0800473 rc = TEMP_FAILURE_RETRY(waitpid(worker, &result, 0));
Josh Gaobc055ca2017-03-29 15:01:15 -0700474 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 Gao5e3fe952017-02-16 14:12:41 -0800490 }
491};
492
493static 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 Gaobc055ca2017-03-29 15:01:15 -0700506TEST_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 Hughescabc77f2017-11-28 12:55:19 -0800519 ASSERT_EQ(0, TEMP_FAILURE_RETRY(waitpid(worker, &result, WNOHANG)));
Josh Gaobc055ca2017-03-29 15:01:15 -0700520 ASSERT_TRUE(WaitForTracer());
Elliott Hughescabc77f2017-11-28 12:55:19 -0800521 ASSERT_EQ(worker, TEMP_FAILURE_RETRY(waitpid(worker, &result, 0)));
Josh Gaobc055ca2017-03-29 15:01:15 -0700522}
523
Josh Gao5e3fe952017-02-16 14:12:41 -0800524TEST_F(PtraceResumptionTest, seize) {
Josh Gaobc055ca2017-03-29 15:01:15 -0700525 StartTracer([this]() { ASSERT_EQ(0, ptrace(PTRACE_SEIZE, worker, 0, 0)) << strerror(errno); });
526 ASSERT_TRUE(WaitForTracer());
527 ASSERT_TRUE(WaitForWorker());
Josh Gao5e3fe952017-02-16 14:12:41 -0800528}
529
530TEST_F(PtraceResumptionTest, seize_interrupt) {
Josh Gaobc055ca2017-03-29 15:01:15 -0700531 StartTracer([this]() {
Josh Gao5e3fe952017-02-16 14:12:41 -0800532 ASSERT_EQ(0, ptrace(PTRACE_SEIZE, worker, 0, 0)) << strerror(errno);
533 ASSERT_EQ(0, ptrace(PTRACE_INTERRUPT, worker, 0, 0)) << strerror(errno);
Josh Gaobc055ca2017-03-29 15:01:15 -0700534 wait_for_ptrace_stop(worker);
Josh Gao5e3fe952017-02-16 14:12:41 -0800535 });
Josh Gaobc055ca2017-03-29 15:01:15 -0700536 ASSERT_TRUE(WaitForTracer());
537 ASSERT_TRUE(WaitForWorker());
Josh Gao5e3fe952017-02-16 14:12:41 -0800538}
539
540TEST_F(PtraceResumptionTest, seize_interrupt_cont) {
Josh Gaobc055ca2017-03-29 15:01:15 -0700541 StartTracer([this]() {
Josh Gao5e3fe952017-02-16 14:12:41 -0800542 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 Gaobc055ca2017-03-29 15:01:15 -0700547 ASSERT_TRUE(WaitForTracer());
548 ASSERT_TRUE(WaitForWorker());
549}
550
551TEST_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
557TEST_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
567TEST_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 Gao5e3fe952017-02-16 14:12:41 -0800576}