blob: 04dcd4ed220c27e075586f981b614497d5efcc13 [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
65static bool is_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);
Pavel Labath95d8fb12017-07-07 11:42:34 +010071 GTEST_LOG_(INFO) << "Hardware debug support disabled at kernel configuration time.";
Pavel Labath1faca6c2016-04-21 15:13:22 +010072 return false;
73 }
Pavel Labath95d8fb12017-07-07 11:42:34 +010074 uint8_t hb_count = capabilities & 0xff;
75 capabilities >>= 8;
76 uint8_t wp_count = capabilities & 0xff;
77 capabilities >>= 8;
78 uint8_t max_wp_size = capabilities & 0xff;
79 if (max_wp_size == 0) {
80 GTEST_LOG_(INFO)
81 << "Kernel reports zero maximum watchpoint size. Hardware debug support missing.";
82 return false;
Pavel Labathfb082ee2017-01-23 15:41:35 +000083 }
Pavel Labath95d8fb12017-07-07 11:42:34 +010084 if (feature == HwFeature::Watchpoint && wp_count == 0) {
85 GTEST_LOG_(INFO) << "Kernel reports zero hardware watchpoints";
86 return false;
87 }
88 if (feature == HwFeature::Breakpoint && hb_count == 0) {
89 GTEST_LOG_(INFO) << "Kernel reports zero hardware breakpoints";
90 return false;
91 }
92 return true;
Pavel Labath1faca6c2016-04-21 15:13:22 +010093#elif defined(__aarch64__)
94 user_hwdebug_state dreg_state;
95 iovec iov;
96 iov.iov_base = &dreg_state;
97 iov.iov_len = sizeof(dreg_state);
98
Pavel Labathfb082ee2017-01-23 15:41:35 +000099 long result = ptrace(PTRACE_GETREGSET, child,
100 feature == HwFeature::Watchpoint ? NT_ARM_HW_WATCH : NT_ARM_HW_BREAK, &iov);
Pavel Labath1faca6c2016-04-21 15:13:22 +0100101 if (result == -1) {
102 EXPECT_EQ(EINVAL, errno);
103 return false;
104 }
105 return (dreg_state.dbg_info & 0xff) > 0;
106#elif defined(__i386__) || defined(__x86_64__)
Pavel Labathfb082ee2017-01-23 15:41:35 +0000107 // We assume watchpoints and breakpoints are always supported on x86.
Pavel Labath3dad8d52017-02-22 18:22:46 +0000108 UNUSED(child);
109 UNUSED(feature);
Pavel Labath1faca6c2016-04-21 15:13:22 +0100110 return true;
111#else
112 // TODO: mips support.
Pavel Labath3dad8d52017-02-22 18:22:46 +0000113 UNUSED(child);
114 UNUSED(feature);
Pavel Labath1faca6c2016-04-21 15:13:22 +0100115 return false;
116#endif
117}
118
Pavel Labath3dad8d52017-02-22 18:22:46 +0000119static void set_watchpoint(pid_t child, uintptr_t address, size_t size) {
120 ASSERT_EQ(0u, address & 0x7) << "address: " << address;
Pavel Labath1faca6c2016-04-21 15:13:22 +0100121#if defined(__arm__) || defined(__aarch64__)
122 const unsigned byte_mask = (1 << size) - 1;
123 const unsigned type = 2; // Write.
124 const unsigned enable = 1;
125 const unsigned control = byte_mask << 5 | type << 3 | enable;
126
127#ifdef __arm__
128 ASSERT_EQ(0, ptrace(PTRACE_SETHBPREGS, child, -1, &address)) << strerror(errno);
129 ASSERT_EQ(0, ptrace(PTRACE_SETHBPREGS, child, -2, &control)) << strerror(errno);
130#else // aarch64
131 user_hwdebug_state dreg_state;
132 memset(&dreg_state, 0, sizeof dreg_state);
Pavel Labath3dad8d52017-02-22 18:22:46 +0000133 dreg_state.dbg_regs[0].addr = address;
Pavel Labath1faca6c2016-04-21 15:13:22 +0100134 dreg_state.dbg_regs[0].ctrl = control;
135
136 iovec iov;
137 iov.iov_base = &dreg_state;
138 iov.iov_len = offsetof(user_hwdebug_state, dbg_regs) + sizeof(dreg_state.dbg_regs[0]);
139
140 ASSERT_EQ(0, ptrace(PTRACE_SETREGSET, child, NT_ARM_HW_WATCH, &iov)) << strerror(errno);
141#endif
142#elif defined(__i386__) || defined(__x86_64__)
143 ASSERT_EQ(0, ptrace(PTRACE_POKEUSER, child, offsetof(user, u_debugreg[0]), address)) << strerror(errno);
144 errno = 0;
145 unsigned data = ptrace(PTRACE_PEEKUSER, child, offsetof(user, u_debugreg[7]), nullptr);
146 ASSERT_EQ(0, errno);
147
148 const unsigned size_flag = (size == 8) ? 2 : size - 1;
149 const unsigned enable = 1;
150 const unsigned type = 1; // Write.
151
152 const unsigned mask = 3 << 18 | 3 << 16 | 1;
153 const unsigned value = size_flag << 18 | type << 16 | enable;
154 data &= mask;
155 data |= value;
156 ASSERT_EQ(0, ptrace(PTRACE_POKEUSER, child, offsetof(user, u_debugreg[7]), data)) << strerror(errno);
157#else
Pavel Labath3dad8d52017-02-22 18:22:46 +0000158 UNUSED(child);
159 UNUSED(address);
160 UNUSED(size);
Pavel Labath1faca6c2016-04-21 15:13:22 +0100161#endif
162}
163
Pavel Labath3dad8d52017-02-22 18:22:46 +0000164template <typename T>
165static void run_watchpoint_test(std::function<void(T&)> child_func, size_t offset, size_t size) {
166 alignas(16) T data{};
Pavel Labath1faca6c2016-04-21 15:13:22 +0100167
168 pid_t child = fork();
169 ASSERT_NE(-1, child) << strerror(errno);
Pavel Labath3dad8d52017-02-22 18:22:46 +0000170 if (child == 0) {
171 // Extra precaution: make sure we go away if anything happens to our parent.
172 if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0) == -1) {
173 perror("prctl(PR_SET_PDEATHSIG)");
174 _exit(1);
175 }
176
177 if (ptrace(PTRACE_TRACEME, 0, nullptr, nullptr) == -1) {
178 perror("ptrace(PTRACE_TRACEME)");
179 _exit(2);
180 }
181
182 child_func(data);
183 _exit(0);
184 }
Pavel Labath1faca6c2016-04-21 15:13:22 +0100185
186 ChildGuard guard(child);
187
188 int status;
Elliott Hughescabc77f2017-11-28 12:55:19 -0800189 ASSERT_EQ(child, TEMP_FAILURE_RETRY(waitpid(child, &status, __WALL))) << strerror(errno);
Pavel Labath1faca6c2016-04-21 15:13:22 +0100190 ASSERT_TRUE(WIFSTOPPED(status)) << "Status was: " << status;
191 ASSERT_EQ(SIGSTOP, WSTOPSIG(status)) << "Status was: " << status;
192
Pavel Labathfb082ee2017-01-23 15:41:35 +0000193 if (!is_hw_feature_supported(child, HwFeature::Watchpoint)) {
194 GTEST_LOG_(INFO) << "Skipping test because hardware support is not available.\n";
195 return;
196 }
Pavel Labath1faca6c2016-04-21 15:13:22 +0100197
Evgenii Stepanov7cc67062019-02-05 18:43:34 -0800198 set_watchpoint(child, uintptr_t(untag_address(&data)) + offset, size);
Pavel Labath1faca6c2016-04-21 15:13:22 +0100199
200 ASSERT_EQ(0, ptrace(PTRACE_CONT, child, nullptr, nullptr)) << strerror(errno);
Elliott Hughescabc77f2017-11-28 12:55:19 -0800201 ASSERT_EQ(child, TEMP_FAILURE_RETRY(waitpid(child, &status, __WALL))) << strerror(errno);
Pavel Labath1faca6c2016-04-21 15:13:22 +0100202 ASSERT_TRUE(WIFSTOPPED(status)) << "Status was: " << status;
203 ASSERT_EQ(SIGTRAP, WSTOPSIG(status)) << "Status was: " << status;
204
205 siginfo_t siginfo;
206 ASSERT_EQ(0, ptrace(PTRACE_GETSIGINFO, child, nullptr, &siginfo)) << strerror(errno);
207 ASSERT_EQ(TRAP_HWBKPT, siginfo.si_code);
208#if defined(__arm__) || defined(__aarch64__)
Pavel Labath3dad8d52017-02-22 18:22:46 +0000209 ASSERT_LE(&data, siginfo.si_addr);
210 ASSERT_GT((&data) + 1, siginfo.si_addr);
Pavel Labath1faca6c2016-04-21 15:13:22 +0100211#endif
212}
213
Pavel Labath3dad8d52017-02-22 18:22:46 +0000214template <typename T>
215static void watchpoint_stress_child(unsigned cpu, T& data) {
216 cpu_set_t cpus;
217 CPU_ZERO(&cpus);
218 CPU_SET(cpu, &cpus);
219 if (sched_setaffinity(0, sizeof cpus, &cpus) == -1) {
220 perror("sched_setaffinity");
221 _exit(3);
222 }
223 raise(SIGSTOP); // Synchronize with the tracer, let it set the watchpoint.
224
225 data = 1; // Now trigger the watchpoint.
226}
227
228template <typename T>
229static void run_watchpoint_stress(size_t cpu) {
230 run_watchpoint_test<T>(std::bind(watchpoint_stress_child<T>, cpu, std::placeholders::_1), 0,
231 sizeof(T));
Pavel Labath1faca6c2016-04-21 15:13:22 +0100232}
233
234// Test watchpoint API. The test is considered successful if our watchpoints get hit OR the
235// system reports that watchpoint support is not present. We run the test for different
236// watchpoint sizes, while pinning the process to each cpu in turn, for better coverage.
Pavel Labathfb082ee2017-01-23 15:41:35 +0000237TEST(sys_ptrace, watchpoint_stress) {
Pavel Labath1faca6c2016-04-21 15:13:22 +0100238 cpu_set_t available_cpus;
239 ASSERT_EQ(0, sched_getaffinity(0, sizeof available_cpus, &available_cpus));
240
241 for (size_t cpu = 0; cpu < CPU_SETSIZE; ++cpu) {
242 if (!CPU_ISSET(cpu, &available_cpus)) continue;
Pavel Labath3dad8d52017-02-22 18:22:46 +0000243
244 run_watchpoint_stress<uint8_t>(cpu);
245 run_watchpoint_stress<uint16_t>(cpu);
246 run_watchpoint_stress<uint32_t>(cpu);
247#if defined(__LP64__)
248 run_watchpoint_stress<uint64_t>(cpu);
249#endif
Pavel Labath1faca6c2016-04-21 15:13:22 +0100250 }
251}
Pavel Labathfb082ee2017-01-23 15:41:35 +0000252
Pavel Labath3dad8d52017-02-22 18:22:46 +0000253struct Uint128_t {
254 uint64_t data[2];
255};
256static void watchpoint_imprecise_child(Uint128_t& data) {
257 raise(SIGSTOP); // Synchronize with the tracer, let it set the watchpoint.
258
259#if defined(__i386__) || defined(__x86_64__)
260 asm volatile("movdqa %%xmm0, %0" : : "m"(data));
261#elif defined(__arm__)
262 asm volatile("stm %0, { r0, r1, r2, r3 }" : : "r"(&data));
263#elif defined(__aarch64__)
264 asm volatile("stp x0, x1, %0" : : "m"(data));
265#elif defined(__mips__)
266// TODO
Pavel Labathfb5a6392017-02-24 10:14:13 +0000267 UNUSED(data);
Pavel Labath3dad8d52017-02-22 18:22:46 +0000268#endif
269}
270
271// Test that the kernel is able to handle the case when the instruction writes
272// to a larger block of memory than the one we are watching. If you see this
273// test fail on arm64, you will likely need to cherry-pick fdfeff0f into your
274// kernel.
275TEST(sys_ptrace, watchpoint_imprecise) {
Yabin Cui143b4542017-11-29 10:34:24 -0800276 // This test relies on the infrastructure to timeout if the test hangs.
Pavel Labath4a620262017-04-26 11:30:06 +0100277 run_watchpoint_test<Uint128_t>(watchpoint_imprecise_child, 8, sizeof(void*));
Pavel Labath3dad8d52017-02-22 18:22:46 +0000278}
279
Pavel Labathfb082ee2017-01-23 15:41:35 +0000280static void __attribute__((noinline)) breakpoint_func() {
281 asm volatile("");
282}
283
284static void __attribute__((noreturn)) breakpoint_fork_child() {
285 // Extra precaution: make sure we go away if anything happens to our parent.
286 if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0) == -1) {
287 perror("prctl(PR_SET_PDEATHSIG)");
288 _exit(1);
289 }
290
291 if (ptrace(PTRACE_TRACEME, 0, nullptr, nullptr) == -1) {
292 perror("ptrace(PTRACE_TRACEME)");
293 _exit(2);
294 }
295
296 raise(SIGSTOP); // Synchronize with the tracer, let it set the breakpoint.
297
298 breakpoint_func(); // Now trigger the breakpoint.
299
300 _exit(0);
301}
302
303static void set_breakpoint(pid_t child) {
304 uintptr_t address = uintptr_t(breakpoint_func);
305#if defined(__arm__) || defined(__aarch64__)
306 address &= ~3;
307 const unsigned byte_mask = 0xf;
308 const unsigned enable = 1;
309 const unsigned control = byte_mask << 5 | enable;
310
311#ifdef __arm__
312 ASSERT_EQ(0, ptrace(PTRACE_SETHBPREGS, child, 1, &address)) << strerror(errno);
313 ASSERT_EQ(0, ptrace(PTRACE_SETHBPREGS, child, 2, &control)) << strerror(errno);
314#else // aarch64
315 user_hwdebug_state dreg_state;
316 memset(&dreg_state, 0, sizeof dreg_state);
317 dreg_state.dbg_regs[0].addr = reinterpret_cast<uintptr_t>(address);
318 dreg_state.dbg_regs[0].ctrl = control;
319
320 iovec iov;
321 iov.iov_base = &dreg_state;
322 iov.iov_len = offsetof(user_hwdebug_state, dbg_regs) + sizeof(dreg_state.dbg_regs[0]);
323
324 ASSERT_EQ(0, ptrace(PTRACE_SETREGSET, child, NT_ARM_HW_BREAK, &iov)) << strerror(errno);
325#endif
326#elif defined(__i386__) || defined(__x86_64__)
327 ASSERT_EQ(0, ptrace(PTRACE_POKEUSER, child, offsetof(user, u_debugreg[0]), address))
328 << strerror(errno);
329 errno = 0;
330 unsigned data = ptrace(PTRACE_PEEKUSER, child, offsetof(user, u_debugreg[7]), nullptr);
331 ASSERT_EQ(0, errno);
332
333 const unsigned size = 0;
334 const unsigned enable = 1;
335 const unsigned type = 0; // Execute
336
337 const unsigned mask = 3 << 18 | 3 << 16 | 1;
338 const unsigned value = size << 18 | type << 16 | enable;
339 data &= mask;
340 data |= value;
341 ASSERT_EQ(0, ptrace(PTRACE_POKEUSER, child, offsetof(user, u_debugreg[7]), data))
342 << strerror(errno);
343#else
Pavel Labath3dad8d52017-02-22 18:22:46 +0000344 UNUSED(child);
345 UNUSED(address);
Pavel Labathfb082ee2017-01-23 15:41:35 +0000346#endif
347}
348
349// Test hardware breakpoint API. The test is considered successful if the breakpoints get hit OR the
350// system reports that hardware breakpoint support is not present.
351TEST(sys_ptrace, hardware_breakpoint) {
352 pid_t child = fork();
353 ASSERT_NE(-1, child) << strerror(errno);
354 if (child == 0) breakpoint_fork_child();
355
356 ChildGuard guard(child);
357
358 int status;
Elliott Hughescabc77f2017-11-28 12:55:19 -0800359 ASSERT_EQ(child, TEMP_FAILURE_RETRY(waitpid(child, &status, __WALL))) << strerror(errno);
Pavel Labathfb082ee2017-01-23 15:41:35 +0000360 ASSERT_TRUE(WIFSTOPPED(status)) << "Status was: " << status;
361 ASSERT_EQ(SIGSTOP, WSTOPSIG(status)) << "Status was: " << status;
362
363 if (!is_hw_feature_supported(child, HwFeature::Breakpoint)) {
364 GTEST_LOG_(INFO) << "Skipping test because hardware support is not available.\n";
365 return;
366 }
367
368 set_breakpoint(child);
369
370 ASSERT_EQ(0, ptrace(PTRACE_CONT, child, nullptr, nullptr)) << strerror(errno);
Elliott Hughescabc77f2017-11-28 12:55:19 -0800371 ASSERT_EQ(child, TEMP_FAILURE_RETRY(waitpid(child, &status, __WALL))) << strerror(errno);
Pavel Labathfb082ee2017-01-23 15:41:35 +0000372 ASSERT_TRUE(WIFSTOPPED(status)) << "Status was: " << status;
373 ASSERT_EQ(SIGTRAP, WSTOPSIG(status)) << "Status was: " << status;
374
375 siginfo_t siginfo;
376 ASSERT_EQ(0, ptrace(PTRACE_GETSIGINFO, child, nullptr, &siginfo)) << strerror(errno);
377 ASSERT_EQ(TRAP_HWBKPT, siginfo.si_code);
378}
Josh Gao5e3fe952017-02-16 14:12:41 -0800379
380class PtraceResumptionTest : public ::testing::Test {
381 public:
Josh Gaobc055ca2017-03-29 15:01:15 -0700382 unique_fd worker_pipe_write;
383
Josh Gao5e3fe952017-02-16 14:12:41 -0800384 pid_t worker = -1;
Josh Gaobc055ca2017-03-29 15:01:15 -0700385 pid_t tracer = -1;
386
Josh Gao5e3fe952017-02-16 14:12:41 -0800387 PtraceResumptionTest() {
Josh Gaobc055ca2017-03-29 15:01:15 -0700388 unique_fd worker_pipe_read;
Luis Hector Chavez7300d832018-04-04 10:13:25 -0700389 if (!android::base::Pipe(&worker_pipe_read, &worker_pipe_write)) {
Josh Gaobc055ca2017-03-29 15:01:15 -0700390 err(1, "failed to create pipe");
391 }
392
Luis Hector Chavez7300d832018-04-04 10:13:25 -0700393 // Second pipe to synchronize the Yama ptracer setup.
394 unique_fd worker_pipe_setup_read, worker_pipe_setup_write;
395 if (!android::base::Pipe(&worker_pipe_setup_read, &worker_pipe_setup_write)) {
396 err(1, "failed to create pipe");
397 }
Josh Gaobc055ca2017-03-29 15:01:15 -0700398
399 worker = fork();
400 if (worker == -1) {
401 err(1, "failed to fork worker");
402 } else if (worker == 0) {
403 char buf;
Luis Hector Chavez7300d832018-04-04 10:13:25 -0700404 // Allow the tracer process, which is not a direct process ancestor, to
405 // be able to use ptrace(2) on this process when Yama LSM is active.
406 if (prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY, 0, 0, 0) == -1) {
407 // if Yama is off prctl(PR_SET_PTRACER) returns EINVAL - don't log in this
408 // case since it's expected behaviour.
409 if (errno != EINVAL) {
410 err(1, "prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY) failed for pid %d", getpid());
411 }
412 }
413 worker_pipe_setup_write.reset();
414
Josh Gaobc055ca2017-03-29 15:01:15 -0700415 worker_pipe_write.reset();
416 TEMP_FAILURE_RETRY(read(worker_pipe_read.get(), &buf, sizeof(buf)));
417 exit(0);
Luis Hector Chavez7300d832018-04-04 10:13:25 -0700418 } else {
419 // Wait until the Yama ptracer is setup.
420 char buf;
421 worker_pipe_setup_write.reset();
422 TEMP_FAILURE_RETRY(read(worker_pipe_setup_read.get(), &buf, sizeof(buf)));
Josh Gaobc055ca2017-03-29 15:01:15 -0700423 }
Josh Gao5e3fe952017-02-16 14:12:41 -0800424 }
425
426 ~PtraceResumptionTest() {
427 }
428
429 void AssertDeath(int signo);
Josh Gao5e3fe952017-02-16 14:12:41 -0800430
Josh Gaobc055ca2017-03-29 15:01:15 -0700431 void StartTracer(std::function<void()> f) {
432 tracer = fork();
Josh Gao5e3fe952017-02-16 14:12:41 -0800433 ASSERT_NE(-1, tracer);
434 if (tracer == 0) {
435 f();
436 if (HasFatalFailure()) {
437 exit(1);
438 }
439 exit(0);
440 }
Josh Gaobc055ca2017-03-29 15:01:15 -0700441 }
442
443 bool WaitForTracer() {
444 if (tracer == -1) {
445 errx(1, "tracer not started");
446 }
Josh Gao5e3fe952017-02-16 14:12:41 -0800447
448 int result;
Elliott Hughescabc77f2017-11-28 12:55:19 -0800449 pid_t rc = TEMP_FAILURE_RETRY(waitpid(tracer, &result, 0));
Josh Gaobc055ca2017-03-29 15:01:15 -0700450 if (rc != tracer) {
451 printf("waitpid returned %d (%s)\n", rc, strerror(errno));
452 return false;
453 }
454
455 if (!WIFEXITED(result) && !WIFSIGNALED(result)) {
456 printf("!WIFEXITED && !WIFSIGNALED\n");
457 return false;
458 }
459
Josh Gao5e3fe952017-02-16 14:12:41 -0800460 if (WIFEXITED(result)) {
461 if (WEXITSTATUS(result) != 0) {
Josh Gaobc055ca2017-03-29 15:01:15 -0700462 printf("tracer failed\n");
463 return false;
Josh Gao5e3fe952017-02-16 14:12:41 -0800464 }
465 }
466
Josh Gaobc055ca2017-03-29 15:01:15 -0700467 return true;
468 }
469
470 bool WaitForWorker() {
471 if (worker == -1) {
472 errx(1, "worker not started");
473 }
474
475 int result;
Elliott Hughescabc77f2017-11-28 12:55:19 -0800476 pid_t rc = TEMP_FAILURE_RETRY(waitpid(worker, &result, WNOHANG));
Josh Gaobc055ca2017-03-29 15:01:15 -0700477 if (rc != 0) {
478 printf("worker exited prematurely\n");
479 return false;
480 }
Josh Gao5e3fe952017-02-16 14:12:41 -0800481
482 worker_pipe_write.reset();
483
Elliott Hughescabc77f2017-11-28 12:55:19 -0800484 rc = TEMP_FAILURE_RETRY(waitpid(worker, &result, 0));
Josh Gaobc055ca2017-03-29 15:01:15 -0700485 if (rc != worker) {
486 printf("waitpid for worker returned %d (%s)\n", rc, strerror(errno));
487 return false;
488 }
489
490 if (!WIFEXITED(result)) {
491 printf("worker didn't exit\n");
492 return false;
493 }
494
495 if (WEXITSTATUS(result) != 0) {
496 printf("worker exited with status %d\n", WEXITSTATUS(result));
497 return false;
498 }
499
500 return true;
Josh Gao5e3fe952017-02-16 14:12:41 -0800501 }
502};
503
504static void wait_for_ptrace_stop(pid_t pid) {
505 while (true) {
506 int status;
507 pid_t rc = TEMP_FAILURE_RETRY(waitpid(pid, &status, __WALL));
508 if (rc != pid) {
509 abort();
510 }
511 if (WIFSTOPPED(status)) {
512 return;
513 }
514 }
515}
516
Josh Gaobc055ca2017-03-29 15:01:15 -0700517TEST_F(PtraceResumptionTest, smoke) {
518 // Make sure that the worker doesn't exit before the tracer stops tracing.
519 StartTracer([this]() {
520 ASSERT_EQ(0, ptrace(PTRACE_SEIZE, worker, 0, 0)) << strerror(errno);
521 ASSERT_EQ(0, ptrace(PTRACE_INTERRUPT, worker, 0, 0)) << strerror(errno);
522 wait_for_ptrace_stop(worker);
523 std::this_thread::sleep_for(500ms);
524 });
525
526 worker_pipe_write.reset();
527 std::this_thread::sleep_for(250ms);
528
529 int result;
Elliott Hughescabc77f2017-11-28 12:55:19 -0800530 ASSERT_EQ(0, TEMP_FAILURE_RETRY(waitpid(worker, &result, WNOHANG)));
Josh Gaobc055ca2017-03-29 15:01:15 -0700531 ASSERT_TRUE(WaitForTracer());
Elliott Hughescabc77f2017-11-28 12:55:19 -0800532 ASSERT_EQ(worker, TEMP_FAILURE_RETRY(waitpid(worker, &result, 0)));
Josh Gaobc055ca2017-03-29 15:01:15 -0700533}
534
Josh Gao5e3fe952017-02-16 14:12:41 -0800535TEST_F(PtraceResumptionTest, seize) {
Josh Gaobc055ca2017-03-29 15:01:15 -0700536 StartTracer([this]() { ASSERT_EQ(0, ptrace(PTRACE_SEIZE, worker, 0, 0)) << strerror(errno); });
537 ASSERT_TRUE(WaitForTracer());
538 ASSERT_TRUE(WaitForWorker());
Josh Gao5e3fe952017-02-16 14:12:41 -0800539}
540
541TEST_F(PtraceResumptionTest, seize_interrupt) {
Josh Gaobc055ca2017-03-29 15:01:15 -0700542 StartTracer([this]() {
Josh Gao5e3fe952017-02-16 14:12:41 -0800543 ASSERT_EQ(0, ptrace(PTRACE_SEIZE, worker, 0, 0)) << strerror(errno);
544 ASSERT_EQ(0, ptrace(PTRACE_INTERRUPT, worker, 0, 0)) << strerror(errno);
Josh Gaobc055ca2017-03-29 15:01:15 -0700545 wait_for_ptrace_stop(worker);
Josh Gao5e3fe952017-02-16 14:12:41 -0800546 });
Josh Gaobc055ca2017-03-29 15:01:15 -0700547 ASSERT_TRUE(WaitForTracer());
548 ASSERT_TRUE(WaitForWorker());
Josh Gao5e3fe952017-02-16 14:12:41 -0800549}
550
551TEST_F(PtraceResumptionTest, seize_interrupt_cont) {
Josh Gaobc055ca2017-03-29 15:01:15 -0700552 StartTracer([this]() {
Josh Gao5e3fe952017-02-16 14:12:41 -0800553 ASSERT_EQ(0, ptrace(PTRACE_SEIZE, worker, 0, 0)) << strerror(errno);
554 ASSERT_EQ(0, ptrace(PTRACE_INTERRUPT, worker, 0, 0)) << strerror(errno);
555 wait_for_ptrace_stop(worker);
556 ASSERT_EQ(0, ptrace(PTRACE_CONT, worker, 0, 0)) << strerror(errno);
557 });
Josh Gaobc055ca2017-03-29 15:01:15 -0700558 ASSERT_TRUE(WaitForTracer());
559 ASSERT_TRUE(WaitForWorker());
560}
561
562TEST_F(PtraceResumptionTest, zombie_seize) {
563 StartTracer([this]() { ASSERT_EQ(0, ptrace(PTRACE_SEIZE, worker, 0, 0)) << strerror(errno); });
564 ASSERT_TRUE(WaitForWorker());
565 ASSERT_TRUE(WaitForTracer());
566}
567
568TEST_F(PtraceResumptionTest, zombie_seize_interrupt) {
569 StartTracer([this]() {
570 ASSERT_EQ(0, ptrace(PTRACE_SEIZE, worker, 0, 0)) << strerror(errno);
571 ASSERT_EQ(0, ptrace(PTRACE_INTERRUPT, worker, 0, 0)) << strerror(errno);
572 wait_for_ptrace_stop(worker);
573 });
574 ASSERT_TRUE(WaitForWorker());
575 ASSERT_TRUE(WaitForTracer());
576}
577
578TEST_F(PtraceResumptionTest, zombie_seize_interrupt_cont) {
579 StartTracer([this]() {
580 ASSERT_EQ(0, ptrace(PTRACE_SEIZE, worker, 0, 0)) << strerror(errno);
581 ASSERT_EQ(0, ptrace(PTRACE_INTERRUPT, worker, 0, 0)) << strerror(errno);
582 wait_for_ptrace_stop(worker);
583 ASSERT_EQ(0, ptrace(PTRACE_CONT, worker, 0, 0)) << strerror(errno);
584 });
585 ASSERT_TRUE(WaitForWorker());
586 ASSERT_TRUE(WaitForTracer());
Josh Gao5e3fe952017-02-16 14:12:41 -0800587}