blob: 83fd93be250d277ad6517f8fefaad72cffeb0a8e [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
Josh Gaobc055ca2017-03-29 15:01:15 -070038using namespace std::chrono_literals;
39
Josh Gao5e3fe952017-02-16 14:12:41 -080040using android::base::unique_fd;
41
Pavel Labath1faca6c2016-04-21 15:13:22 +010042// Host libc does not define this.
43#ifndef TRAP_HWBKPT
44#define TRAP_HWBKPT 4
45#endif
46
Pavel Labath1faca6c2016-04-21 15:13:22 +010047class ChildGuard {
48 public:
Chih-Hung Hsieh62e3a072016-05-03 12:08:05 -070049 explicit ChildGuard(pid_t pid) : pid(pid) {}
Pavel Labath1faca6c2016-04-21 15:13:22 +010050
51 ~ChildGuard() {
52 kill(pid, SIGKILL);
53 int status;
Elliott Hughescabc77f2017-11-28 12:55:19 -080054 TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
Pavel Labath1faca6c2016-04-21 15:13:22 +010055 }
56
57 private:
58 pid_t pid;
59};
60
Pavel Labathfb082ee2017-01-23 15:41:35 +000061enum class HwFeature { Watchpoint, Breakpoint };
62
63static bool is_hw_feature_supported(pid_t child, HwFeature feature) {
Pavel Labath1faca6c2016-04-21 15:13:22 +010064#if defined(__arm__)
65 long capabilities;
66 long result = ptrace(PTRACE_GETHBPREGS, child, 0, &capabilities);
67 if (result == -1) {
68 EXPECT_EQ(EIO, errno);
Pavel Labath95d8fb12017-07-07 11:42:34 +010069 GTEST_LOG_(INFO) << "Hardware debug support disabled at kernel configuration time.";
Pavel Labath1faca6c2016-04-21 15:13:22 +010070 return false;
71 }
Pavel Labath95d8fb12017-07-07 11:42:34 +010072 uint8_t hb_count = capabilities & 0xff;
73 capabilities >>= 8;
74 uint8_t wp_count = capabilities & 0xff;
75 capabilities >>= 8;
76 uint8_t max_wp_size = capabilities & 0xff;
77 if (max_wp_size == 0) {
78 GTEST_LOG_(INFO)
79 << "Kernel reports zero maximum watchpoint size. Hardware debug support missing.";
80 return false;
Pavel Labathfb082ee2017-01-23 15:41:35 +000081 }
Pavel Labath95d8fb12017-07-07 11:42:34 +010082 if (feature == HwFeature::Watchpoint && wp_count == 0) {
83 GTEST_LOG_(INFO) << "Kernel reports zero hardware watchpoints";
84 return false;
85 }
86 if (feature == HwFeature::Breakpoint && hb_count == 0) {
87 GTEST_LOG_(INFO) << "Kernel reports zero hardware breakpoints";
88 return false;
89 }
90 return true;
Pavel Labath1faca6c2016-04-21 15:13:22 +010091#elif defined(__aarch64__)
92 user_hwdebug_state dreg_state;
93 iovec iov;
94 iov.iov_base = &dreg_state;
95 iov.iov_len = sizeof(dreg_state);
96
Pavel Labathfb082ee2017-01-23 15:41:35 +000097 long result = ptrace(PTRACE_GETREGSET, child,
98 feature == HwFeature::Watchpoint ? NT_ARM_HW_WATCH : NT_ARM_HW_BREAK, &iov);
Pavel Labath1faca6c2016-04-21 15:13:22 +010099 if (result == -1) {
100 EXPECT_EQ(EINVAL, errno);
101 return false;
102 }
103 return (dreg_state.dbg_info & 0xff) > 0;
104#elif defined(__i386__) || defined(__x86_64__)
Pavel Labathfb082ee2017-01-23 15:41:35 +0000105 // We assume watchpoints and breakpoints are always supported on x86.
Pavel Labath3dad8d52017-02-22 18:22:46 +0000106 UNUSED(child);
107 UNUSED(feature);
Pavel Labath1faca6c2016-04-21 15:13:22 +0100108 return true;
109#else
110 // TODO: mips support.
Pavel Labath3dad8d52017-02-22 18:22:46 +0000111 UNUSED(child);
112 UNUSED(feature);
Pavel Labath1faca6c2016-04-21 15:13:22 +0100113 return false;
114#endif
115}
116
Pavel Labath3dad8d52017-02-22 18:22:46 +0000117static void set_watchpoint(pid_t child, uintptr_t address, size_t size) {
118 ASSERT_EQ(0u, address & 0x7) << "address: " << address;
Pavel Labath1faca6c2016-04-21 15:13:22 +0100119#if defined(__arm__) || defined(__aarch64__)
120 const unsigned byte_mask = (1 << size) - 1;
121 const unsigned type = 2; // Write.
122 const unsigned enable = 1;
123 const unsigned control = byte_mask << 5 | type << 3 | enable;
124
125#ifdef __arm__
126 ASSERT_EQ(0, ptrace(PTRACE_SETHBPREGS, child, -1, &address)) << strerror(errno);
127 ASSERT_EQ(0, ptrace(PTRACE_SETHBPREGS, child, -2, &control)) << strerror(errno);
128#else // aarch64
129 user_hwdebug_state dreg_state;
130 memset(&dreg_state, 0, sizeof dreg_state);
Pavel Labath3dad8d52017-02-22 18:22:46 +0000131 dreg_state.dbg_regs[0].addr = address;
Pavel Labath1faca6c2016-04-21 15:13:22 +0100132 dreg_state.dbg_regs[0].ctrl = control;
133
134 iovec iov;
135 iov.iov_base = &dreg_state;
136 iov.iov_len = offsetof(user_hwdebug_state, dbg_regs) + sizeof(dreg_state.dbg_regs[0]);
137
138 ASSERT_EQ(0, ptrace(PTRACE_SETREGSET, child, NT_ARM_HW_WATCH, &iov)) << strerror(errno);
139#endif
140#elif defined(__i386__) || defined(__x86_64__)
141 ASSERT_EQ(0, ptrace(PTRACE_POKEUSER, child, offsetof(user, u_debugreg[0]), address)) << strerror(errno);
142 errno = 0;
143 unsigned data = ptrace(PTRACE_PEEKUSER, child, offsetof(user, u_debugreg[7]), nullptr);
144 ASSERT_EQ(0, errno);
145
146 const unsigned size_flag = (size == 8) ? 2 : size - 1;
147 const unsigned enable = 1;
148 const unsigned type = 1; // Write.
149
150 const unsigned mask = 3 << 18 | 3 << 16 | 1;
151 const unsigned value = size_flag << 18 | type << 16 | enable;
152 data &= mask;
153 data |= value;
154 ASSERT_EQ(0, ptrace(PTRACE_POKEUSER, child, offsetof(user, u_debugreg[7]), data)) << strerror(errno);
155#else
Pavel Labath3dad8d52017-02-22 18:22:46 +0000156 UNUSED(child);
157 UNUSED(address);
158 UNUSED(size);
Pavel Labath1faca6c2016-04-21 15:13:22 +0100159#endif
160}
161
Pavel Labath3dad8d52017-02-22 18:22:46 +0000162template <typename T>
163static void run_watchpoint_test(std::function<void(T&)> child_func, size_t offset, size_t size) {
164 alignas(16) T data{};
Pavel Labath1faca6c2016-04-21 15:13:22 +0100165
166 pid_t child = fork();
167 ASSERT_NE(-1, child) << strerror(errno);
Pavel Labath3dad8d52017-02-22 18:22:46 +0000168 if (child == 0) {
169 // Extra precaution: make sure we go away if anything happens to our parent.
170 if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0) == -1) {
171 perror("prctl(PR_SET_PDEATHSIG)");
172 _exit(1);
173 }
174
175 if (ptrace(PTRACE_TRACEME, 0, nullptr, nullptr) == -1) {
176 perror("ptrace(PTRACE_TRACEME)");
177 _exit(2);
178 }
179
180 child_func(data);
181 _exit(0);
182 }
Pavel Labath1faca6c2016-04-21 15:13:22 +0100183
184 ChildGuard guard(child);
185
186 int status;
Elliott Hughescabc77f2017-11-28 12:55:19 -0800187 ASSERT_EQ(child, TEMP_FAILURE_RETRY(waitpid(child, &status, __WALL))) << strerror(errno);
Pavel Labath1faca6c2016-04-21 15:13:22 +0100188 ASSERT_TRUE(WIFSTOPPED(status)) << "Status was: " << status;
189 ASSERT_EQ(SIGSTOP, WSTOPSIG(status)) << "Status was: " << status;
190
Pavel Labathfb082ee2017-01-23 15:41:35 +0000191 if (!is_hw_feature_supported(child, HwFeature::Watchpoint)) {
192 GTEST_LOG_(INFO) << "Skipping test because hardware support is not available.\n";
193 return;
194 }
Pavel Labath1faca6c2016-04-21 15:13:22 +0100195
Pavel Labath3dad8d52017-02-22 18:22:46 +0000196 set_watchpoint(child, uintptr_t(&data) + offset, size);
Pavel Labath1faca6c2016-04-21 15:13:22 +0100197
198 ASSERT_EQ(0, ptrace(PTRACE_CONT, child, nullptr, nullptr)) << strerror(errno);
Elliott Hughescabc77f2017-11-28 12:55:19 -0800199 ASSERT_EQ(child, TEMP_FAILURE_RETRY(waitpid(child, &status, __WALL))) << strerror(errno);
Pavel Labath1faca6c2016-04-21 15:13:22 +0100200 ASSERT_TRUE(WIFSTOPPED(status)) << "Status was: " << status;
201 ASSERT_EQ(SIGTRAP, WSTOPSIG(status)) << "Status was: " << status;
202
203 siginfo_t siginfo;
204 ASSERT_EQ(0, ptrace(PTRACE_GETSIGINFO, child, nullptr, &siginfo)) << strerror(errno);
205 ASSERT_EQ(TRAP_HWBKPT, siginfo.si_code);
206#if defined(__arm__) || defined(__aarch64__)
Pavel Labath3dad8d52017-02-22 18:22:46 +0000207 ASSERT_LE(&data, siginfo.si_addr);
208 ASSERT_GT((&data) + 1, siginfo.si_addr);
Pavel Labath1faca6c2016-04-21 15:13:22 +0100209#endif
210}
211
Pavel Labath3dad8d52017-02-22 18:22:46 +0000212template <typename T>
213static void watchpoint_stress_child(unsigned cpu, T& data) {
214 cpu_set_t cpus;
215 CPU_ZERO(&cpus);
216 CPU_SET(cpu, &cpus);
217 if (sched_setaffinity(0, sizeof cpus, &cpus) == -1) {
218 perror("sched_setaffinity");
219 _exit(3);
220 }
221 raise(SIGSTOP); // Synchronize with the tracer, let it set the watchpoint.
222
223 data = 1; // Now trigger the watchpoint.
224}
225
226template <typename T>
227static void run_watchpoint_stress(size_t cpu) {
228 run_watchpoint_test<T>(std::bind(watchpoint_stress_child<T>, cpu, std::placeholders::_1), 0,
229 sizeof(T));
Pavel Labath1faca6c2016-04-21 15:13:22 +0100230}
231
232// Test watchpoint API. The test is considered successful if our watchpoints get hit OR the
233// system reports that watchpoint support is not present. We run the test for different
234// watchpoint sizes, while pinning the process to each cpu in turn, for better coverage.
Pavel Labathfb082ee2017-01-23 15:41:35 +0000235TEST(sys_ptrace, watchpoint_stress) {
Pavel Labath1faca6c2016-04-21 15:13:22 +0100236 cpu_set_t available_cpus;
237 ASSERT_EQ(0, sched_getaffinity(0, sizeof available_cpus, &available_cpus));
238
239 for (size_t cpu = 0; cpu < CPU_SETSIZE; ++cpu) {
240 if (!CPU_ISSET(cpu, &available_cpus)) continue;
Pavel Labath3dad8d52017-02-22 18:22:46 +0000241
242 run_watchpoint_stress<uint8_t>(cpu);
243 run_watchpoint_stress<uint16_t>(cpu);
244 run_watchpoint_stress<uint32_t>(cpu);
245#if defined(__LP64__)
246 run_watchpoint_stress<uint64_t>(cpu);
247#endif
Pavel Labath1faca6c2016-04-21 15:13:22 +0100248 }
249}
Pavel Labathfb082ee2017-01-23 15:41:35 +0000250
Pavel Labath3dad8d52017-02-22 18:22:46 +0000251struct Uint128_t {
252 uint64_t data[2];
253};
254static void watchpoint_imprecise_child(Uint128_t& data) {
255 raise(SIGSTOP); // Synchronize with the tracer, let it set the watchpoint.
256
257#if defined(__i386__) || defined(__x86_64__)
258 asm volatile("movdqa %%xmm0, %0" : : "m"(data));
259#elif defined(__arm__)
260 asm volatile("stm %0, { r0, r1, r2, r3 }" : : "r"(&data));
261#elif defined(__aarch64__)
262 asm volatile("stp x0, x1, %0" : : "m"(data));
263#elif defined(__mips__)
264// TODO
Pavel Labathfb5a6392017-02-24 10:14:13 +0000265 UNUSED(data);
Pavel Labath3dad8d52017-02-22 18:22:46 +0000266#endif
267}
268
269// Test that the kernel is able to handle the case when the instruction writes
270// to a larger block of memory than the one we are watching. If you see this
271// test fail on arm64, you will likely need to cherry-pick fdfeff0f into your
272// kernel.
273TEST(sys_ptrace, watchpoint_imprecise) {
Yabin Cui143b4542017-11-29 10:34:24 -0800274 // This test relies on the infrastructure to timeout if the test hangs.
Pavel Labath4a620262017-04-26 11:30:06 +0100275 run_watchpoint_test<Uint128_t>(watchpoint_imprecise_child, 8, sizeof(void*));
Pavel Labath3dad8d52017-02-22 18:22:46 +0000276}
277
Pavel Labathfb082ee2017-01-23 15:41:35 +0000278static void __attribute__((noinline)) breakpoint_func() {
279 asm volatile("");
280}
281
282static void __attribute__((noreturn)) breakpoint_fork_child() {
283 // Extra precaution: make sure we go away if anything happens to our parent.
284 if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0) == -1) {
285 perror("prctl(PR_SET_PDEATHSIG)");
286 _exit(1);
287 }
288
289 if (ptrace(PTRACE_TRACEME, 0, nullptr, nullptr) == -1) {
290 perror("ptrace(PTRACE_TRACEME)");
291 _exit(2);
292 }
293
294 raise(SIGSTOP); // Synchronize with the tracer, let it set the breakpoint.
295
296 breakpoint_func(); // Now trigger the breakpoint.
297
298 _exit(0);
299}
300
301static void set_breakpoint(pid_t child) {
302 uintptr_t address = uintptr_t(breakpoint_func);
303#if defined(__arm__) || defined(__aarch64__)
304 address &= ~3;
305 const unsigned byte_mask = 0xf;
306 const unsigned enable = 1;
307 const unsigned control = byte_mask << 5 | enable;
308
309#ifdef __arm__
310 ASSERT_EQ(0, ptrace(PTRACE_SETHBPREGS, child, 1, &address)) << strerror(errno);
311 ASSERT_EQ(0, ptrace(PTRACE_SETHBPREGS, child, 2, &control)) << strerror(errno);
312#else // aarch64
313 user_hwdebug_state dreg_state;
314 memset(&dreg_state, 0, sizeof dreg_state);
315 dreg_state.dbg_regs[0].addr = reinterpret_cast<uintptr_t>(address);
316 dreg_state.dbg_regs[0].ctrl = control;
317
318 iovec iov;
319 iov.iov_base = &dreg_state;
320 iov.iov_len = offsetof(user_hwdebug_state, dbg_regs) + sizeof(dreg_state.dbg_regs[0]);
321
322 ASSERT_EQ(0, ptrace(PTRACE_SETREGSET, child, NT_ARM_HW_BREAK, &iov)) << strerror(errno);
323#endif
324#elif defined(__i386__) || defined(__x86_64__)
325 ASSERT_EQ(0, ptrace(PTRACE_POKEUSER, child, offsetof(user, u_debugreg[0]), address))
326 << strerror(errno);
327 errno = 0;
328 unsigned data = ptrace(PTRACE_PEEKUSER, child, offsetof(user, u_debugreg[7]), nullptr);
329 ASSERT_EQ(0, errno);
330
331 const unsigned size = 0;
332 const unsigned enable = 1;
333 const unsigned type = 0; // Execute
334
335 const unsigned mask = 3 << 18 | 3 << 16 | 1;
336 const unsigned value = size << 18 | type << 16 | enable;
337 data &= mask;
338 data |= value;
339 ASSERT_EQ(0, ptrace(PTRACE_POKEUSER, child, offsetof(user, u_debugreg[7]), data))
340 << strerror(errno);
341#else
Pavel Labath3dad8d52017-02-22 18:22:46 +0000342 UNUSED(child);
343 UNUSED(address);
Pavel Labathfb082ee2017-01-23 15:41:35 +0000344#endif
345}
346
347// Test hardware breakpoint API. The test is considered successful if the breakpoints get hit OR the
348// system reports that hardware breakpoint support is not present.
349TEST(sys_ptrace, hardware_breakpoint) {
350 pid_t child = fork();
351 ASSERT_NE(-1, child) << strerror(errno);
352 if (child == 0) breakpoint_fork_child();
353
354 ChildGuard guard(child);
355
356 int status;
Elliott Hughescabc77f2017-11-28 12:55:19 -0800357 ASSERT_EQ(child, TEMP_FAILURE_RETRY(waitpid(child, &status, __WALL))) << strerror(errno);
Pavel Labathfb082ee2017-01-23 15:41:35 +0000358 ASSERT_TRUE(WIFSTOPPED(status)) << "Status was: " << status;
359 ASSERT_EQ(SIGSTOP, WSTOPSIG(status)) << "Status was: " << status;
360
361 if (!is_hw_feature_supported(child, HwFeature::Breakpoint)) {
362 GTEST_LOG_(INFO) << "Skipping test because hardware support is not available.\n";
363 return;
364 }
365
366 set_breakpoint(child);
367
368 ASSERT_EQ(0, ptrace(PTRACE_CONT, child, nullptr, nullptr)) << strerror(errno);
Elliott Hughescabc77f2017-11-28 12:55:19 -0800369 ASSERT_EQ(child, TEMP_FAILURE_RETRY(waitpid(child, &status, __WALL))) << strerror(errno);
Pavel Labathfb082ee2017-01-23 15:41:35 +0000370 ASSERT_TRUE(WIFSTOPPED(status)) << "Status was: " << status;
371 ASSERT_EQ(SIGTRAP, WSTOPSIG(status)) << "Status was: " << status;
372
373 siginfo_t siginfo;
374 ASSERT_EQ(0, ptrace(PTRACE_GETSIGINFO, child, nullptr, &siginfo)) << strerror(errno);
375 ASSERT_EQ(TRAP_HWBKPT, siginfo.si_code);
376}
Josh Gao5e3fe952017-02-16 14:12:41 -0800377
378class PtraceResumptionTest : public ::testing::Test {
379 public:
Josh Gaobc055ca2017-03-29 15:01:15 -0700380 unique_fd worker_pipe_write;
381
Josh Gao5e3fe952017-02-16 14:12:41 -0800382 pid_t worker = -1;
Josh Gaobc055ca2017-03-29 15:01:15 -0700383 pid_t tracer = -1;
384
Josh Gao5e3fe952017-02-16 14:12:41 -0800385 PtraceResumptionTest() {
Josh Gaobc055ca2017-03-29 15:01:15 -0700386 unique_fd worker_pipe_read;
Luis Hector Chavez7300d832018-04-04 10:13:25 -0700387 if (!android::base::Pipe(&worker_pipe_read, &worker_pipe_write)) {
Josh Gaobc055ca2017-03-29 15:01:15 -0700388 err(1, "failed to create pipe");
389 }
390
Luis Hector Chavez7300d832018-04-04 10:13:25 -0700391 // Second pipe to synchronize the Yama ptracer setup.
392 unique_fd worker_pipe_setup_read, worker_pipe_setup_write;
393 if (!android::base::Pipe(&worker_pipe_setup_read, &worker_pipe_setup_write)) {
394 err(1, "failed to create pipe");
395 }
Josh Gaobc055ca2017-03-29 15:01:15 -0700396
397 worker = fork();
398 if (worker == -1) {
399 err(1, "failed to fork worker");
400 } else if (worker == 0) {
401 char buf;
Luis Hector Chavez7300d832018-04-04 10:13:25 -0700402 // Allow the tracer process, which is not a direct process ancestor, to
403 // be able to use ptrace(2) on this process when Yama LSM is active.
404 if (prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY, 0, 0, 0) == -1) {
405 // if Yama is off prctl(PR_SET_PTRACER) returns EINVAL - don't log in this
406 // case since it's expected behaviour.
407 if (errno != EINVAL) {
408 err(1, "prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY) failed for pid %d", getpid());
409 }
410 }
411 worker_pipe_setup_write.reset();
412
Josh Gaobc055ca2017-03-29 15:01:15 -0700413 worker_pipe_write.reset();
414 TEMP_FAILURE_RETRY(read(worker_pipe_read.get(), &buf, sizeof(buf)));
415 exit(0);
Luis Hector Chavez7300d832018-04-04 10:13:25 -0700416 } else {
417 // Wait until the Yama ptracer is setup.
418 char buf;
419 worker_pipe_setup_write.reset();
420 TEMP_FAILURE_RETRY(read(worker_pipe_setup_read.get(), &buf, sizeof(buf)));
Josh Gaobc055ca2017-03-29 15:01:15 -0700421 }
Josh Gao5e3fe952017-02-16 14:12:41 -0800422 }
423
424 ~PtraceResumptionTest() {
425 }
426
427 void AssertDeath(int signo);
Josh Gao5e3fe952017-02-16 14:12:41 -0800428
Josh Gaobc055ca2017-03-29 15:01:15 -0700429 void StartTracer(std::function<void()> f) {
430 tracer = fork();
Josh Gao5e3fe952017-02-16 14:12:41 -0800431 ASSERT_NE(-1, tracer);
432 if (tracer == 0) {
433 f();
434 if (HasFatalFailure()) {
435 exit(1);
436 }
437 exit(0);
438 }
Josh Gaobc055ca2017-03-29 15:01:15 -0700439 }
440
441 bool WaitForTracer() {
442 if (tracer == -1) {
443 errx(1, "tracer not started");
444 }
Josh Gao5e3fe952017-02-16 14:12:41 -0800445
446 int result;
Elliott Hughescabc77f2017-11-28 12:55:19 -0800447 pid_t rc = TEMP_FAILURE_RETRY(waitpid(tracer, &result, 0));
Josh Gaobc055ca2017-03-29 15:01:15 -0700448 if (rc != tracer) {
449 printf("waitpid returned %d (%s)\n", rc, strerror(errno));
450 return false;
451 }
452
453 if (!WIFEXITED(result) && !WIFSIGNALED(result)) {
454 printf("!WIFEXITED && !WIFSIGNALED\n");
455 return false;
456 }
457
Josh Gao5e3fe952017-02-16 14:12:41 -0800458 if (WIFEXITED(result)) {
459 if (WEXITSTATUS(result) != 0) {
Josh Gaobc055ca2017-03-29 15:01:15 -0700460 printf("tracer failed\n");
461 return false;
Josh Gao5e3fe952017-02-16 14:12:41 -0800462 }
463 }
464
Josh Gaobc055ca2017-03-29 15:01:15 -0700465 return true;
466 }
467
468 bool WaitForWorker() {
469 if (worker == -1) {
470 errx(1, "worker not started");
471 }
472
473 int result;
Elliott Hughescabc77f2017-11-28 12:55:19 -0800474 pid_t rc = TEMP_FAILURE_RETRY(waitpid(worker, &result, WNOHANG));
Josh Gaobc055ca2017-03-29 15:01:15 -0700475 if (rc != 0) {
476 printf("worker exited prematurely\n");
477 return false;
478 }
Josh Gao5e3fe952017-02-16 14:12:41 -0800479
480 worker_pipe_write.reset();
481
Elliott Hughescabc77f2017-11-28 12:55:19 -0800482 rc = TEMP_FAILURE_RETRY(waitpid(worker, &result, 0));
Josh Gaobc055ca2017-03-29 15:01:15 -0700483 if (rc != worker) {
484 printf("waitpid for worker returned %d (%s)\n", rc, strerror(errno));
485 return false;
486 }
487
488 if (!WIFEXITED(result)) {
489 printf("worker didn't exit\n");
490 return false;
491 }
492
493 if (WEXITSTATUS(result) != 0) {
494 printf("worker exited with status %d\n", WEXITSTATUS(result));
495 return false;
496 }
497
498 return true;
Josh Gao5e3fe952017-02-16 14:12:41 -0800499 }
500};
501
502static void wait_for_ptrace_stop(pid_t pid) {
503 while (true) {
504 int status;
505 pid_t rc = TEMP_FAILURE_RETRY(waitpid(pid, &status, __WALL));
506 if (rc != pid) {
507 abort();
508 }
509 if (WIFSTOPPED(status)) {
510 return;
511 }
512 }
513}
514
Josh Gaobc055ca2017-03-29 15:01:15 -0700515TEST_F(PtraceResumptionTest, smoke) {
516 // Make sure that the worker doesn't exit before the tracer stops tracing.
517 StartTracer([this]() {
518 ASSERT_EQ(0, ptrace(PTRACE_SEIZE, worker, 0, 0)) << strerror(errno);
519 ASSERT_EQ(0, ptrace(PTRACE_INTERRUPT, worker, 0, 0)) << strerror(errno);
520 wait_for_ptrace_stop(worker);
521 std::this_thread::sleep_for(500ms);
522 });
523
524 worker_pipe_write.reset();
525 std::this_thread::sleep_for(250ms);
526
527 int result;
Elliott Hughescabc77f2017-11-28 12:55:19 -0800528 ASSERT_EQ(0, TEMP_FAILURE_RETRY(waitpid(worker, &result, WNOHANG)));
Josh Gaobc055ca2017-03-29 15:01:15 -0700529 ASSERT_TRUE(WaitForTracer());
Elliott Hughescabc77f2017-11-28 12:55:19 -0800530 ASSERT_EQ(worker, TEMP_FAILURE_RETRY(waitpid(worker, &result, 0)));
Josh Gaobc055ca2017-03-29 15:01:15 -0700531}
532
Josh Gao5e3fe952017-02-16 14:12:41 -0800533TEST_F(PtraceResumptionTest, seize) {
Josh Gaobc055ca2017-03-29 15:01:15 -0700534 StartTracer([this]() { ASSERT_EQ(0, ptrace(PTRACE_SEIZE, worker, 0, 0)) << strerror(errno); });
535 ASSERT_TRUE(WaitForTracer());
536 ASSERT_TRUE(WaitForWorker());
Josh Gao5e3fe952017-02-16 14:12:41 -0800537}
538
539TEST_F(PtraceResumptionTest, seize_interrupt) {
Josh Gaobc055ca2017-03-29 15:01:15 -0700540 StartTracer([this]() {
Josh Gao5e3fe952017-02-16 14:12:41 -0800541 ASSERT_EQ(0, ptrace(PTRACE_SEIZE, worker, 0, 0)) << strerror(errno);
542 ASSERT_EQ(0, ptrace(PTRACE_INTERRUPT, worker, 0, 0)) << strerror(errno);
Josh Gaobc055ca2017-03-29 15:01:15 -0700543 wait_for_ptrace_stop(worker);
Josh Gao5e3fe952017-02-16 14:12:41 -0800544 });
Josh Gaobc055ca2017-03-29 15:01:15 -0700545 ASSERT_TRUE(WaitForTracer());
546 ASSERT_TRUE(WaitForWorker());
Josh Gao5e3fe952017-02-16 14:12:41 -0800547}
548
549TEST_F(PtraceResumptionTest, seize_interrupt_cont) {
Josh Gaobc055ca2017-03-29 15:01:15 -0700550 StartTracer([this]() {
Josh Gao5e3fe952017-02-16 14:12:41 -0800551 ASSERT_EQ(0, ptrace(PTRACE_SEIZE, worker, 0, 0)) << strerror(errno);
552 ASSERT_EQ(0, ptrace(PTRACE_INTERRUPT, worker, 0, 0)) << strerror(errno);
553 wait_for_ptrace_stop(worker);
554 ASSERT_EQ(0, ptrace(PTRACE_CONT, worker, 0, 0)) << strerror(errno);
555 });
Josh Gaobc055ca2017-03-29 15:01:15 -0700556 ASSERT_TRUE(WaitForTracer());
557 ASSERT_TRUE(WaitForWorker());
558}
559
560TEST_F(PtraceResumptionTest, zombie_seize) {
561 StartTracer([this]() { ASSERT_EQ(0, ptrace(PTRACE_SEIZE, worker, 0, 0)) << strerror(errno); });
562 ASSERT_TRUE(WaitForWorker());
563 ASSERT_TRUE(WaitForTracer());
564}
565
566TEST_F(PtraceResumptionTest, zombie_seize_interrupt) {
567 StartTracer([this]() {
568 ASSERT_EQ(0, ptrace(PTRACE_SEIZE, worker, 0, 0)) << strerror(errno);
569 ASSERT_EQ(0, ptrace(PTRACE_INTERRUPT, worker, 0, 0)) << strerror(errno);
570 wait_for_ptrace_stop(worker);
571 });
572 ASSERT_TRUE(WaitForWorker());
573 ASSERT_TRUE(WaitForTracer());
574}
575
576TEST_F(PtraceResumptionTest, zombie_seize_interrupt_cont) {
577 StartTracer([this]() {
578 ASSERT_EQ(0, ptrace(PTRACE_SEIZE, worker, 0, 0)) << strerror(errno);
579 ASSERT_EQ(0, ptrace(PTRACE_INTERRUPT, worker, 0, 0)) << strerror(errno);
580 wait_for_ptrace_stop(worker);
581 ASSERT_EQ(0, ptrace(PTRACE_CONT, worker, 0, 0)) << strerror(errno);
582 });
583 ASSERT_TRUE(WaitForWorker());
584 ASSERT_TRUE(WaitForTracer());
Josh Gao5e3fe952017-02-16 14:12:41 -0800585}