blob: da8ad37c5b8e2e2200160cd88bf6a96400c00d45 [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>
19#include <unistd.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 Gaocbe70cb2016-10-18 18:17:52 -070022#include <sys/types.h>
23
24#include <chrono>
25#include <regex>
26#include <thread>
27
Josh Gao502cfd22017-02-17 01:39:15 -080028#include <android/set_abort_message.h>
29
Josh Gaocbe70cb2016-10-18 18:17:52 -070030#include <android-base/file.h>
31#include <android-base/logging.h>
Josh Gao2e7b8e22017-05-04 17:12:57 -070032#include <android-base/macros.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070033#include <android-base/parseint.h>
34#include <android-base/properties.h>
35#include <android-base/strings.h>
36#include <android-base/unique_fd.h>
37#include <cutils/sockets.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070038#include <gtest/gtest.h>
39
Narayan Kamath2d377cd2017-05-10 10:58:59 +010040#include "debuggerd/handler.h"
41#include "protocol.h"
42#include "tombstoned/tombstoned.h"
43#include "util.h"
44
Josh Gaocbe70cb2016-10-18 18:17:52 -070045using namespace std::chrono_literals;
46using android::base::unique_fd;
47
48#if defined(__LP64__)
Josh Gaocbe70cb2016-10-18 18:17:52 -070049#define ARCH_SUFFIX "64"
50#else
Josh Gaocbe70cb2016-10-18 18:17:52 -070051#define ARCH_SUFFIX ""
52#endif
53
54constexpr char kWaitForGdbKey[] = "debug.debuggerd.wait_for_gdb";
55
56#define TIMEOUT(seconds, expr) \
57 [&]() { \
58 struct sigaction old_sigaction; \
59 struct sigaction new_sigaction = {}; \
60 new_sigaction.sa_handler = [](int) {}; \
61 if (sigaction(SIGALRM, &new_sigaction, &new_sigaction) != 0) { \
62 err(1, "sigaction failed"); \
63 } \
64 alarm(seconds); \
65 auto value = expr; \
66 int saved_errno = errno; \
67 if (sigaction(SIGALRM, &old_sigaction, nullptr) != 0) { \
68 err(1, "sigaction failed"); \
69 } \
70 alarm(0); \
71 errno = saved_errno; \
72 return value; \
73 }()
74
75#define ASSERT_MATCH(str, pattern) \
76 do { \
77 std::regex r((pattern)); \
78 if (!std::regex_search((str), r)) { \
79 FAIL() << "regex mismatch: expected " << (pattern) << " in: \n" << (str); \
80 } \
81 } while (0)
82
Josh Gaoe06f2a42017-04-27 16:50:38 -070083#define ASSERT_NOT_MATCH(str, pattern) \
84 do { \
85 std::regex r((pattern)); \
86 if (std::regex_search((str), r)) { \
87 FAIL() << "regex mismatch: expected to not find " << (pattern) << " in: \n" << (str); \
88 } \
89 } while (0)
90
Narayan Kamatha73df602017-05-24 15:07:25 +010091static void tombstoned_intercept(pid_t target_pid, unique_fd* intercept_fd, unique_fd* output_fd,
Narayan Kamathca5e9082017-06-02 15:42:06 +010092 InterceptStatus* status, DebuggerdDumpType intercept_type) {
Josh Gao460b3362017-03-30 16:40:47 -070093 intercept_fd->reset(socket_local_client(kTombstonedInterceptSocketName,
94 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_SEQPACKET));
95 if (intercept_fd->get() == -1) {
96 FAIL() << "failed to contact tombstoned: " << strerror(errno);
97 }
98
Narayan Kamatha73df602017-05-24 15:07:25 +010099 InterceptRequest req = {.pid = target_pid, .dump_type = intercept_type};
Josh Gao460b3362017-03-30 16:40:47 -0700100
101 unique_fd output_pipe_write;
102 if (!Pipe(output_fd, &output_pipe_write)) {
103 FAIL() << "failed to create output pipe: " << strerror(errno);
104 }
105
106 std::string pipe_size_str;
107 int pipe_buffer_size;
108 if (!android::base::ReadFileToString("/proc/sys/fs/pipe-max-size", &pipe_size_str)) {
109 FAIL() << "failed to read /proc/sys/fs/pipe-max-size: " << strerror(errno);
110 }
111
112 pipe_size_str = android::base::Trim(pipe_size_str);
113
114 if (!android::base::ParseInt(pipe_size_str.c_str(), &pipe_buffer_size, 0)) {
115 FAIL() << "failed to parse pipe max size";
116 }
117
118 if (fcntl(output_fd->get(), F_SETPIPE_SZ, pipe_buffer_size) != pipe_buffer_size) {
119 FAIL() << "failed to set pipe size: " << strerror(errno);
120 }
121
Josh Gao5675f3c2017-06-01 12:19:53 -0700122 ASSERT_GE(pipe_buffer_size, 1024 * 1024);
123
Josh Gao460b3362017-03-30 16:40:47 -0700124 if (send_fd(intercept_fd->get(), &req, sizeof(req), std::move(output_pipe_write)) != sizeof(req)) {
125 FAIL() << "failed to send output fd to tombstoned: " << strerror(errno);
126 }
127
128 InterceptResponse response;
129 ssize_t rc = TEMP_FAILURE_RETRY(read(intercept_fd->get(), &response, sizeof(response)));
130 if (rc == -1) {
131 FAIL() << "failed to read response from tombstoned: " << strerror(errno);
132 } else if (rc == 0) {
133 FAIL() << "failed to read response from tombstoned (EOF)";
134 } else if (rc != sizeof(response)) {
135 FAIL() << "received packet of unexpected length from tombstoned: expected " << sizeof(response)
136 << ", received " << rc;
137 }
138
Narayan Kamathca5e9082017-06-02 15:42:06 +0100139 *status = response.status;
Josh Gao460b3362017-03-30 16:40:47 -0700140}
141
Josh Gaocbe70cb2016-10-18 18:17:52 -0700142class CrasherTest : public ::testing::Test {
143 public:
144 pid_t crasher_pid = -1;
145 bool previous_wait_for_gdb;
146 unique_fd crasher_pipe;
147 unique_fd intercept_fd;
148
149 CrasherTest();
150 ~CrasherTest();
151
Narayan Kamatha73df602017-05-24 15:07:25 +0100152 void StartIntercept(unique_fd* output_fd, DebuggerdDumpType intercept_type = kDebuggerdTombstone);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700153
154 // Returns -1 if we fail to read a response from tombstoned, otherwise the received return code.
155 void FinishIntercept(int* result);
156
Josh Gao2e7b8e22017-05-04 17:12:57 -0700157 void StartProcess(std::function<void()> function, std::function<pid_t()> forker = fork);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700158 void StartCrasher(const std::string& crash_type);
159 void FinishCrasher();
160 void AssertDeath(int signo);
161};
162
163CrasherTest::CrasherTest() {
164 previous_wait_for_gdb = android::base::GetBoolProperty(kWaitForGdbKey, false);
165 android::base::SetProperty(kWaitForGdbKey, "0");
166}
167
168CrasherTest::~CrasherTest() {
169 if (crasher_pid != -1) {
170 kill(crasher_pid, SIGKILL);
171 int status;
172 waitpid(crasher_pid, &status, WUNTRACED);
173 }
174
175 android::base::SetProperty(kWaitForGdbKey, previous_wait_for_gdb ? "1" : "0");
176}
177
Narayan Kamatha73df602017-05-24 15:07:25 +0100178void CrasherTest::StartIntercept(unique_fd* output_fd, DebuggerdDumpType intercept_type) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700179 if (crasher_pid == -1) {
180 FAIL() << "crasher hasn't been started";
181 }
182
Narayan Kamathca5e9082017-06-02 15:42:06 +0100183 InterceptStatus status;
184 tombstoned_intercept(crasher_pid, &this->intercept_fd, output_fd, &status, intercept_type);
185 ASSERT_EQ(InterceptStatus::kRegistered, status);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700186}
187
188void CrasherTest::FinishIntercept(int* result) {
189 InterceptResponse response;
190
191 // Timeout for tombstoned intercept is 10 seconds.
192 ssize_t rc = TIMEOUT(20, read(intercept_fd.get(), &response, sizeof(response)));
193 if (rc == -1) {
194 FAIL() << "failed to read response from tombstoned: " << strerror(errno);
195 } else if (rc == 0) {
196 *result = -1;
197 } else if (rc != sizeof(response)) {
198 FAIL() << "received packet of unexpected length from tombstoned: expected " << sizeof(response)
199 << ", received " << rc;
200 } else {
Josh Gao460b3362017-03-30 16:40:47 -0700201 *result = response.status == InterceptStatus::kStarted ? 1 : 0;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700202 }
203}
204
Josh Gao2e7b8e22017-05-04 17:12:57 -0700205void CrasherTest::StartProcess(std::function<void()> function, std::function<pid_t()> forker) {
Josh Gaofca7ca32017-01-23 12:05:35 -0800206 unique_fd read_pipe;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700207 unique_fd crasher_read_pipe;
208 if (!Pipe(&crasher_read_pipe, &crasher_pipe)) {
209 FAIL() << "failed to create pipe: " << strerror(errno);
210 }
211
Josh Gao2e7b8e22017-05-04 17:12:57 -0700212 crasher_pid = forker();
Josh Gaocbe70cb2016-10-18 18:17:52 -0700213 if (crasher_pid == -1) {
214 FAIL() << "fork failed: " << strerror(errno);
215 } else if (crasher_pid == 0) {
Josh Gao502cfd22017-02-17 01:39:15 -0800216 char dummy;
217 crasher_pipe.reset();
218 TEMP_FAILURE_RETRY(read(crasher_read_pipe.get(), &dummy, 1));
Josh Gaofca7ca32017-01-23 12:05:35 -0800219 function();
220 _exit(0);
221 }
222}
223
Josh Gaocbe70cb2016-10-18 18:17:52 -0700224void CrasherTest::FinishCrasher() {
225 if (crasher_pipe == -1) {
226 FAIL() << "crasher pipe uninitialized";
227 }
228
229 ssize_t rc = write(crasher_pipe.get(), "\n", 1);
230 if (rc == -1) {
231 FAIL() << "failed to write to crasher pipe: " << strerror(errno);
232 } else if (rc == 0) {
233 FAIL() << "crasher pipe was closed";
234 }
235}
236
237void CrasherTest::AssertDeath(int signo) {
238 int status;
239 pid_t pid = TIMEOUT(5, waitpid(crasher_pid, &status, 0));
240 if (pid != crasher_pid) {
241 FAIL() << "failed to wait for crasher: " << strerror(errno);
242 }
243
Josh Gaoe06f2a42017-04-27 16:50:38 -0700244 if (signo == 0) {
245 ASSERT_TRUE(WIFEXITED(status));
246 ASSERT_EQ(0, WEXITSTATUS(signo));
247 } else {
248 ASSERT_FALSE(WIFEXITED(status));
249 ASSERT_TRUE(WIFSIGNALED(status)) << "crasher didn't terminate via a signal";
250 ASSERT_EQ(signo, WTERMSIG(status));
Josh Gaocbe70cb2016-10-18 18:17:52 -0700251 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700252 crasher_pid = -1;
253}
254
255static void ConsumeFd(unique_fd fd, std::string* output) {
256 constexpr size_t read_length = PAGE_SIZE;
257 std::string result;
258
259 while (true) {
260 size_t offset = result.size();
261 result.resize(result.size() + PAGE_SIZE);
262 ssize_t rc = TEMP_FAILURE_RETRY(read(fd.get(), &result[offset], read_length));
263 if (rc == -1) {
264 FAIL() << "read failed: " << strerror(errno);
265 } else if (rc == 0) {
266 result.resize(result.size() - PAGE_SIZE);
267 break;
268 }
269
270 result.resize(result.size() - PAGE_SIZE + rc);
271 }
272
273 *output = std::move(result);
274}
275
276TEST_F(CrasherTest, smoke) {
277 int intercept_result;
278 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800279 StartProcess([]() {
280 *reinterpret_cast<volatile char*>(0xdead) = '1';
281 });
282
Josh Gaocbe70cb2016-10-18 18:17:52 -0700283 StartIntercept(&output_fd);
284 FinishCrasher();
285 AssertDeath(SIGSEGV);
286 FinishIntercept(&intercept_result);
287
288 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
289
290 std::string result;
291 ConsumeFd(std::move(output_fd), &result);
292 ASSERT_MATCH(result, R"(signal 11 \(SIGSEGV\), code 1 \(SEGV_MAPERR\), fault addr 0xdead)");
293}
294
295TEST_F(CrasherTest, abort) {
296 int intercept_result;
297 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800298 StartProcess([]() {
299 abort();
300 });
Josh Gaocbe70cb2016-10-18 18:17:52 -0700301 StartIntercept(&output_fd);
302 FinishCrasher();
303 AssertDeath(SIGABRT);
304 FinishIntercept(&intercept_result);
305
306 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
307
308 std::string result;
309 ConsumeFd(std::move(output_fd), &result);
310 ASSERT_MATCH(result, R"(#00 pc [0-9a-f]+\s+ /system/lib)" ARCH_SUFFIX R"(/libc.so \(tgkill)");
311}
312
313TEST_F(CrasherTest, signal) {
314 int intercept_result;
315 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800316 StartProcess([]() {
317 abort();
318 });
Josh Gaocbe70cb2016-10-18 18:17:52 -0700319 StartIntercept(&output_fd);
320
321 // Wait for a bit, or we might end up killing the process before the signal
322 // handler even gets a chance to be registered.
323 std::this_thread::sleep_for(100ms);
324 ASSERT_EQ(0, kill(crasher_pid, SIGSEGV));
325
326 AssertDeath(SIGSEGV);
327 FinishIntercept(&intercept_result);
328
329 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
330
331 std::string result;
332 ConsumeFd(std::move(output_fd), &result);
333 ASSERT_MATCH(result, R"(signal 11 \(SIGSEGV\), code 0 \(SI_USER\), fault addr --------)");
334 ASSERT_MATCH(result, R"(backtrace:)");
335}
336
337TEST_F(CrasherTest, abort_message) {
338 int intercept_result;
339 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800340 StartProcess([]() {
341 android_set_abort_message("abort message goes here");
342 abort();
343 });
Josh Gaocbe70cb2016-10-18 18:17:52 -0700344 StartIntercept(&output_fd);
345 FinishCrasher();
346 AssertDeath(SIGABRT);
347 FinishIntercept(&intercept_result);
348
349 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
350
351 std::string result;
352 ConsumeFd(std::move(output_fd), &result);
Josh Gao502cfd22017-02-17 01:39:15 -0800353 ASSERT_MATCH(result, R"(Abort message: 'abort message goes here')");
Josh Gaocbe70cb2016-10-18 18:17:52 -0700354}
355
Josh Gaoe06f2a42017-04-27 16:50:38 -0700356TEST_F(CrasherTest, abort_message_backtrace) {
357 int intercept_result;
358 unique_fd output_fd;
359 StartProcess([]() {
360 android_set_abort_message("not actually aborting");
361 raise(DEBUGGER_SIGNAL);
362 exit(0);
363 });
364 StartIntercept(&output_fd);
365 FinishCrasher();
366 AssertDeath(0);
367 FinishIntercept(&intercept_result);
368
369 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
370
371 std::string result;
372 ConsumeFd(std::move(output_fd), &result);
373 ASSERT_NOT_MATCH(result, R"(Abort message:)");
374}
375
Josh Gaocbe70cb2016-10-18 18:17:52 -0700376TEST_F(CrasherTest, intercept_timeout) {
377 int intercept_result;
378 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800379 StartProcess([]() {
380 abort();
381 });
Josh Gaocbe70cb2016-10-18 18:17:52 -0700382 StartIntercept(&output_fd);
383
384 // Don't let crasher finish until we timeout.
385 FinishIntercept(&intercept_result);
386
387 ASSERT_NE(1, intercept_result) << "tombstoned reported success? (intercept_result = "
388 << intercept_result << ")";
389
390 FinishCrasher();
391 AssertDeath(SIGABRT);
392}
393
394TEST_F(CrasherTest, wait_for_gdb) {
395 if (!android::base::SetProperty(kWaitForGdbKey, "1")) {
396 FAIL() << "failed to enable wait_for_gdb";
397 }
398 sleep(1);
399
Josh Gao502cfd22017-02-17 01:39:15 -0800400 StartProcess([]() {
401 abort();
402 });
Josh Gaocbe70cb2016-10-18 18:17:52 -0700403 FinishCrasher();
404
405 int status;
406 ASSERT_EQ(crasher_pid, waitpid(crasher_pid, &status, WUNTRACED));
407 ASSERT_TRUE(WIFSTOPPED(status));
408 ASSERT_EQ(SIGSTOP, WSTOPSIG(status));
409
410 ASSERT_EQ(0, kill(crasher_pid, SIGCONT));
411
412 AssertDeath(SIGABRT);
413}
414
Josh Gao7c6e3132017-01-22 17:59:02 -0800415// wait_for_gdb shouldn't trigger on manually sent signals.
Josh Gaocbe70cb2016-10-18 18:17:52 -0700416TEST_F(CrasherTest, wait_for_gdb_signal) {
417 if (!android::base::SetProperty(kWaitForGdbKey, "1")) {
418 FAIL() << "failed to enable wait_for_gdb";
419 }
420
Josh Gao502cfd22017-02-17 01:39:15 -0800421 StartProcess([]() {
422 abort();
423 });
Josh Gao7c6e3132017-01-22 17:59:02 -0800424 ASSERT_EQ(0, kill(crasher_pid, SIGSEGV)) << strerror(errno);
425 AssertDeath(SIGSEGV);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700426}
427
428TEST_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);
446 ASSERT_MATCH(result, R"(#00 pc [0-9a-f]+ /system/lib)" ARCH_SUFFIX R"(/libc.so \(read\+)");
447
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);
457 ASSERT_MATCH(result, R"(#00 pc [0-9a-f]+\s+ /system/lib)" ARCH_SUFFIX R"(/libc.so \(tgkill)");
458}
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);
477 ASSERT_MATCH(result, R"(#00 pc [0-9a-f]+\s+ /system/lib)" ARCH_SUFFIX R"(/libc.so \(tgkill)");
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) <<<)");
Josh Gao502cfd22017-02-17 01:39:15 -0800534 ASSERT_MATCH(result, R"(#00 pc [0-9a-f]+\s+ /system/lib)" ARCH_SUFFIX R"(/libc.so \(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);
565 ASSERT_MATCH(result, R"(#00 pc [0-9a-f]+\s+ /system/lib)" ARCH_SUFFIX R"(/libc.so \(tgkill)");
566}
567
Josh Gaoc3c8c022017-02-13 16:36:18 -0800568TEST(crash_dump, zombie) {
569 pid_t forkpid = fork();
570
Josh Gaoc3c8c022017-02-13 16:36:18 -0800571 pid_t rc;
572 int status;
573
574 if (forkpid == 0) {
575 errno = 0;
576 rc = waitpid(-1, &status, WNOHANG | __WALL | __WNOTHREAD);
577 if (rc != -1 || errno != ECHILD) {
578 errx(2, "first waitpid returned %d (%s), expected failure with ECHILD", rc, strerror(errno));
579 }
580
581 raise(DEBUGGER_SIGNAL);
582
583 errno = 0;
584 rc = waitpid(-1, &status, __WALL | __WNOTHREAD);
585 if (rc != -1 || errno != ECHILD) {
586 errx(2, "second waitpid returned %d (%s), expected failure with ECHILD", rc, strerror(errno));
587 }
588 _exit(0);
589 } else {
590 rc = waitpid(forkpid, &status, 0);
591 ASSERT_EQ(forkpid, rc);
592 ASSERT_TRUE(WIFEXITED(status));
593 ASSERT_EQ(0, WEXITSTATUS(status));
594 }
595}
Josh Gao352a8452017-03-30 16:46:21 -0700596
597TEST(tombstoned, no_notify) {
598 // Do this a few times.
599 for (int i = 0; i < 3; ++i) {
600 pid_t pid = 123'456'789 + i;
601
602 unique_fd intercept_fd, output_fd;
Narayan Kamathca5e9082017-06-02 15:42:06 +0100603 InterceptStatus status;
604 tombstoned_intercept(pid, &intercept_fd, &output_fd, &status, kDebuggerdTombstone);
605 ASSERT_EQ(InterceptStatus::kRegistered, status);
Josh Gao352a8452017-03-30 16:46:21 -0700606
607 {
608 unique_fd tombstoned_socket, input_fd;
Narayan Kamatha73df602017-05-24 15:07:25 +0100609 ASSERT_TRUE(tombstoned_connect(pid, &tombstoned_socket, &input_fd, kDebuggerdTombstone));
Josh Gao352a8452017-03-30 16:46:21 -0700610 ASSERT_TRUE(android::base::WriteFully(input_fd.get(), &pid, sizeof(pid)));
611 }
612
613 pid_t read_pid;
614 ASSERT_TRUE(android::base::ReadFully(output_fd.get(), &read_pid, sizeof(read_pid)));
615 ASSERT_EQ(read_pid, pid);
616 }
617}
618
619TEST(tombstoned, stress) {
620 // Spawn threads to simultaneously do a bunch of failing dumps and a bunch of successful dumps.
621 static constexpr int kDumpCount = 100;
622
623 std::atomic<bool> start(false);
624 std::vector<std::thread> threads;
625 threads.emplace_back([&start]() {
626 while (!start) {
627 continue;
628 }
629
630 // Use a way out of range pid, to avoid stomping on an actual process.
631 pid_t pid_base = 1'000'000;
632
633 for (int dump = 0; dump < kDumpCount; ++dump) {
634 pid_t pid = pid_base + dump;
635
636 unique_fd intercept_fd, output_fd;
Narayan Kamathca5e9082017-06-02 15:42:06 +0100637 InterceptStatus status;
638 tombstoned_intercept(pid, &intercept_fd, &output_fd, &status, kDebuggerdTombstone);
639 ASSERT_EQ(InterceptStatus::kRegistered, status);
Josh Gao352a8452017-03-30 16:46:21 -0700640
641 // Pretend to crash, and then immediately close the socket.
642 unique_fd sockfd(socket_local_client(kTombstonedCrashSocketName,
643 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_SEQPACKET));
644 if (sockfd == -1) {
645 FAIL() << "failed to connect to tombstoned: " << strerror(errno);
646 }
647 TombstonedCrashPacket packet = {};
648 packet.packet_type = CrashPacketType::kDumpRequest;
649 packet.packet.dump_request.pid = pid;
650 if (TEMP_FAILURE_RETRY(write(sockfd, &packet, sizeof(packet))) != sizeof(packet)) {
651 FAIL() << "failed to write to tombstoned: " << strerror(errno);
652 }
653
654 continue;
655 }
656 });
657
658 threads.emplace_back([&start]() {
659 while (!start) {
660 continue;
661 }
662
663 // Use a way out of range pid, to avoid stomping on an actual process.
664 pid_t pid_base = 2'000'000;
665
666 for (int dump = 0; dump < kDumpCount; ++dump) {
667 pid_t pid = pid_base + dump;
668
669 unique_fd intercept_fd, output_fd;
Narayan Kamathca5e9082017-06-02 15:42:06 +0100670 InterceptStatus status;
671 tombstoned_intercept(pid, &intercept_fd, &output_fd, &status, kDebuggerdTombstone);
672 ASSERT_EQ(InterceptStatus::kRegistered, status);
Josh Gao352a8452017-03-30 16:46:21 -0700673
674 {
675 unique_fd tombstoned_socket, input_fd;
Narayan Kamatha73df602017-05-24 15:07:25 +0100676 ASSERT_TRUE(tombstoned_connect(pid, &tombstoned_socket, &input_fd, kDebuggerdTombstone));
Josh Gao352a8452017-03-30 16:46:21 -0700677 ASSERT_TRUE(android::base::WriteFully(input_fd.get(), &pid, sizeof(pid)));
678 tombstoned_notify_completion(tombstoned_socket.get());
679 }
680
681 // TODO: Fix the race that requires this sleep.
682 std::this_thread::sleep_for(50ms);
683
684 pid_t read_pid;
685 ASSERT_TRUE(android::base::ReadFully(output_fd.get(), &read_pid, sizeof(read_pid)));
686 ASSERT_EQ(read_pid, pid);
687 }
688 });
689
690 start = true;
691
692 for (std::thread& thread : threads) {
693 thread.join();
694 }
695}
Narayan Kamathca5e9082017-06-02 15:42:06 +0100696
697TEST(tombstoned, java_trace_intercept_smoke) {
698 // Using a "real" PID is a little dangerous here - if the test fails
699 // or crashes, we might end up getting a bogus / unreliable stack
700 // trace.
701 const pid_t self = getpid();
702
703 unique_fd intercept_fd, output_fd;
704 InterceptStatus status;
705 tombstoned_intercept(self, &intercept_fd, &output_fd, &status, kDebuggerdJavaBacktrace);
706 ASSERT_EQ(InterceptStatus::kRegistered, status);
707
708 // First connect to tombstoned requesting a native backtrace. This
709 // should result in a "regular" FD and not the installed intercept.
710 const char native[] = "native";
711 unique_fd tombstoned_socket, input_fd;
712 ASSERT_TRUE(tombstoned_connect(self, &tombstoned_socket, &input_fd, kDebuggerdNativeBacktrace));
713 ASSERT_TRUE(android::base::WriteFully(input_fd.get(), native, sizeof(native)));
714 tombstoned_notify_completion(tombstoned_socket.get());
715
716 // Then, connect to tombstoned asking for a java backtrace. This *should*
717 // trigger the intercept.
718 const char java[] = "java";
719 ASSERT_TRUE(tombstoned_connect(self, &tombstoned_socket, &input_fd, kDebuggerdJavaBacktrace));
720 ASSERT_TRUE(android::base::WriteFully(input_fd.get(), java, sizeof(java)));
721 tombstoned_notify_completion(tombstoned_socket.get());
722
723 char outbuf[sizeof(java)];
724 ASSERT_TRUE(android::base::ReadFully(output_fd.get(), outbuf, sizeof(outbuf)));
725 ASSERT_STREQ("java", outbuf);
726}
727
728TEST(tombstoned, multiple_intercepts) {
729 const pid_t fake_pid = 1'234'567;
730 unique_fd intercept_fd, output_fd;
731 InterceptStatus status;
732 tombstoned_intercept(fake_pid, &intercept_fd, &output_fd, &status, kDebuggerdJavaBacktrace);
733 ASSERT_EQ(InterceptStatus::kRegistered, status);
734
735 unique_fd intercept_fd_2, output_fd_2;
736 tombstoned_intercept(fake_pid, &intercept_fd_2, &output_fd_2, &status, kDebuggerdNativeBacktrace);
737 ASSERT_EQ(InterceptStatus::kFailedAlreadyRegistered, status);
738}
739
740TEST(tombstoned, intercept_any) {
741 const pid_t fake_pid = 1'234'567;
742
743 unique_fd intercept_fd, output_fd;
744 InterceptStatus status;
745 tombstoned_intercept(fake_pid, &intercept_fd, &output_fd, &status, kDebuggerdNativeBacktrace);
746 ASSERT_EQ(InterceptStatus::kRegistered, status);
747
748 const char any[] = "any";
749 unique_fd tombstoned_socket, input_fd;
750 ASSERT_TRUE(tombstoned_connect(fake_pid, &tombstoned_socket, &input_fd, kDebuggerdAnyIntercept));
751 ASSERT_TRUE(android::base::WriteFully(input_fd.get(), any, sizeof(any)));
752 tombstoned_notify_completion(tombstoned_socket.get());
753
754 char outbuf[sizeof(any)];
755 ASSERT_TRUE(android::base::ReadFully(output_fd.get(), outbuf, sizeof(outbuf)));
756 ASSERT_STREQ("any", outbuf);
757}