blob: 939f4d2576b8d0f76735743314f787f835c175ad [file] [log] [blame]
Josh Gaocbe70cb2016-10-18 18:17:52 -07001/*
2 * Copyright 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 <err.h>
18#include <fcntl.h>
Josh Gaocdea7502017-11-01 15:00:40 -070019#include <stdlib.h>
Josh Gao502cfd22017-02-17 01:39:15 -080020#include <sys/capability.h>
Josh Gaofca7ca32017-01-23 12:05:35 -080021#include <sys/prctl.h>
Josh Gaofd13bf02017-08-18 15:37:26 -070022#include <sys/ptrace.h>
Dan Albertc38057a2017-10-11 11:35:40 -070023#include <sys/syscall.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070024#include <sys/types.h>
Josh Gaofd13bf02017-08-18 15:37:26 -070025#include <unistd.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070026
27#include <chrono>
28#include <regex>
29#include <thread>
30
Josh Gao502cfd22017-02-17 01:39:15 -080031#include <android/set_abort_message.h>
32
Josh Gaocbe70cb2016-10-18 18:17:52 -070033#include <android-base/file.h>
34#include <android-base/logging.h>
Josh Gao2e7b8e22017-05-04 17:12:57 -070035#include <android-base/macros.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070036#include <android-base/parseint.h>
37#include <android-base/properties.h>
38#include <android-base/strings.h>
Josh Gao30171a82017-04-27 19:48:44 -070039#include <android-base/test_utils.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070040#include <android-base/unique_fd.h>
41#include <cutils/sockets.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070042#include <gtest/gtest.h>
43
Narayan Kamath2d377cd2017-05-10 10:58:59 +010044#include "debuggerd/handler.h"
45#include "protocol.h"
46#include "tombstoned/tombstoned.h"
47#include "util.h"
48
Josh Gaocbe70cb2016-10-18 18:17:52 -070049using namespace std::chrono_literals;
50using android::base::unique_fd;
51
52#if defined(__LP64__)
Josh Gaocbe70cb2016-10-18 18:17:52 -070053#define ARCH_SUFFIX "64"
54#else
Josh Gaocbe70cb2016-10-18 18:17:52 -070055#define ARCH_SUFFIX ""
56#endif
57
58constexpr char kWaitForGdbKey[] = "debug.debuggerd.wait_for_gdb";
59
60#define TIMEOUT(seconds, expr) \
61 [&]() { \
62 struct sigaction old_sigaction; \
63 struct sigaction new_sigaction = {}; \
64 new_sigaction.sa_handler = [](int) {}; \
65 if (sigaction(SIGALRM, &new_sigaction, &new_sigaction) != 0) { \
66 err(1, "sigaction failed"); \
67 } \
68 alarm(seconds); \
69 auto value = expr; \
70 int saved_errno = errno; \
71 if (sigaction(SIGALRM, &old_sigaction, nullptr) != 0) { \
72 err(1, "sigaction failed"); \
73 } \
74 alarm(0); \
75 errno = saved_errno; \
76 return value; \
77 }()
78
Jaesung Chung58778e12017-06-15 18:20:34 +090079#define ASSERT_BACKTRACE_FRAME(result, frame_name) \
80 ASSERT_MATCH(result, R"(#\d\d pc [0-9a-f]+\s+ /system/lib)" ARCH_SUFFIX \
81 R"(/libc.so \()" frame_name R"(\+)")
82
Narayan Kamatha73df602017-05-24 15:07:25 +010083static void tombstoned_intercept(pid_t target_pid, unique_fd* intercept_fd, unique_fd* output_fd,
Narayan Kamathca5e9082017-06-02 15:42:06 +010084 InterceptStatus* status, DebuggerdDumpType intercept_type) {
Josh Gao460b3362017-03-30 16:40:47 -070085 intercept_fd->reset(socket_local_client(kTombstonedInterceptSocketName,
86 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_SEQPACKET));
87 if (intercept_fd->get() == -1) {
88 FAIL() << "failed to contact tombstoned: " << strerror(errno);
89 }
90
Narayan Kamatha73df602017-05-24 15:07:25 +010091 InterceptRequest req = {.pid = target_pid, .dump_type = intercept_type};
Josh Gao460b3362017-03-30 16:40:47 -070092
93 unique_fd output_pipe_write;
94 if (!Pipe(output_fd, &output_pipe_write)) {
95 FAIL() << "failed to create output pipe: " << strerror(errno);
96 }
97
98 std::string pipe_size_str;
99 int pipe_buffer_size;
100 if (!android::base::ReadFileToString("/proc/sys/fs/pipe-max-size", &pipe_size_str)) {
101 FAIL() << "failed to read /proc/sys/fs/pipe-max-size: " << strerror(errno);
102 }
103
104 pipe_size_str = android::base::Trim(pipe_size_str);
105
106 if (!android::base::ParseInt(pipe_size_str.c_str(), &pipe_buffer_size, 0)) {
107 FAIL() << "failed to parse pipe max size";
108 }
109
110 if (fcntl(output_fd->get(), F_SETPIPE_SZ, pipe_buffer_size) != pipe_buffer_size) {
111 FAIL() << "failed to set pipe size: " << strerror(errno);
112 }
113
Josh Gao5675f3c2017-06-01 12:19:53 -0700114 ASSERT_GE(pipe_buffer_size, 1024 * 1024);
115
Josh Gao460b3362017-03-30 16:40:47 -0700116 if (send_fd(intercept_fd->get(), &req, sizeof(req), std::move(output_pipe_write)) != sizeof(req)) {
117 FAIL() << "failed to send output fd to tombstoned: " << strerror(errno);
118 }
119
120 InterceptResponse response;
121 ssize_t rc = TEMP_FAILURE_RETRY(read(intercept_fd->get(), &response, sizeof(response)));
122 if (rc == -1) {
123 FAIL() << "failed to read response from tombstoned: " << strerror(errno);
124 } else if (rc == 0) {
125 FAIL() << "failed to read response from tombstoned (EOF)";
126 } else if (rc != sizeof(response)) {
127 FAIL() << "received packet of unexpected length from tombstoned: expected " << sizeof(response)
128 << ", received " << rc;
129 }
130
Narayan Kamathca5e9082017-06-02 15:42:06 +0100131 *status = response.status;
Josh Gao460b3362017-03-30 16:40:47 -0700132}
133
Josh Gaocbe70cb2016-10-18 18:17:52 -0700134class CrasherTest : public ::testing::Test {
135 public:
136 pid_t crasher_pid = -1;
137 bool previous_wait_for_gdb;
138 unique_fd crasher_pipe;
139 unique_fd intercept_fd;
140
141 CrasherTest();
142 ~CrasherTest();
143
Narayan Kamatha73df602017-05-24 15:07:25 +0100144 void StartIntercept(unique_fd* output_fd, DebuggerdDumpType intercept_type = kDebuggerdTombstone);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700145
146 // Returns -1 if we fail to read a response from tombstoned, otherwise the received return code.
147 void FinishIntercept(int* result);
148
Josh Gao2e7b8e22017-05-04 17:12:57 -0700149 void StartProcess(std::function<void()> function, std::function<pid_t()> forker = fork);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700150 void StartCrasher(const std::string& crash_type);
151 void FinishCrasher();
152 void AssertDeath(int signo);
153};
154
155CrasherTest::CrasherTest() {
156 previous_wait_for_gdb = android::base::GetBoolProperty(kWaitForGdbKey, false);
157 android::base::SetProperty(kWaitForGdbKey, "0");
158}
159
160CrasherTest::~CrasherTest() {
161 if (crasher_pid != -1) {
162 kill(crasher_pid, SIGKILL);
163 int status;
164 waitpid(crasher_pid, &status, WUNTRACED);
165 }
166
167 android::base::SetProperty(kWaitForGdbKey, previous_wait_for_gdb ? "1" : "0");
168}
169
Narayan Kamatha73df602017-05-24 15:07:25 +0100170void CrasherTest::StartIntercept(unique_fd* output_fd, DebuggerdDumpType intercept_type) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700171 if (crasher_pid == -1) {
172 FAIL() << "crasher hasn't been started";
173 }
174
Narayan Kamathca5e9082017-06-02 15:42:06 +0100175 InterceptStatus status;
176 tombstoned_intercept(crasher_pid, &this->intercept_fd, output_fd, &status, intercept_type);
177 ASSERT_EQ(InterceptStatus::kRegistered, status);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700178}
179
180void CrasherTest::FinishIntercept(int* result) {
181 InterceptResponse response;
182
183 // Timeout for tombstoned intercept is 10 seconds.
184 ssize_t rc = TIMEOUT(20, read(intercept_fd.get(), &response, sizeof(response)));
185 if (rc == -1) {
186 FAIL() << "failed to read response from tombstoned: " << strerror(errno);
187 } else if (rc == 0) {
188 *result = -1;
189 } else if (rc != sizeof(response)) {
190 FAIL() << "received packet of unexpected length from tombstoned: expected " << sizeof(response)
191 << ", received " << rc;
192 } else {
Josh Gao460b3362017-03-30 16:40:47 -0700193 *result = response.status == InterceptStatus::kStarted ? 1 : 0;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700194 }
195}
196
Josh Gao2e7b8e22017-05-04 17:12:57 -0700197void CrasherTest::StartProcess(std::function<void()> function, std::function<pid_t()> forker) {
Josh Gaofca7ca32017-01-23 12:05:35 -0800198 unique_fd read_pipe;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700199 unique_fd crasher_read_pipe;
200 if (!Pipe(&crasher_read_pipe, &crasher_pipe)) {
201 FAIL() << "failed to create pipe: " << strerror(errno);
202 }
203
Josh Gao2e7b8e22017-05-04 17:12:57 -0700204 crasher_pid = forker();
Josh Gaocbe70cb2016-10-18 18:17:52 -0700205 if (crasher_pid == -1) {
206 FAIL() << "fork failed: " << strerror(errno);
207 } else if (crasher_pid == 0) {
Josh Gao502cfd22017-02-17 01:39:15 -0800208 char dummy;
209 crasher_pipe.reset();
210 TEMP_FAILURE_RETRY(read(crasher_read_pipe.get(), &dummy, 1));
Josh Gaofca7ca32017-01-23 12:05:35 -0800211 function();
212 _exit(0);
213 }
214}
215
Josh Gaocbe70cb2016-10-18 18:17:52 -0700216void CrasherTest::FinishCrasher() {
217 if (crasher_pipe == -1) {
218 FAIL() << "crasher pipe uninitialized";
219 }
220
221 ssize_t rc = write(crasher_pipe.get(), "\n", 1);
222 if (rc == -1) {
223 FAIL() << "failed to write to crasher pipe: " << strerror(errno);
224 } else if (rc == 0) {
225 FAIL() << "crasher pipe was closed";
226 }
227}
228
229void CrasherTest::AssertDeath(int signo) {
230 int status;
231 pid_t pid = TIMEOUT(5, waitpid(crasher_pid, &status, 0));
232 if (pid != crasher_pid) {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700233 printf("failed to wait for crasher (pid %d)\n", crasher_pid);
234 sleep(100);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700235 FAIL() << "failed to wait for crasher: " << strerror(errno);
236 }
237
Josh Gaoe06f2a42017-04-27 16:50:38 -0700238 if (signo == 0) {
239 ASSERT_TRUE(WIFEXITED(status));
240 ASSERT_EQ(0, WEXITSTATUS(signo));
241 } else {
242 ASSERT_FALSE(WIFEXITED(status));
243 ASSERT_TRUE(WIFSIGNALED(status)) << "crasher didn't terminate via a signal";
244 ASSERT_EQ(signo, WTERMSIG(status));
Josh Gaocbe70cb2016-10-18 18:17:52 -0700245 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700246 crasher_pid = -1;
247}
248
249static void ConsumeFd(unique_fd fd, std::string* output) {
250 constexpr size_t read_length = PAGE_SIZE;
251 std::string result;
252
253 while (true) {
254 size_t offset = result.size();
255 result.resize(result.size() + PAGE_SIZE);
256 ssize_t rc = TEMP_FAILURE_RETRY(read(fd.get(), &result[offset], read_length));
257 if (rc == -1) {
258 FAIL() << "read failed: " << strerror(errno);
259 } else if (rc == 0) {
260 result.resize(result.size() - PAGE_SIZE);
261 break;
262 }
263
264 result.resize(result.size() - PAGE_SIZE + rc);
265 }
266
267 *output = std::move(result);
268}
269
270TEST_F(CrasherTest, smoke) {
271 int intercept_result;
272 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800273 StartProcess([]() {
274 *reinterpret_cast<volatile char*>(0xdead) = '1';
275 });
276
Josh Gaocbe70cb2016-10-18 18:17:52 -0700277 StartIntercept(&output_fd);
278 FinishCrasher();
279 AssertDeath(SIGSEGV);
280 FinishIntercept(&intercept_result);
281
282 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
283
284 std::string result;
285 ConsumeFd(std::move(output_fd), &result);
286 ASSERT_MATCH(result, R"(signal 11 \(SIGSEGV\), code 1 \(SEGV_MAPERR\), fault addr 0xdead)");
287}
288
Josh Gaocdea7502017-11-01 15:00:40 -0700289TEST_F(CrasherTest, LD_PRELOAD) {
290 int intercept_result;
291 unique_fd output_fd;
292 StartProcess([]() {
293 setenv("LD_PRELOAD", "nonexistent.so", 1);
294 *reinterpret_cast<volatile char*>(0xdead) = '1';
295 });
296
297 StartIntercept(&output_fd);
298 FinishCrasher();
299 AssertDeath(SIGSEGV);
300 FinishIntercept(&intercept_result);
301
302 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
303
304 std::string result;
305 ConsumeFd(std::move(output_fd), &result);
306 ASSERT_MATCH(result, R"(signal 11 \(SIGSEGV\), code 1 \(SEGV_MAPERR\), fault addr 0xdead)");
307}
308
Josh Gaocbe70cb2016-10-18 18:17:52 -0700309TEST_F(CrasherTest, abort) {
310 int intercept_result;
311 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800312 StartProcess([]() {
313 abort();
314 });
Josh Gaocbe70cb2016-10-18 18:17:52 -0700315 StartIntercept(&output_fd);
316 FinishCrasher();
317 AssertDeath(SIGABRT);
318 FinishIntercept(&intercept_result);
319
320 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
321
322 std::string result;
323 ConsumeFd(std::move(output_fd), &result);
Andreas Gampe26cbafb2017-06-22 20:14:43 -0700324 ASSERT_BACKTRACE_FRAME(result, "abort");
Josh Gaocbe70cb2016-10-18 18:17:52 -0700325}
326
327TEST_F(CrasherTest, signal) {
328 int intercept_result;
329 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800330 StartProcess([]() {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700331 while (true) {
332 sleep(1);
333 }
Josh Gao502cfd22017-02-17 01:39:15 -0800334 });
Josh Gaocbe70cb2016-10-18 18:17:52 -0700335 StartIntercept(&output_fd);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700336 FinishCrasher();
Josh Gaocbe70cb2016-10-18 18:17:52 -0700337 ASSERT_EQ(0, kill(crasher_pid, SIGSEGV));
338
339 AssertDeath(SIGSEGV);
340 FinishIntercept(&intercept_result);
341
342 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
343
344 std::string result;
345 ConsumeFd(std::move(output_fd), &result);
346 ASSERT_MATCH(result, R"(signal 11 \(SIGSEGV\), code 0 \(SI_USER\), fault addr --------)");
347 ASSERT_MATCH(result, R"(backtrace:)");
348}
349
350TEST_F(CrasherTest, abort_message) {
351 int intercept_result;
352 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800353 StartProcess([]() {
354 android_set_abort_message("abort message goes here");
355 abort();
356 });
Josh Gaocbe70cb2016-10-18 18:17:52 -0700357 StartIntercept(&output_fd);
358 FinishCrasher();
359 AssertDeath(SIGABRT);
360 FinishIntercept(&intercept_result);
361
362 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
363
364 std::string result;
365 ConsumeFd(std::move(output_fd), &result);
Josh Gao502cfd22017-02-17 01:39:15 -0800366 ASSERT_MATCH(result, R"(Abort message: 'abort message goes here')");
Josh Gaocbe70cb2016-10-18 18:17:52 -0700367}
368
Josh Gaoe06f2a42017-04-27 16:50:38 -0700369TEST_F(CrasherTest, abort_message_backtrace) {
370 int intercept_result;
371 unique_fd output_fd;
372 StartProcess([]() {
373 android_set_abort_message("not actually aborting");
374 raise(DEBUGGER_SIGNAL);
375 exit(0);
376 });
377 StartIntercept(&output_fd);
378 FinishCrasher();
379 AssertDeath(0);
380 FinishIntercept(&intercept_result);
381
382 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
383
384 std::string result;
385 ConsumeFd(std::move(output_fd), &result);
386 ASSERT_NOT_MATCH(result, R"(Abort message:)");
387}
388
Josh Gaocbe70cb2016-10-18 18:17:52 -0700389TEST_F(CrasherTest, intercept_timeout) {
390 int intercept_result;
391 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800392 StartProcess([]() {
393 abort();
394 });
Josh Gaocbe70cb2016-10-18 18:17:52 -0700395 StartIntercept(&output_fd);
396
397 // Don't let crasher finish until we timeout.
398 FinishIntercept(&intercept_result);
399
400 ASSERT_NE(1, intercept_result) << "tombstoned reported success? (intercept_result = "
401 << intercept_result << ")";
402
403 FinishCrasher();
404 AssertDeath(SIGABRT);
405}
406
407TEST_F(CrasherTest, wait_for_gdb) {
408 if (!android::base::SetProperty(kWaitForGdbKey, "1")) {
409 FAIL() << "failed to enable wait_for_gdb";
410 }
411 sleep(1);
412
Josh Gao502cfd22017-02-17 01:39:15 -0800413 StartProcess([]() {
414 abort();
415 });
Josh Gaocbe70cb2016-10-18 18:17:52 -0700416 FinishCrasher();
417
418 int status;
419 ASSERT_EQ(crasher_pid, waitpid(crasher_pid, &status, WUNTRACED));
420 ASSERT_TRUE(WIFSTOPPED(status));
421 ASSERT_EQ(SIGSTOP, WSTOPSIG(status));
422
423 ASSERT_EQ(0, kill(crasher_pid, SIGCONT));
424
425 AssertDeath(SIGABRT);
426}
427
Josh Gaocbe70cb2016-10-18 18:17:52 -0700428TEST_F(CrasherTest, backtrace) {
429 std::string result;
430 int intercept_result;
431 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800432
433 StartProcess([]() {
434 abort();
435 });
Narayan Kamatha73df602017-05-24 15:07:25 +0100436 StartIntercept(&output_fd, kDebuggerdNativeBacktrace);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700437
438 std::this_thread::sleep_for(500ms);
439
440 sigval val;
441 val.sival_int = 1;
442 ASSERT_EQ(0, sigqueue(crasher_pid, DEBUGGER_SIGNAL, val)) << strerror(errno);
443 FinishIntercept(&intercept_result);
444 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
445 ConsumeFd(std::move(output_fd), &result);
Jaesung Chung58778e12017-06-15 18:20:34 +0900446 ASSERT_BACKTRACE_FRAME(result, "read");
Josh Gaocbe70cb2016-10-18 18:17:52 -0700447
448 int status;
449 ASSERT_EQ(0, waitpid(crasher_pid, &status, WNOHANG | WUNTRACED));
450
451 StartIntercept(&output_fd);
452 FinishCrasher();
453 AssertDeath(SIGABRT);
454 FinishIntercept(&intercept_result);
455 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
456 ConsumeFd(std::move(output_fd), &result);
Andreas Gampe26cbafb2017-06-22 20:14:43 -0700457 ASSERT_BACKTRACE_FRAME(result, "abort");
Josh Gaocbe70cb2016-10-18 18:17:52 -0700458}
Josh Gaofca7ca32017-01-23 12:05:35 -0800459
460TEST_F(CrasherTest, PR_SET_DUMPABLE_0_crash) {
Josh Gao502cfd22017-02-17 01:39:15 -0800461 int intercept_result;
462 unique_fd output_fd;
Josh Gaofca7ca32017-01-23 12:05:35 -0800463 StartProcess([]() {
464 prctl(PR_SET_DUMPABLE, 0);
Josh Gao502cfd22017-02-17 01:39:15 -0800465 abort();
Josh Gaofca7ca32017-01-23 12:05:35 -0800466 });
Josh Gao502cfd22017-02-17 01:39:15 -0800467
468 StartIntercept(&output_fd);
469 FinishCrasher();
470 AssertDeath(SIGABRT);
471 FinishIntercept(&intercept_result);
472
473 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
474
475 std::string result;
476 ConsumeFd(std::move(output_fd), &result);
Andreas Gampe26cbafb2017-06-22 20:14:43 -0700477 ASSERT_BACKTRACE_FRAME(result, "abort");
Josh Gaofca7ca32017-01-23 12:05:35 -0800478}
479
Josh Gao502cfd22017-02-17 01:39:15 -0800480TEST_F(CrasherTest, capabilities) {
481 ASSERT_EQ(0U, getuid()) << "capability test requires root";
482
Josh Gaofca7ca32017-01-23 12:05:35 -0800483 StartProcess([]() {
Josh Gao502cfd22017-02-17 01:39:15 -0800484 if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) != 0) {
485 err(1, "failed to set PR_SET_KEEPCAPS");
486 }
487
488 if (setresuid(1, 1, 1) != 0) {
489 err(1, "setresuid failed");
490 }
491
492 __user_cap_header_struct capheader;
493 __user_cap_data_struct capdata[2];
494 memset(&capheader, 0, sizeof(capheader));
495 memset(&capdata, 0, sizeof(capdata));
496
497 capheader.version = _LINUX_CAPABILITY_VERSION_3;
498 capheader.pid = 0;
499
500 // Turn on every third capability.
501 static_assert(CAP_LAST_CAP > 33, "CAP_LAST_CAP <= 32");
502 for (int i = 0; i < CAP_LAST_CAP; i += 3) {
503 capdata[CAP_TO_INDEX(i)].permitted |= CAP_TO_MASK(i);
504 capdata[CAP_TO_INDEX(i)].effective |= CAP_TO_MASK(i);
505 }
506
507 // Make sure CAP_SYS_PTRACE is off.
508 capdata[CAP_TO_INDEX(CAP_SYS_PTRACE)].permitted &= ~(CAP_TO_MASK(CAP_SYS_PTRACE));
509 capdata[CAP_TO_INDEX(CAP_SYS_PTRACE)].effective &= ~(CAP_TO_MASK(CAP_SYS_PTRACE));
510
511 if (capset(&capheader, &capdata[0]) != 0) {
512 err(1, "capset failed");
513 }
514
515 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) != 0) {
516 err(1, "failed to drop ambient capabilities");
517 }
518
Josh Gaoa5199a92017-04-03 13:18:34 -0700519 pthread_setname_np(pthread_self(), "thread_name");
Josh Gao502cfd22017-02-17 01:39:15 -0800520 raise(SIGSYS);
Josh Gaofca7ca32017-01-23 12:05:35 -0800521 });
Josh Gao502cfd22017-02-17 01:39:15 -0800522
523 unique_fd output_fd;
524 StartIntercept(&output_fd);
525 FinishCrasher();
526 AssertDeath(SIGSYS);
527
528 std::string result;
529 int intercept_result;
530 FinishIntercept(&intercept_result);
531 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
532 ConsumeFd(std::move(output_fd), &result);
Josh Gaoa5199a92017-04-03 13:18:34 -0700533 ASSERT_MATCH(result, R"(name: thread_name\s+>>> .+debuggerd_test(32|64) <<<)");
Jaesung Chung58778e12017-06-15 18:20:34 +0900534 ASSERT_BACKTRACE_FRAME(result, "tgkill");
Josh Gaofca7ca32017-01-23 12:05:35 -0800535}
Josh Gaoc3c8c022017-02-13 16:36:18 -0800536
Josh Gao2e7b8e22017-05-04 17:12:57 -0700537TEST_F(CrasherTest, fake_pid) {
538 int intercept_result;
539 unique_fd output_fd;
540
541 // Prime the getpid/gettid caches.
542 UNUSED(getpid());
543 UNUSED(gettid());
544
545 std::function<pid_t()> clone_fn = []() {
546 return syscall(__NR_clone, SIGCHLD, nullptr, nullptr, nullptr, nullptr);
547 };
548 StartProcess(
549 []() {
550 ASSERT_NE(getpid(), syscall(__NR_getpid));
551 ASSERT_NE(gettid(), syscall(__NR_gettid));
552 raise(SIGSEGV);
553 },
554 clone_fn);
555
556 StartIntercept(&output_fd);
557 FinishCrasher();
558 AssertDeath(SIGSEGV);
559 FinishIntercept(&intercept_result);
560
561 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
562
563 std::string result;
564 ConsumeFd(std::move(output_fd), &result);
Jaesung Chung58778e12017-06-15 18:20:34 +0900565 ASSERT_BACKTRACE_FRAME(result, "tgkill");
Josh Gao2e7b8e22017-05-04 17:12:57 -0700566}
567
Josh Gaofd13bf02017-08-18 15:37:26 -0700568TEST_F(CrasherTest, competing_tracer) {
569 int intercept_result;
570 unique_fd output_fd;
571 StartProcess([]() {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700572 raise(SIGABRT);
Josh Gaofd13bf02017-08-18 15:37:26 -0700573 });
574
575 StartIntercept(&output_fd);
Josh Gaofd13bf02017-08-18 15:37:26 -0700576
577 ASSERT_EQ(0, ptrace(PTRACE_SEIZE, crasher_pid, 0, 0));
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700578 FinishCrasher();
Josh Gaofd13bf02017-08-18 15:37:26 -0700579
580 int status;
581 ASSERT_EQ(crasher_pid, waitpid(crasher_pid, &status, 0));
582 ASSERT_TRUE(WIFSTOPPED(status));
583 ASSERT_EQ(SIGABRT, WSTOPSIG(status));
584
585 ASSERT_EQ(0, ptrace(PTRACE_CONT, crasher_pid, 0, SIGABRT));
586 FinishIntercept(&intercept_result);
587 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
588
589 std::string result;
590 ConsumeFd(std::move(output_fd), &result);
591 std::string regex = R"(failed to attach to thread \d+, already traced by )";
592 regex += std::to_string(gettid());
593 regex += R"( \(.+debuggerd_test)";
594 ASSERT_MATCH(result, regex.c_str());
595
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700596 ASSERT_EQ(crasher_pid, waitpid(crasher_pid, &status, 0));
597 ASSERT_TRUE(WIFSTOPPED(status));
598 ASSERT_EQ(SIGABRT, WSTOPSIG(status));
599
Josh Gaofd13bf02017-08-18 15:37:26 -0700600 ASSERT_EQ(0, ptrace(PTRACE_DETACH, crasher_pid, 0, SIGABRT));
601 AssertDeath(SIGABRT);
602}
603
Josh Gaoc3c8c022017-02-13 16:36:18 -0800604TEST(crash_dump, zombie) {
605 pid_t forkpid = fork();
606
Josh Gaoc3c8c022017-02-13 16:36:18 -0800607 pid_t rc;
608 int status;
609
610 if (forkpid == 0) {
611 errno = 0;
612 rc = waitpid(-1, &status, WNOHANG | __WALL | __WNOTHREAD);
613 if (rc != -1 || errno != ECHILD) {
614 errx(2, "first waitpid returned %d (%s), expected failure with ECHILD", rc, strerror(errno));
615 }
616
617 raise(DEBUGGER_SIGNAL);
618
619 errno = 0;
620 rc = waitpid(-1, &status, __WALL | __WNOTHREAD);
621 if (rc != -1 || errno != ECHILD) {
622 errx(2, "second waitpid returned %d (%s), expected failure with ECHILD", rc, strerror(errno));
623 }
624 _exit(0);
625 } else {
626 rc = waitpid(forkpid, &status, 0);
627 ASSERT_EQ(forkpid, rc);
628 ASSERT_TRUE(WIFEXITED(status));
629 ASSERT_EQ(0, WEXITSTATUS(status));
630 }
631}
Josh Gao352a8452017-03-30 16:46:21 -0700632
633TEST(tombstoned, no_notify) {
634 // Do this a few times.
635 for (int i = 0; i < 3; ++i) {
636 pid_t pid = 123'456'789 + i;
637
638 unique_fd intercept_fd, output_fd;
Narayan Kamathca5e9082017-06-02 15:42:06 +0100639 InterceptStatus status;
640 tombstoned_intercept(pid, &intercept_fd, &output_fd, &status, kDebuggerdTombstone);
641 ASSERT_EQ(InterceptStatus::kRegistered, status);
Josh Gao352a8452017-03-30 16:46:21 -0700642
643 {
644 unique_fd tombstoned_socket, input_fd;
Narayan Kamatha73df602017-05-24 15:07:25 +0100645 ASSERT_TRUE(tombstoned_connect(pid, &tombstoned_socket, &input_fd, kDebuggerdTombstone));
Josh Gao352a8452017-03-30 16:46:21 -0700646 ASSERT_TRUE(android::base::WriteFully(input_fd.get(), &pid, sizeof(pid)));
647 }
648
649 pid_t read_pid;
650 ASSERT_TRUE(android::base::ReadFully(output_fd.get(), &read_pid, sizeof(read_pid)));
651 ASSERT_EQ(read_pid, pid);
652 }
653}
654
655TEST(tombstoned, stress) {
656 // Spawn threads to simultaneously do a bunch of failing dumps and a bunch of successful dumps.
657 static constexpr int kDumpCount = 100;
658
659 std::atomic<bool> start(false);
660 std::vector<std::thread> threads;
661 threads.emplace_back([&start]() {
662 while (!start) {
663 continue;
664 }
665
666 // Use a way out of range pid, to avoid stomping on an actual process.
667 pid_t pid_base = 1'000'000;
668
669 for (int dump = 0; dump < kDumpCount; ++dump) {
670 pid_t pid = pid_base + dump;
671
672 unique_fd intercept_fd, output_fd;
Narayan Kamathca5e9082017-06-02 15:42:06 +0100673 InterceptStatus status;
674 tombstoned_intercept(pid, &intercept_fd, &output_fd, &status, kDebuggerdTombstone);
675 ASSERT_EQ(InterceptStatus::kRegistered, status);
Josh Gao352a8452017-03-30 16:46:21 -0700676
677 // Pretend to crash, and then immediately close the socket.
678 unique_fd sockfd(socket_local_client(kTombstonedCrashSocketName,
679 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_SEQPACKET));
680 if (sockfd == -1) {
681 FAIL() << "failed to connect to tombstoned: " << strerror(errno);
682 }
683 TombstonedCrashPacket packet = {};
684 packet.packet_type = CrashPacketType::kDumpRequest;
685 packet.packet.dump_request.pid = pid;
686 if (TEMP_FAILURE_RETRY(write(sockfd, &packet, sizeof(packet))) != sizeof(packet)) {
687 FAIL() << "failed to write to tombstoned: " << strerror(errno);
688 }
689
690 continue;
691 }
692 });
693
694 threads.emplace_back([&start]() {
695 while (!start) {
696 continue;
697 }
698
699 // Use a way out of range pid, to avoid stomping on an actual process.
700 pid_t pid_base = 2'000'000;
701
702 for (int dump = 0; dump < kDumpCount; ++dump) {
703 pid_t pid = pid_base + dump;
704
705 unique_fd intercept_fd, output_fd;
Narayan Kamathca5e9082017-06-02 15:42:06 +0100706 InterceptStatus status;
707 tombstoned_intercept(pid, &intercept_fd, &output_fd, &status, kDebuggerdTombstone);
708 ASSERT_EQ(InterceptStatus::kRegistered, status);
Josh Gao352a8452017-03-30 16:46:21 -0700709
710 {
711 unique_fd tombstoned_socket, input_fd;
Narayan Kamatha73df602017-05-24 15:07:25 +0100712 ASSERT_TRUE(tombstoned_connect(pid, &tombstoned_socket, &input_fd, kDebuggerdTombstone));
Josh Gao352a8452017-03-30 16:46:21 -0700713 ASSERT_TRUE(android::base::WriteFully(input_fd.get(), &pid, sizeof(pid)));
714 tombstoned_notify_completion(tombstoned_socket.get());
715 }
716
717 // TODO: Fix the race that requires this sleep.
718 std::this_thread::sleep_for(50ms);
719
720 pid_t read_pid;
721 ASSERT_TRUE(android::base::ReadFully(output_fd.get(), &read_pid, sizeof(read_pid)));
722 ASSERT_EQ(read_pid, pid);
723 }
724 });
725
726 start = true;
727
728 for (std::thread& thread : threads) {
729 thread.join();
730 }
731}
Narayan Kamathca5e9082017-06-02 15:42:06 +0100732
733TEST(tombstoned, java_trace_intercept_smoke) {
734 // Using a "real" PID is a little dangerous here - if the test fails
735 // or crashes, we might end up getting a bogus / unreliable stack
736 // trace.
737 const pid_t self = getpid();
738
739 unique_fd intercept_fd, output_fd;
740 InterceptStatus status;
741 tombstoned_intercept(self, &intercept_fd, &output_fd, &status, kDebuggerdJavaBacktrace);
742 ASSERT_EQ(InterceptStatus::kRegistered, status);
743
744 // First connect to tombstoned requesting a native backtrace. This
745 // should result in a "regular" FD and not the installed intercept.
746 const char native[] = "native";
747 unique_fd tombstoned_socket, input_fd;
748 ASSERT_TRUE(tombstoned_connect(self, &tombstoned_socket, &input_fd, kDebuggerdNativeBacktrace));
749 ASSERT_TRUE(android::base::WriteFully(input_fd.get(), native, sizeof(native)));
750 tombstoned_notify_completion(tombstoned_socket.get());
751
752 // Then, connect to tombstoned asking for a java backtrace. This *should*
753 // trigger the intercept.
754 const char java[] = "java";
755 ASSERT_TRUE(tombstoned_connect(self, &tombstoned_socket, &input_fd, kDebuggerdJavaBacktrace));
756 ASSERT_TRUE(android::base::WriteFully(input_fd.get(), java, sizeof(java)));
757 tombstoned_notify_completion(tombstoned_socket.get());
758
759 char outbuf[sizeof(java)];
760 ASSERT_TRUE(android::base::ReadFully(output_fd.get(), outbuf, sizeof(outbuf)));
761 ASSERT_STREQ("java", outbuf);
762}
763
764TEST(tombstoned, multiple_intercepts) {
765 const pid_t fake_pid = 1'234'567;
766 unique_fd intercept_fd, output_fd;
767 InterceptStatus status;
768 tombstoned_intercept(fake_pid, &intercept_fd, &output_fd, &status, kDebuggerdJavaBacktrace);
769 ASSERT_EQ(InterceptStatus::kRegistered, status);
770
771 unique_fd intercept_fd_2, output_fd_2;
772 tombstoned_intercept(fake_pid, &intercept_fd_2, &output_fd_2, &status, kDebuggerdNativeBacktrace);
773 ASSERT_EQ(InterceptStatus::kFailedAlreadyRegistered, status);
774}
775
776TEST(tombstoned, intercept_any) {
777 const pid_t fake_pid = 1'234'567;
778
779 unique_fd intercept_fd, output_fd;
780 InterceptStatus status;
781 tombstoned_intercept(fake_pid, &intercept_fd, &output_fd, &status, kDebuggerdNativeBacktrace);
782 ASSERT_EQ(InterceptStatus::kRegistered, status);
783
784 const char any[] = "any";
785 unique_fd tombstoned_socket, input_fd;
786 ASSERT_TRUE(tombstoned_connect(fake_pid, &tombstoned_socket, &input_fd, kDebuggerdAnyIntercept));
787 ASSERT_TRUE(android::base::WriteFully(input_fd.get(), any, sizeof(any)));
788 tombstoned_notify_completion(tombstoned_socket.get());
789
790 char outbuf[sizeof(any)];
791 ASSERT_TRUE(android::base::ReadFully(output_fd.get(), outbuf, sizeof(outbuf)));
792 ASSERT_STREQ("any", outbuf);
793}