blob: 9008b95ac5d861206e82f20a0912ec5c63983008 [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,
92 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
139 ASSERT_EQ(InterceptStatus::kRegistered, response.status);
140}
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 Kamatha73df602017-05-24 15:07:25 +0100183 tombstoned_intercept(crasher_pid, &this->intercept_fd, output_fd, intercept_type);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700184}
185
186void CrasherTest::FinishIntercept(int* result) {
187 InterceptResponse response;
188
189 // Timeout for tombstoned intercept is 10 seconds.
190 ssize_t rc = TIMEOUT(20, read(intercept_fd.get(), &response, sizeof(response)));
191 if (rc == -1) {
192 FAIL() << "failed to read response from tombstoned: " << strerror(errno);
193 } else if (rc == 0) {
194 *result = -1;
195 } else if (rc != sizeof(response)) {
196 FAIL() << "received packet of unexpected length from tombstoned: expected " << sizeof(response)
197 << ", received " << rc;
198 } else {
Josh Gao460b3362017-03-30 16:40:47 -0700199 *result = response.status == InterceptStatus::kStarted ? 1 : 0;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700200 }
201}
202
Josh Gao2e7b8e22017-05-04 17:12:57 -0700203void CrasherTest::StartProcess(std::function<void()> function, std::function<pid_t()> forker) {
Josh Gaofca7ca32017-01-23 12:05:35 -0800204 unique_fd read_pipe;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700205 unique_fd crasher_read_pipe;
206 if (!Pipe(&crasher_read_pipe, &crasher_pipe)) {
207 FAIL() << "failed to create pipe: " << strerror(errno);
208 }
209
Josh Gao2e7b8e22017-05-04 17:12:57 -0700210 crasher_pid = forker();
Josh Gaocbe70cb2016-10-18 18:17:52 -0700211 if (crasher_pid == -1) {
212 FAIL() << "fork failed: " << strerror(errno);
213 } else if (crasher_pid == 0) {
Josh Gao502cfd22017-02-17 01:39:15 -0800214 char dummy;
215 crasher_pipe.reset();
216 TEMP_FAILURE_RETRY(read(crasher_read_pipe.get(), &dummy, 1));
Josh Gaofca7ca32017-01-23 12:05:35 -0800217 function();
218 _exit(0);
219 }
220}
221
Josh Gaocbe70cb2016-10-18 18:17:52 -0700222void CrasherTest::FinishCrasher() {
223 if (crasher_pipe == -1) {
224 FAIL() << "crasher pipe uninitialized";
225 }
226
227 ssize_t rc = write(crasher_pipe.get(), "\n", 1);
228 if (rc == -1) {
229 FAIL() << "failed to write to crasher pipe: " << strerror(errno);
230 } else if (rc == 0) {
231 FAIL() << "crasher pipe was closed";
232 }
233}
234
235void CrasherTest::AssertDeath(int signo) {
236 int status;
237 pid_t pid = TIMEOUT(5, waitpid(crasher_pid, &status, 0));
238 if (pid != crasher_pid) {
239 FAIL() << "failed to wait for crasher: " << strerror(errno);
240 }
241
Josh Gaoe06f2a42017-04-27 16:50:38 -0700242 if (signo == 0) {
243 ASSERT_TRUE(WIFEXITED(status));
244 ASSERT_EQ(0, WEXITSTATUS(signo));
245 } else {
246 ASSERT_FALSE(WIFEXITED(status));
247 ASSERT_TRUE(WIFSIGNALED(status)) << "crasher didn't terminate via a signal";
248 ASSERT_EQ(signo, WTERMSIG(status));
Josh Gaocbe70cb2016-10-18 18:17:52 -0700249 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700250 crasher_pid = -1;
251}
252
253static void ConsumeFd(unique_fd fd, std::string* output) {
254 constexpr size_t read_length = PAGE_SIZE;
255 std::string result;
256
257 while (true) {
258 size_t offset = result.size();
259 result.resize(result.size() + PAGE_SIZE);
260 ssize_t rc = TEMP_FAILURE_RETRY(read(fd.get(), &result[offset], read_length));
261 if (rc == -1) {
262 FAIL() << "read failed: " << strerror(errno);
263 } else if (rc == 0) {
264 result.resize(result.size() - PAGE_SIZE);
265 break;
266 }
267
268 result.resize(result.size() - PAGE_SIZE + rc);
269 }
270
271 *output = std::move(result);
272}
273
274TEST_F(CrasherTest, smoke) {
275 int intercept_result;
276 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800277 StartProcess([]() {
278 *reinterpret_cast<volatile char*>(0xdead) = '1';
279 });
280
Josh Gaocbe70cb2016-10-18 18:17:52 -0700281 StartIntercept(&output_fd);
282 FinishCrasher();
283 AssertDeath(SIGSEGV);
284 FinishIntercept(&intercept_result);
285
286 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
287
288 std::string result;
289 ConsumeFd(std::move(output_fd), &result);
290 ASSERT_MATCH(result, R"(signal 11 \(SIGSEGV\), code 1 \(SEGV_MAPERR\), fault addr 0xdead)");
291}
292
293TEST_F(CrasherTest, abort) {
294 int intercept_result;
295 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800296 StartProcess([]() {
297 abort();
298 });
Josh Gaocbe70cb2016-10-18 18:17:52 -0700299 StartIntercept(&output_fd);
300 FinishCrasher();
301 AssertDeath(SIGABRT);
302 FinishIntercept(&intercept_result);
303
304 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
305
306 std::string result;
307 ConsumeFd(std::move(output_fd), &result);
308 ASSERT_MATCH(result, R"(#00 pc [0-9a-f]+\s+ /system/lib)" ARCH_SUFFIX R"(/libc.so \(tgkill)");
309}
310
311TEST_F(CrasherTest, signal) {
312 int intercept_result;
313 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800314 StartProcess([]() {
315 abort();
316 });
Josh Gaocbe70cb2016-10-18 18:17:52 -0700317 StartIntercept(&output_fd);
318
319 // Wait for a bit, or we might end up killing the process before the signal
320 // handler even gets a chance to be registered.
321 std::this_thread::sleep_for(100ms);
322 ASSERT_EQ(0, kill(crasher_pid, SIGSEGV));
323
324 AssertDeath(SIGSEGV);
325 FinishIntercept(&intercept_result);
326
327 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
328
329 std::string result;
330 ConsumeFd(std::move(output_fd), &result);
331 ASSERT_MATCH(result, R"(signal 11 \(SIGSEGV\), code 0 \(SI_USER\), fault addr --------)");
332 ASSERT_MATCH(result, R"(backtrace:)");
333}
334
335TEST_F(CrasherTest, abort_message) {
336 int intercept_result;
337 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800338 StartProcess([]() {
339 android_set_abort_message("abort message goes here");
340 abort();
341 });
Josh Gaocbe70cb2016-10-18 18:17:52 -0700342 StartIntercept(&output_fd);
343 FinishCrasher();
344 AssertDeath(SIGABRT);
345 FinishIntercept(&intercept_result);
346
347 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
348
349 std::string result;
350 ConsumeFd(std::move(output_fd), &result);
Josh Gao502cfd22017-02-17 01:39:15 -0800351 ASSERT_MATCH(result, R"(Abort message: 'abort message goes here')");
Josh Gaocbe70cb2016-10-18 18:17:52 -0700352}
353
Josh Gaoe06f2a42017-04-27 16:50:38 -0700354TEST_F(CrasherTest, abort_message_backtrace) {
355 int intercept_result;
356 unique_fd output_fd;
357 StartProcess([]() {
358 android_set_abort_message("not actually aborting");
359 raise(DEBUGGER_SIGNAL);
360 exit(0);
361 });
362 StartIntercept(&output_fd);
363 FinishCrasher();
364 AssertDeath(0);
365 FinishIntercept(&intercept_result);
366
367 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
368
369 std::string result;
370 ConsumeFd(std::move(output_fd), &result);
371 ASSERT_NOT_MATCH(result, R"(Abort message:)");
372}
373
Josh Gaocbe70cb2016-10-18 18:17:52 -0700374TEST_F(CrasherTest, intercept_timeout) {
375 int intercept_result;
376 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800377 StartProcess([]() {
378 abort();
379 });
Josh Gaocbe70cb2016-10-18 18:17:52 -0700380 StartIntercept(&output_fd);
381
382 // Don't let crasher finish until we timeout.
383 FinishIntercept(&intercept_result);
384
385 ASSERT_NE(1, intercept_result) << "tombstoned reported success? (intercept_result = "
386 << intercept_result << ")";
387
388 FinishCrasher();
389 AssertDeath(SIGABRT);
390}
391
392TEST_F(CrasherTest, wait_for_gdb) {
393 if (!android::base::SetProperty(kWaitForGdbKey, "1")) {
394 FAIL() << "failed to enable wait_for_gdb";
395 }
396 sleep(1);
397
Josh Gao502cfd22017-02-17 01:39:15 -0800398 StartProcess([]() {
399 abort();
400 });
Josh Gaocbe70cb2016-10-18 18:17:52 -0700401 FinishCrasher();
402
403 int status;
404 ASSERT_EQ(crasher_pid, waitpid(crasher_pid, &status, WUNTRACED));
405 ASSERT_TRUE(WIFSTOPPED(status));
406 ASSERT_EQ(SIGSTOP, WSTOPSIG(status));
407
408 ASSERT_EQ(0, kill(crasher_pid, SIGCONT));
409
410 AssertDeath(SIGABRT);
411}
412
Josh Gao7c6e3132017-01-22 17:59:02 -0800413// wait_for_gdb shouldn't trigger on manually sent signals.
Josh Gaocbe70cb2016-10-18 18:17:52 -0700414TEST_F(CrasherTest, wait_for_gdb_signal) {
415 if (!android::base::SetProperty(kWaitForGdbKey, "1")) {
416 FAIL() << "failed to enable wait_for_gdb";
417 }
418
Josh Gao502cfd22017-02-17 01:39:15 -0800419 StartProcess([]() {
420 abort();
421 });
Josh Gao7c6e3132017-01-22 17:59:02 -0800422 ASSERT_EQ(0, kill(crasher_pid, SIGSEGV)) << strerror(errno);
423 AssertDeath(SIGSEGV);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700424}
425
426TEST_F(CrasherTest, backtrace) {
427 std::string result;
428 int intercept_result;
429 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800430
431 StartProcess([]() {
432 abort();
433 });
Narayan Kamatha73df602017-05-24 15:07:25 +0100434 StartIntercept(&output_fd, kDebuggerdNativeBacktrace);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700435
436 std::this_thread::sleep_for(500ms);
437
438 sigval val;
439 val.sival_int = 1;
440 ASSERT_EQ(0, sigqueue(crasher_pid, DEBUGGER_SIGNAL, val)) << strerror(errno);
441 FinishIntercept(&intercept_result);
442 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
443 ConsumeFd(std::move(output_fd), &result);
444 ASSERT_MATCH(result, R"(#00 pc [0-9a-f]+ /system/lib)" ARCH_SUFFIX R"(/libc.so \(read\+)");
445
446 int status;
447 ASSERT_EQ(0, waitpid(crasher_pid, &status, WNOHANG | WUNTRACED));
448
449 StartIntercept(&output_fd);
450 FinishCrasher();
451 AssertDeath(SIGABRT);
452 FinishIntercept(&intercept_result);
453 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
454 ConsumeFd(std::move(output_fd), &result);
455 ASSERT_MATCH(result, R"(#00 pc [0-9a-f]+\s+ /system/lib)" ARCH_SUFFIX R"(/libc.so \(tgkill)");
456}
Josh Gaofca7ca32017-01-23 12:05:35 -0800457
458TEST_F(CrasherTest, PR_SET_DUMPABLE_0_crash) {
Josh Gao502cfd22017-02-17 01:39:15 -0800459 int intercept_result;
460 unique_fd output_fd;
Josh Gaofca7ca32017-01-23 12:05:35 -0800461 StartProcess([]() {
462 prctl(PR_SET_DUMPABLE, 0);
Josh Gao502cfd22017-02-17 01:39:15 -0800463 abort();
Josh Gaofca7ca32017-01-23 12:05:35 -0800464 });
Josh Gao502cfd22017-02-17 01:39:15 -0800465
466 StartIntercept(&output_fd);
467 FinishCrasher();
468 AssertDeath(SIGABRT);
469 FinishIntercept(&intercept_result);
470
471 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
472
473 std::string result;
474 ConsumeFd(std::move(output_fd), &result);
475 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 -0800476}
477
Josh Gao502cfd22017-02-17 01:39:15 -0800478TEST_F(CrasherTest, capabilities) {
479 ASSERT_EQ(0U, getuid()) << "capability test requires root";
480
Josh Gaofca7ca32017-01-23 12:05:35 -0800481 StartProcess([]() {
Josh Gao502cfd22017-02-17 01:39:15 -0800482 if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) != 0) {
483 err(1, "failed to set PR_SET_KEEPCAPS");
484 }
485
486 if (setresuid(1, 1, 1) != 0) {
487 err(1, "setresuid failed");
488 }
489
490 __user_cap_header_struct capheader;
491 __user_cap_data_struct capdata[2];
492 memset(&capheader, 0, sizeof(capheader));
493 memset(&capdata, 0, sizeof(capdata));
494
495 capheader.version = _LINUX_CAPABILITY_VERSION_3;
496 capheader.pid = 0;
497
498 // Turn on every third capability.
499 static_assert(CAP_LAST_CAP > 33, "CAP_LAST_CAP <= 32");
500 for (int i = 0; i < CAP_LAST_CAP; i += 3) {
501 capdata[CAP_TO_INDEX(i)].permitted |= CAP_TO_MASK(i);
502 capdata[CAP_TO_INDEX(i)].effective |= CAP_TO_MASK(i);
503 }
504
505 // Make sure CAP_SYS_PTRACE is off.
506 capdata[CAP_TO_INDEX(CAP_SYS_PTRACE)].permitted &= ~(CAP_TO_MASK(CAP_SYS_PTRACE));
507 capdata[CAP_TO_INDEX(CAP_SYS_PTRACE)].effective &= ~(CAP_TO_MASK(CAP_SYS_PTRACE));
508
509 if (capset(&capheader, &capdata[0]) != 0) {
510 err(1, "capset failed");
511 }
512
513 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) != 0) {
514 err(1, "failed to drop ambient capabilities");
515 }
516
Josh Gaoa5199a92017-04-03 13:18:34 -0700517 pthread_setname_np(pthread_self(), "thread_name");
Josh Gao502cfd22017-02-17 01:39:15 -0800518 raise(SIGSYS);
Josh Gaofca7ca32017-01-23 12:05:35 -0800519 });
Josh Gao502cfd22017-02-17 01:39:15 -0800520
521 unique_fd output_fd;
522 StartIntercept(&output_fd);
523 FinishCrasher();
524 AssertDeath(SIGSYS);
525
526 std::string result;
527 int intercept_result;
528 FinishIntercept(&intercept_result);
529 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
530 ConsumeFd(std::move(output_fd), &result);
Josh Gaoa5199a92017-04-03 13:18:34 -0700531 ASSERT_MATCH(result, R"(name: thread_name\s+>>> .+debuggerd_test(32|64) <<<)");
Josh Gao502cfd22017-02-17 01:39:15 -0800532 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 -0800533}
Josh Gaoc3c8c022017-02-13 16:36:18 -0800534
Josh Gao2e7b8e22017-05-04 17:12:57 -0700535TEST_F(CrasherTest, fake_pid) {
536 int intercept_result;
537 unique_fd output_fd;
538
539 // Prime the getpid/gettid caches.
540 UNUSED(getpid());
541 UNUSED(gettid());
542
543 std::function<pid_t()> clone_fn = []() {
544 return syscall(__NR_clone, SIGCHLD, nullptr, nullptr, nullptr, nullptr);
545 };
546 StartProcess(
547 []() {
548 ASSERT_NE(getpid(), syscall(__NR_getpid));
549 ASSERT_NE(gettid(), syscall(__NR_gettid));
550 raise(SIGSEGV);
551 },
552 clone_fn);
553
554 StartIntercept(&output_fd);
555 FinishCrasher();
556 AssertDeath(SIGSEGV);
557 FinishIntercept(&intercept_result);
558
559 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
560
561 std::string result;
562 ConsumeFd(std::move(output_fd), &result);
563 ASSERT_MATCH(result, R"(#00 pc [0-9a-f]+\s+ /system/lib)" ARCH_SUFFIX R"(/libc.so \(tgkill)");
564}
565
Josh Gaoc3c8c022017-02-13 16:36:18 -0800566TEST(crash_dump, zombie) {
567 pid_t forkpid = fork();
568
Josh Gaoc3c8c022017-02-13 16:36:18 -0800569 pid_t rc;
570 int status;
571
572 if (forkpid == 0) {
573 errno = 0;
574 rc = waitpid(-1, &status, WNOHANG | __WALL | __WNOTHREAD);
575 if (rc != -1 || errno != ECHILD) {
576 errx(2, "first waitpid returned %d (%s), expected failure with ECHILD", rc, strerror(errno));
577 }
578
579 raise(DEBUGGER_SIGNAL);
580
581 errno = 0;
582 rc = waitpid(-1, &status, __WALL | __WNOTHREAD);
583 if (rc != -1 || errno != ECHILD) {
584 errx(2, "second waitpid returned %d (%s), expected failure with ECHILD", rc, strerror(errno));
585 }
586 _exit(0);
587 } else {
588 rc = waitpid(forkpid, &status, 0);
589 ASSERT_EQ(forkpid, rc);
590 ASSERT_TRUE(WIFEXITED(status));
591 ASSERT_EQ(0, WEXITSTATUS(status));
592 }
593}
Josh Gao352a8452017-03-30 16:46:21 -0700594
595TEST(tombstoned, no_notify) {
596 // Do this a few times.
597 for (int i = 0; i < 3; ++i) {
598 pid_t pid = 123'456'789 + i;
599
600 unique_fd intercept_fd, output_fd;
Narayan Kamatha73df602017-05-24 15:07:25 +0100601 tombstoned_intercept(pid, &intercept_fd, &output_fd, kDebuggerdTombstone);
Josh Gao352a8452017-03-30 16:46:21 -0700602
603 {
604 unique_fd tombstoned_socket, input_fd;
Narayan Kamatha73df602017-05-24 15:07:25 +0100605 ASSERT_TRUE(tombstoned_connect(pid, &tombstoned_socket, &input_fd, kDebuggerdTombstone));
Josh Gao352a8452017-03-30 16:46:21 -0700606 ASSERT_TRUE(android::base::WriteFully(input_fd.get(), &pid, sizeof(pid)));
607 }
608
609 pid_t read_pid;
610 ASSERT_TRUE(android::base::ReadFully(output_fd.get(), &read_pid, sizeof(read_pid)));
611 ASSERT_EQ(read_pid, pid);
612 }
613}
614
615TEST(tombstoned, stress) {
616 // Spawn threads to simultaneously do a bunch of failing dumps and a bunch of successful dumps.
617 static constexpr int kDumpCount = 100;
618
619 std::atomic<bool> start(false);
620 std::vector<std::thread> threads;
621 threads.emplace_back([&start]() {
622 while (!start) {
623 continue;
624 }
625
626 // Use a way out of range pid, to avoid stomping on an actual process.
627 pid_t pid_base = 1'000'000;
628
629 for (int dump = 0; dump < kDumpCount; ++dump) {
630 pid_t pid = pid_base + dump;
631
632 unique_fd intercept_fd, output_fd;
Narayan Kamatha73df602017-05-24 15:07:25 +0100633 tombstoned_intercept(pid, &intercept_fd, &output_fd, kDebuggerdTombstone);
Josh Gao352a8452017-03-30 16:46:21 -0700634
635 // Pretend to crash, and then immediately close the socket.
636 unique_fd sockfd(socket_local_client(kTombstonedCrashSocketName,
637 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_SEQPACKET));
638 if (sockfd == -1) {
639 FAIL() << "failed to connect to tombstoned: " << strerror(errno);
640 }
641 TombstonedCrashPacket packet = {};
642 packet.packet_type = CrashPacketType::kDumpRequest;
643 packet.packet.dump_request.pid = pid;
644 if (TEMP_FAILURE_RETRY(write(sockfd, &packet, sizeof(packet))) != sizeof(packet)) {
645 FAIL() << "failed to write to tombstoned: " << strerror(errno);
646 }
647
648 continue;
649 }
650 });
651
652 threads.emplace_back([&start]() {
653 while (!start) {
654 continue;
655 }
656
657 // Use a way out of range pid, to avoid stomping on an actual process.
658 pid_t pid_base = 2'000'000;
659
660 for (int dump = 0; dump < kDumpCount; ++dump) {
661 pid_t pid = pid_base + dump;
662
663 unique_fd intercept_fd, output_fd;
Narayan Kamatha73df602017-05-24 15:07:25 +0100664 tombstoned_intercept(pid, &intercept_fd, &output_fd, kDebuggerdTombstone);
Josh Gao352a8452017-03-30 16:46:21 -0700665
666 {
667 unique_fd tombstoned_socket, input_fd;
Narayan Kamatha73df602017-05-24 15:07:25 +0100668 ASSERT_TRUE(tombstoned_connect(pid, &tombstoned_socket, &input_fd, kDebuggerdTombstone));
Josh Gao352a8452017-03-30 16:46:21 -0700669 ASSERT_TRUE(android::base::WriteFully(input_fd.get(), &pid, sizeof(pid)));
670 tombstoned_notify_completion(tombstoned_socket.get());
671 }
672
673 // TODO: Fix the race that requires this sleep.
674 std::this_thread::sleep_for(50ms);
675
676 pid_t read_pid;
677 ASSERT_TRUE(android::base::ReadFully(output_fd.get(), &read_pid, sizeof(read_pid)));
678 ASSERT_EQ(read_pid, pid);
679 }
680 });
681
682 start = true;
683
684 for (std::thread& thread : threads) {
685 thread.join();
686 }
687}