blob: 1f0e4201e7f2e457f435de8a4ca34c512c2563b6 [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>
Josh Gao70adac62018-02-22 11:38:33 -080023#include <sys/resource.h>
Dan Albertc38057a2017-10-11 11:35:40 -070024#include <sys/syscall.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070025#include <sys/types.h>
Josh Gaofd13bf02017-08-18 15:37:26 -070026#include <unistd.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070027
28#include <chrono>
29#include <regex>
30#include <thread>
31
Josh Gaobf06a402018-08-27 16:34:01 -070032#include <android/fdsan.h>
Josh Gao502cfd22017-02-17 01:39:15 -080033#include <android/set_abort_message.h>
34
Josh Gao5f87bbd2019-01-09 17:01:49 -080035#include <android-base/cmsg.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070036#include <android-base/file.h>
37#include <android-base/logging.h>
Josh Gao2e7b8e22017-05-04 17:12:57 -070038#include <android-base/macros.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070039#include <android-base/parseint.h>
40#include <android-base/properties.h>
Josh Gao2b22ae12018-09-12 14:51:03 -070041#include <android-base/stringprintf.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070042#include <android-base/strings.h>
Josh Gao30171a82017-04-27 19:48:44 -070043#include <android-base/test_utils.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070044#include <android-base/unique_fd.h>
45#include <cutils/sockets.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070046#include <gtest/gtest.h>
47
Josh Gaoe04ca272018-01-16 15:38:17 -080048#include <libminijail.h>
49#include <scoped_minijail.h>
50
Narayan Kamath2d377cd2017-05-10 10:58:59 +010051#include "debuggerd/handler.h"
52#include "protocol.h"
53#include "tombstoned/tombstoned.h"
54#include "util.h"
55
Josh Gaocbe70cb2016-10-18 18:17:52 -070056using namespace std::chrono_literals;
Josh Gao5f87bbd2019-01-09 17:01:49 -080057
58using android::base::SendFileDescriptors;
Josh Gaocbe70cb2016-10-18 18:17:52 -070059using android::base::unique_fd;
60
61#if defined(__LP64__)
Josh Gaocbe70cb2016-10-18 18:17:52 -070062#define ARCH_SUFFIX "64"
63#else
Josh Gaocbe70cb2016-10-18 18:17:52 -070064#define ARCH_SUFFIX ""
65#endif
66
67constexpr char kWaitForGdbKey[] = "debug.debuggerd.wait_for_gdb";
68
69#define TIMEOUT(seconds, expr) \
70 [&]() { \
71 struct sigaction old_sigaction; \
72 struct sigaction new_sigaction = {}; \
73 new_sigaction.sa_handler = [](int) {}; \
74 if (sigaction(SIGALRM, &new_sigaction, &new_sigaction) != 0) { \
75 err(1, "sigaction failed"); \
76 } \
77 alarm(seconds); \
78 auto value = expr; \
79 int saved_errno = errno; \
80 if (sigaction(SIGALRM, &old_sigaction, nullptr) != 0) { \
81 err(1, "sigaction failed"); \
82 } \
83 alarm(0); \
84 errno = saved_errno; \
85 return value; \
86 }()
87
Chih-Hung Hsieh3249b3a2018-05-11 16:01:21 -070088// Backtrace frame dump could contain:
89// #01 pc 0001cded /data/tmp/debuggerd_test32 (raise_debugger_signal+80)
90// or
91// #01 pc 00022a09 /data/tmp/debuggerd_test32 (offset 0x12000) (raise_debugger_signal+80)
Josh Gaoe04ca272018-01-16 15:38:17 -080092#define ASSERT_BACKTRACE_FRAME(result, frame_name) \
Chih-Hung Hsieh3249b3a2018-05-11 16:01:21 -070093 ASSERT_MATCH(result, \
94 R"(#\d\d pc [0-9a-f]+\s+ \S+ (\(offset 0x[0-9a-f]+\) )?\()" frame_name R"(\+)");
Jaesung Chung58778e12017-06-15 18:20:34 +090095
Narayan Kamatha73df602017-05-24 15:07:25 +010096static void tombstoned_intercept(pid_t target_pid, unique_fd* intercept_fd, unique_fd* output_fd,
Narayan Kamathca5e9082017-06-02 15:42:06 +010097 InterceptStatus* status, DebuggerdDumpType intercept_type) {
Josh Gao460b3362017-03-30 16:40:47 -070098 intercept_fd->reset(socket_local_client(kTombstonedInterceptSocketName,
99 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_SEQPACKET));
100 if (intercept_fd->get() == -1) {
101 FAIL() << "failed to contact tombstoned: " << strerror(errno);
102 }
103
Narayan Kamatha73df602017-05-24 15:07:25 +0100104 InterceptRequest req = {.pid = target_pid, .dump_type = intercept_type};
Josh Gao460b3362017-03-30 16:40:47 -0700105
106 unique_fd output_pipe_write;
107 if (!Pipe(output_fd, &output_pipe_write)) {
108 FAIL() << "failed to create output pipe: " << strerror(errno);
109 }
110
111 std::string pipe_size_str;
112 int pipe_buffer_size;
113 if (!android::base::ReadFileToString("/proc/sys/fs/pipe-max-size", &pipe_size_str)) {
114 FAIL() << "failed to read /proc/sys/fs/pipe-max-size: " << strerror(errno);
115 }
116
117 pipe_size_str = android::base::Trim(pipe_size_str);
118
119 if (!android::base::ParseInt(pipe_size_str.c_str(), &pipe_buffer_size, 0)) {
120 FAIL() << "failed to parse pipe max size";
121 }
122
123 if (fcntl(output_fd->get(), F_SETPIPE_SZ, pipe_buffer_size) != pipe_buffer_size) {
124 FAIL() << "failed to set pipe size: " << strerror(errno);
125 }
126
Josh Gao5675f3c2017-06-01 12:19:53 -0700127 ASSERT_GE(pipe_buffer_size, 1024 * 1024);
128
Josh Gao5f87bbd2019-01-09 17:01:49 -0800129 ssize_t rc = SendFileDescriptors(intercept_fd->get(), &req, sizeof(req), output_pipe_write.get());
130 output_pipe_write.reset();
131 if (rc != sizeof(req)) {
Josh Gao460b3362017-03-30 16:40:47 -0700132 FAIL() << "failed to send output fd to tombstoned: " << strerror(errno);
133 }
134
135 InterceptResponse response;
Josh Gao5f87bbd2019-01-09 17:01:49 -0800136 rc = TEMP_FAILURE_RETRY(read(intercept_fd->get(), &response, sizeof(response)));
Josh Gao460b3362017-03-30 16:40:47 -0700137 if (rc == -1) {
138 FAIL() << "failed to read response from tombstoned: " << strerror(errno);
139 } else if (rc == 0) {
140 FAIL() << "failed to read response from tombstoned (EOF)";
141 } else if (rc != sizeof(response)) {
142 FAIL() << "received packet of unexpected length from tombstoned: expected " << sizeof(response)
143 << ", received " << rc;
144 }
145
Narayan Kamathca5e9082017-06-02 15:42:06 +0100146 *status = response.status;
Josh Gao460b3362017-03-30 16:40:47 -0700147}
148
Josh Gaocbe70cb2016-10-18 18:17:52 -0700149class CrasherTest : public ::testing::Test {
150 public:
151 pid_t crasher_pid = -1;
152 bool previous_wait_for_gdb;
153 unique_fd crasher_pipe;
154 unique_fd intercept_fd;
155
156 CrasherTest();
157 ~CrasherTest();
158
Narayan Kamatha73df602017-05-24 15:07:25 +0100159 void StartIntercept(unique_fd* output_fd, DebuggerdDumpType intercept_type = kDebuggerdTombstone);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700160
161 // Returns -1 if we fail to read a response from tombstoned, otherwise the received return code.
162 void FinishIntercept(int* result);
163
Josh Gao2e7b8e22017-05-04 17:12:57 -0700164 void StartProcess(std::function<void()> function, std::function<pid_t()> forker = fork);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700165 void StartCrasher(const std::string& crash_type);
166 void FinishCrasher();
167 void AssertDeath(int signo);
168};
169
170CrasherTest::CrasherTest() {
171 previous_wait_for_gdb = android::base::GetBoolProperty(kWaitForGdbKey, false);
172 android::base::SetProperty(kWaitForGdbKey, "0");
173}
174
175CrasherTest::~CrasherTest() {
176 if (crasher_pid != -1) {
177 kill(crasher_pid, SIGKILL);
178 int status;
179 waitpid(crasher_pid, &status, WUNTRACED);
180 }
181
182 android::base::SetProperty(kWaitForGdbKey, previous_wait_for_gdb ? "1" : "0");
183}
184
Narayan Kamatha73df602017-05-24 15:07:25 +0100185void CrasherTest::StartIntercept(unique_fd* output_fd, DebuggerdDumpType intercept_type) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700186 if (crasher_pid == -1) {
187 FAIL() << "crasher hasn't been started";
188 }
189
Narayan Kamathca5e9082017-06-02 15:42:06 +0100190 InterceptStatus status;
191 tombstoned_intercept(crasher_pid, &this->intercept_fd, output_fd, &status, intercept_type);
192 ASSERT_EQ(InterceptStatus::kRegistered, status);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700193}
194
195void CrasherTest::FinishIntercept(int* result) {
196 InterceptResponse response;
197
198 // Timeout for tombstoned intercept is 10 seconds.
199 ssize_t rc = TIMEOUT(20, read(intercept_fd.get(), &response, sizeof(response)));
200 if (rc == -1) {
201 FAIL() << "failed to read response from tombstoned: " << strerror(errno);
202 } else if (rc == 0) {
203 *result = -1;
204 } else if (rc != sizeof(response)) {
205 FAIL() << "received packet of unexpected length from tombstoned: expected " << sizeof(response)
206 << ", received " << rc;
207 } else {
Josh Gao460b3362017-03-30 16:40:47 -0700208 *result = response.status == InterceptStatus::kStarted ? 1 : 0;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700209 }
210}
211
Josh Gao2e7b8e22017-05-04 17:12:57 -0700212void CrasherTest::StartProcess(std::function<void()> function, std::function<pid_t()> forker) {
Josh Gaofca7ca32017-01-23 12:05:35 -0800213 unique_fd read_pipe;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700214 unique_fd crasher_read_pipe;
215 if (!Pipe(&crasher_read_pipe, &crasher_pipe)) {
216 FAIL() << "failed to create pipe: " << strerror(errno);
217 }
218
Josh Gao2e7b8e22017-05-04 17:12:57 -0700219 crasher_pid = forker();
Josh Gaocbe70cb2016-10-18 18:17:52 -0700220 if (crasher_pid == -1) {
221 FAIL() << "fork failed: " << strerror(errno);
222 } else if (crasher_pid == 0) {
Josh Gao502cfd22017-02-17 01:39:15 -0800223 char dummy;
224 crasher_pipe.reset();
225 TEMP_FAILURE_RETRY(read(crasher_read_pipe.get(), &dummy, 1));
Josh Gaofca7ca32017-01-23 12:05:35 -0800226 function();
227 _exit(0);
228 }
229}
230
Josh Gaocbe70cb2016-10-18 18:17:52 -0700231void CrasherTest::FinishCrasher() {
232 if (crasher_pipe == -1) {
233 FAIL() << "crasher pipe uninitialized";
234 }
235
236 ssize_t rc = write(crasher_pipe.get(), "\n", 1);
237 if (rc == -1) {
238 FAIL() << "failed to write to crasher pipe: " << strerror(errno);
239 } else if (rc == 0) {
240 FAIL() << "crasher pipe was closed";
241 }
242}
243
244void CrasherTest::AssertDeath(int signo) {
245 int status;
Christopher Ferrisafc0ff72019-06-26 15:08:51 -0700246 pid_t pid = TIMEOUT(10, waitpid(crasher_pid, &status, 0));
Josh Gaocbe70cb2016-10-18 18:17:52 -0700247 if (pid != crasher_pid) {
Christopher Ferrisafc0ff72019-06-26 15:08:51 -0700248 printf("failed to wait for crasher (expected pid %d, return value %d): %s\n", crasher_pid, pid,
249 strerror(errno));
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700250 sleep(100);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700251 FAIL() << "failed to wait for crasher: " << strerror(errno);
252 }
253
Josh Gaoe06f2a42017-04-27 16:50:38 -0700254 if (signo == 0) {
255 ASSERT_TRUE(WIFEXITED(status));
256 ASSERT_EQ(0, WEXITSTATUS(signo));
257 } else {
258 ASSERT_FALSE(WIFEXITED(status));
259 ASSERT_TRUE(WIFSIGNALED(status)) << "crasher didn't terminate via a signal";
260 ASSERT_EQ(signo, WTERMSIG(status));
Josh Gaocbe70cb2016-10-18 18:17:52 -0700261 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700262 crasher_pid = -1;
263}
264
265static void ConsumeFd(unique_fd fd, std::string* output) {
266 constexpr size_t read_length = PAGE_SIZE;
267 std::string result;
268
269 while (true) {
270 size_t offset = result.size();
271 result.resize(result.size() + PAGE_SIZE);
272 ssize_t rc = TEMP_FAILURE_RETRY(read(fd.get(), &result[offset], read_length));
273 if (rc == -1) {
274 FAIL() << "read failed: " << strerror(errno);
275 } else if (rc == 0) {
276 result.resize(result.size() - PAGE_SIZE);
277 break;
278 }
279
280 result.resize(result.size() - PAGE_SIZE + rc);
281 }
282
283 *output = std::move(result);
284}
285
286TEST_F(CrasherTest, smoke) {
287 int intercept_result;
288 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800289 StartProcess([]() {
290 *reinterpret_cast<volatile char*>(0xdead) = '1';
291 });
292
Josh Gaocbe70cb2016-10-18 18:17:52 -0700293 StartIntercept(&output_fd);
294 FinishCrasher();
295 AssertDeath(SIGSEGV);
296 FinishIntercept(&intercept_result);
297
298 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
299
300 std::string result;
301 ConsumeFd(std::move(output_fd), &result);
302 ASSERT_MATCH(result, R"(signal 11 \(SIGSEGV\), code 1 \(SEGV_MAPERR\), fault addr 0xdead)");
303}
304
Josh Gaocdea7502017-11-01 15:00:40 -0700305TEST_F(CrasherTest, LD_PRELOAD) {
306 int intercept_result;
307 unique_fd output_fd;
308 StartProcess([]() {
309 setenv("LD_PRELOAD", "nonexistent.so", 1);
310 *reinterpret_cast<volatile char*>(0xdead) = '1';
311 });
312
313 StartIntercept(&output_fd);
314 FinishCrasher();
315 AssertDeath(SIGSEGV);
316 FinishIntercept(&intercept_result);
317
318 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
319
320 std::string result;
321 ConsumeFd(std::move(output_fd), &result);
322 ASSERT_MATCH(result, R"(signal 11 \(SIGSEGV\), code 1 \(SEGV_MAPERR\), fault addr 0xdead)");
323}
324
Josh Gaocbe70cb2016-10-18 18:17:52 -0700325TEST_F(CrasherTest, abort) {
326 int intercept_result;
327 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800328 StartProcess([]() {
329 abort();
330 });
Josh Gaocbe70cb2016-10-18 18:17:52 -0700331 StartIntercept(&output_fd);
332 FinishCrasher();
333 AssertDeath(SIGABRT);
334 FinishIntercept(&intercept_result);
335
336 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
337
338 std::string result;
339 ConsumeFd(std::move(output_fd), &result);
Andreas Gampe26cbafb2017-06-22 20:14:43 -0700340 ASSERT_BACKTRACE_FRAME(result, "abort");
Josh Gaocbe70cb2016-10-18 18:17:52 -0700341}
342
343TEST_F(CrasherTest, signal) {
344 int intercept_result;
345 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800346 StartProcess([]() {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700347 while (true) {
348 sleep(1);
349 }
Josh Gao502cfd22017-02-17 01:39:15 -0800350 });
Josh Gaocbe70cb2016-10-18 18:17:52 -0700351 StartIntercept(&output_fd);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700352 FinishCrasher();
Josh Gaocbe70cb2016-10-18 18:17:52 -0700353 ASSERT_EQ(0, kill(crasher_pid, SIGSEGV));
354
355 AssertDeath(SIGSEGV);
356 FinishIntercept(&intercept_result);
357
358 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
359
360 std::string result;
361 ConsumeFd(std::move(output_fd), &result);
Elliott Hughes89722702018-05-02 10:47:00 -0700362 ASSERT_MATCH(
363 result,
364 R"(signal 11 \(SIGSEGV\), code 0 \(SI_USER from pid \d+, uid \d+\), fault addr --------)");
Josh Gaocbe70cb2016-10-18 18:17:52 -0700365 ASSERT_MATCH(result, R"(backtrace:)");
366}
367
368TEST_F(CrasherTest, abort_message) {
369 int intercept_result;
370 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800371 StartProcess([]() {
Josh Gao1cc7bd82018-02-13 13:16:17 -0800372 // Arrived at experimentally;
373 // logd truncates at 4062.
374 // strlen("Abort message: ''") is 17.
375 // That's 4045, but we also want a NUL.
376 char buf[4045 + 1];
377 memset(buf, 'x', sizeof(buf));
378 buf[sizeof(buf) - 1] = '\0';
379 android_set_abort_message(buf);
Josh Gao502cfd22017-02-17 01:39:15 -0800380 abort();
381 });
Josh Gaocbe70cb2016-10-18 18:17:52 -0700382 StartIntercept(&output_fd);
383 FinishCrasher();
384 AssertDeath(SIGABRT);
385 FinishIntercept(&intercept_result);
386
387 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
388
389 std::string result;
390 ConsumeFd(std::move(output_fd), &result);
Josh Gao1cc7bd82018-02-13 13:16:17 -0800391 ASSERT_MATCH(result, R"(Abort message: 'x{4045}')");
Josh Gaocbe70cb2016-10-18 18:17:52 -0700392}
393
Josh Gaoe06f2a42017-04-27 16:50:38 -0700394TEST_F(CrasherTest, abort_message_backtrace) {
395 int intercept_result;
396 unique_fd output_fd;
397 StartProcess([]() {
398 android_set_abort_message("not actually aborting");
399 raise(DEBUGGER_SIGNAL);
400 exit(0);
401 });
402 StartIntercept(&output_fd);
403 FinishCrasher();
404 AssertDeath(0);
405 FinishIntercept(&intercept_result);
406
407 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
408
409 std::string result;
410 ConsumeFd(std::move(output_fd), &result);
411 ASSERT_NOT_MATCH(result, R"(Abort message:)");
412}
413
Josh Gaocbe70cb2016-10-18 18:17:52 -0700414TEST_F(CrasherTest, intercept_timeout) {
415 int intercept_result;
416 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800417 StartProcess([]() {
418 abort();
419 });
Josh Gaocbe70cb2016-10-18 18:17:52 -0700420 StartIntercept(&output_fd);
421
422 // Don't let crasher finish until we timeout.
423 FinishIntercept(&intercept_result);
424
425 ASSERT_NE(1, intercept_result) << "tombstoned reported success? (intercept_result = "
426 << intercept_result << ")";
427
428 FinishCrasher();
429 AssertDeath(SIGABRT);
430}
431
432TEST_F(CrasherTest, wait_for_gdb) {
433 if (!android::base::SetProperty(kWaitForGdbKey, "1")) {
434 FAIL() << "failed to enable wait_for_gdb";
435 }
436 sleep(1);
437
Josh Gao502cfd22017-02-17 01:39:15 -0800438 StartProcess([]() {
439 abort();
440 });
Josh Gaocbe70cb2016-10-18 18:17:52 -0700441 FinishCrasher();
442
443 int status;
444 ASSERT_EQ(crasher_pid, waitpid(crasher_pid, &status, WUNTRACED));
445 ASSERT_TRUE(WIFSTOPPED(status));
446 ASSERT_EQ(SIGSTOP, WSTOPSIG(status));
447
448 ASSERT_EQ(0, kill(crasher_pid, SIGCONT));
449
450 AssertDeath(SIGABRT);
451}
452
Josh Gaocbe70cb2016-10-18 18:17:52 -0700453TEST_F(CrasherTest, backtrace) {
454 std::string result;
455 int intercept_result;
456 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800457
458 StartProcess([]() {
459 abort();
460 });
Narayan Kamatha73df602017-05-24 15:07:25 +0100461 StartIntercept(&output_fd, kDebuggerdNativeBacktrace);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700462
463 std::this_thread::sleep_for(500ms);
464
465 sigval val;
466 val.sival_int = 1;
467 ASSERT_EQ(0, sigqueue(crasher_pid, DEBUGGER_SIGNAL, val)) << strerror(errno);
468 FinishIntercept(&intercept_result);
469 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
470 ConsumeFd(std::move(output_fd), &result);
Jaesung Chung58778e12017-06-15 18:20:34 +0900471 ASSERT_BACKTRACE_FRAME(result, "read");
Josh Gaocbe70cb2016-10-18 18:17:52 -0700472
473 int status;
474 ASSERT_EQ(0, waitpid(crasher_pid, &status, WNOHANG | WUNTRACED));
475
476 StartIntercept(&output_fd);
477 FinishCrasher();
478 AssertDeath(SIGABRT);
479 FinishIntercept(&intercept_result);
480 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
481 ConsumeFd(std::move(output_fd), &result);
Andreas Gampe26cbafb2017-06-22 20:14:43 -0700482 ASSERT_BACKTRACE_FRAME(result, "abort");
Josh Gaocbe70cb2016-10-18 18:17:52 -0700483}
Josh Gaofca7ca32017-01-23 12:05:35 -0800484
485TEST_F(CrasherTest, PR_SET_DUMPABLE_0_crash) {
Josh Gao502cfd22017-02-17 01:39:15 -0800486 int intercept_result;
487 unique_fd output_fd;
Josh Gaofca7ca32017-01-23 12:05:35 -0800488 StartProcess([]() {
489 prctl(PR_SET_DUMPABLE, 0);
Josh Gao502cfd22017-02-17 01:39:15 -0800490 abort();
Josh Gaofca7ca32017-01-23 12:05:35 -0800491 });
Josh Gao502cfd22017-02-17 01:39:15 -0800492
493 StartIntercept(&output_fd);
494 FinishCrasher();
495 AssertDeath(SIGABRT);
496 FinishIntercept(&intercept_result);
497
498 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
499
500 std::string result;
501 ConsumeFd(std::move(output_fd), &result);
Andreas Gampe26cbafb2017-06-22 20:14:43 -0700502 ASSERT_BACKTRACE_FRAME(result, "abort");
Josh Gaofca7ca32017-01-23 12:05:35 -0800503}
504
Josh Gao502cfd22017-02-17 01:39:15 -0800505TEST_F(CrasherTest, capabilities) {
506 ASSERT_EQ(0U, getuid()) << "capability test requires root";
507
Josh Gaofca7ca32017-01-23 12:05:35 -0800508 StartProcess([]() {
Josh Gao502cfd22017-02-17 01:39:15 -0800509 if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) != 0) {
510 err(1, "failed to set PR_SET_KEEPCAPS");
511 }
512
513 if (setresuid(1, 1, 1) != 0) {
514 err(1, "setresuid failed");
515 }
516
517 __user_cap_header_struct capheader;
518 __user_cap_data_struct capdata[2];
519 memset(&capheader, 0, sizeof(capheader));
520 memset(&capdata, 0, sizeof(capdata));
521
522 capheader.version = _LINUX_CAPABILITY_VERSION_3;
523 capheader.pid = 0;
524
525 // Turn on every third capability.
526 static_assert(CAP_LAST_CAP > 33, "CAP_LAST_CAP <= 32");
527 for (int i = 0; i < CAP_LAST_CAP; i += 3) {
528 capdata[CAP_TO_INDEX(i)].permitted |= CAP_TO_MASK(i);
529 capdata[CAP_TO_INDEX(i)].effective |= CAP_TO_MASK(i);
530 }
531
532 // Make sure CAP_SYS_PTRACE is off.
533 capdata[CAP_TO_INDEX(CAP_SYS_PTRACE)].permitted &= ~(CAP_TO_MASK(CAP_SYS_PTRACE));
534 capdata[CAP_TO_INDEX(CAP_SYS_PTRACE)].effective &= ~(CAP_TO_MASK(CAP_SYS_PTRACE));
535
536 if (capset(&capheader, &capdata[0]) != 0) {
537 err(1, "capset failed");
538 }
539
540 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) != 0) {
541 err(1, "failed to drop ambient capabilities");
542 }
543
Josh Gaoa5199a92017-04-03 13:18:34 -0700544 pthread_setname_np(pthread_self(), "thread_name");
Josh Gao502cfd22017-02-17 01:39:15 -0800545 raise(SIGSYS);
Josh Gaofca7ca32017-01-23 12:05:35 -0800546 });
Josh Gao502cfd22017-02-17 01:39:15 -0800547
548 unique_fd output_fd;
549 StartIntercept(&output_fd);
550 FinishCrasher();
551 AssertDeath(SIGSYS);
552
553 std::string result;
554 int intercept_result;
555 FinishIntercept(&intercept_result);
556 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
557 ConsumeFd(std::move(output_fd), &result);
Josh Gaoa5199a92017-04-03 13:18:34 -0700558 ASSERT_MATCH(result, R"(name: thread_name\s+>>> .+debuggerd_test(32|64) <<<)");
Jaesung Chung58778e12017-06-15 18:20:34 +0900559 ASSERT_BACKTRACE_FRAME(result, "tgkill");
Josh Gaofca7ca32017-01-23 12:05:35 -0800560}
Josh Gaoc3c8c022017-02-13 16:36:18 -0800561
Josh Gao2e7b8e22017-05-04 17:12:57 -0700562TEST_F(CrasherTest, fake_pid) {
563 int intercept_result;
564 unique_fd output_fd;
565
566 // Prime the getpid/gettid caches.
567 UNUSED(getpid());
568 UNUSED(gettid());
569
570 std::function<pid_t()> clone_fn = []() {
571 return syscall(__NR_clone, SIGCHLD, nullptr, nullptr, nullptr, nullptr);
572 };
573 StartProcess(
574 []() {
575 ASSERT_NE(getpid(), syscall(__NR_getpid));
576 ASSERT_NE(gettid(), syscall(__NR_gettid));
577 raise(SIGSEGV);
578 },
579 clone_fn);
580
581 StartIntercept(&output_fd);
582 FinishCrasher();
583 AssertDeath(SIGSEGV);
584 FinishIntercept(&intercept_result);
585
586 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
587
588 std::string result;
589 ConsumeFd(std::move(output_fd), &result);
Jaesung Chung58778e12017-06-15 18:20:34 +0900590 ASSERT_BACKTRACE_FRAME(result, "tgkill");
Josh Gao2e7b8e22017-05-04 17:12:57 -0700591}
592
Josh Gaoe04ca272018-01-16 15:38:17 -0800593static const char* const kDebuggerdSeccompPolicy =
594 "/system/etc/seccomp_policy/crash_dump." ABI_STRING ".policy";
595
Josh Gao70adac62018-02-22 11:38:33 -0800596static pid_t seccomp_fork_impl(void (*prejail)()) {
Josh Gao6f9eeec2018-09-12 13:55:47 -0700597 std::string policy;
598 if (!android::base::ReadFileToString(kDebuggerdSeccompPolicy, &policy)) {
599 PLOG(FATAL) << "failed to read policy file";
600 }
601
602 // Allow a bunch of syscalls used by the tests.
603 policy += "\nclone: 1";
604 policy += "\nsigaltstack: 1";
605 policy += "\nnanosleep: 1";
606
607 FILE* tmp_file = tmpfile();
608 if (!tmp_file) {
609 PLOG(FATAL) << "tmpfile failed";
610 }
611
612 unique_fd tmp_fd(dup(fileno(tmp_file)));
613 if (!android::base::WriteStringToFd(policy, tmp_fd.get())) {
614 PLOG(FATAL) << "failed to write policy to tmpfile";
615 }
616
617 if (lseek(tmp_fd.get(), 0, SEEK_SET) != 0) {
618 PLOG(FATAL) << "failed to seek tmp_fd";
Josh Gaoe04ca272018-01-16 15:38:17 -0800619 }
620
621 ScopedMinijail jail{minijail_new()};
622 if (!jail) {
623 LOG(FATAL) << "failed to create minijail";
624 }
625
626 minijail_no_new_privs(jail.get());
627 minijail_log_seccomp_filter_failures(jail.get());
628 minijail_use_seccomp_filter(jail.get());
Josh Gao6f9eeec2018-09-12 13:55:47 -0700629 minijail_parse_seccomp_filters_from_fd(jail.get(), tmp_fd.release());
Josh Gaoe04ca272018-01-16 15:38:17 -0800630
631 pid_t result = fork();
632 if (result == -1) {
633 return result;
634 } else if (result != 0) {
635 return result;
636 }
637
638 // Spawn and detach a thread that spins forever.
639 std::atomic<bool> thread_ready(false);
640 std::thread thread([&jail, &thread_ready]() {
641 minijail_enter(jail.get());
642 thread_ready = true;
643 for (;;)
644 ;
645 });
646 thread.detach();
647
648 while (!thread_ready) {
649 continue;
650 }
651
Josh Gao70adac62018-02-22 11:38:33 -0800652 if (prejail) {
653 prejail();
654 }
655
Josh Gaoe04ca272018-01-16 15:38:17 -0800656 minijail_enter(jail.get());
657 return result;
658}
659
Josh Gao70adac62018-02-22 11:38:33 -0800660static pid_t seccomp_fork() {
661 return seccomp_fork_impl(nullptr);
662}
663
Josh Gaoe04ca272018-01-16 15:38:17 -0800664TEST_F(CrasherTest, seccomp_crash) {
665 int intercept_result;
666 unique_fd output_fd;
667
668 StartProcess([]() { abort(); }, &seccomp_fork);
669
670 StartIntercept(&output_fd);
671 FinishCrasher();
672 AssertDeath(SIGABRT);
673 FinishIntercept(&intercept_result);
674 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
675
676 std::string result;
677 ConsumeFd(std::move(output_fd), &result);
678 ASSERT_BACKTRACE_FRAME(result, "abort");
679}
680
Josh Gao70adac62018-02-22 11:38:33 -0800681static pid_t seccomp_fork_rlimit() {
682 return seccomp_fork_impl([]() {
683 struct rlimit rlim = {
684 .rlim_cur = 512 * 1024 * 1024,
685 .rlim_max = 512 * 1024 * 1024,
686 };
687
688 if (setrlimit(RLIMIT_AS, &rlim) != 0) {
689 raise(SIGINT);
690 }
691 });
692}
693
694TEST_F(CrasherTest, seccomp_crash_oom) {
695 int intercept_result;
696 unique_fd output_fd;
697
698 StartProcess(
699 []() {
700 std::vector<void*> vec;
701 for (int i = 0; i < 512; ++i) {
702 char* buf = static_cast<char*>(malloc(1024 * 1024));
703 if (!buf) {
704 abort();
705 }
706 memset(buf, 0xff, 1024 * 1024);
707 vec.push_back(buf);
708 }
709 },
710 &seccomp_fork_rlimit);
711
712 StartIntercept(&output_fd);
713 FinishCrasher();
714 AssertDeath(SIGABRT);
715 FinishIntercept(&intercept_result);
716 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
717
718 // We can't actually generate a backtrace, just make sure that the process terminates.
719}
720
Josh Gaoe04ca272018-01-16 15:38:17 -0800721__attribute__((noinline)) extern "C" bool raise_debugger_signal(DebuggerdDumpType dump_type) {
722 siginfo_t siginfo;
723 siginfo.si_code = SI_QUEUE;
724 siginfo.si_pid = getpid();
725 siginfo.si_uid = getuid();
726
727 if (dump_type != kDebuggerdNativeBacktrace && dump_type != kDebuggerdTombstone) {
728 PLOG(FATAL) << "invalid dump type";
729 }
730
731 siginfo.si_value.sival_int = dump_type == kDebuggerdNativeBacktrace;
732
733 if (syscall(__NR_rt_tgsigqueueinfo, getpid(), gettid(), DEBUGGER_SIGNAL, &siginfo) != 0) {
734 PLOG(ERROR) << "libdebuggerd_client: failed to send signal to self";
735 return false;
736 }
737
738 return true;
739}
740
741TEST_F(CrasherTest, seccomp_tombstone) {
742 int intercept_result;
743 unique_fd output_fd;
744
745 static const auto dump_type = kDebuggerdTombstone;
746 StartProcess(
747 []() {
748 raise_debugger_signal(dump_type);
749 _exit(0);
750 },
751 &seccomp_fork);
752
753 StartIntercept(&output_fd, dump_type);
754 FinishCrasher();
755 AssertDeath(0);
756 FinishIntercept(&intercept_result);
757 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
758
759 std::string result;
760 ConsumeFd(std::move(output_fd), &result);
761 ASSERT_BACKTRACE_FRAME(result, "raise_debugger_signal");
762}
763
Josh Gao6f9eeec2018-09-12 13:55:47 -0700764extern "C" void foo() {
765 LOG(INFO) << "foo";
766 std::this_thread::sleep_for(1s);
767}
768
769extern "C" void bar() {
770 LOG(INFO) << "bar";
771 std::this_thread::sleep_for(1s);
772}
773
Josh Gaoe04ca272018-01-16 15:38:17 -0800774TEST_F(CrasherTest, seccomp_backtrace) {
775 int intercept_result;
776 unique_fd output_fd;
777
778 static const auto dump_type = kDebuggerdNativeBacktrace;
779 StartProcess(
780 []() {
Josh Gao6f9eeec2018-09-12 13:55:47 -0700781 std::thread a(foo);
782 std::thread b(bar);
783
784 std::this_thread::sleep_for(100ms);
785
Josh Gaoe04ca272018-01-16 15:38:17 -0800786 raise_debugger_signal(dump_type);
787 _exit(0);
788 },
789 &seccomp_fork);
790
791 StartIntercept(&output_fd, dump_type);
792 FinishCrasher();
793 AssertDeath(0);
794 FinishIntercept(&intercept_result);
795 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
796
797 std::string result;
798 ConsumeFd(std::move(output_fd), &result);
799 ASSERT_BACKTRACE_FRAME(result, "raise_debugger_signal");
Josh Gao6f9eeec2018-09-12 13:55:47 -0700800 ASSERT_BACKTRACE_FRAME(result, "foo");
801 ASSERT_BACKTRACE_FRAME(result, "bar");
Josh Gaoe04ca272018-01-16 15:38:17 -0800802}
803
804TEST_F(CrasherTest, seccomp_crash_logcat) {
805 StartProcess([]() { abort(); }, &seccomp_fork);
806 FinishCrasher();
807
808 // Make sure we don't get SIGSYS when trying to dump a crash to logcat.
809 AssertDeath(SIGABRT);
810}
811
Josh Gaofd13bf02017-08-18 15:37:26 -0700812TEST_F(CrasherTest, competing_tracer) {
813 int intercept_result;
814 unique_fd output_fd;
815 StartProcess([]() {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700816 raise(SIGABRT);
Josh Gaofd13bf02017-08-18 15:37:26 -0700817 });
818
819 StartIntercept(&output_fd);
Josh Gaofd13bf02017-08-18 15:37:26 -0700820
821 ASSERT_EQ(0, ptrace(PTRACE_SEIZE, crasher_pid, 0, 0));
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700822 FinishCrasher();
Josh Gaofd13bf02017-08-18 15:37:26 -0700823
824 int status;
825 ASSERT_EQ(crasher_pid, waitpid(crasher_pid, &status, 0));
826 ASSERT_TRUE(WIFSTOPPED(status));
827 ASSERT_EQ(SIGABRT, WSTOPSIG(status));
828
829 ASSERT_EQ(0, ptrace(PTRACE_CONT, crasher_pid, 0, SIGABRT));
830 FinishIntercept(&intercept_result);
831 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
832
833 std::string result;
834 ConsumeFd(std::move(output_fd), &result);
835 std::string regex = R"(failed to attach to thread \d+, already traced by )";
836 regex += std::to_string(gettid());
837 regex += R"( \(.+debuggerd_test)";
838 ASSERT_MATCH(result, regex.c_str());
839
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700840 ASSERT_EQ(crasher_pid, waitpid(crasher_pid, &status, 0));
841 ASSERT_TRUE(WIFSTOPPED(status));
842 ASSERT_EQ(SIGABRT, WSTOPSIG(status));
843
Josh Gaofd13bf02017-08-18 15:37:26 -0700844 ASSERT_EQ(0, ptrace(PTRACE_DETACH, crasher_pid, 0, SIGABRT));
845 AssertDeath(SIGABRT);
846}
847
Josh Gaobf06a402018-08-27 16:34:01 -0700848TEST_F(CrasherTest, fdsan_warning_abort_message) {
849 int intercept_result;
850 unique_fd output_fd;
851
852 StartProcess([]() {
853 android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_WARN_ONCE);
854 unique_fd fd(open("/dev/null", O_RDONLY | O_CLOEXEC));
855 if (fd == -1) {
856 abort();
857 }
858 close(fd.get());
859 _exit(0);
860 });
861
862 StartIntercept(&output_fd);
863 FinishCrasher();
864 AssertDeath(0);
865 FinishIntercept(&intercept_result);
866 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
867
868 std::string result;
869 ConsumeFd(std::move(output_fd), &result);
870 ASSERT_MATCH(result, "Abort message: 'attempted to close");
871}
872
Josh Gaoc3c8c022017-02-13 16:36:18 -0800873TEST(crash_dump, zombie) {
874 pid_t forkpid = fork();
875
Josh Gaoc3c8c022017-02-13 16:36:18 -0800876 pid_t rc;
877 int status;
878
879 if (forkpid == 0) {
880 errno = 0;
881 rc = waitpid(-1, &status, WNOHANG | __WALL | __WNOTHREAD);
882 if (rc != -1 || errno != ECHILD) {
883 errx(2, "first waitpid returned %d (%s), expected failure with ECHILD", rc, strerror(errno));
884 }
885
886 raise(DEBUGGER_SIGNAL);
887
888 errno = 0;
889 rc = waitpid(-1, &status, __WALL | __WNOTHREAD);
890 if (rc != -1 || errno != ECHILD) {
891 errx(2, "second waitpid returned %d (%s), expected failure with ECHILD", rc, strerror(errno));
892 }
893 _exit(0);
894 } else {
895 rc = waitpid(forkpid, &status, 0);
896 ASSERT_EQ(forkpid, rc);
897 ASSERT_TRUE(WIFEXITED(status));
898 ASSERT_EQ(0, WEXITSTATUS(status));
899 }
900}
Josh Gao352a8452017-03-30 16:46:21 -0700901
902TEST(tombstoned, no_notify) {
903 // Do this a few times.
904 for (int i = 0; i < 3; ++i) {
905 pid_t pid = 123'456'789 + i;
906
907 unique_fd intercept_fd, output_fd;
Narayan Kamathca5e9082017-06-02 15:42:06 +0100908 InterceptStatus status;
909 tombstoned_intercept(pid, &intercept_fd, &output_fd, &status, kDebuggerdTombstone);
910 ASSERT_EQ(InterceptStatus::kRegistered, status);
Josh Gao352a8452017-03-30 16:46:21 -0700911
912 {
913 unique_fd tombstoned_socket, input_fd;
Narayan Kamatha73df602017-05-24 15:07:25 +0100914 ASSERT_TRUE(tombstoned_connect(pid, &tombstoned_socket, &input_fd, kDebuggerdTombstone));
Josh Gao352a8452017-03-30 16:46:21 -0700915 ASSERT_TRUE(android::base::WriteFully(input_fd.get(), &pid, sizeof(pid)));
916 }
917
918 pid_t read_pid;
919 ASSERT_TRUE(android::base::ReadFully(output_fd.get(), &read_pid, sizeof(read_pid)));
920 ASSERT_EQ(read_pid, pid);
921 }
922}
923
924TEST(tombstoned, stress) {
925 // Spawn threads to simultaneously do a bunch of failing dumps and a bunch of successful dumps.
926 static constexpr int kDumpCount = 100;
927
928 std::atomic<bool> start(false);
929 std::vector<std::thread> threads;
930 threads.emplace_back([&start]() {
931 while (!start) {
932 continue;
933 }
934
935 // Use a way out of range pid, to avoid stomping on an actual process.
936 pid_t pid_base = 1'000'000;
937
938 for (int dump = 0; dump < kDumpCount; ++dump) {
939 pid_t pid = pid_base + dump;
940
941 unique_fd intercept_fd, output_fd;
Narayan Kamathca5e9082017-06-02 15:42:06 +0100942 InterceptStatus status;
943 tombstoned_intercept(pid, &intercept_fd, &output_fd, &status, kDebuggerdTombstone);
944 ASSERT_EQ(InterceptStatus::kRegistered, status);
Josh Gao352a8452017-03-30 16:46:21 -0700945
946 // Pretend to crash, and then immediately close the socket.
947 unique_fd sockfd(socket_local_client(kTombstonedCrashSocketName,
948 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_SEQPACKET));
949 if (sockfd == -1) {
950 FAIL() << "failed to connect to tombstoned: " << strerror(errno);
951 }
952 TombstonedCrashPacket packet = {};
953 packet.packet_type = CrashPacketType::kDumpRequest;
954 packet.packet.dump_request.pid = pid;
955 if (TEMP_FAILURE_RETRY(write(sockfd, &packet, sizeof(packet))) != sizeof(packet)) {
956 FAIL() << "failed to write to tombstoned: " << strerror(errno);
957 }
958
959 continue;
960 }
961 });
962
963 threads.emplace_back([&start]() {
964 while (!start) {
965 continue;
966 }
967
968 // Use a way out of range pid, to avoid stomping on an actual process.
969 pid_t pid_base = 2'000'000;
970
971 for (int dump = 0; dump < kDumpCount; ++dump) {
972 pid_t pid = pid_base + dump;
973
974 unique_fd intercept_fd, output_fd;
Narayan Kamathca5e9082017-06-02 15:42:06 +0100975 InterceptStatus status;
976 tombstoned_intercept(pid, &intercept_fd, &output_fd, &status, kDebuggerdTombstone);
977 ASSERT_EQ(InterceptStatus::kRegistered, status);
Josh Gao352a8452017-03-30 16:46:21 -0700978
979 {
980 unique_fd tombstoned_socket, input_fd;
Narayan Kamatha73df602017-05-24 15:07:25 +0100981 ASSERT_TRUE(tombstoned_connect(pid, &tombstoned_socket, &input_fd, kDebuggerdTombstone));
Josh Gao352a8452017-03-30 16:46:21 -0700982 ASSERT_TRUE(android::base::WriteFully(input_fd.get(), &pid, sizeof(pid)));
983 tombstoned_notify_completion(tombstoned_socket.get());
984 }
985
986 // TODO: Fix the race that requires this sleep.
987 std::this_thread::sleep_for(50ms);
988
989 pid_t read_pid;
990 ASSERT_TRUE(android::base::ReadFully(output_fd.get(), &read_pid, sizeof(read_pid)));
991 ASSERT_EQ(read_pid, pid);
992 }
993 });
994
995 start = true;
996
997 for (std::thread& thread : threads) {
998 thread.join();
999 }
1000}
Narayan Kamathca5e9082017-06-02 15:42:06 +01001001
1002TEST(tombstoned, java_trace_intercept_smoke) {
1003 // Using a "real" PID is a little dangerous here - if the test fails
1004 // or crashes, we might end up getting a bogus / unreliable stack
1005 // trace.
1006 const pid_t self = getpid();
1007
1008 unique_fd intercept_fd, output_fd;
1009 InterceptStatus status;
1010 tombstoned_intercept(self, &intercept_fd, &output_fd, &status, kDebuggerdJavaBacktrace);
1011 ASSERT_EQ(InterceptStatus::kRegistered, status);
1012
1013 // First connect to tombstoned requesting a native backtrace. This
1014 // should result in a "regular" FD and not the installed intercept.
1015 const char native[] = "native";
1016 unique_fd tombstoned_socket, input_fd;
1017 ASSERT_TRUE(tombstoned_connect(self, &tombstoned_socket, &input_fd, kDebuggerdNativeBacktrace));
1018 ASSERT_TRUE(android::base::WriteFully(input_fd.get(), native, sizeof(native)));
1019 tombstoned_notify_completion(tombstoned_socket.get());
1020
1021 // Then, connect to tombstoned asking for a java backtrace. This *should*
1022 // trigger the intercept.
1023 const char java[] = "java";
1024 ASSERT_TRUE(tombstoned_connect(self, &tombstoned_socket, &input_fd, kDebuggerdJavaBacktrace));
1025 ASSERT_TRUE(android::base::WriteFully(input_fd.get(), java, sizeof(java)));
1026 tombstoned_notify_completion(tombstoned_socket.get());
1027
1028 char outbuf[sizeof(java)];
1029 ASSERT_TRUE(android::base::ReadFully(output_fd.get(), outbuf, sizeof(outbuf)));
1030 ASSERT_STREQ("java", outbuf);
1031}
1032
1033TEST(tombstoned, multiple_intercepts) {
1034 const pid_t fake_pid = 1'234'567;
1035 unique_fd intercept_fd, output_fd;
1036 InterceptStatus status;
1037 tombstoned_intercept(fake_pid, &intercept_fd, &output_fd, &status, kDebuggerdJavaBacktrace);
1038 ASSERT_EQ(InterceptStatus::kRegistered, status);
1039
1040 unique_fd intercept_fd_2, output_fd_2;
1041 tombstoned_intercept(fake_pid, &intercept_fd_2, &output_fd_2, &status, kDebuggerdNativeBacktrace);
1042 ASSERT_EQ(InterceptStatus::kFailedAlreadyRegistered, status);
1043}
1044
1045TEST(tombstoned, intercept_any) {
1046 const pid_t fake_pid = 1'234'567;
1047
1048 unique_fd intercept_fd, output_fd;
1049 InterceptStatus status;
1050 tombstoned_intercept(fake_pid, &intercept_fd, &output_fd, &status, kDebuggerdNativeBacktrace);
1051 ASSERT_EQ(InterceptStatus::kRegistered, status);
1052
1053 const char any[] = "any";
1054 unique_fd tombstoned_socket, input_fd;
1055 ASSERT_TRUE(tombstoned_connect(fake_pid, &tombstoned_socket, &input_fd, kDebuggerdAnyIntercept));
1056 ASSERT_TRUE(android::base::WriteFully(input_fd.get(), any, sizeof(any)));
1057 tombstoned_notify_completion(tombstoned_socket.get());
1058
1059 char outbuf[sizeof(any)];
1060 ASSERT_TRUE(android::base::ReadFully(output_fd.get(), outbuf, sizeof(outbuf)));
1061 ASSERT_STREQ("any", outbuf);
1062}
Josh Gao2b22ae12018-09-12 14:51:03 -07001063
1064TEST(tombstoned, interceptless_backtrace) {
1065 // Generate 50 backtraces, and then check to see that we haven't created 50 new tombstones.
1066 auto get_tombstone_timestamps = []() -> std::map<int, time_t> {
1067 std::map<int, time_t> result;
1068 for (int i = 0; i < 99; ++i) {
1069 std::string path = android::base::StringPrintf("/data/tombstones/tombstone_%02d", i);
1070 struct stat st;
1071 if (stat(path.c_str(), &st) == 0) {
1072 result[i] = st.st_mtim.tv_sec;
1073 }
1074 }
1075 return result;
1076 };
1077
1078 auto before = get_tombstone_timestamps();
1079 for (int i = 0; i < 50; ++i) {
1080 raise_debugger_signal(kDebuggerdNativeBacktrace);
1081 }
1082 auto after = get_tombstone_timestamps();
1083
1084 int diff = 0;
1085 for (int i = 0; i < 99; ++i) {
1086 if (after.count(i) == 0) {
1087 continue;
1088 }
1089 if (before.count(i) == 0) {
1090 ++diff;
1091 continue;
1092 }
1093 if (before[i] != after[i]) {
1094 ++diff;
1095 }
1096 }
1097
1098 // We can't be sure that nothing's crash looping in the background.
1099 // This should be good enough, though...
1100 ASSERT_LT(diff, 10) << "too many new tombstones; is something crashing in the background?";
1101}