blob: 1f9c2a2986474a98a907ad63fcc9a6ca1a3fe0f8 [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 +010044class ChildGuard {
45 public:
Chih-Hung Hsieh62e3a072016-05-03 12:08:05 -070046 explicit ChildGuard(pid_t pid) : pid(pid) {}
Pavel Labath1faca6c2016-04-21 15:13:22 +010047
48 ~ChildGuard() {
49 kill(pid, SIGKILL);
50 int status;
Elliott Hughescabc77f2017-11-28 12:55:19 -080051 TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
Pavel Labath1faca6c2016-04-21 15:13:22 +010052 }
53
54 private:
55 pid_t pid;
56};
57
Pavel Labathfb082ee2017-01-23 15:41:35 +000058enum class HwFeature { Watchpoint, Breakpoint };
59
Elliott Hughesbcaa4542019-03-08 15:20:23 -080060static void check_hw_feature_supported(pid_t child, HwFeature feature) {
Pavel Labath1faca6c2016-04-21 15:13:22 +010061#if defined(__arm__)
Elliott Hughes95646e62023-09-21 14:11:19 -070062 errno = 0;
Pavel Labath1faca6c2016-04-21 15:13:22 +010063 long capabilities;
64 long result = ptrace(PTRACE_GETHBPREGS, child, 0, &capabilities);
65 if (result == -1) {
Elliott Hughes95646e62023-09-21 14:11:19 -070066 EXPECT_ERRNO(EIO);
Elliott Hughesbcaa4542019-03-08 15:20:23 -080067 GTEST_SKIP() << "Hardware debug support disabled at kernel configuration time";
Pavel Labath1faca6c2016-04-21 15:13:22 +010068 }
Pavel Labath95d8fb12017-07-07 11:42:34 +010069 uint8_t hb_count = capabilities & 0xff;
70 capabilities >>= 8;
71 uint8_t wp_count = capabilities & 0xff;
72 capabilities >>= 8;
73 uint8_t max_wp_size = capabilities & 0xff;
74 if (max_wp_size == 0) {
Elliott Hughesbcaa4542019-03-08 15:20:23 -080075 GTEST_SKIP() << "Kernel reports zero maximum watchpoint size";
76 } else if (feature == HwFeature::Watchpoint && wp_count == 0) {
77 GTEST_SKIP() << "Kernel reports zero hardware watchpoints";
78 } else if (feature == HwFeature::Breakpoint && hb_count == 0) {
79 GTEST_SKIP() << "Kernel reports zero hardware breakpoints";
Pavel Labathfb082ee2017-01-23 15:41:35 +000080 }
Pavel Labath1faca6c2016-04-21 15:13:22 +010081#elif defined(__aarch64__)
82 user_hwdebug_state dreg_state;
83 iovec iov;
84 iov.iov_base = &dreg_state;
85 iov.iov_len = sizeof(dreg_state);
86
Elliott Hughes95646e62023-09-21 14:11:19 -070087 errno = 0;
Pavel Labathfb082ee2017-01-23 15:41:35 +000088 long result = ptrace(PTRACE_GETREGSET, child,
89 feature == HwFeature::Watchpoint ? NT_ARM_HW_WATCH : NT_ARM_HW_BREAK, &iov);
Pavel Labath1faca6c2016-04-21 15:13:22 +010090 if (result == -1) {
Elliott Hughes95646e62023-09-21 14:11:19 -070091 ASSERT_ERRNO(EINVAL);
Evgeny Eltsinbd1c6302019-12-11 15:30:16 +010092 GTEST_SKIP() << "Hardware support missing";
93 } else if ((dreg_state.dbg_info & 0xff) == 0) {
94 if (feature == HwFeature::Watchpoint) {
95 GTEST_SKIP() << "Kernel reports zero hardware watchpoints";
96 } else {
97 GTEST_SKIP() << "Kernel reports zero hardware breakpoints";
98 }
Pavel Labath1faca6c2016-04-21 15:13:22 +010099 }
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800100#else
Pavel Labathfb082ee2017-01-23 15:41:35 +0000101 // We assume watchpoints and breakpoints are always supported on x86.
Pavel Labath3dad8d52017-02-22 18:22:46 +0000102 UNUSED(child);
103 UNUSED(feature);
Pavel Labath1faca6c2016-04-21 15:13:22 +0100104#endif
105}
106
Pavel Labath3dad8d52017-02-22 18:22:46 +0000107static void set_watchpoint(pid_t child, uintptr_t address, size_t size) {
108 ASSERT_EQ(0u, address & 0x7) << "address: " << address;
Pavel Labath1faca6c2016-04-21 15:13:22 +0100109#if defined(__arm__) || defined(__aarch64__)
110 const unsigned byte_mask = (1 << size) - 1;
111 const unsigned type = 2; // Write.
112 const unsigned enable = 1;
113 const unsigned control = byte_mask << 5 | type << 3 | enable;
114
115#ifdef __arm__
116 ASSERT_EQ(0, ptrace(PTRACE_SETHBPREGS, child, -1, &address)) << strerror(errno);
117 ASSERT_EQ(0, ptrace(PTRACE_SETHBPREGS, child, -2, &control)) << strerror(errno);
118#else // aarch64
Christopher Ferris2a391882024-12-19 13:44:35 -0800119 user_hwdebug_state dreg_state = {};
Pavel Labath3dad8d52017-02-22 18:22:46 +0000120 dreg_state.dbg_regs[0].addr = address;
Pavel Labath1faca6c2016-04-21 15:13:22 +0100121 dreg_state.dbg_regs[0].ctrl = control;
122
123 iovec iov;
124 iov.iov_base = &dreg_state;
125 iov.iov_len = offsetof(user_hwdebug_state, dbg_regs) + sizeof(dreg_state.dbg_regs[0]);
126
127 ASSERT_EQ(0, ptrace(PTRACE_SETREGSET, child, NT_ARM_HW_WATCH, &iov)) << strerror(errno);
128#endif
129#elif defined(__i386__) || defined(__x86_64__)
130 ASSERT_EQ(0, ptrace(PTRACE_POKEUSER, child, offsetof(user, u_debugreg[0]), address)) << strerror(errno);
131 errno = 0;
132 unsigned data = ptrace(PTRACE_PEEKUSER, child, offsetof(user, u_debugreg[7]), nullptr);
Elliott Hughes95646e62023-09-21 14:11:19 -0700133 ASSERT_ERRNO(0);
Pavel Labath1faca6c2016-04-21 15:13:22 +0100134
135 const unsigned size_flag = (size == 8) ? 2 : size - 1;
136 const unsigned enable = 1;
137 const unsigned type = 1; // Write.
138
139 const unsigned mask = 3 << 18 | 3 << 16 | 1;
140 const unsigned value = size_flag << 18 | type << 16 | enable;
141 data &= mask;
142 data |= value;
143 ASSERT_EQ(0, ptrace(PTRACE_POKEUSER, child, offsetof(user, u_debugreg[7]), data)) << strerror(errno);
144#else
Pavel Labath3dad8d52017-02-22 18:22:46 +0000145 UNUSED(child);
146 UNUSED(address);
147 UNUSED(size);
Pavel Labath1faca6c2016-04-21 15:13:22 +0100148#endif
149}
150
Pavel Labath3dad8d52017-02-22 18:22:46 +0000151template <typename T>
152static void run_watchpoint_test(std::function<void(T&)> child_func, size_t offset, size_t size) {
153 alignas(16) T data{};
Pavel Labath1faca6c2016-04-21 15:13:22 +0100154
155 pid_t child = fork();
156 ASSERT_NE(-1, child) << strerror(errno);
Pavel Labath3dad8d52017-02-22 18:22:46 +0000157 if (child == 0) {
158 // Extra precaution: make sure we go away if anything happens to our parent.
159 if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0) == -1) {
160 perror("prctl(PR_SET_PDEATHSIG)");
161 _exit(1);
162 }
163
164 if (ptrace(PTRACE_TRACEME, 0, nullptr, nullptr) == -1) {
165 perror("ptrace(PTRACE_TRACEME)");
166 _exit(2);
167 }
168
169 child_func(data);
170 _exit(0);
171 }
Pavel Labath1faca6c2016-04-21 15:13:22 +0100172
173 ChildGuard guard(child);
174
175 int status;
Elliott Hughescabc77f2017-11-28 12:55:19 -0800176 ASSERT_EQ(child, TEMP_FAILURE_RETRY(waitpid(child, &status, __WALL))) << strerror(errno);
Pavel Labath1faca6c2016-04-21 15:13:22 +0100177 ASSERT_TRUE(WIFSTOPPED(status)) << "Status was: " << status;
178 ASSERT_EQ(SIGSTOP, WSTOPSIG(status)) << "Status was: " << status;
179
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800180 check_hw_feature_supported(child, HwFeature::Watchpoint);
Christopher Ferris103b9982019-09-23 09:03:10 -0700181 if (::testing::Test::IsSkipped()) {
182 return;
183 }
Pavel Labath1faca6c2016-04-21 15:13:22 +0100184
Evgenii Stepanov7cc67062019-02-05 18:43:34 -0800185 set_watchpoint(child, uintptr_t(untag_address(&data)) + offset, size);
Pavel Labath1faca6c2016-04-21 15:13:22 +0100186
187 ASSERT_EQ(0, ptrace(PTRACE_CONT, child, nullptr, nullptr)) << strerror(errno);
Elliott Hughescabc77f2017-11-28 12:55:19 -0800188 ASSERT_EQ(child, TEMP_FAILURE_RETRY(waitpid(child, &status, __WALL))) << strerror(errno);
Pavel Labath1faca6c2016-04-21 15:13:22 +0100189 ASSERT_TRUE(WIFSTOPPED(status)) << "Status was: " << status;
190 ASSERT_EQ(SIGTRAP, WSTOPSIG(status)) << "Status was: " << status;
191
192 siginfo_t siginfo;
193 ASSERT_EQ(0, ptrace(PTRACE_GETSIGINFO, child, nullptr, &siginfo)) << strerror(errno);
194 ASSERT_EQ(TRAP_HWBKPT, siginfo.si_code);
195#if defined(__arm__) || defined(__aarch64__)
Pavel Labath3dad8d52017-02-22 18:22:46 +0000196 ASSERT_LE(&data, siginfo.si_addr);
197 ASSERT_GT((&data) + 1, siginfo.si_addr);
Pavel Labath1faca6c2016-04-21 15:13:22 +0100198#endif
199}
200
Pavel Labath3dad8d52017-02-22 18:22:46 +0000201template <typename T>
202static void watchpoint_stress_child(unsigned cpu, T& data) {
203 cpu_set_t cpus;
204 CPU_ZERO(&cpus);
205 CPU_SET(cpu, &cpus);
206 if (sched_setaffinity(0, sizeof cpus, &cpus) == -1) {
207 perror("sched_setaffinity");
208 _exit(3);
209 }
210 raise(SIGSTOP); // Synchronize with the tracer, let it set the watchpoint.
211
212 data = 1; // Now trigger the watchpoint.
213}
214
215template <typename T>
216static void run_watchpoint_stress(size_t cpu) {
217 run_watchpoint_test<T>(std::bind(watchpoint_stress_child<T>, cpu, std::placeholders::_1), 0,
218 sizeof(T));
Pavel Labath1faca6c2016-04-21 15:13:22 +0100219}
220
221// Test watchpoint API. The test is considered successful if our watchpoints get hit OR the
222// system reports that watchpoint support is not present. We run the test for different
223// watchpoint sizes, while pinning the process to each cpu in turn, for better coverage.
Pavel Labathfb082ee2017-01-23 15:41:35 +0000224TEST(sys_ptrace, watchpoint_stress) {
Pavel Labath1faca6c2016-04-21 15:13:22 +0100225 cpu_set_t available_cpus;
226 ASSERT_EQ(0, sched_getaffinity(0, sizeof available_cpus, &available_cpus));
227
228 for (size_t cpu = 0; cpu < CPU_SETSIZE; ++cpu) {
229 if (!CPU_ISSET(cpu, &available_cpus)) continue;
Pavel Labath3dad8d52017-02-22 18:22:46 +0000230
231 run_watchpoint_stress<uint8_t>(cpu);
Christopher Ferris103b9982019-09-23 09:03:10 -0700232 if (::testing::Test::IsSkipped()) {
233 // Only check first case, since all others would skip for same reason.
234 return;
235 }
Pavel Labath3dad8d52017-02-22 18:22:46 +0000236 run_watchpoint_stress<uint16_t>(cpu);
237 run_watchpoint_stress<uint32_t>(cpu);
238#if defined(__LP64__)
239 run_watchpoint_stress<uint64_t>(cpu);
240#endif
Pavel Labath1faca6c2016-04-21 15:13:22 +0100241 }
242}
Pavel Labathfb082ee2017-01-23 15:41:35 +0000243
Pavel Labath3dad8d52017-02-22 18:22:46 +0000244struct Uint128_t {
245 uint64_t data[2];
246};
247static void watchpoint_imprecise_child(Uint128_t& data) {
248 raise(SIGSTOP); // Synchronize with the tracer, let it set the watchpoint.
249
250#if defined(__i386__) || defined(__x86_64__)
251 asm volatile("movdqa %%xmm0, %0" : : "m"(data));
252#elif defined(__arm__)
253 asm volatile("stm %0, { r0, r1, r2, r3 }" : : "r"(&data));
254#elif defined(__aarch64__)
255 asm volatile("stp x0, x1, %0" : : "m"(data));
Elliott Hughes89719df2022-11-11 22:54:02 +0000256#elif defined(__riscv)
257 UNUSED(data);
258 GTEST_LOG_(INFO) << "missing riscv64 instruction to store > 64 bits in one instruction";
Pavel Labath3dad8d52017-02-22 18:22:46 +0000259#endif
260}
261
262// Test that the kernel is able to handle the case when the instruction writes
263// to a larger block of memory than the one we are watching. If you see this
264// test fail on arm64, you will likely need to cherry-pick fdfeff0f into your
265// kernel.
266TEST(sys_ptrace, watchpoint_imprecise) {
Yabin Cui143b4542017-11-29 10:34:24 -0800267 // This test relies on the infrastructure to timeout if the test hangs.
Pavel Labath4a620262017-04-26 11:30:06 +0100268 run_watchpoint_test<Uint128_t>(watchpoint_imprecise_child, 8, sizeof(void*));
Pavel Labath3dad8d52017-02-22 18:22:46 +0000269}
270
Pavel Labathfb082ee2017-01-23 15:41:35 +0000271static void __attribute__((noinline)) breakpoint_func() {
272 asm volatile("");
273}
274
275static void __attribute__((noreturn)) breakpoint_fork_child() {
276 // Extra precaution: make sure we go away if anything happens to our parent.
277 if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0) == -1) {
278 perror("prctl(PR_SET_PDEATHSIG)");
279 _exit(1);
280 }
281
282 if (ptrace(PTRACE_TRACEME, 0, nullptr, nullptr) == -1) {
283 perror("ptrace(PTRACE_TRACEME)");
284 _exit(2);
285 }
286
287 raise(SIGSTOP); // Synchronize with the tracer, let it set the breakpoint.
288
289 breakpoint_func(); // Now trigger the breakpoint.
290
291 _exit(0);
292}
293
294static void set_breakpoint(pid_t child) {
295 uintptr_t address = uintptr_t(breakpoint_func);
296#if defined(__arm__) || defined(__aarch64__)
297 address &= ~3;
298 const unsigned byte_mask = 0xf;
299 const unsigned enable = 1;
300 const unsigned control = byte_mask << 5 | enable;
301
302#ifdef __arm__
303 ASSERT_EQ(0, ptrace(PTRACE_SETHBPREGS, child, 1, &address)) << strerror(errno);
304 ASSERT_EQ(0, ptrace(PTRACE_SETHBPREGS, child, 2, &control)) << strerror(errno);
305#else // aarch64
Christopher Ferris2a391882024-12-19 13:44:35 -0800306 user_hwdebug_state dreg_state = {};
Pavel Labathfb082ee2017-01-23 15:41:35 +0000307 dreg_state.dbg_regs[0].addr = reinterpret_cast<uintptr_t>(address);
308 dreg_state.dbg_regs[0].ctrl = control;
309
310 iovec iov;
311 iov.iov_base = &dreg_state;
312 iov.iov_len = offsetof(user_hwdebug_state, dbg_regs) + sizeof(dreg_state.dbg_regs[0]);
313
314 ASSERT_EQ(0, ptrace(PTRACE_SETREGSET, child, NT_ARM_HW_BREAK, &iov)) << strerror(errno);
315#endif
316#elif defined(__i386__) || defined(__x86_64__)
317 ASSERT_EQ(0, ptrace(PTRACE_POKEUSER, child, offsetof(user, u_debugreg[0]), address))
318 << strerror(errno);
319 errno = 0;
320 unsigned data = ptrace(PTRACE_PEEKUSER, child, offsetof(user, u_debugreg[7]), nullptr);
Elliott Hughes95646e62023-09-21 14:11:19 -0700321 ASSERT_ERRNO(0);
Pavel Labathfb082ee2017-01-23 15:41:35 +0000322
323 const unsigned size = 0;
324 const unsigned enable = 1;
325 const unsigned type = 0; // Execute
326
327 const unsigned mask = 3 << 18 | 3 << 16 | 1;
328 const unsigned value = size << 18 | type << 16 | enable;
329 data &= mask;
330 data |= value;
331 ASSERT_EQ(0, ptrace(PTRACE_POKEUSER, child, offsetof(user, u_debugreg[7]), data))
332 << strerror(errno);
333#else
Pavel Labath3dad8d52017-02-22 18:22:46 +0000334 UNUSED(child);
335 UNUSED(address);
Pavel Labathfb082ee2017-01-23 15:41:35 +0000336#endif
337}
338
339// Test hardware breakpoint API. The test is considered successful if the breakpoints get hit OR the
340// system reports that hardware breakpoint support is not present.
341TEST(sys_ptrace, hardware_breakpoint) {
342 pid_t child = fork();
343 ASSERT_NE(-1, child) << strerror(errno);
344 if (child == 0) breakpoint_fork_child();
345
346 ChildGuard guard(child);
347
348 int status;
Elliott Hughescabc77f2017-11-28 12:55:19 -0800349 ASSERT_EQ(child, TEMP_FAILURE_RETRY(waitpid(child, &status, __WALL))) << strerror(errno);
Pavel Labathfb082ee2017-01-23 15:41:35 +0000350 ASSERT_TRUE(WIFSTOPPED(status)) << "Status was: " << status;
351 ASSERT_EQ(SIGSTOP, WSTOPSIG(status)) << "Status was: " << status;
352
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800353 check_hw_feature_supported(child, HwFeature::Breakpoint);
Christopher Ferris103b9982019-09-23 09:03:10 -0700354 if (::testing::Test::IsSkipped()) {
355 return;
356 }
Pavel Labathfb082ee2017-01-23 15:41:35 +0000357
358 set_breakpoint(child);
359
360 ASSERT_EQ(0, ptrace(PTRACE_CONT, child, nullptr, nullptr)) << strerror(errno);
Elliott Hughescabc77f2017-11-28 12:55:19 -0800361 ASSERT_EQ(child, TEMP_FAILURE_RETRY(waitpid(child, &status, __WALL))) << strerror(errno);
Pavel Labathfb082ee2017-01-23 15:41:35 +0000362 ASSERT_TRUE(WIFSTOPPED(status)) << "Status was: " << status;
363 ASSERT_EQ(SIGTRAP, WSTOPSIG(status)) << "Status was: " << status;
364
365 siginfo_t siginfo;
366 ASSERT_EQ(0, ptrace(PTRACE_GETSIGINFO, child, nullptr, &siginfo)) << strerror(errno);
367 ASSERT_EQ(TRAP_HWBKPT, siginfo.si_code);
368}
Josh Gao5e3fe952017-02-16 14:12:41 -0800369
370class PtraceResumptionTest : public ::testing::Test {
371 public:
Josh Gaobc055ca2017-03-29 15:01:15 -0700372 unique_fd worker_pipe_write;
373
Josh Gao5e3fe952017-02-16 14:12:41 -0800374 pid_t worker = -1;
Josh Gaobc055ca2017-03-29 15:01:15 -0700375 pid_t tracer = -1;
376
Josh Gao5e3fe952017-02-16 14:12:41 -0800377 PtraceResumptionTest() {
Josh Gaobc055ca2017-03-29 15:01:15 -0700378 unique_fd worker_pipe_read;
Luis Hector Chavez7300d832018-04-04 10:13:25 -0700379 if (!android::base::Pipe(&worker_pipe_read, &worker_pipe_write)) {
Josh Gaobc055ca2017-03-29 15:01:15 -0700380 err(1, "failed to create pipe");
381 }
382
Luis Hector Chavez7300d832018-04-04 10:13:25 -0700383 // Second pipe to synchronize the Yama ptracer setup.
384 unique_fd worker_pipe_setup_read, worker_pipe_setup_write;
385 if (!android::base::Pipe(&worker_pipe_setup_read, &worker_pipe_setup_write)) {
386 err(1, "failed to create pipe");
387 }
Josh Gaobc055ca2017-03-29 15:01:15 -0700388
389 worker = fork();
390 if (worker == -1) {
391 err(1, "failed to fork worker");
392 } else if (worker == 0) {
393 char buf;
Luis Hector Chavez7300d832018-04-04 10:13:25 -0700394 // Allow the tracer process, which is not a direct process ancestor, to
395 // be able to use ptrace(2) on this process when Yama LSM is active.
396 if (prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY, 0, 0, 0) == -1) {
397 // if Yama is off prctl(PR_SET_PTRACER) returns EINVAL - don't log in this
398 // case since it's expected behaviour.
399 if (errno != EINVAL) {
400 err(1, "prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY) failed for pid %d", getpid());
401 }
402 }
403 worker_pipe_setup_write.reset();
404
Josh Gaobc055ca2017-03-29 15:01:15 -0700405 worker_pipe_write.reset();
406 TEMP_FAILURE_RETRY(read(worker_pipe_read.get(), &buf, sizeof(buf)));
407 exit(0);
Luis Hector Chavez7300d832018-04-04 10:13:25 -0700408 } else {
409 // Wait until the Yama ptracer is setup.
410 char buf;
411 worker_pipe_setup_write.reset();
412 TEMP_FAILURE_RETRY(read(worker_pipe_setup_read.get(), &buf, sizeof(buf)));
Josh Gaobc055ca2017-03-29 15:01:15 -0700413 }
Josh Gao5e3fe952017-02-16 14:12:41 -0800414 }
415
Yi Kong358603a2019-03-29 14:25:16 -0700416 ~PtraceResumptionTest() override {
Josh Gao5e3fe952017-02-16 14:12:41 -0800417 }
418
419 void AssertDeath(int signo);
Josh Gao5e3fe952017-02-16 14:12:41 -0800420
Josh Gaobc055ca2017-03-29 15:01:15 -0700421 void StartTracer(std::function<void()> f) {
422 tracer = fork();
Josh Gao5e3fe952017-02-16 14:12:41 -0800423 ASSERT_NE(-1, tracer);
424 if (tracer == 0) {
425 f();
426 if (HasFatalFailure()) {
427 exit(1);
428 }
429 exit(0);
430 }
Josh Gaobc055ca2017-03-29 15:01:15 -0700431 }
432
433 bool WaitForTracer() {
434 if (tracer == -1) {
435 errx(1, "tracer not started");
436 }
Josh Gao5e3fe952017-02-16 14:12:41 -0800437
438 int result;
Elliott Hughescabc77f2017-11-28 12:55:19 -0800439 pid_t rc = TEMP_FAILURE_RETRY(waitpid(tracer, &result, 0));
Josh Gaobc055ca2017-03-29 15:01:15 -0700440 if (rc != tracer) {
441 printf("waitpid returned %d (%s)\n", rc, strerror(errno));
442 return false;
443 }
444
445 if (!WIFEXITED(result) && !WIFSIGNALED(result)) {
446 printf("!WIFEXITED && !WIFSIGNALED\n");
447 return false;
448 }
449
Josh Gao5e3fe952017-02-16 14:12:41 -0800450 if (WIFEXITED(result)) {
451 if (WEXITSTATUS(result) != 0) {
Josh Gaobc055ca2017-03-29 15:01:15 -0700452 printf("tracer failed\n");
453 return false;
Josh Gao5e3fe952017-02-16 14:12:41 -0800454 }
455 }
456
Josh Gaobc055ca2017-03-29 15:01:15 -0700457 return true;
458 }
459
460 bool WaitForWorker() {
461 if (worker == -1) {
462 errx(1, "worker not started");
463 }
464
465 int result;
Elliott Hughescabc77f2017-11-28 12:55:19 -0800466 pid_t rc = TEMP_FAILURE_RETRY(waitpid(worker, &result, WNOHANG));
Josh Gaobc055ca2017-03-29 15:01:15 -0700467 if (rc != 0) {
468 printf("worker exited prematurely\n");
469 return false;
470 }
Josh Gao5e3fe952017-02-16 14:12:41 -0800471
472 worker_pipe_write.reset();
473
Elliott Hughescabc77f2017-11-28 12:55:19 -0800474 rc = TEMP_FAILURE_RETRY(waitpid(worker, &result, 0));
Josh Gaobc055ca2017-03-29 15:01:15 -0700475 if (rc != worker) {
476 printf("waitpid for worker returned %d (%s)\n", rc, strerror(errno));
477 return false;
478 }
479
480 if (!WIFEXITED(result)) {
481 printf("worker didn't exit\n");
482 return false;
483 }
484
485 if (WEXITSTATUS(result) != 0) {
486 printf("worker exited with status %d\n", WEXITSTATUS(result));
487 return false;
488 }
489
490 return true;
Josh Gao5e3fe952017-02-16 14:12:41 -0800491 }
492};
493
494static void wait_for_ptrace_stop(pid_t pid) {
495 while (true) {
496 int status;
497 pid_t rc = TEMP_FAILURE_RETRY(waitpid(pid, &status, __WALL));
498 if (rc != pid) {
499 abort();
500 }
501 if (WIFSTOPPED(status)) {
502 return;
503 }
504 }
505}
506
Josh Gaobc055ca2017-03-29 15:01:15 -0700507TEST_F(PtraceResumptionTest, smoke) {
508 // Make sure that the worker doesn't exit before the tracer stops tracing.
509 StartTracer([this]() {
510 ASSERT_EQ(0, ptrace(PTRACE_SEIZE, worker, 0, 0)) << strerror(errno);
511 ASSERT_EQ(0, ptrace(PTRACE_INTERRUPT, worker, 0, 0)) << strerror(errno);
512 wait_for_ptrace_stop(worker);
513 std::this_thread::sleep_for(500ms);
514 });
515
516 worker_pipe_write.reset();
517 std::this_thread::sleep_for(250ms);
518
519 int result;
Elliott Hughescabc77f2017-11-28 12:55:19 -0800520 ASSERT_EQ(0, TEMP_FAILURE_RETRY(waitpid(worker, &result, WNOHANG)));
Josh Gaobc055ca2017-03-29 15:01:15 -0700521 ASSERT_TRUE(WaitForTracer());
Elliott Hughescabc77f2017-11-28 12:55:19 -0800522 ASSERT_EQ(worker, TEMP_FAILURE_RETRY(waitpid(worker, &result, 0)));
Josh Gaobc055ca2017-03-29 15:01:15 -0700523}
524
Josh Gao5e3fe952017-02-16 14:12:41 -0800525TEST_F(PtraceResumptionTest, seize) {
Josh Gaobc055ca2017-03-29 15:01:15 -0700526 StartTracer([this]() { ASSERT_EQ(0, ptrace(PTRACE_SEIZE, worker, 0, 0)) << strerror(errno); });
527 ASSERT_TRUE(WaitForTracer());
528 ASSERT_TRUE(WaitForWorker());
Josh Gao5e3fe952017-02-16 14:12:41 -0800529}
530
531TEST_F(PtraceResumptionTest, seize_interrupt) {
Josh Gaobc055ca2017-03-29 15:01:15 -0700532 StartTracer([this]() {
Josh Gao5e3fe952017-02-16 14:12:41 -0800533 ASSERT_EQ(0, ptrace(PTRACE_SEIZE, worker, 0, 0)) << strerror(errno);
534 ASSERT_EQ(0, ptrace(PTRACE_INTERRUPT, worker, 0, 0)) << strerror(errno);
Josh Gaobc055ca2017-03-29 15:01:15 -0700535 wait_for_ptrace_stop(worker);
Josh Gao5e3fe952017-02-16 14:12:41 -0800536 });
Josh Gaobc055ca2017-03-29 15:01:15 -0700537 ASSERT_TRUE(WaitForTracer());
538 ASSERT_TRUE(WaitForWorker());
Josh Gao5e3fe952017-02-16 14:12:41 -0800539}
540
541TEST_F(PtraceResumptionTest, seize_interrupt_cont) {
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);
545 wait_for_ptrace_stop(worker);
546 ASSERT_EQ(0, ptrace(PTRACE_CONT, worker, 0, 0)) << strerror(errno);
547 });
Josh Gaobc055ca2017-03-29 15:01:15 -0700548 ASSERT_TRUE(WaitForTracer());
549 ASSERT_TRUE(WaitForWorker());
550}
551
552TEST_F(PtraceResumptionTest, zombie_seize) {
553 StartTracer([this]() { ASSERT_EQ(0, ptrace(PTRACE_SEIZE, worker, 0, 0)) << strerror(errno); });
554 ASSERT_TRUE(WaitForWorker());
555 ASSERT_TRUE(WaitForTracer());
556}
557
558TEST_F(PtraceResumptionTest, zombie_seize_interrupt) {
559 StartTracer([this]() {
560 ASSERT_EQ(0, ptrace(PTRACE_SEIZE, worker, 0, 0)) << strerror(errno);
561 ASSERT_EQ(0, ptrace(PTRACE_INTERRUPT, worker, 0, 0)) << strerror(errno);
562 wait_for_ptrace_stop(worker);
563 });
564 ASSERT_TRUE(WaitForWorker());
565 ASSERT_TRUE(WaitForTracer());
566}
567
568TEST_F(PtraceResumptionTest, zombie_seize_interrupt_cont) {
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 ASSERT_EQ(0, ptrace(PTRACE_CONT, worker, 0, 0)) << strerror(errno);
574 });
575 ASSERT_TRUE(WaitForWorker());
576 ASSERT_TRUE(WaitForTracer());
Josh Gao5e3fe952017-02-16 14:12:41 -0800577}