blob: 0b4bbfb606ee2d0e9dc9a441bb446b9265978192 [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>
38#include <debuggerd/handler.h>
39#include <debuggerd/protocol.h>
Josh Gao352a8452017-03-30 16:46:21 -070040#include <debuggerd/tombstoned.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070041#include <debuggerd/util.h>
42#include <gtest/gtest.h>
43
44using namespace std::chrono_literals;
45using android::base::unique_fd;
46
47#if defined(__LP64__)
Josh Gaocbe70cb2016-10-18 18:17:52 -070048#define ARCH_SUFFIX "64"
49#else
Josh Gaocbe70cb2016-10-18 18:17:52 -070050#define ARCH_SUFFIX ""
51#endif
52
53constexpr char kWaitForGdbKey[] = "debug.debuggerd.wait_for_gdb";
54
55#define TIMEOUT(seconds, expr) \
56 [&]() { \
57 struct sigaction old_sigaction; \
58 struct sigaction new_sigaction = {}; \
59 new_sigaction.sa_handler = [](int) {}; \
60 if (sigaction(SIGALRM, &new_sigaction, &new_sigaction) != 0) { \
61 err(1, "sigaction failed"); \
62 } \
63 alarm(seconds); \
64 auto value = expr; \
65 int saved_errno = errno; \
66 if (sigaction(SIGALRM, &old_sigaction, nullptr) != 0) { \
67 err(1, "sigaction failed"); \
68 } \
69 alarm(0); \
70 errno = saved_errno; \
71 return value; \
72 }()
73
74#define ASSERT_MATCH(str, pattern) \
75 do { \
76 std::regex r((pattern)); \
77 if (!std::regex_search((str), r)) { \
78 FAIL() << "regex mismatch: expected " << (pattern) << " in: \n" << (str); \
79 } \
80 } while (0)
81
Josh Gaoe06f2a42017-04-27 16:50:38 -070082#define ASSERT_NOT_MATCH(str, pattern) \
83 do { \
84 std::regex r((pattern)); \
85 if (std::regex_search((str), r)) { \
86 FAIL() << "regex mismatch: expected to not find " << (pattern) << " in: \n" << (str); \
87 } \
88 } while (0)
89
Josh Gao460b3362017-03-30 16:40:47 -070090static void tombstoned_intercept(pid_t target_pid, unique_fd* intercept_fd, unique_fd* output_fd) {
91 intercept_fd->reset(socket_local_client(kTombstonedInterceptSocketName,
92 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_SEQPACKET));
93 if (intercept_fd->get() == -1) {
94 FAIL() << "failed to contact tombstoned: " << strerror(errno);
95 }
96
97 InterceptRequest req = {.pid = target_pid};
98
99 unique_fd output_pipe_write;
100 if (!Pipe(output_fd, &output_pipe_write)) {
101 FAIL() << "failed to create output pipe: " << strerror(errno);
102 }
103
104 std::string pipe_size_str;
105 int pipe_buffer_size;
106 if (!android::base::ReadFileToString("/proc/sys/fs/pipe-max-size", &pipe_size_str)) {
107 FAIL() << "failed to read /proc/sys/fs/pipe-max-size: " << strerror(errno);
108 }
109
110 pipe_size_str = android::base::Trim(pipe_size_str);
111
112 if (!android::base::ParseInt(pipe_size_str.c_str(), &pipe_buffer_size, 0)) {
113 FAIL() << "failed to parse pipe max size";
114 }
115
116 if (fcntl(output_fd->get(), F_SETPIPE_SZ, pipe_buffer_size) != pipe_buffer_size) {
117 FAIL() << "failed to set pipe size: " << strerror(errno);
118 }
119
120 if (send_fd(intercept_fd->get(), &req, sizeof(req), std::move(output_pipe_write)) != sizeof(req)) {
121 FAIL() << "failed to send output fd to tombstoned: " << strerror(errno);
122 }
123
124 InterceptResponse response;
125 ssize_t rc = TEMP_FAILURE_RETRY(read(intercept_fd->get(), &response, sizeof(response)));
126 if (rc == -1) {
127 FAIL() << "failed to read response from tombstoned: " << strerror(errno);
128 } else if (rc == 0) {
129 FAIL() << "failed to read response from tombstoned (EOF)";
130 } else if (rc != sizeof(response)) {
131 FAIL() << "received packet of unexpected length from tombstoned: expected " << sizeof(response)
132 << ", received " << rc;
133 }
134
135 ASSERT_EQ(InterceptStatus::kRegistered, response.status);
136}
137
Josh Gaocbe70cb2016-10-18 18:17:52 -0700138class CrasherTest : public ::testing::Test {
139 public:
140 pid_t crasher_pid = -1;
141 bool previous_wait_for_gdb;
142 unique_fd crasher_pipe;
143 unique_fd intercept_fd;
144
145 CrasherTest();
146 ~CrasherTest();
147
148 void StartIntercept(unique_fd* output_fd);
149
150 // Returns -1 if we fail to read a response from tombstoned, otherwise the received return code.
151 void FinishIntercept(int* result);
152
Josh Gao2e7b8e22017-05-04 17:12:57 -0700153 void StartProcess(std::function<void()> function, std::function<pid_t()> forker = fork);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700154 void StartCrasher(const std::string& crash_type);
155 void FinishCrasher();
156 void AssertDeath(int signo);
157};
158
159CrasherTest::CrasherTest() {
160 previous_wait_for_gdb = android::base::GetBoolProperty(kWaitForGdbKey, false);
161 android::base::SetProperty(kWaitForGdbKey, "0");
162}
163
164CrasherTest::~CrasherTest() {
165 if (crasher_pid != -1) {
166 kill(crasher_pid, SIGKILL);
167 int status;
168 waitpid(crasher_pid, &status, WUNTRACED);
169 }
170
171 android::base::SetProperty(kWaitForGdbKey, previous_wait_for_gdb ? "1" : "0");
172}
173
174void CrasherTest::StartIntercept(unique_fd* output_fd) {
175 if (crasher_pid == -1) {
176 FAIL() << "crasher hasn't been started";
177 }
178
Josh Gao460b3362017-03-30 16:40:47 -0700179 tombstoned_intercept(crasher_pid, &this->intercept_fd, output_fd);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700180}
181
182void CrasherTest::FinishIntercept(int* result) {
183 InterceptResponse response;
184
185 // Timeout for tombstoned intercept is 10 seconds.
186 ssize_t rc = TIMEOUT(20, read(intercept_fd.get(), &response, sizeof(response)));
187 if (rc == -1) {
188 FAIL() << "failed to read response from tombstoned: " << strerror(errno);
189 } else if (rc == 0) {
190 *result = -1;
191 } else if (rc != sizeof(response)) {
192 FAIL() << "received packet of unexpected length from tombstoned: expected " << sizeof(response)
193 << ", received " << rc;
194 } else {
Josh Gao460b3362017-03-30 16:40:47 -0700195 *result = response.status == InterceptStatus::kStarted ? 1 : 0;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700196 }
197}
198
Josh Gao2e7b8e22017-05-04 17:12:57 -0700199void CrasherTest::StartProcess(std::function<void()> function, std::function<pid_t()> forker) {
Josh Gaofca7ca32017-01-23 12:05:35 -0800200 unique_fd read_pipe;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700201 unique_fd crasher_read_pipe;
202 if (!Pipe(&crasher_read_pipe, &crasher_pipe)) {
203 FAIL() << "failed to create pipe: " << strerror(errno);
204 }
205
Josh Gao2e7b8e22017-05-04 17:12:57 -0700206 crasher_pid = forker();
Josh Gaocbe70cb2016-10-18 18:17:52 -0700207 if (crasher_pid == -1) {
208 FAIL() << "fork failed: " << strerror(errno);
209 } else if (crasher_pid == 0) {
Josh Gao502cfd22017-02-17 01:39:15 -0800210 char dummy;
211 crasher_pipe.reset();
212 TEMP_FAILURE_RETRY(read(crasher_read_pipe.get(), &dummy, 1));
Josh Gaofca7ca32017-01-23 12:05:35 -0800213 function();
214 _exit(0);
215 }
216}
217
Josh Gaocbe70cb2016-10-18 18:17:52 -0700218void CrasherTest::FinishCrasher() {
219 if (crasher_pipe == -1) {
220 FAIL() << "crasher pipe uninitialized";
221 }
222
223 ssize_t rc = write(crasher_pipe.get(), "\n", 1);
224 if (rc == -1) {
225 FAIL() << "failed to write to crasher pipe: " << strerror(errno);
226 } else if (rc == 0) {
227 FAIL() << "crasher pipe was closed";
228 }
229}
230
231void CrasherTest::AssertDeath(int signo) {
232 int status;
233 pid_t pid = TIMEOUT(5, waitpid(crasher_pid, &status, 0));
234 if (pid != crasher_pid) {
235 FAIL() << "failed to wait for crasher: " << strerror(errno);
236 }
237
Josh Gaoe06f2a42017-04-27 16:50:38 -0700238 if (signo == 0) {
239 ASSERT_TRUE(WIFEXITED(status));
240 ASSERT_EQ(0, WEXITSTATUS(signo));
241 } else {
242 ASSERT_FALSE(WIFEXITED(status));
243 ASSERT_TRUE(WIFSIGNALED(status)) << "crasher didn't terminate via a signal";
244 ASSERT_EQ(signo, WTERMSIG(status));
Josh Gaocbe70cb2016-10-18 18:17:52 -0700245 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700246 crasher_pid = -1;
247}
248
249static void ConsumeFd(unique_fd fd, std::string* output) {
250 constexpr size_t read_length = PAGE_SIZE;
251 std::string result;
252
253 while (true) {
254 size_t offset = result.size();
255 result.resize(result.size() + PAGE_SIZE);
256 ssize_t rc = TEMP_FAILURE_RETRY(read(fd.get(), &result[offset], read_length));
257 if (rc == -1) {
258 FAIL() << "read failed: " << strerror(errno);
259 } else if (rc == 0) {
260 result.resize(result.size() - PAGE_SIZE);
261 break;
262 }
263
264 result.resize(result.size() - PAGE_SIZE + rc);
265 }
266
267 *output = std::move(result);
268}
269
270TEST_F(CrasherTest, smoke) {
271 int intercept_result;
272 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800273 StartProcess([]() {
274 *reinterpret_cast<volatile char*>(0xdead) = '1';
275 });
276
Josh Gaocbe70cb2016-10-18 18:17:52 -0700277 StartIntercept(&output_fd);
278 FinishCrasher();
279 AssertDeath(SIGSEGV);
280 FinishIntercept(&intercept_result);
281
282 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
283
284 std::string result;
285 ConsumeFd(std::move(output_fd), &result);
286 ASSERT_MATCH(result, R"(signal 11 \(SIGSEGV\), code 1 \(SEGV_MAPERR\), fault addr 0xdead)");
287}
288
289TEST_F(CrasherTest, abort) {
290 int intercept_result;
291 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800292 StartProcess([]() {
293 abort();
294 });
Josh Gaocbe70cb2016-10-18 18:17:52 -0700295 StartIntercept(&output_fd);
296 FinishCrasher();
297 AssertDeath(SIGABRT);
298 FinishIntercept(&intercept_result);
299
300 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
301
302 std::string result;
303 ConsumeFd(std::move(output_fd), &result);
304 ASSERT_MATCH(result, R"(#00 pc [0-9a-f]+\s+ /system/lib)" ARCH_SUFFIX R"(/libc.so \(tgkill)");
305}
306
307TEST_F(CrasherTest, signal) {
308 int intercept_result;
309 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800310 StartProcess([]() {
311 abort();
312 });
Josh Gaocbe70cb2016-10-18 18:17:52 -0700313 StartIntercept(&output_fd);
314
315 // Wait for a bit, or we might end up killing the process before the signal
316 // handler even gets a chance to be registered.
317 std::this_thread::sleep_for(100ms);
318 ASSERT_EQ(0, kill(crasher_pid, SIGSEGV));
319
320 AssertDeath(SIGSEGV);
321 FinishIntercept(&intercept_result);
322
323 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
324
325 std::string result;
326 ConsumeFd(std::move(output_fd), &result);
327 ASSERT_MATCH(result, R"(signal 11 \(SIGSEGV\), code 0 \(SI_USER\), fault addr --------)");
328 ASSERT_MATCH(result, R"(backtrace:)");
329}
330
331TEST_F(CrasherTest, abort_message) {
332 int intercept_result;
333 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800334 StartProcess([]() {
335 android_set_abort_message("abort message goes here");
336 abort();
337 });
Josh Gaocbe70cb2016-10-18 18:17:52 -0700338 StartIntercept(&output_fd);
339 FinishCrasher();
340 AssertDeath(SIGABRT);
341 FinishIntercept(&intercept_result);
342
343 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
344
345 std::string result;
346 ConsumeFd(std::move(output_fd), &result);
Josh Gao502cfd22017-02-17 01:39:15 -0800347 ASSERT_MATCH(result, R"(Abort message: 'abort message goes here')");
Josh Gaocbe70cb2016-10-18 18:17:52 -0700348}
349
Josh Gaoe06f2a42017-04-27 16:50:38 -0700350TEST_F(CrasherTest, abort_message_backtrace) {
351 int intercept_result;
352 unique_fd output_fd;
353 StartProcess([]() {
354 android_set_abort_message("not actually aborting");
355 raise(DEBUGGER_SIGNAL);
356 exit(0);
357 });
358 StartIntercept(&output_fd);
359 FinishCrasher();
360 AssertDeath(0);
361 FinishIntercept(&intercept_result);
362
363 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
364
365 std::string result;
366 ConsumeFd(std::move(output_fd), &result);
367 ASSERT_NOT_MATCH(result, R"(Abort message:)");
368}
369
Josh Gaocbe70cb2016-10-18 18:17:52 -0700370TEST_F(CrasherTest, intercept_timeout) {
371 int intercept_result;
372 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800373 StartProcess([]() {
374 abort();
375 });
Josh Gaocbe70cb2016-10-18 18:17:52 -0700376 StartIntercept(&output_fd);
377
378 // Don't let crasher finish until we timeout.
379 FinishIntercept(&intercept_result);
380
381 ASSERT_NE(1, intercept_result) << "tombstoned reported success? (intercept_result = "
382 << intercept_result << ")";
383
384 FinishCrasher();
385 AssertDeath(SIGABRT);
386}
387
388TEST_F(CrasherTest, wait_for_gdb) {
389 if (!android::base::SetProperty(kWaitForGdbKey, "1")) {
390 FAIL() << "failed to enable wait_for_gdb";
391 }
392 sleep(1);
393
Josh Gao502cfd22017-02-17 01:39:15 -0800394 StartProcess([]() {
395 abort();
396 });
Josh Gaocbe70cb2016-10-18 18:17:52 -0700397 FinishCrasher();
398
399 int status;
400 ASSERT_EQ(crasher_pid, waitpid(crasher_pid, &status, WUNTRACED));
401 ASSERT_TRUE(WIFSTOPPED(status));
402 ASSERT_EQ(SIGSTOP, WSTOPSIG(status));
403
404 ASSERT_EQ(0, kill(crasher_pid, SIGCONT));
405
406 AssertDeath(SIGABRT);
407}
408
Josh Gao7c6e3132017-01-22 17:59:02 -0800409// wait_for_gdb shouldn't trigger on manually sent signals.
Josh Gaocbe70cb2016-10-18 18:17:52 -0700410TEST_F(CrasherTest, wait_for_gdb_signal) {
411 if (!android::base::SetProperty(kWaitForGdbKey, "1")) {
412 FAIL() << "failed to enable wait_for_gdb";
413 }
414
Josh Gao502cfd22017-02-17 01:39:15 -0800415 StartProcess([]() {
416 abort();
417 });
Josh Gao7c6e3132017-01-22 17:59:02 -0800418 ASSERT_EQ(0, kill(crasher_pid, SIGSEGV)) << strerror(errno);
419 AssertDeath(SIGSEGV);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700420}
421
422TEST_F(CrasherTest, backtrace) {
423 std::string result;
424 int intercept_result;
425 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800426
427 StartProcess([]() {
428 abort();
429 });
Josh Gaocbe70cb2016-10-18 18:17:52 -0700430 StartIntercept(&output_fd);
431
432 std::this_thread::sleep_for(500ms);
433
434 sigval val;
435 val.sival_int = 1;
436 ASSERT_EQ(0, sigqueue(crasher_pid, DEBUGGER_SIGNAL, val)) << strerror(errno);
437 FinishIntercept(&intercept_result);
438 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
439 ConsumeFd(std::move(output_fd), &result);
440 ASSERT_MATCH(result, R"(#00 pc [0-9a-f]+ /system/lib)" ARCH_SUFFIX R"(/libc.so \(read\+)");
441
442 int status;
443 ASSERT_EQ(0, waitpid(crasher_pid, &status, WNOHANG | WUNTRACED));
444
445 StartIntercept(&output_fd);
446 FinishCrasher();
447 AssertDeath(SIGABRT);
448 FinishIntercept(&intercept_result);
449 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
450 ConsumeFd(std::move(output_fd), &result);
451 ASSERT_MATCH(result, R"(#00 pc [0-9a-f]+\s+ /system/lib)" ARCH_SUFFIX R"(/libc.so \(tgkill)");
452}
Josh Gaofca7ca32017-01-23 12:05:35 -0800453
454TEST_F(CrasherTest, PR_SET_DUMPABLE_0_crash) {
Josh Gao502cfd22017-02-17 01:39:15 -0800455 int intercept_result;
456 unique_fd output_fd;
Josh Gaofca7ca32017-01-23 12:05:35 -0800457 StartProcess([]() {
458 prctl(PR_SET_DUMPABLE, 0);
Josh Gao502cfd22017-02-17 01:39:15 -0800459 abort();
Josh Gaofca7ca32017-01-23 12:05:35 -0800460 });
Josh Gao502cfd22017-02-17 01:39:15 -0800461
462 StartIntercept(&output_fd);
463 FinishCrasher();
464 AssertDeath(SIGABRT);
465 FinishIntercept(&intercept_result);
466
467 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
468
469 std::string result;
470 ConsumeFd(std::move(output_fd), &result);
471 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 -0800472}
473
Josh Gao502cfd22017-02-17 01:39:15 -0800474TEST_F(CrasherTest, capabilities) {
475 ASSERT_EQ(0U, getuid()) << "capability test requires root";
476
Josh Gaofca7ca32017-01-23 12:05:35 -0800477 StartProcess([]() {
Josh Gao502cfd22017-02-17 01:39:15 -0800478 if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) != 0) {
479 err(1, "failed to set PR_SET_KEEPCAPS");
480 }
481
482 if (setresuid(1, 1, 1) != 0) {
483 err(1, "setresuid failed");
484 }
485
486 __user_cap_header_struct capheader;
487 __user_cap_data_struct capdata[2];
488 memset(&capheader, 0, sizeof(capheader));
489 memset(&capdata, 0, sizeof(capdata));
490
491 capheader.version = _LINUX_CAPABILITY_VERSION_3;
492 capheader.pid = 0;
493
494 // Turn on every third capability.
495 static_assert(CAP_LAST_CAP > 33, "CAP_LAST_CAP <= 32");
496 for (int i = 0; i < CAP_LAST_CAP; i += 3) {
497 capdata[CAP_TO_INDEX(i)].permitted |= CAP_TO_MASK(i);
498 capdata[CAP_TO_INDEX(i)].effective |= CAP_TO_MASK(i);
499 }
500
501 // Make sure CAP_SYS_PTRACE is off.
502 capdata[CAP_TO_INDEX(CAP_SYS_PTRACE)].permitted &= ~(CAP_TO_MASK(CAP_SYS_PTRACE));
503 capdata[CAP_TO_INDEX(CAP_SYS_PTRACE)].effective &= ~(CAP_TO_MASK(CAP_SYS_PTRACE));
504
505 if (capset(&capheader, &capdata[0]) != 0) {
506 err(1, "capset failed");
507 }
508
509 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) != 0) {
510 err(1, "failed to drop ambient capabilities");
511 }
512
Josh Gaoa5199a92017-04-03 13:18:34 -0700513 pthread_setname_np(pthread_self(), "thread_name");
Josh Gao502cfd22017-02-17 01:39:15 -0800514 raise(SIGSYS);
Josh Gaofca7ca32017-01-23 12:05:35 -0800515 });
Josh Gao502cfd22017-02-17 01:39:15 -0800516
517 unique_fd output_fd;
518 StartIntercept(&output_fd);
519 FinishCrasher();
520 AssertDeath(SIGSYS);
521
522 std::string result;
523 int intercept_result;
524 FinishIntercept(&intercept_result);
525 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
526 ConsumeFd(std::move(output_fd), &result);
Josh Gaoa5199a92017-04-03 13:18:34 -0700527 ASSERT_MATCH(result, R"(name: thread_name\s+>>> .+debuggerd_test(32|64) <<<)");
Josh Gao502cfd22017-02-17 01:39:15 -0800528 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 -0800529}
Josh Gaoc3c8c022017-02-13 16:36:18 -0800530
Josh Gao2e7b8e22017-05-04 17:12:57 -0700531TEST_F(CrasherTest, fake_pid) {
532 int intercept_result;
533 unique_fd output_fd;
534
535 // Prime the getpid/gettid caches.
536 UNUSED(getpid());
537 UNUSED(gettid());
538
539 std::function<pid_t()> clone_fn = []() {
540 return syscall(__NR_clone, SIGCHLD, nullptr, nullptr, nullptr, nullptr);
541 };
542 StartProcess(
543 []() {
544 ASSERT_NE(getpid(), syscall(__NR_getpid));
545 ASSERT_NE(gettid(), syscall(__NR_gettid));
546 raise(SIGSEGV);
547 },
548 clone_fn);
549
550 StartIntercept(&output_fd);
551 FinishCrasher();
552 AssertDeath(SIGSEGV);
553 FinishIntercept(&intercept_result);
554
555 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
556
557 std::string result;
558 ConsumeFd(std::move(output_fd), &result);
559 ASSERT_MATCH(result, R"(#00 pc [0-9a-f]+\s+ /system/lib)" ARCH_SUFFIX R"(/libc.so \(tgkill)");
560}
561
Josh Gaoc3c8c022017-02-13 16:36:18 -0800562TEST(crash_dump, zombie) {
563 pid_t forkpid = fork();
564
Josh Gaoc3c8c022017-02-13 16:36:18 -0800565 pid_t rc;
566 int status;
567
568 if (forkpid == 0) {
569 errno = 0;
570 rc = waitpid(-1, &status, WNOHANG | __WALL | __WNOTHREAD);
571 if (rc != -1 || errno != ECHILD) {
572 errx(2, "first waitpid returned %d (%s), expected failure with ECHILD", rc, strerror(errno));
573 }
574
575 raise(DEBUGGER_SIGNAL);
576
577 errno = 0;
578 rc = waitpid(-1, &status, __WALL | __WNOTHREAD);
579 if (rc != -1 || errno != ECHILD) {
580 errx(2, "second waitpid returned %d (%s), expected failure with ECHILD", rc, strerror(errno));
581 }
582 _exit(0);
583 } else {
584 rc = waitpid(forkpid, &status, 0);
585 ASSERT_EQ(forkpid, rc);
586 ASSERT_TRUE(WIFEXITED(status));
587 ASSERT_EQ(0, WEXITSTATUS(status));
588 }
589}
Josh Gao352a8452017-03-30 16:46:21 -0700590
591TEST(tombstoned, no_notify) {
592 // Do this a few times.
593 for (int i = 0; i < 3; ++i) {
594 pid_t pid = 123'456'789 + i;
595
596 unique_fd intercept_fd, output_fd;
597 tombstoned_intercept(pid, &intercept_fd, &output_fd);
598
599 {
600 unique_fd tombstoned_socket, input_fd;
601 ASSERT_TRUE(tombstoned_connect(pid, &tombstoned_socket, &input_fd));
602 ASSERT_TRUE(android::base::WriteFully(input_fd.get(), &pid, sizeof(pid)));
603 }
604
605 pid_t read_pid;
606 ASSERT_TRUE(android::base::ReadFully(output_fd.get(), &read_pid, sizeof(read_pid)));
607 ASSERT_EQ(read_pid, pid);
608 }
609}
610
611TEST(tombstoned, stress) {
612 // Spawn threads to simultaneously do a bunch of failing dumps and a bunch of successful dumps.
613 static constexpr int kDumpCount = 100;
614
615 std::atomic<bool> start(false);
616 std::vector<std::thread> threads;
617 threads.emplace_back([&start]() {
618 while (!start) {
619 continue;
620 }
621
622 // Use a way out of range pid, to avoid stomping on an actual process.
623 pid_t pid_base = 1'000'000;
624
625 for (int dump = 0; dump < kDumpCount; ++dump) {
626 pid_t pid = pid_base + dump;
627
628 unique_fd intercept_fd, output_fd;
629 tombstoned_intercept(pid, &intercept_fd, &output_fd);
630
631 // Pretend to crash, and then immediately close the socket.
632 unique_fd sockfd(socket_local_client(kTombstonedCrashSocketName,
633 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_SEQPACKET));
634 if (sockfd == -1) {
635 FAIL() << "failed to connect to tombstoned: " << strerror(errno);
636 }
637 TombstonedCrashPacket packet = {};
638 packet.packet_type = CrashPacketType::kDumpRequest;
639 packet.packet.dump_request.pid = pid;
640 if (TEMP_FAILURE_RETRY(write(sockfd, &packet, sizeof(packet))) != sizeof(packet)) {
641 FAIL() << "failed to write to tombstoned: " << strerror(errno);
642 }
643
644 continue;
645 }
646 });
647
648 threads.emplace_back([&start]() {
649 while (!start) {
650 continue;
651 }
652
653 // Use a way out of range pid, to avoid stomping on an actual process.
654 pid_t pid_base = 2'000'000;
655
656 for (int dump = 0; dump < kDumpCount; ++dump) {
657 pid_t pid = pid_base + dump;
658
659 unique_fd intercept_fd, output_fd;
660 tombstoned_intercept(pid, &intercept_fd, &output_fd);
661
662 {
663 unique_fd tombstoned_socket, input_fd;
664 ASSERT_TRUE(tombstoned_connect(pid, &tombstoned_socket, &input_fd));
665 ASSERT_TRUE(android::base::WriteFully(input_fd.get(), &pid, sizeof(pid)));
666 tombstoned_notify_completion(tombstoned_socket.get());
667 }
668
669 // TODO: Fix the race that requires this sleep.
670 std::this_thread::sleep_for(50ms);
671
672 pid_t read_pid;
673 ASSERT_TRUE(android::base::ReadFully(output_fd.get(), &read_pid, sizeof(read_pid)));
674 ASSERT_EQ(read_pid, pid);
675 }
676 });
677
678 start = true;
679
680 for (std::thread& thread : threads) {
681 thread.join();
682 }
683}