blob: 6a8cc563d60b5c3db6f36c3e0ddfbc4e98f3006c [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>
Josh Gaoa48b41b2019-12-13 14:11:04 -080034#include <bionic/reserved_signals.h>
Josh Gao502cfd22017-02-17 01:39:15 -080035
Josh Gao5f87bbd2019-01-09 17:01:49 -080036#include <android-base/cmsg.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070037#include <android-base/file.h>
38#include <android-base/logging.h>
Josh Gao2e7b8e22017-05-04 17:12:57 -070039#include <android-base/macros.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070040#include <android-base/parseint.h>
41#include <android-base/properties.h>
Josh Gao2b22ae12018-09-12 14:51:03 -070042#include <android-base/stringprintf.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070043#include <android-base/strings.h>
Josh Gao30171a82017-04-27 19:48:44 -070044#include <android-base/test_utils.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070045#include <android-base/unique_fd.h>
46#include <cutils/sockets.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070047#include <gtest/gtest.h>
48
Josh Gaoe04ca272018-01-16 15:38:17 -080049#include <libminijail.h>
50#include <scoped_minijail.h>
51
Narayan Kamath2d377cd2017-05-10 10:58:59 +010052#include "debuggerd/handler.h"
53#include "protocol.h"
54#include "tombstoned/tombstoned.h"
55#include "util.h"
56
Josh Gaocbe70cb2016-10-18 18:17:52 -070057using namespace std::chrono_literals;
Josh Gao5f87bbd2019-01-09 17:01:49 -080058
59using android::base::SendFileDescriptors;
Josh Gaocbe70cb2016-10-18 18:17:52 -070060using android::base::unique_fd;
61
62#if defined(__LP64__)
Josh Gaocbe70cb2016-10-18 18:17:52 -070063#define ARCH_SUFFIX "64"
64#else
Josh Gaocbe70cb2016-10-18 18:17:52 -070065#define ARCH_SUFFIX ""
66#endif
67
68constexpr char kWaitForGdbKey[] = "debug.debuggerd.wait_for_gdb";
69
70#define TIMEOUT(seconds, expr) \
71 [&]() { \
72 struct sigaction old_sigaction; \
73 struct sigaction new_sigaction = {}; \
74 new_sigaction.sa_handler = [](int) {}; \
75 if (sigaction(SIGALRM, &new_sigaction, &new_sigaction) != 0) { \
76 err(1, "sigaction failed"); \
77 } \
78 alarm(seconds); \
79 auto value = expr; \
80 int saved_errno = errno; \
81 if (sigaction(SIGALRM, &old_sigaction, nullptr) != 0) { \
82 err(1, "sigaction failed"); \
83 } \
84 alarm(0); \
85 errno = saved_errno; \
86 return value; \
87 }()
88
Chih-Hung Hsieh3249b3a2018-05-11 16:01:21 -070089// Backtrace frame dump could contain:
90// #01 pc 0001cded /data/tmp/debuggerd_test32 (raise_debugger_signal+80)
91// or
92// #01 pc 00022a09 /data/tmp/debuggerd_test32 (offset 0x12000) (raise_debugger_signal+80)
Josh Gaoe04ca272018-01-16 15:38:17 -080093#define ASSERT_BACKTRACE_FRAME(result, frame_name) \
Chih-Hung Hsieh3249b3a2018-05-11 16:01:21 -070094 ASSERT_MATCH(result, \
95 R"(#\d\d pc [0-9a-f]+\s+ \S+ (\(offset 0x[0-9a-f]+\) )?\()" frame_name R"(\+)");
Jaesung Chung58778e12017-06-15 18:20:34 +090096
Narayan Kamatha73df602017-05-24 15:07:25 +010097static void tombstoned_intercept(pid_t target_pid, unique_fd* intercept_fd, unique_fd* output_fd,
Narayan Kamathca5e9082017-06-02 15:42:06 +010098 InterceptStatus* status, DebuggerdDumpType intercept_type) {
Josh Gao460b3362017-03-30 16:40:47 -070099 intercept_fd->reset(socket_local_client(kTombstonedInterceptSocketName,
100 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_SEQPACKET));
101 if (intercept_fd->get() == -1) {
102 FAIL() << "failed to contact tombstoned: " << strerror(errno);
103 }
104
Nick Desaulniers67d52aa2019-10-07 23:28:15 -0700105 InterceptRequest req = {
106 .dump_type = intercept_type,
107 .pid = target_pid,
108 };
Josh Gao460b3362017-03-30 16:40:47 -0700109
110 unique_fd output_pipe_write;
111 if (!Pipe(output_fd, &output_pipe_write)) {
112 FAIL() << "failed to create output pipe: " << strerror(errno);
113 }
114
115 std::string pipe_size_str;
116 int pipe_buffer_size;
117 if (!android::base::ReadFileToString("/proc/sys/fs/pipe-max-size", &pipe_size_str)) {
118 FAIL() << "failed to read /proc/sys/fs/pipe-max-size: " << strerror(errno);
119 }
120
121 pipe_size_str = android::base::Trim(pipe_size_str);
122
123 if (!android::base::ParseInt(pipe_size_str.c_str(), &pipe_buffer_size, 0)) {
124 FAIL() << "failed to parse pipe max size";
125 }
126
127 if (fcntl(output_fd->get(), F_SETPIPE_SZ, pipe_buffer_size) != pipe_buffer_size) {
128 FAIL() << "failed to set pipe size: " << strerror(errno);
129 }
130
Josh Gao5675f3c2017-06-01 12:19:53 -0700131 ASSERT_GE(pipe_buffer_size, 1024 * 1024);
132
Josh Gao5f87bbd2019-01-09 17:01:49 -0800133 ssize_t rc = SendFileDescriptors(intercept_fd->get(), &req, sizeof(req), output_pipe_write.get());
134 output_pipe_write.reset();
135 if (rc != sizeof(req)) {
Josh Gao460b3362017-03-30 16:40:47 -0700136 FAIL() << "failed to send output fd to tombstoned: " << strerror(errno);
137 }
138
139 InterceptResponse response;
Josh Gao5f87bbd2019-01-09 17:01:49 -0800140 rc = TEMP_FAILURE_RETRY(read(intercept_fd->get(), &response, sizeof(response)));
Josh Gao460b3362017-03-30 16:40:47 -0700141 if (rc == -1) {
142 FAIL() << "failed to read response from tombstoned: " << strerror(errno);
143 } else if (rc == 0) {
144 FAIL() << "failed to read response from tombstoned (EOF)";
145 } else if (rc != sizeof(response)) {
146 FAIL() << "received packet of unexpected length from tombstoned: expected " << sizeof(response)
147 << ", received " << rc;
148 }
149
Narayan Kamathca5e9082017-06-02 15:42:06 +0100150 *status = response.status;
Josh Gao460b3362017-03-30 16:40:47 -0700151}
152
Josh Gaocbe70cb2016-10-18 18:17:52 -0700153class CrasherTest : public ::testing::Test {
154 public:
155 pid_t crasher_pid = -1;
156 bool previous_wait_for_gdb;
157 unique_fd crasher_pipe;
158 unique_fd intercept_fd;
159
160 CrasherTest();
161 ~CrasherTest();
162
Narayan Kamatha73df602017-05-24 15:07:25 +0100163 void StartIntercept(unique_fd* output_fd, DebuggerdDumpType intercept_type = kDebuggerdTombstone);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700164
165 // Returns -1 if we fail to read a response from tombstoned, otherwise the received return code.
166 void FinishIntercept(int* result);
167
Josh Gao2e7b8e22017-05-04 17:12:57 -0700168 void StartProcess(std::function<void()> function, std::function<pid_t()> forker = fork);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700169 void StartCrasher(const std::string& crash_type);
170 void FinishCrasher();
171 void AssertDeath(int signo);
172};
173
174CrasherTest::CrasherTest() {
175 previous_wait_for_gdb = android::base::GetBoolProperty(kWaitForGdbKey, false);
176 android::base::SetProperty(kWaitForGdbKey, "0");
177}
178
179CrasherTest::~CrasherTest() {
180 if (crasher_pid != -1) {
181 kill(crasher_pid, SIGKILL);
182 int status;
Christopher Ferris172b0a02019-09-18 17:48:30 -0700183 TEMP_FAILURE_RETRY(waitpid(crasher_pid, &status, WUNTRACED));
Josh Gaocbe70cb2016-10-18 18:17:52 -0700184 }
185
186 android::base::SetProperty(kWaitForGdbKey, previous_wait_for_gdb ? "1" : "0");
187}
188
Narayan Kamatha73df602017-05-24 15:07:25 +0100189void CrasherTest::StartIntercept(unique_fd* output_fd, DebuggerdDumpType intercept_type) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700190 if (crasher_pid == -1) {
191 FAIL() << "crasher hasn't been started";
192 }
193
Narayan Kamathca5e9082017-06-02 15:42:06 +0100194 InterceptStatus status;
195 tombstoned_intercept(crasher_pid, &this->intercept_fd, output_fd, &status, intercept_type);
196 ASSERT_EQ(InterceptStatus::kRegistered, status);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700197}
198
199void CrasherTest::FinishIntercept(int* result) {
200 InterceptResponse response;
201
Christopher Ferris11555f02019-09-20 14:18:55 -0700202 ssize_t rc = TIMEOUT(30, read(intercept_fd.get(), &response, sizeof(response)));
Josh Gaocbe70cb2016-10-18 18:17:52 -0700203 if (rc == -1) {
204 FAIL() << "failed to read response from tombstoned: " << strerror(errno);
205 } else if (rc == 0) {
206 *result = -1;
207 } else if (rc != sizeof(response)) {
208 FAIL() << "received packet of unexpected length from tombstoned: expected " << sizeof(response)
209 << ", received " << rc;
210 } else {
Josh Gao460b3362017-03-30 16:40:47 -0700211 *result = response.status == InterceptStatus::kStarted ? 1 : 0;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700212 }
213}
214
Josh Gao2e7b8e22017-05-04 17:12:57 -0700215void CrasherTest::StartProcess(std::function<void()> function, std::function<pid_t()> forker) {
Josh Gaofca7ca32017-01-23 12:05:35 -0800216 unique_fd read_pipe;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700217 unique_fd crasher_read_pipe;
218 if (!Pipe(&crasher_read_pipe, &crasher_pipe)) {
219 FAIL() << "failed to create pipe: " << strerror(errno);
220 }
221
Josh Gao2e7b8e22017-05-04 17:12:57 -0700222 crasher_pid = forker();
Josh Gaocbe70cb2016-10-18 18:17:52 -0700223 if (crasher_pid == -1) {
224 FAIL() << "fork failed: " << strerror(errno);
225 } else if (crasher_pid == 0) {
Josh Gao502cfd22017-02-17 01:39:15 -0800226 char dummy;
227 crasher_pipe.reset();
228 TEMP_FAILURE_RETRY(read(crasher_read_pipe.get(), &dummy, 1));
Josh Gaofca7ca32017-01-23 12:05:35 -0800229 function();
230 _exit(0);
231 }
232}
233
Josh Gaocbe70cb2016-10-18 18:17:52 -0700234void CrasherTest::FinishCrasher() {
235 if (crasher_pipe == -1) {
236 FAIL() << "crasher pipe uninitialized";
237 }
238
Christopher Ferris172b0a02019-09-18 17:48:30 -0700239 ssize_t rc = TEMP_FAILURE_RETRY(write(crasher_pipe.get(), "\n", 1));
Josh Gaocbe70cb2016-10-18 18:17:52 -0700240 if (rc == -1) {
241 FAIL() << "failed to write to crasher pipe: " << strerror(errno);
242 } else if (rc == 0) {
243 FAIL() << "crasher pipe was closed";
244 }
245}
246
247void CrasherTest::AssertDeath(int signo) {
248 int status;
Christopher Ferris11555f02019-09-20 14:18:55 -0700249 pid_t pid = TIMEOUT(30, waitpid(crasher_pid, &status, 0));
Josh Gaocbe70cb2016-10-18 18:17:52 -0700250 if (pid != crasher_pid) {
Christopher Ferrisafc0ff72019-06-26 15:08:51 -0700251 printf("failed to wait for crasher (expected pid %d, return value %d): %s\n", crasher_pid, pid,
252 strerror(errno));
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700253 sleep(100);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700254 FAIL() << "failed to wait for crasher: " << strerror(errno);
255 }
256
Josh Gaoe06f2a42017-04-27 16:50:38 -0700257 if (signo == 0) {
258 ASSERT_TRUE(WIFEXITED(status));
259 ASSERT_EQ(0, WEXITSTATUS(signo));
260 } else {
261 ASSERT_FALSE(WIFEXITED(status));
262 ASSERT_TRUE(WIFSIGNALED(status)) << "crasher didn't terminate via a signal";
263 ASSERT_EQ(signo, WTERMSIG(status));
Josh Gaocbe70cb2016-10-18 18:17:52 -0700264 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700265 crasher_pid = -1;
266}
267
268static void ConsumeFd(unique_fd fd, std::string* output) {
269 constexpr size_t read_length = PAGE_SIZE;
270 std::string result;
271
272 while (true) {
273 size_t offset = result.size();
274 result.resize(result.size() + PAGE_SIZE);
275 ssize_t rc = TEMP_FAILURE_RETRY(read(fd.get(), &result[offset], read_length));
276 if (rc == -1) {
277 FAIL() << "read failed: " << strerror(errno);
278 } else if (rc == 0) {
279 result.resize(result.size() - PAGE_SIZE);
280 break;
281 }
282
283 result.resize(result.size() - PAGE_SIZE + rc);
284 }
285
286 *output = std::move(result);
287}
288
289TEST_F(CrasherTest, smoke) {
290 int intercept_result;
291 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800292 StartProcess([]() {
293 *reinterpret_cast<volatile char*>(0xdead) = '1';
294 });
295
Josh Gaocbe70cb2016-10-18 18:17:52 -0700296 StartIntercept(&output_fd);
297 FinishCrasher();
298 AssertDeath(SIGSEGV);
299 FinishIntercept(&intercept_result);
300
301 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
302
303 std::string result;
304 ConsumeFd(std::move(output_fd), &result);
305 ASSERT_MATCH(result, R"(signal 11 \(SIGSEGV\), code 1 \(SEGV_MAPERR\), fault addr 0xdead)");
306}
307
Josh Gaocdea7502017-11-01 15:00:40 -0700308TEST_F(CrasherTest, LD_PRELOAD) {
309 int intercept_result;
310 unique_fd output_fd;
311 StartProcess([]() {
312 setenv("LD_PRELOAD", "nonexistent.so", 1);
313 *reinterpret_cast<volatile char*>(0xdead) = '1';
314 });
315
316 StartIntercept(&output_fd);
317 FinishCrasher();
318 AssertDeath(SIGSEGV);
319 FinishIntercept(&intercept_result);
320
321 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
322
323 std::string result;
324 ConsumeFd(std::move(output_fd), &result);
325 ASSERT_MATCH(result, R"(signal 11 \(SIGSEGV\), code 1 \(SEGV_MAPERR\), fault addr 0xdead)");
326}
327
Josh Gaocbe70cb2016-10-18 18:17:52 -0700328TEST_F(CrasherTest, abort) {
329 int intercept_result;
330 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800331 StartProcess([]() {
332 abort();
333 });
Josh Gaocbe70cb2016-10-18 18:17:52 -0700334 StartIntercept(&output_fd);
335 FinishCrasher();
336 AssertDeath(SIGABRT);
337 FinishIntercept(&intercept_result);
338
339 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
340
341 std::string result;
342 ConsumeFd(std::move(output_fd), &result);
Andreas Gampe26cbafb2017-06-22 20:14:43 -0700343 ASSERT_BACKTRACE_FRAME(result, "abort");
Josh Gaocbe70cb2016-10-18 18:17:52 -0700344}
345
346TEST_F(CrasherTest, signal) {
347 int intercept_result;
348 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800349 StartProcess([]() {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700350 while (true) {
351 sleep(1);
352 }
Josh Gao502cfd22017-02-17 01:39:15 -0800353 });
Josh Gaocbe70cb2016-10-18 18:17:52 -0700354 StartIntercept(&output_fd);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700355 FinishCrasher();
Josh Gaocbe70cb2016-10-18 18:17:52 -0700356 ASSERT_EQ(0, kill(crasher_pid, SIGSEGV));
357
358 AssertDeath(SIGSEGV);
359 FinishIntercept(&intercept_result);
360
361 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
362
363 std::string result;
364 ConsumeFd(std::move(output_fd), &result);
Elliott Hughes89722702018-05-02 10:47:00 -0700365 ASSERT_MATCH(
366 result,
367 R"(signal 11 \(SIGSEGV\), code 0 \(SI_USER from pid \d+, uid \d+\), fault addr --------)");
Josh Gaocbe70cb2016-10-18 18:17:52 -0700368 ASSERT_MATCH(result, R"(backtrace:)");
369}
370
371TEST_F(CrasherTest, abort_message) {
372 int intercept_result;
373 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800374 StartProcess([]() {
Josh Gao1cc7bd82018-02-13 13:16:17 -0800375 // Arrived at experimentally;
376 // logd truncates at 4062.
377 // strlen("Abort message: ''") is 17.
378 // That's 4045, but we also want a NUL.
379 char buf[4045 + 1];
380 memset(buf, 'x', sizeof(buf));
381 buf[sizeof(buf) - 1] = '\0';
382 android_set_abort_message(buf);
Josh Gao502cfd22017-02-17 01:39:15 -0800383 abort();
384 });
Josh Gaocbe70cb2016-10-18 18:17:52 -0700385 StartIntercept(&output_fd);
386 FinishCrasher();
387 AssertDeath(SIGABRT);
388 FinishIntercept(&intercept_result);
389
390 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
391
392 std::string result;
393 ConsumeFd(std::move(output_fd), &result);
Josh Gao1cc7bd82018-02-13 13:16:17 -0800394 ASSERT_MATCH(result, R"(Abort message: 'x{4045}')");
Josh Gaocbe70cb2016-10-18 18:17:52 -0700395}
396
Josh Gaoe06f2a42017-04-27 16:50:38 -0700397TEST_F(CrasherTest, abort_message_backtrace) {
398 int intercept_result;
399 unique_fd output_fd;
400 StartProcess([]() {
401 android_set_abort_message("not actually aborting");
Josh Gaoa48b41b2019-12-13 14:11:04 -0800402 raise(BIONIC_SIGNAL_DEBUGGER);
Josh Gaoe06f2a42017-04-27 16:50:38 -0700403 exit(0);
404 });
405 StartIntercept(&output_fd);
406 FinishCrasher();
407 AssertDeath(0);
408 FinishIntercept(&intercept_result);
409
410 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
411
412 std::string result;
413 ConsumeFd(std::move(output_fd), &result);
414 ASSERT_NOT_MATCH(result, R"(Abort message:)");
415}
416
Josh Gaocbe70cb2016-10-18 18:17:52 -0700417TEST_F(CrasherTest, intercept_timeout) {
418 int intercept_result;
419 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800420 StartProcess([]() {
421 abort();
422 });
Josh Gaocbe70cb2016-10-18 18:17:52 -0700423 StartIntercept(&output_fd);
424
425 // Don't let crasher finish until we timeout.
426 FinishIntercept(&intercept_result);
427
428 ASSERT_NE(1, intercept_result) << "tombstoned reported success? (intercept_result = "
429 << intercept_result << ")";
430
431 FinishCrasher();
432 AssertDeath(SIGABRT);
433}
434
435TEST_F(CrasherTest, wait_for_gdb) {
436 if (!android::base::SetProperty(kWaitForGdbKey, "1")) {
437 FAIL() << "failed to enable wait_for_gdb";
438 }
439 sleep(1);
440
Josh Gao502cfd22017-02-17 01:39:15 -0800441 StartProcess([]() {
442 abort();
443 });
Josh Gaocbe70cb2016-10-18 18:17:52 -0700444 FinishCrasher();
445
446 int status;
Christopher Ferris172b0a02019-09-18 17:48:30 -0700447 ASSERT_EQ(crasher_pid, TEMP_FAILURE_RETRY(waitpid(crasher_pid, &status, WUNTRACED)));
Josh Gaocbe70cb2016-10-18 18:17:52 -0700448 ASSERT_TRUE(WIFSTOPPED(status));
449 ASSERT_EQ(SIGSTOP, WSTOPSIG(status));
450
451 ASSERT_EQ(0, kill(crasher_pid, SIGCONT));
452
453 AssertDeath(SIGABRT);
454}
455
Josh Gaocbe70cb2016-10-18 18:17:52 -0700456TEST_F(CrasherTest, backtrace) {
457 std::string result;
458 int intercept_result;
459 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800460
461 StartProcess([]() {
462 abort();
463 });
Narayan Kamatha73df602017-05-24 15:07:25 +0100464 StartIntercept(&output_fd, kDebuggerdNativeBacktrace);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700465
466 std::this_thread::sleep_for(500ms);
467
468 sigval val;
469 val.sival_int = 1;
Josh Gaoa48b41b2019-12-13 14:11:04 -0800470 ASSERT_EQ(0, sigqueue(crasher_pid, BIONIC_SIGNAL_DEBUGGER, val)) << strerror(errno);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700471 FinishIntercept(&intercept_result);
472 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
473 ConsumeFd(std::move(output_fd), &result);
Jaesung Chung58778e12017-06-15 18:20:34 +0900474 ASSERT_BACKTRACE_FRAME(result, "read");
Josh Gaocbe70cb2016-10-18 18:17:52 -0700475
476 int status;
477 ASSERT_EQ(0, waitpid(crasher_pid, &status, WNOHANG | WUNTRACED));
478
479 StartIntercept(&output_fd);
480 FinishCrasher();
481 AssertDeath(SIGABRT);
482 FinishIntercept(&intercept_result);
483 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
484 ConsumeFd(std::move(output_fd), &result);
Andreas Gampe26cbafb2017-06-22 20:14:43 -0700485 ASSERT_BACKTRACE_FRAME(result, "abort");
Josh Gaocbe70cb2016-10-18 18:17:52 -0700486}
Josh Gaofca7ca32017-01-23 12:05:35 -0800487
488TEST_F(CrasherTest, PR_SET_DUMPABLE_0_crash) {
Josh Gao502cfd22017-02-17 01:39:15 -0800489 int intercept_result;
490 unique_fd output_fd;
Josh Gaofca7ca32017-01-23 12:05:35 -0800491 StartProcess([]() {
492 prctl(PR_SET_DUMPABLE, 0);
Josh Gao502cfd22017-02-17 01:39:15 -0800493 abort();
Josh Gaofca7ca32017-01-23 12:05:35 -0800494 });
Josh Gao502cfd22017-02-17 01:39:15 -0800495
496 StartIntercept(&output_fd);
497 FinishCrasher();
498 AssertDeath(SIGABRT);
499 FinishIntercept(&intercept_result);
500
501 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
502
503 std::string result;
504 ConsumeFd(std::move(output_fd), &result);
Andreas Gampe26cbafb2017-06-22 20:14:43 -0700505 ASSERT_BACKTRACE_FRAME(result, "abort");
Josh Gaofca7ca32017-01-23 12:05:35 -0800506}
507
Josh Gao502cfd22017-02-17 01:39:15 -0800508TEST_F(CrasherTest, capabilities) {
509 ASSERT_EQ(0U, getuid()) << "capability test requires root";
510
Josh Gaofca7ca32017-01-23 12:05:35 -0800511 StartProcess([]() {
Josh Gao502cfd22017-02-17 01:39:15 -0800512 if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) != 0) {
513 err(1, "failed to set PR_SET_KEEPCAPS");
514 }
515
516 if (setresuid(1, 1, 1) != 0) {
517 err(1, "setresuid failed");
518 }
519
520 __user_cap_header_struct capheader;
521 __user_cap_data_struct capdata[2];
522 memset(&capheader, 0, sizeof(capheader));
523 memset(&capdata, 0, sizeof(capdata));
524
525 capheader.version = _LINUX_CAPABILITY_VERSION_3;
526 capheader.pid = 0;
527
528 // Turn on every third capability.
529 static_assert(CAP_LAST_CAP > 33, "CAP_LAST_CAP <= 32");
530 for (int i = 0; i < CAP_LAST_CAP; i += 3) {
531 capdata[CAP_TO_INDEX(i)].permitted |= CAP_TO_MASK(i);
532 capdata[CAP_TO_INDEX(i)].effective |= CAP_TO_MASK(i);
533 }
534
535 // Make sure CAP_SYS_PTRACE is off.
536 capdata[CAP_TO_INDEX(CAP_SYS_PTRACE)].permitted &= ~(CAP_TO_MASK(CAP_SYS_PTRACE));
537 capdata[CAP_TO_INDEX(CAP_SYS_PTRACE)].effective &= ~(CAP_TO_MASK(CAP_SYS_PTRACE));
538
539 if (capset(&capheader, &capdata[0]) != 0) {
540 err(1, "capset failed");
541 }
542
543 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) != 0) {
544 err(1, "failed to drop ambient capabilities");
545 }
546
Josh Gaoa5199a92017-04-03 13:18:34 -0700547 pthread_setname_np(pthread_self(), "thread_name");
Josh Gao502cfd22017-02-17 01:39:15 -0800548 raise(SIGSYS);
Josh Gaofca7ca32017-01-23 12:05:35 -0800549 });
Josh Gao502cfd22017-02-17 01:39:15 -0800550
551 unique_fd output_fd;
552 StartIntercept(&output_fd);
553 FinishCrasher();
554 AssertDeath(SIGSYS);
555
556 std::string result;
557 int intercept_result;
558 FinishIntercept(&intercept_result);
559 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
560 ConsumeFd(std::move(output_fd), &result);
Josh Gaoa5199a92017-04-03 13:18:34 -0700561 ASSERT_MATCH(result, R"(name: thread_name\s+>>> .+debuggerd_test(32|64) <<<)");
Jaesung Chung58778e12017-06-15 18:20:34 +0900562 ASSERT_BACKTRACE_FRAME(result, "tgkill");
Josh Gaofca7ca32017-01-23 12:05:35 -0800563}
Josh Gaoc3c8c022017-02-13 16:36:18 -0800564
Josh Gao2e7b8e22017-05-04 17:12:57 -0700565TEST_F(CrasherTest, fake_pid) {
566 int intercept_result;
567 unique_fd output_fd;
568
569 // Prime the getpid/gettid caches.
570 UNUSED(getpid());
571 UNUSED(gettid());
572
573 std::function<pid_t()> clone_fn = []() {
574 return syscall(__NR_clone, SIGCHLD, nullptr, nullptr, nullptr, nullptr);
575 };
576 StartProcess(
577 []() {
578 ASSERT_NE(getpid(), syscall(__NR_getpid));
579 ASSERT_NE(gettid(), syscall(__NR_gettid));
580 raise(SIGSEGV);
581 },
582 clone_fn);
583
584 StartIntercept(&output_fd);
585 FinishCrasher();
586 AssertDeath(SIGSEGV);
587 FinishIntercept(&intercept_result);
588
589 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
590
591 std::string result;
592 ConsumeFd(std::move(output_fd), &result);
Jaesung Chung58778e12017-06-15 18:20:34 +0900593 ASSERT_BACKTRACE_FRAME(result, "tgkill");
Josh Gao2e7b8e22017-05-04 17:12:57 -0700594}
595
Josh Gaoe04ca272018-01-16 15:38:17 -0800596static const char* const kDebuggerdSeccompPolicy =
597 "/system/etc/seccomp_policy/crash_dump." ABI_STRING ".policy";
598
Josh Gao70adac62018-02-22 11:38:33 -0800599static pid_t seccomp_fork_impl(void (*prejail)()) {
Josh Gao6f9eeec2018-09-12 13:55:47 -0700600 std::string policy;
601 if (!android::base::ReadFileToString(kDebuggerdSeccompPolicy, &policy)) {
602 PLOG(FATAL) << "failed to read policy file";
603 }
604
605 // Allow a bunch of syscalls used by the tests.
606 policy += "\nclone: 1";
607 policy += "\nsigaltstack: 1";
608 policy += "\nnanosleep: 1";
Christopher Ferrisab606682019-09-17 15:31:47 -0700609 policy += "\ngetrlimit: 1";
610 policy += "\nugetrlimit: 1";
Josh Gao6f9eeec2018-09-12 13:55:47 -0700611
612 FILE* tmp_file = tmpfile();
613 if (!tmp_file) {
614 PLOG(FATAL) << "tmpfile failed";
615 }
616
Christopher Ferris172b0a02019-09-18 17:48:30 -0700617 unique_fd tmp_fd(TEMP_FAILURE_RETRY(dup(fileno(tmp_file))));
Josh Gao6f9eeec2018-09-12 13:55:47 -0700618 if (!android::base::WriteStringToFd(policy, tmp_fd.get())) {
619 PLOG(FATAL) << "failed to write policy to tmpfile";
620 }
621
622 if (lseek(tmp_fd.get(), 0, SEEK_SET) != 0) {
623 PLOG(FATAL) << "failed to seek tmp_fd";
Josh Gaoe04ca272018-01-16 15:38:17 -0800624 }
625
626 ScopedMinijail jail{minijail_new()};
627 if (!jail) {
628 LOG(FATAL) << "failed to create minijail";
629 }
630
631 minijail_no_new_privs(jail.get());
632 minijail_log_seccomp_filter_failures(jail.get());
633 minijail_use_seccomp_filter(jail.get());
Josh Gao6f9eeec2018-09-12 13:55:47 -0700634 minijail_parse_seccomp_filters_from_fd(jail.get(), tmp_fd.release());
Josh Gaoe04ca272018-01-16 15:38:17 -0800635
636 pid_t result = fork();
637 if (result == -1) {
638 return result;
639 } else if (result != 0) {
640 return result;
641 }
642
643 // Spawn and detach a thread that spins forever.
644 std::atomic<bool> thread_ready(false);
645 std::thread thread([&jail, &thread_ready]() {
646 minijail_enter(jail.get());
647 thread_ready = true;
648 for (;;)
649 ;
650 });
651 thread.detach();
652
653 while (!thread_ready) {
654 continue;
655 }
656
Josh Gao70adac62018-02-22 11:38:33 -0800657 if (prejail) {
658 prejail();
659 }
660
Josh Gaoe04ca272018-01-16 15:38:17 -0800661 minijail_enter(jail.get());
662 return result;
663}
664
Josh Gao70adac62018-02-22 11:38:33 -0800665static pid_t seccomp_fork() {
666 return seccomp_fork_impl(nullptr);
667}
668
Josh Gaoe04ca272018-01-16 15:38:17 -0800669TEST_F(CrasherTest, seccomp_crash) {
670 int intercept_result;
671 unique_fd output_fd;
672
673 StartProcess([]() { abort(); }, &seccomp_fork);
674
675 StartIntercept(&output_fd);
676 FinishCrasher();
677 AssertDeath(SIGABRT);
678 FinishIntercept(&intercept_result);
679 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
680
681 std::string result;
682 ConsumeFd(std::move(output_fd), &result);
683 ASSERT_BACKTRACE_FRAME(result, "abort");
684}
685
Josh Gao70adac62018-02-22 11:38:33 -0800686static pid_t seccomp_fork_rlimit() {
687 return seccomp_fork_impl([]() {
688 struct rlimit rlim = {
689 .rlim_cur = 512 * 1024 * 1024,
690 .rlim_max = 512 * 1024 * 1024,
691 };
692
693 if (setrlimit(RLIMIT_AS, &rlim) != 0) {
694 raise(SIGINT);
695 }
696 });
697}
698
699TEST_F(CrasherTest, seccomp_crash_oom) {
700 int intercept_result;
701 unique_fd output_fd;
702
703 StartProcess(
704 []() {
705 std::vector<void*> vec;
706 for (int i = 0; i < 512; ++i) {
707 char* buf = static_cast<char*>(malloc(1024 * 1024));
708 if (!buf) {
709 abort();
710 }
711 memset(buf, 0xff, 1024 * 1024);
712 vec.push_back(buf);
713 }
714 },
715 &seccomp_fork_rlimit);
716
717 StartIntercept(&output_fd);
718 FinishCrasher();
719 AssertDeath(SIGABRT);
720 FinishIntercept(&intercept_result);
721 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
722
723 // We can't actually generate a backtrace, just make sure that the process terminates.
724}
725
Josh Gaoe04ca272018-01-16 15:38:17 -0800726__attribute__((noinline)) extern "C" bool raise_debugger_signal(DebuggerdDumpType dump_type) {
727 siginfo_t siginfo;
728 siginfo.si_code = SI_QUEUE;
729 siginfo.si_pid = getpid();
730 siginfo.si_uid = getuid();
731
732 if (dump_type != kDebuggerdNativeBacktrace && dump_type != kDebuggerdTombstone) {
733 PLOG(FATAL) << "invalid dump type";
734 }
735
736 siginfo.si_value.sival_int = dump_type == kDebuggerdNativeBacktrace;
737
Josh Gaoa48b41b2019-12-13 14:11:04 -0800738 if (syscall(__NR_rt_tgsigqueueinfo, getpid(), gettid(), BIONIC_SIGNAL_DEBUGGER, &siginfo) != 0) {
Josh Gaoe04ca272018-01-16 15:38:17 -0800739 PLOG(ERROR) << "libdebuggerd_client: failed to send signal to self";
740 return false;
741 }
742
743 return true;
744}
745
746TEST_F(CrasherTest, seccomp_tombstone) {
747 int intercept_result;
748 unique_fd output_fd;
749
750 static const auto dump_type = kDebuggerdTombstone;
751 StartProcess(
752 []() {
753 raise_debugger_signal(dump_type);
754 _exit(0);
755 },
756 &seccomp_fork);
757
758 StartIntercept(&output_fd, dump_type);
759 FinishCrasher();
760 AssertDeath(0);
761 FinishIntercept(&intercept_result);
762 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
763
764 std::string result;
765 ConsumeFd(std::move(output_fd), &result);
766 ASSERT_BACKTRACE_FRAME(result, "raise_debugger_signal");
767}
768
Josh Gao6f9eeec2018-09-12 13:55:47 -0700769extern "C" void foo() {
770 LOG(INFO) << "foo";
771 std::this_thread::sleep_for(1s);
772}
773
774extern "C" void bar() {
775 LOG(INFO) << "bar";
776 std::this_thread::sleep_for(1s);
777}
778
Josh Gaoe04ca272018-01-16 15:38:17 -0800779TEST_F(CrasherTest, seccomp_backtrace) {
780 int intercept_result;
781 unique_fd output_fd;
782
783 static const auto dump_type = kDebuggerdNativeBacktrace;
784 StartProcess(
785 []() {
Josh Gao6f9eeec2018-09-12 13:55:47 -0700786 std::thread a(foo);
787 std::thread b(bar);
788
789 std::this_thread::sleep_for(100ms);
790
Josh Gaoe04ca272018-01-16 15:38:17 -0800791 raise_debugger_signal(dump_type);
792 _exit(0);
793 },
794 &seccomp_fork);
795
796 StartIntercept(&output_fd, dump_type);
797 FinishCrasher();
798 AssertDeath(0);
799 FinishIntercept(&intercept_result);
800 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
801
802 std::string result;
803 ConsumeFd(std::move(output_fd), &result);
804 ASSERT_BACKTRACE_FRAME(result, "raise_debugger_signal");
Josh Gao6f9eeec2018-09-12 13:55:47 -0700805 ASSERT_BACKTRACE_FRAME(result, "foo");
806 ASSERT_BACKTRACE_FRAME(result, "bar");
Josh Gaoe04ca272018-01-16 15:38:17 -0800807}
808
809TEST_F(CrasherTest, seccomp_crash_logcat) {
810 StartProcess([]() { abort(); }, &seccomp_fork);
811 FinishCrasher();
812
813 // Make sure we don't get SIGSYS when trying to dump a crash to logcat.
814 AssertDeath(SIGABRT);
815}
816
Josh Gaofd13bf02017-08-18 15:37:26 -0700817TEST_F(CrasherTest, competing_tracer) {
818 int intercept_result;
819 unique_fd output_fd;
820 StartProcess([]() {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700821 raise(SIGABRT);
Josh Gaofd13bf02017-08-18 15:37:26 -0700822 });
823
824 StartIntercept(&output_fd);
Josh Gaofd13bf02017-08-18 15:37:26 -0700825
826 ASSERT_EQ(0, ptrace(PTRACE_SEIZE, crasher_pid, 0, 0));
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700827 FinishCrasher();
Josh Gaofd13bf02017-08-18 15:37:26 -0700828
829 int status;
Christopher Ferris172b0a02019-09-18 17:48:30 -0700830 ASSERT_EQ(crasher_pid, TEMP_FAILURE_RETRY(waitpid(crasher_pid, &status, 0)));
Josh Gaofd13bf02017-08-18 15:37:26 -0700831 ASSERT_TRUE(WIFSTOPPED(status));
832 ASSERT_EQ(SIGABRT, WSTOPSIG(status));
833
834 ASSERT_EQ(0, ptrace(PTRACE_CONT, crasher_pid, 0, SIGABRT));
835 FinishIntercept(&intercept_result);
836 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
837
838 std::string result;
839 ConsumeFd(std::move(output_fd), &result);
840 std::string regex = R"(failed to attach to thread \d+, already traced by )";
841 regex += std::to_string(gettid());
842 regex += R"( \(.+debuggerd_test)";
843 ASSERT_MATCH(result, regex.c_str());
844
Christopher Ferris172b0a02019-09-18 17:48:30 -0700845 ASSERT_EQ(crasher_pid, TEMP_FAILURE_RETRY(waitpid(crasher_pid, &status, 0)));
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700846 ASSERT_TRUE(WIFSTOPPED(status));
847 ASSERT_EQ(SIGABRT, WSTOPSIG(status));
848
Josh Gaofd13bf02017-08-18 15:37:26 -0700849 ASSERT_EQ(0, ptrace(PTRACE_DETACH, crasher_pid, 0, SIGABRT));
850 AssertDeath(SIGABRT);
851}
852
Josh Gaobf06a402018-08-27 16:34:01 -0700853TEST_F(CrasherTest, fdsan_warning_abort_message) {
854 int intercept_result;
855 unique_fd output_fd;
856
857 StartProcess([]() {
858 android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_WARN_ONCE);
Christopher Ferris172b0a02019-09-18 17:48:30 -0700859 unique_fd fd(TEMP_FAILURE_RETRY(open("/dev/null", O_RDONLY | O_CLOEXEC)));
Josh Gaobf06a402018-08-27 16:34:01 -0700860 if (fd == -1) {
861 abort();
862 }
863 close(fd.get());
864 _exit(0);
865 });
866
867 StartIntercept(&output_fd);
868 FinishCrasher();
869 AssertDeath(0);
870 FinishIntercept(&intercept_result);
871 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
872
873 std::string result;
874 ConsumeFd(std::move(output_fd), &result);
875 ASSERT_MATCH(result, "Abort message: 'attempted to close");
876}
877
Josh Gaoc3c8c022017-02-13 16:36:18 -0800878TEST(crash_dump, zombie) {
879 pid_t forkpid = fork();
880
Josh Gaoc3c8c022017-02-13 16:36:18 -0800881 pid_t rc;
882 int status;
883
884 if (forkpid == 0) {
885 errno = 0;
886 rc = waitpid(-1, &status, WNOHANG | __WALL | __WNOTHREAD);
887 if (rc != -1 || errno != ECHILD) {
888 errx(2, "first waitpid returned %d (%s), expected failure with ECHILD", rc, strerror(errno));
889 }
890
Josh Gaoa48b41b2019-12-13 14:11:04 -0800891 raise(BIONIC_SIGNAL_DEBUGGER);
Josh Gaoc3c8c022017-02-13 16:36:18 -0800892
893 errno = 0;
Christopher Ferris172b0a02019-09-18 17:48:30 -0700894 rc = TEMP_FAILURE_RETRY(waitpid(-1, &status, __WALL | __WNOTHREAD));
Josh Gaoc3c8c022017-02-13 16:36:18 -0800895 if (rc != -1 || errno != ECHILD) {
896 errx(2, "second waitpid returned %d (%s), expected failure with ECHILD", rc, strerror(errno));
897 }
898 _exit(0);
899 } else {
Christopher Ferris172b0a02019-09-18 17:48:30 -0700900 rc = TEMP_FAILURE_RETRY(waitpid(forkpid, &status, 0));
Josh Gaoc3c8c022017-02-13 16:36:18 -0800901 ASSERT_EQ(forkpid, rc);
902 ASSERT_TRUE(WIFEXITED(status));
903 ASSERT_EQ(0, WEXITSTATUS(status));
904 }
905}
Josh Gao352a8452017-03-30 16:46:21 -0700906
907TEST(tombstoned, no_notify) {
908 // Do this a few times.
909 for (int i = 0; i < 3; ++i) {
910 pid_t pid = 123'456'789 + i;
911
912 unique_fd intercept_fd, output_fd;
Narayan Kamathca5e9082017-06-02 15:42:06 +0100913 InterceptStatus status;
914 tombstoned_intercept(pid, &intercept_fd, &output_fd, &status, kDebuggerdTombstone);
915 ASSERT_EQ(InterceptStatus::kRegistered, status);
Josh Gao352a8452017-03-30 16:46:21 -0700916
917 {
918 unique_fd tombstoned_socket, input_fd;
Narayan Kamatha73df602017-05-24 15:07:25 +0100919 ASSERT_TRUE(tombstoned_connect(pid, &tombstoned_socket, &input_fd, kDebuggerdTombstone));
Josh Gao352a8452017-03-30 16:46:21 -0700920 ASSERT_TRUE(android::base::WriteFully(input_fd.get(), &pid, sizeof(pid)));
921 }
922
923 pid_t read_pid;
924 ASSERT_TRUE(android::base::ReadFully(output_fd.get(), &read_pid, sizeof(read_pid)));
925 ASSERT_EQ(read_pid, pid);
926 }
927}
928
929TEST(tombstoned, stress) {
930 // Spawn threads to simultaneously do a bunch of failing dumps and a bunch of successful dumps.
931 static constexpr int kDumpCount = 100;
932
933 std::atomic<bool> start(false);
934 std::vector<std::thread> threads;
935 threads.emplace_back([&start]() {
936 while (!start) {
937 continue;
938 }
939
940 // Use a way out of range pid, to avoid stomping on an actual process.
941 pid_t pid_base = 1'000'000;
942
943 for (int dump = 0; dump < kDumpCount; ++dump) {
944 pid_t pid = pid_base + dump;
945
946 unique_fd intercept_fd, output_fd;
Narayan Kamathca5e9082017-06-02 15:42:06 +0100947 InterceptStatus status;
948 tombstoned_intercept(pid, &intercept_fd, &output_fd, &status, kDebuggerdTombstone);
949 ASSERT_EQ(InterceptStatus::kRegistered, status);
Josh Gao352a8452017-03-30 16:46:21 -0700950
951 // Pretend to crash, and then immediately close the socket.
952 unique_fd sockfd(socket_local_client(kTombstonedCrashSocketName,
953 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_SEQPACKET));
954 if (sockfd == -1) {
955 FAIL() << "failed to connect to tombstoned: " << strerror(errno);
956 }
957 TombstonedCrashPacket packet = {};
958 packet.packet_type = CrashPacketType::kDumpRequest;
959 packet.packet.dump_request.pid = pid;
960 if (TEMP_FAILURE_RETRY(write(sockfd, &packet, sizeof(packet))) != sizeof(packet)) {
961 FAIL() << "failed to write to tombstoned: " << strerror(errno);
962 }
963
964 continue;
965 }
966 });
967
968 threads.emplace_back([&start]() {
969 while (!start) {
970 continue;
971 }
972
973 // Use a way out of range pid, to avoid stomping on an actual process.
974 pid_t pid_base = 2'000'000;
975
976 for (int dump = 0; dump < kDumpCount; ++dump) {
977 pid_t pid = pid_base + dump;
978
979 unique_fd intercept_fd, output_fd;
Narayan Kamathca5e9082017-06-02 15:42:06 +0100980 InterceptStatus status;
981 tombstoned_intercept(pid, &intercept_fd, &output_fd, &status, kDebuggerdTombstone);
982 ASSERT_EQ(InterceptStatus::kRegistered, status);
Josh Gao352a8452017-03-30 16:46:21 -0700983
984 {
985 unique_fd tombstoned_socket, input_fd;
Narayan Kamatha73df602017-05-24 15:07:25 +0100986 ASSERT_TRUE(tombstoned_connect(pid, &tombstoned_socket, &input_fd, kDebuggerdTombstone));
Josh Gao352a8452017-03-30 16:46:21 -0700987 ASSERT_TRUE(android::base::WriteFully(input_fd.get(), &pid, sizeof(pid)));
988 tombstoned_notify_completion(tombstoned_socket.get());
989 }
990
991 // TODO: Fix the race that requires this sleep.
992 std::this_thread::sleep_for(50ms);
993
994 pid_t read_pid;
995 ASSERT_TRUE(android::base::ReadFully(output_fd.get(), &read_pid, sizeof(read_pid)));
996 ASSERT_EQ(read_pid, pid);
997 }
998 });
999
1000 start = true;
1001
1002 for (std::thread& thread : threads) {
1003 thread.join();
1004 }
1005}
Narayan Kamathca5e9082017-06-02 15:42:06 +01001006
1007TEST(tombstoned, java_trace_intercept_smoke) {
1008 // Using a "real" PID is a little dangerous here - if the test fails
1009 // or crashes, we might end up getting a bogus / unreliable stack
1010 // trace.
1011 const pid_t self = getpid();
1012
1013 unique_fd intercept_fd, output_fd;
1014 InterceptStatus status;
1015 tombstoned_intercept(self, &intercept_fd, &output_fd, &status, kDebuggerdJavaBacktrace);
1016 ASSERT_EQ(InterceptStatus::kRegistered, status);
1017
1018 // First connect to tombstoned requesting a native backtrace. This
1019 // should result in a "regular" FD and not the installed intercept.
1020 const char native[] = "native";
1021 unique_fd tombstoned_socket, input_fd;
1022 ASSERT_TRUE(tombstoned_connect(self, &tombstoned_socket, &input_fd, kDebuggerdNativeBacktrace));
1023 ASSERT_TRUE(android::base::WriteFully(input_fd.get(), native, sizeof(native)));
1024 tombstoned_notify_completion(tombstoned_socket.get());
1025
1026 // Then, connect to tombstoned asking for a java backtrace. This *should*
1027 // trigger the intercept.
1028 const char java[] = "java";
1029 ASSERT_TRUE(tombstoned_connect(self, &tombstoned_socket, &input_fd, kDebuggerdJavaBacktrace));
1030 ASSERT_TRUE(android::base::WriteFully(input_fd.get(), java, sizeof(java)));
1031 tombstoned_notify_completion(tombstoned_socket.get());
1032
1033 char outbuf[sizeof(java)];
1034 ASSERT_TRUE(android::base::ReadFully(output_fd.get(), outbuf, sizeof(outbuf)));
1035 ASSERT_STREQ("java", outbuf);
1036}
1037
1038TEST(tombstoned, multiple_intercepts) {
1039 const pid_t fake_pid = 1'234'567;
1040 unique_fd intercept_fd, output_fd;
1041 InterceptStatus status;
1042 tombstoned_intercept(fake_pid, &intercept_fd, &output_fd, &status, kDebuggerdJavaBacktrace);
1043 ASSERT_EQ(InterceptStatus::kRegistered, status);
1044
1045 unique_fd intercept_fd_2, output_fd_2;
1046 tombstoned_intercept(fake_pid, &intercept_fd_2, &output_fd_2, &status, kDebuggerdNativeBacktrace);
1047 ASSERT_EQ(InterceptStatus::kFailedAlreadyRegistered, status);
1048}
1049
1050TEST(tombstoned, intercept_any) {
1051 const pid_t fake_pid = 1'234'567;
1052
1053 unique_fd intercept_fd, output_fd;
1054 InterceptStatus status;
1055 tombstoned_intercept(fake_pid, &intercept_fd, &output_fd, &status, kDebuggerdNativeBacktrace);
1056 ASSERT_EQ(InterceptStatus::kRegistered, status);
1057
1058 const char any[] = "any";
1059 unique_fd tombstoned_socket, input_fd;
1060 ASSERT_TRUE(tombstoned_connect(fake_pid, &tombstoned_socket, &input_fd, kDebuggerdAnyIntercept));
1061 ASSERT_TRUE(android::base::WriteFully(input_fd.get(), any, sizeof(any)));
1062 tombstoned_notify_completion(tombstoned_socket.get());
1063
1064 char outbuf[sizeof(any)];
1065 ASSERT_TRUE(android::base::ReadFully(output_fd.get(), outbuf, sizeof(outbuf)));
1066 ASSERT_STREQ("any", outbuf);
1067}
Josh Gao2b22ae12018-09-12 14:51:03 -07001068
1069TEST(tombstoned, interceptless_backtrace) {
1070 // Generate 50 backtraces, and then check to see that we haven't created 50 new tombstones.
1071 auto get_tombstone_timestamps = []() -> std::map<int, time_t> {
1072 std::map<int, time_t> result;
1073 for (int i = 0; i < 99; ++i) {
1074 std::string path = android::base::StringPrintf("/data/tombstones/tombstone_%02d", i);
1075 struct stat st;
1076 if (stat(path.c_str(), &st) == 0) {
1077 result[i] = st.st_mtim.tv_sec;
1078 }
1079 }
1080 return result;
1081 };
1082
1083 auto before = get_tombstone_timestamps();
1084 for (int i = 0; i < 50; ++i) {
1085 raise_debugger_signal(kDebuggerdNativeBacktrace);
1086 }
1087 auto after = get_tombstone_timestamps();
1088
1089 int diff = 0;
1090 for (int i = 0; i < 99; ++i) {
1091 if (after.count(i) == 0) {
1092 continue;
1093 }
1094 if (before.count(i) == 0) {
1095 ++diff;
1096 continue;
1097 }
1098 if (before[i] != after[i]) {
1099 ++diff;
1100 }
1101 }
1102
1103 // We can't be sure that nothing's crash looping in the background.
1104 // This should be good enough, though...
1105 ASSERT_LT(diff, 10) << "too many new tombstones; is something crashing in the background?";
1106}
Christopher Ferris481e8372019-07-15 17:13:24 -07001107
1108static __attribute__((__noinline__)) void overflow_stack(void* p) {
1109 void* buf[1];
1110 buf[0] = p;
1111 static volatile void* global = buf;
1112 if (global) {
1113 global = buf;
1114 overflow_stack(&buf);
1115 }
1116}
1117
1118TEST_F(CrasherTest, stack_overflow) {
1119 int intercept_result;
1120 unique_fd output_fd;
1121 StartProcess([]() { overflow_stack(nullptr); });
1122
1123 StartIntercept(&output_fd);
1124 FinishCrasher();
1125 AssertDeath(SIGSEGV);
1126 FinishIntercept(&intercept_result);
1127
1128 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
1129
1130 std::string result;
1131 ConsumeFd(std::move(output_fd), &result);
1132 ASSERT_MATCH(result, R"(Cause: stack pointer[^\n]*stack overflow.\n)");
1133}