blob: 90539fe8cca3e37eb7de30a195382aecfc34119a [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);
Pavel Labath1faca6c2016-04-21 15:13:22 +0100179
Evgenii Stepanov7cc67062019-02-05 18:43:34 -0800180 set_watchpoint(child, uintptr_t(untag_address(&data)) + offset, size);
Pavel Labath1faca6c2016-04-21 15:13:22 +0100181
182 ASSERT_EQ(0, ptrace(PTRACE_CONT, child, nullptr, nullptr)) << strerror(errno);
Elliott Hughescabc77f2017-11-28 12:55:19 -0800183 ASSERT_EQ(child, TEMP_FAILURE_RETRY(waitpid(child, &status, __WALL))) << strerror(errno);
Pavel Labath1faca6c2016-04-21 15:13:22 +0100184 ASSERT_TRUE(WIFSTOPPED(status)) << "Status was: " << status;
185 ASSERT_EQ(SIGTRAP, WSTOPSIG(status)) << "Status was: " << status;
186
187 siginfo_t siginfo;
188 ASSERT_EQ(0, ptrace(PTRACE_GETSIGINFO, child, nullptr, &siginfo)) << strerror(errno);
189 ASSERT_EQ(TRAP_HWBKPT, siginfo.si_code);
190#if defined(__arm__) || defined(__aarch64__)
Pavel Labath3dad8d52017-02-22 18:22:46 +0000191 ASSERT_LE(&data, siginfo.si_addr);
192 ASSERT_GT((&data) + 1, siginfo.si_addr);
Pavel Labath1faca6c2016-04-21 15:13:22 +0100193#endif
194}
195
Pavel Labath3dad8d52017-02-22 18:22:46 +0000196template <typename T>
197static void watchpoint_stress_child(unsigned cpu, T& data) {
198 cpu_set_t cpus;
199 CPU_ZERO(&cpus);
200 CPU_SET(cpu, &cpus);
201 if (sched_setaffinity(0, sizeof cpus, &cpus) == -1) {
202 perror("sched_setaffinity");
203 _exit(3);
204 }
205 raise(SIGSTOP); // Synchronize with the tracer, let it set the watchpoint.
206
207 data = 1; // Now trigger the watchpoint.
208}
209
210template <typename T>
211static void run_watchpoint_stress(size_t cpu) {
212 run_watchpoint_test<T>(std::bind(watchpoint_stress_child<T>, cpu, std::placeholders::_1), 0,
213 sizeof(T));
Pavel Labath1faca6c2016-04-21 15:13:22 +0100214}
215
216// Test watchpoint API. The test is considered successful if our watchpoints get hit OR the
217// system reports that watchpoint support is not present. We run the test for different
218// watchpoint sizes, while pinning the process to each cpu in turn, for better coverage.
Pavel Labathfb082ee2017-01-23 15:41:35 +0000219TEST(sys_ptrace, watchpoint_stress) {
Pavel Labath1faca6c2016-04-21 15:13:22 +0100220 cpu_set_t available_cpus;
221 ASSERT_EQ(0, sched_getaffinity(0, sizeof available_cpus, &available_cpus));
222
223 for (size_t cpu = 0; cpu < CPU_SETSIZE; ++cpu) {
224 if (!CPU_ISSET(cpu, &available_cpus)) continue;
Pavel Labath3dad8d52017-02-22 18:22:46 +0000225
226 run_watchpoint_stress<uint8_t>(cpu);
227 run_watchpoint_stress<uint16_t>(cpu);
228 run_watchpoint_stress<uint32_t>(cpu);
229#if defined(__LP64__)
230 run_watchpoint_stress<uint64_t>(cpu);
231#endif
Pavel Labath1faca6c2016-04-21 15:13:22 +0100232 }
233}
Pavel Labathfb082ee2017-01-23 15:41:35 +0000234
Pavel Labath3dad8d52017-02-22 18:22:46 +0000235struct Uint128_t {
236 uint64_t data[2];
237};
238static void watchpoint_imprecise_child(Uint128_t& data) {
239 raise(SIGSTOP); // Synchronize with the tracer, let it set the watchpoint.
240
241#if defined(__i386__) || defined(__x86_64__)
242 asm volatile("movdqa %%xmm0, %0" : : "m"(data));
243#elif defined(__arm__)
244 asm volatile("stm %0, { r0, r1, r2, r3 }" : : "r"(&data));
245#elif defined(__aarch64__)
246 asm volatile("stp x0, x1, %0" : : "m"(data));
247#elif defined(__mips__)
248// TODO
Pavel Labathfb5a6392017-02-24 10:14:13 +0000249 UNUSED(data);
Pavel Labath3dad8d52017-02-22 18:22:46 +0000250#endif
251}
252
253// Test that the kernel is able to handle the case when the instruction writes
254// to a larger block of memory than the one we are watching. If you see this
255// test fail on arm64, you will likely need to cherry-pick fdfeff0f into your
256// kernel.
257TEST(sys_ptrace, watchpoint_imprecise) {
Yabin Cui143b4542017-11-29 10:34:24 -0800258 // This test relies on the infrastructure to timeout if the test hangs.
Pavel Labath4a620262017-04-26 11:30:06 +0100259 run_watchpoint_test<Uint128_t>(watchpoint_imprecise_child, 8, sizeof(void*));
Pavel Labath3dad8d52017-02-22 18:22:46 +0000260}
261
Pavel Labathfb082ee2017-01-23 15:41:35 +0000262static void __attribute__((noinline)) breakpoint_func() {
263 asm volatile("");
264}
265
266static void __attribute__((noreturn)) breakpoint_fork_child() {
267 // Extra precaution: make sure we go away if anything happens to our parent.
268 if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0) == -1) {
269 perror("prctl(PR_SET_PDEATHSIG)");
270 _exit(1);
271 }
272
273 if (ptrace(PTRACE_TRACEME, 0, nullptr, nullptr) == -1) {
274 perror("ptrace(PTRACE_TRACEME)");
275 _exit(2);
276 }
277
278 raise(SIGSTOP); // Synchronize with the tracer, let it set the breakpoint.
279
280 breakpoint_func(); // Now trigger the breakpoint.
281
282 _exit(0);
283}
284
285static void set_breakpoint(pid_t child) {
286 uintptr_t address = uintptr_t(breakpoint_func);
287#if defined(__arm__) || defined(__aarch64__)
288 address &= ~3;
289 const unsigned byte_mask = 0xf;
290 const unsigned enable = 1;
291 const unsigned control = byte_mask << 5 | enable;
292
293#ifdef __arm__
294 ASSERT_EQ(0, ptrace(PTRACE_SETHBPREGS, child, 1, &address)) << strerror(errno);
295 ASSERT_EQ(0, ptrace(PTRACE_SETHBPREGS, child, 2, &control)) << strerror(errno);
296#else // aarch64
297 user_hwdebug_state dreg_state;
298 memset(&dreg_state, 0, sizeof dreg_state);
299 dreg_state.dbg_regs[0].addr = reinterpret_cast<uintptr_t>(address);
300 dreg_state.dbg_regs[0].ctrl = control;
301
302 iovec iov;
303 iov.iov_base = &dreg_state;
304 iov.iov_len = offsetof(user_hwdebug_state, dbg_regs) + sizeof(dreg_state.dbg_regs[0]);
305
306 ASSERT_EQ(0, ptrace(PTRACE_SETREGSET, child, NT_ARM_HW_BREAK, &iov)) << strerror(errno);
307#endif
308#elif defined(__i386__) || defined(__x86_64__)
309 ASSERT_EQ(0, ptrace(PTRACE_POKEUSER, child, offsetof(user, u_debugreg[0]), address))
310 << strerror(errno);
311 errno = 0;
312 unsigned data = ptrace(PTRACE_PEEKUSER, child, offsetof(user, u_debugreg[7]), nullptr);
313 ASSERT_EQ(0, errno);
314
315 const unsigned size = 0;
316 const unsigned enable = 1;
317 const unsigned type = 0; // Execute
318
319 const unsigned mask = 3 << 18 | 3 << 16 | 1;
320 const unsigned value = size << 18 | type << 16 | enable;
321 data &= mask;
322 data |= value;
323 ASSERT_EQ(0, ptrace(PTRACE_POKEUSER, child, offsetof(user, u_debugreg[7]), data))
324 << strerror(errno);
325#else
Pavel Labath3dad8d52017-02-22 18:22:46 +0000326 UNUSED(child);
327 UNUSED(address);
Pavel Labathfb082ee2017-01-23 15:41:35 +0000328#endif
329}
330
331// Test hardware breakpoint API. The test is considered successful if the breakpoints get hit OR the
332// system reports that hardware breakpoint support is not present.
333TEST(sys_ptrace, hardware_breakpoint) {
334 pid_t child = fork();
335 ASSERT_NE(-1, child) << strerror(errno);
336 if (child == 0) breakpoint_fork_child();
337
338 ChildGuard guard(child);
339
340 int status;
Elliott Hughescabc77f2017-11-28 12:55:19 -0800341 ASSERT_EQ(child, TEMP_FAILURE_RETRY(waitpid(child, &status, __WALL))) << strerror(errno);
Pavel Labathfb082ee2017-01-23 15:41:35 +0000342 ASSERT_TRUE(WIFSTOPPED(status)) << "Status was: " << status;
343 ASSERT_EQ(SIGSTOP, WSTOPSIG(status)) << "Status was: " << status;
344
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800345 check_hw_feature_supported(child, HwFeature::Breakpoint);
Pavel Labathfb082ee2017-01-23 15:41:35 +0000346
347 set_breakpoint(child);
348
349 ASSERT_EQ(0, ptrace(PTRACE_CONT, child, nullptr, nullptr)) << strerror(errno);
Elliott Hughescabc77f2017-11-28 12:55:19 -0800350 ASSERT_EQ(child, TEMP_FAILURE_RETRY(waitpid(child, &status, __WALL))) << strerror(errno);
Pavel Labathfb082ee2017-01-23 15:41:35 +0000351 ASSERT_TRUE(WIFSTOPPED(status)) << "Status was: " << status;
352 ASSERT_EQ(SIGTRAP, WSTOPSIG(status)) << "Status was: " << status;
353
354 siginfo_t siginfo;
355 ASSERT_EQ(0, ptrace(PTRACE_GETSIGINFO, child, nullptr, &siginfo)) << strerror(errno);
356 ASSERT_EQ(TRAP_HWBKPT, siginfo.si_code);
357}
Josh Gao5e3fe952017-02-16 14:12:41 -0800358
359class PtraceResumptionTest : public ::testing::Test {
360 public:
Josh Gaobc055ca2017-03-29 15:01:15 -0700361 unique_fd worker_pipe_write;
362
Josh Gao5e3fe952017-02-16 14:12:41 -0800363 pid_t worker = -1;
Josh Gaobc055ca2017-03-29 15:01:15 -0700364 pid_t tracer = -1;
365
Josh Gao5e3fe952017-02-16 14:12:41 -0800366 PtraceResumptionTest() {
Josh Gaobc055ca2017-03-29 15:01:15 -0700367 unique_fd worker_pipe_read;
Luis Hector Chavez7300d832018-04-04 10:13:25 -0700368 if (!android::base::Pipe(&worker_pipe_read, &worker_pipe_write)) {
Josh Gaobc055ca2017-03-29 15:01:15 -0700369 err(1, "failed to create pipe");
370 }
371
Luis Hector Chavez7300d832018-04-04 10:13:25 -0700372 // Second pipe to synchronize the Yama ptracer setup.
373 unique_fd worker_pipe_setup_read, worker_pipe_setup_write;
374 if (!android::base::Pipe(&worker_pipe_setup_read, &worker_pipe_setup_write)) {
375 err(1, "failed to create pipe");
376 }
Josh Gaobc055ca2017-03-29 15:01:15 -0700377
378 worker = fork();
379 if (worker == -1) {
380 err(1, "failed to fork worker");
381 } else if (worker == 0) {
382 char buf;
Luis Hector Chavez7300d832018-04-04 10:13:25 -0700383 // Allow the tracer process, which is not a direct process ancestor, to
384 // be able to use ptrace(2) on this process when Yama LSM is active.
385 if (prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY, 0, 0, 0) == -1) {
386 // if Yama is off prctl(PR_SET_PTRACER) returns EINVAL - don't log in this
387 // case since it's expected behaviour.
388 if (errno != EINVAL) {
389 err(1, "prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY) failed for pid %d", getpid());
390 }
391 }
392 worker_pipe_setup_write.reset();
393
Josh Gaobc055ca2017-03-29 15:01:15 -0700394 worker_pipe_write.reset();
395 TEMP_FAILURE_RETRY(read(worker_pipe_read.get(), &buf, sizeof(buf)));
396 exit(0);
Luis Hector Chavez7300d832018-04-04 10:13:25 -0700397 } else {
398 // Wait until the Yama ptracer is setup.
399 char buf;
400 worker_pipe_setup_write.reset();
401 TEMP_FAILURE_RETRY(read(worker_pipe_setup_read.get(), &buf, sizeof(buf)));
Josh Gaobc055ca2017-03-29 15:01:15 -0700402 }
Josh Gao5e3fe952017-02-16 14:12:41 -0800403 }
404
Yi Kong358603a2019-03-29 14:25:16 -0700405 ~PtraceResumptionTest() override {
Josh Gao5e3fe952017-02-16 14:12:41 -0800406 }
407
408 void AssertDeath(int signo);
Josh Gao5e3fe952017-02-16 14:12:41 -0800409
Josh Gaobc055ca2017-03-29 15:01:15 -0700410 void StartTracer(std::function<void()> f) {
411 tracer = fork();
Josh Gao5e3fe952017-02-16 14:12:41 -0800412 ASSERT_NE(-1, tracer);
413 if (tracer == 0) {
414 f();
415 if (HasFatalFailure()) {
416 exit(1);
417 }
418 exit(0);
419 }
Josh Gaobc055ca2017-03-29 15:01:15 -0700420 }
421
422 bool WaitForTracer() {
423 if (tracer == -1) {
424 errx(1, "tracer not started");
425 }
Josh Gao5e3fe952017-02-16 14:12:41 -0800426
427 int result;
Elliott Hughescabc77f2017-11-28 12:55:19 -0800428 pid_t rc = TEMP_FAILURE_RETRY(waitpid(tracer, &result, 0));
Josh Gaobc055ca2017-03-29 15:01:15 -0700429 if (rc != tracer) {
430 printf("waitpid returned %d (%s)\n", rc, strerror(errno));
431 return false;
432 }
433
434 if (!WIFEXITED(result) && !WIFSIGNALED(result)) {
435 printf("!WIFEXITED && !WIFSIGNALED\n");
436 return false;
437 }
438
Josh Gao5e3fe952017-02-16 14:12:41 -0800439 if (WIFEXITED(result)) {
440 if (WEXITSTATUS(result) != 0) {
Josh Gaobc055ca2017-03-29 15:01:15 -0700441 printf("tracer failed\n");
442 return false;
Josh Gao5e3fe952017-02-16 14:12:41 -0800443 }
444 }
445
Josh Gaobc055ca2017-03-29 15:01:15 -0700446 return true;
447 }
448
449 bool WaitForWorker() {
450 if (worker == -1) {
451 errx(1, "worker not started");
452 }
453
454 int result;
Elliott Hughescabc77f2017-11-28 12:55:19 -0800455 pid_t rc = TEMP_FAILURE_RETRY(waitpid(worker, &result, WNOHANG));
Josh Gaobc055ca2017-03-29 15:01:15 -0700456 if (rc != 0) {
457 printf("worker exited prematurely\n");
458 return false;
459 }
Josh Gao5e3fe952017-02-16 14:12:41 -0800460
461 worker_pipe_write.reset();
462
Elliott Hughescabc77f2017-11-28 12:55:19 -0800463 rc = TEMP_FAILURE_RETRY(waitpid(worker, &result, 0));
Josh Gaobc055ca2017-03-29 15:01:15 -0700464 if (rc != worker) {
465 printf("waitpid for worker returned %d (%s)\n", rc, strerror(errno));
466 return false;
467 }
468
469 if (!WIFEXITED(result)) {
470 printf("worker didn't exit\n");
471 return false;
472 }
473
474 if (WEXITSTATUS(result) != 0) {
475 printf("worker exited with status %d\n", WEXITSTATUS(result));
476 return false;
477 }
478
479 return true;
Josh Gao5e3fe952017-02-16 14:12:41 -0800480 }
481};
482
483static void wait_for_ptrace_stop(pid_t pid) {
484 while (true) {
485 int status;
486 pid_t rc = TEMP_FAILURE_RETRY(waitpid(pid, &status, __WALL));
487 if (rc != pid) {
488 abort();
489 }
490 if (WIFSTOPPED(status)) {
491 return;
492 }
493 }
494}
495
Josh Gaobc055ca2017-03-29 15:01:15 -0700496TEST_F(PtraceResumptionTest, smoke) {
497 // Make sure that the worker doesn't exit before the tracer stops tracing.
498 StartTracer([this]() {
499 ASSERT_EQ(0, ptrace(PTRACE_SEIZE, worker, 0, 0)) << strerror(errno);
500 ASSERT_EQ(0, ptrace(PTRACE_INTERRUPT, worker, 0, 0)) << strerror(errno);
501 wait_for_ptrace_stop(worker);
502 std::this_thread::sleep_for(500ms);
503 });
504
505 worker_pipe_write.reset();
506 std::this_thread::sleep_for(250ms);
507
508 int result;
Elliott Hughescabc77f2017-11-28 12:55:19 -0800509 ASSERT_EQ(0, TEMP_FAILURE_RETRY(waitpid(worker, &result, WNOHANG)));
Josh Gaobc055ca2017-03-29 15:01:15 -0700510 ASSERT_TRUE(WaitForTracer());
Elliott Hughescabc77f2017-11-28 12:55:19 -0800511 ASSERT_EQ(worker, TEMP_FAILURE_RETRY(waitpid(worker, &result, 0)));
Josh Gaobc055ca2017-03-29 15:01:15 -0700512}
513
Josh Gao5e3fe952017-02-16 14:12:41 -0800514TEST_F(PtraceResumptionTest, seize) {
Josh Gaobc055ca2017-03-29 15:01:15 -0700515 StartTracer([this]() { ASSERT_EQ(0, ptrace(PTRACE_SEIZE, worker, 0, 0)) << strerror(errno); });
516 ASSERT_TRUE(WaitForTracer());
517 ASSERT_TRUE(WaitForWorker());
Josh Gao5e3fe952017-02-16 14:12:41 -0800518}
519
520TEST_F(PtraceResumptionTest, seize_interrupt) {
Josh Gaobc055ca2017-03-29 15:01:15 -0700521 StartTracer([this]() {
Josh Gao5e3fe952017-02-16 14:12:41 -0800522 ASSERT_EQ(0, ptrace(PTRACE_SEIZE, worker, 0, 0)) << strerror(errno);
523 ASSERT_EQ(0, ptrace(PTRACE_INTERRUPT, worker, 0, 0)) << strerror(errno);
Josh Gaobc055ca2017-03-29 15:01:15 -0700524 wait_for_ptrace_stop(worker);
Josh Gao5e3fe952017-02-16 14:12:41 -0800525 });
Josh Gaobc055ca2017-03-29 15:01:15 -0700526 ASSERT_TRUE(WaitForTracer());
527 ASSERT_TRUE(WaitForWorker());
Josh Gao5e3fe952017-02-16 14:12:41 -0800528}
529
530TEST_F(PtraceResumptionTest, seize_interrupt_cont) {
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);
534 wait_for_ptrace_stop(worker);
535 ASSERT_EQ(0, ptrace(PTRACE_CONT, worker, 0, 0)) << strerror(errno);
536 });
Josh Gaobc055ca2017-03-29 15:01:15 -0700537 ASSERT_TRUE(WaitForTracer());
538 ASSERT_TRUE(WaitForWorker());
539}
540
541TEST_F(PtraceResumptionTest, zombie_seize) {
542 StartTracer([this]() { ASSERT_EQ(0, ptrace(PTRACE_SEIZE, worker, 0, 0)) << strerror(errno); });
543 ASSERT_TRUE(WaitForWorker());
544 ASSERT_TRUE(WaitForTracer());
545}
546
547TEST_F(PtraceResumptionTest, zombie_seize_interrupt) {
548 StartTracer([this]() {
549 ASSERT_EQ(0, ptrace(PTRACE_SEIZE, worker, 0, 0)) << strerror(errno);
550 ASSERT_EQ(0, ptrace(PTRACE_INTERRUPT, worker, 0, 0)) << strerror(errno);
551 wait_for_ptrace_stop(worker);
552 });
553 ASSERT_TRUE(WaitForWorker());
554 ASSERT_TRUE(WaitForTracer());
555}
556
557TEST_F(PtraceResumptionTest, zombie_seize_interrupt_cont) {
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 ASSERT_EQ(0, ptrace(PTRACE_CONT, worker, 0, 0)) << strerror(errno);
563 });
564 ASSERT_TRUE(WaitForWorker());
565 ASSERT_TRUE(WaitForTracer());
Josh Gao5e3fe952017-02-16 14:12:41 -0800566}