blob: 054f8367501ad2f2295569b983a7468b7aeeba6a [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
Peter Collingbournef03af882020-03-20 18:09:00 -0700308TEST_F(CrasherTest, tagged_fault_addr) {
309#if !defined(__aarch64__)
310 GTEST_SKIP() << "Requires aarch64";
311#endif
312 int intercept_result;
313 unique_fd output_fd;
314 StartProcess([]() {
315 *reinterpret_cast<volatile char*>(0x100000000000dead) = '1';
316 });
317
318 StartIntercept(&output_fd);
319 FinishCrasher();
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
328 // The address can either be tagged (new kernels) or untagged (old kernels).
329 ASSERT_MATCH(
330 result,
331 R"(signal 11 \(SIGSEGV\), code 1 \(SEGV_MAPERR\), fault addr (0x100000000000dead|0xdead))");
332}
333
Josh Gaocdea7502017-11-01 15:00:40 -0700334TEST_F(CrasherTest, LD_PRELOAD) {
335 int intercept_result;
336 unique_fd output_fd;
337 StartProcess([]() {
338 setenv("LD_PRELOAD", "nonexistent.so", 1);
339 *reinterpret_cast<volatile char*>(0xdead) = '1';
340 });
341
342 StartIntercept(&output_fd);
343 FinishCrasher();
344 AssertDeath(SIGSEGV);
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);
351 ASSERT_MATCH(result, R"(signal 11 \(SIGSEGV\), code 1 \(SEGV_MAPERR\), fault addr 0xdead)");
352}
353
Josh Gaocbe70cb2016-10-18 18:17:52 -0700354TEST_F(CrasherTest, abort) {
355 int intercept_result;
356 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800357 StartProcess([]() {
358 abort();
359 });
Josh Gaocbe70cb2016-10-18 18:17:52 -0700360 StartIntercept(&output_fd);
361 FinishCrasher();
362 AssertDeath(SIGABRT);
363 FinishIntercept(&intercept_result);
364
365 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
366
367 std::string result;
368 ConsumeFd(std::move(output_fd), &result);
Andreas Gampe26cbafb2017-06-22 20:14:43 -0700369 ASSERT_BACKTRACE_FRAME(result, "abort");
Josh Gaocbe70cb2016-10-18 18:17:52 -0700370}
371
372TEST_F(CrasherTest, signal) {
373 int intercept_result;
374 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800375 StartProcess([]() {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700376 while (true) {
377 sleep(1);
378 }
Josh Gao502cfd22017-02-17 01:39:15 -0800379 });
Josh Gaocbe70cb2016-10-18 18:17:52 -0700380 StartIntercept(&output_fd);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700381 FinishCrasher();
Josh Gaocbe70cb2016-10-18 18:17:52 -0700382 ASSERT_EQ(0, kill(crasher_pid, SIGSEGV));
383
384 AssertDeath(SIGSEGV);
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);
Elliott Hughes89722702018-05-02 10:47:00 -0700391 ASSERT_MATCH(
392 result,
393 R"(signal 11 \(SIGSEGV\), code 0 \(SI_USER from pid \d+, uid \d+\), fault addr --------)");
Josh Gaocbe70cb2016-10-18 18:17:52 -0700394 ASSERT_MATCH(result, R"(backtrace:)");
395}
396
397TEST_F(CrasherTest, abort_message) {
398 int intercept_result;
399 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800400 StartProcess([]() {
Josh Gao1cc7bd82018-02-13 13:16:17 -0800401 // Arrived at experimentally;
402 // logd truncates at 4062.
403 // strlen("Abort message: ''") is 17.
404 // That's 4045, but we also want a NUL.
405 char buf[4045 + 1];
406 memset(buf, 'x', sizeof(buf));
407 buf[sizeof(buf) - 1] = '\0';
408 android_set_abort_message(buf);
Josh Gao502cfd22017-02-17 01:39:15 -0800409 abort();
410 });
Josh Gaocbe70cb2016-10-18 18:17:52 -0700411 StartIntercept(&output_fd);
412 FinishCrasher();
413 AssertDeath(SIGABRT);
414 FinishIntercept(&intercept_result);
415
416 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
417
418 std::string result;
419 ConsumeFd(std::move(output_fd), &result);
Josh Gao1cc7bd82018-02-13 13:16:17 -0800420 ASSERT_MATCH(result, R"(Abort message: 'x{4045}')");
Josh Gaocbe70cb2016-10-18 18:17:52 -0700421}
422
Josh Gaoe06f2a42017-04-27 16:50:38 -0700423TEST_F(CrasherTest, abort_message_backtrace) {
424 int intercept_result;
425 unique_fd output_fd;
426 StartProcess([]() {
427 android_set_abort_message("not actually aborting");
Josh Gaoa48b41b2019-12-13 14:11:04 -0800428 raise(BIONIC_SIGNAL_DEBUGGER);
Josh Gaoe06f2a42017-04-27 16:50:38 -0700429 exit(0);
430 });
431 StartIntercept(&output_fd);
432 FinishCrasher();
433 AssertDeath(0);
434 FinishIntercept(&intercept_result);
435
436 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
437
438 std::string result;
439 ConsumeFd(std::move(output_fd), &result);
440 ASSERT_NOT_MATCH(result, R"(Abort message:)");
441}
442
Josh Gaocbe70cb2016-10-18 18:17:52 -0700443TEST_F(CrasherTest, intercept_timeout) {
444 int intercept_result;
445 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800446 StartProcess([]() {
447 abort();
448 });
Josh Gaocbe70cb2016-10-18 18:17:52 -0700449 StartIntercept(&output_fd);
450
451 // Don't let crasher finish until we timeout.
452 FinishIntercept(&intercept_result);
453
454 ASSERT_NE(1, intercept_result) << "tombstoned reported success? (intercept_result = "
455 << intercept_result << ")";
456
457 FinishCrasher();
458 AssertDeath(SIGABRT);
459}
460
461TEST_F(CrasherTest, wait_for_gdb) {
462 if (!android::base::SetProperty(kWaitForGdbKey, "1")) {
463 FAIL() << "failed to enable wait_for_gdb";
464 }
465 sleep(1);
466
Josh Gao502cfd22017-02-17 01:39:15 -0800467 StartProcess([]() {
468 abort();
469 });
Josh Gaocbe70cb2016-10-18 18:17:52 -0700470 FinishCrasher();
471
472 int status;
Christopher Ferris172b0a02019-09-18 17:48:30 -0700473 ASSERT_EQ(crasher_pid, TEMP_FAILURE_RETRY(waitpid(crasher_pid, &status, WUNTRACED)));
Josh Gaocbe70cb2016-10-18 18:17:52 -0700474 ASSERT_TRUE(WIFSTOPPED(status));
475 ASSERT_EQ(SIGSTOP, WSTOPSIG(status));
476
477 ASSERT_EQ(0, kill(crasher_pid, SIGCONT));
478
479 AssertDeath(SIGABRT);
480}
481
Josh Gaocbe70cb2016-10-18 18:17:52 -0700482TEST_F(CrasherTest, backtrace) {
483 std::string result;
484 int intercept_result;
485 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800486
487 StartProcess([]() {
488 abort();
489 });
Narayan Kamatha73df602017-05-24 15:07:25 +0100490 StartIntercept(&output_fd, kDebuggerdNativeBacktrace);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700491
492 std::this_thread::sleep_for(500ms);
493
494 sigval val;
495 val.sival_int = 1;
Josh Gaoa48b41b2019-12-13 14:11:04 -0800496 ASSERT_EQ(0, sigqueue(crasher_pid, BIONIC_SIGNAL_DEBUGGER, val)) << strerror(errno);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700497 FinishIntercept(&intercept_result);
498 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
499 ConsumeFd(std::move(output_fd), &result);
Jaesung Chung58778e12017-06-15 18:20:34 +0900500 ASSERT_BACKTRACE_FRAME(result, "read");
Josh Gaocbe70cb2016-10-18 18:17:52 -0700501
502 int status;
503 ASSERT_EQ(0, waitpid(crasher_pid, &status, WNOHANG | WUNTRACED));
504
505 StartIntercept(&output_fd);
506 FinishCrasher();
507 AssertDeath(SIGABRT);
508 FinishIntercept(&intercept_result);
509 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
510 ConsumeFd(std::move(output_fd), &result);
Andreas Gampe26cbafb2017-06-22 20:14:43 -0700511 ASSERT_BACKTRACE_FRAME(result, "abort");
Josh Gaocbe70cb2016-10-18 18:17:52 -0700512}
Josh Gaofca7ca32017-01-23 12:05:35 -0800513
514TEST_F(CrasherTest, PR_SET_DUMPABLE_0_crash) {
Josh Gao502cfd22017-02-17 01:39:15 -0800515 int intercept_result;
516 unique_fd output_fd;
Josh Gaofca7ca32017-01-23 12:05:35 -0800517 StartProcess([]() {
518 prctl(PR_SET_DUMPABLE, 0);
Josh Gao502cfd22017-02-17 01:39:15 -0800519 abort();
Josh Gaofca7ca32017-01-23 12:05:35 -0800520 });
Josh Gao502cfd22017-02-17 01:39:15 -0800521
522 StartIntercept(&output_fd);
523 FinishCrasher();
524 AssertDeath(SIGABRT);
525 FinishIntercept(&intercept_result);
526
527 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
528
529 std::string result;
530 ConsumeFd(std::move(output_fd), &result);
Andreas Gampe26cbafb2017-06-22 20:14:43 -0700531 ASSERT_BACKTRACE_FRAME(result, "abort");
Josh Gaofca7ca32017-01-23 12:05:35 -0800532}
533
Josh Gao502cfd22017-02-17 01:39:15 -0800534TEST_F(CrasherTest, capabilities) {
535 ASSERT_EQ(0U, getuid()) << "capability test requires root";
536
Josh Gaofca7ca32017-01-23 12:05:35 -0800537 StartProcess([]() {
Josh Gao502cfd22017-02-17 01:39:15 -0800538 if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) != 0) {
539 err(1, "failed to set PR_SET_KEEPCAPS");
540 }
541
542 if (setresuid(1, 1, 1) != 0) {
543 err(1, "setresuid failed");
544 }
545
546 __user_cap_header_struct capheader;
547 __user_cap_data_struct capdata[2];
548 memset(&capheader, 0, sizeof(capheader));
549 memset(&capdata, 0, sizeof(capdata));
550
551 capheader.version = _LINUX_CAPABILITY_VERSION_3;
552 capheader.pid = 0;
553
554 // Turn on every third capability.
555 static_assert(CAP_LAST_CAP > 33, "CAP_LAST_CAP <= 32");
556 for (int i = 0; i < CAP_LAST_CAP; i += 3) {
557 capdata[CAP_TO_INDEX(i)].permitted |= CAP_TO_MASK(i);
558 capdata[CAP_TO_INDEX(i)].effective |= CAP_TO_MASK(i);
559 }
560
561 // Make sure CAP_SYS_PTRACE is off.
562 capdata[CAP_TO_INDEX(CAP_SYS_PTRACE)].permitted &= ~(CAP_TO_MASK(CAP_SYS_PTRACE));
563 capdata[CAP_TO_INDEX(CAP_SYS_PTRACE)].effective &= ~(CAP_TO_MASK(CAP_SYS_PTRACE));
564
565 if (capset(&capheader, &capdata[0]) != 0) {
566 err(1, "capset failed");
567 }
568
569 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) != 0) {
570 err(1, "failed to drop ambient capabilities");
571 }
572
Josh Gaoa5199a92017-04-03 13:18:34 -0700573 pthread_setname_np(pthread_self(), "thread_name");
Josh Gao502cfd22017-02-17 01:39:15 -0800574 raise(SIGSYS);
Josh Gaofca7ca32017-01-23 12:05:35 -0800575 });
Josh Gao502cfd22017-02-17 01:39:15 -0800576
577 unique_fd output_fd;
578 StartIntercept(&output_fd);
579 FinishCrasher();
580 AssertDeath(SIGSYS);
581
582 std::string result;
583 int intercept_result;
584 FinishIntercept(&intercept_result);
585 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
586 ConsumeFd(std::move(output_fd), &result);
Josh Gaoa5199a92017-04-03 13:18:34 -0700587 ASSERT_MATCH(result, R"(name: thread_name\s+>>> .+debuggerd_test(32|64) <<<)");
Jaesung Chung58778e12017-06-15 18:20:34 +0900588 ASSERT_BACKTRACE_FRAME(result, "tgkill");
Josh Gaofca7ca32017-01-23 12:05:35 -0800589}
Josh Gaoc3c8c022017-02-13 16:36:18 -0800590
Josh Gao2e7b8e22017-05-04 17:12:57 -0700591TEST_F(CrasherTest, fake_pid) {
592 int intercept_result;
593 unique_fd output_fd;
594
595 // Prime the getpid/gettid caches.
596 UNUSED(getpid());
597 UNUSED(gettid());
598
599 std::function<pid_t()> clone_fn = []() {
600 return syscall(__NR_clone, SIGCHLD, nullptr, nullptr, nullptr, nullptr);
601 };
602 StartProcess(
603 []() {
604 ASSERT_NE(getpid(), syscall(__NR_getpid));
605 ASSERT_NE(gettid(), syscall(__NR_gettid));
606 raise(SIGSEGV);
607 },
608 clone_fn);
609
610 StartIntercept(&output_fd);
611 FinishCrasher();
612 AssertDeath(SIGSEGV);
613 FinishIntercept(&intercept_result);
614
615 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
616
617 std::string result;
618 ConsumeFd(std::move(output_fd), &result);
Jaesung Chung58778e12017-06-15 18:20:34 +0900619 ASSERT_BACKTRACE_FRAME(result, "tgkill");
Josh Gao2e7b8e22017-05-04 17:12:57 -0700620}
621
Josh Gaoe04ca272018-01-16 15:38:17 -0800622static const char* const kDebuggerdSeccompPolicy =
623 "/system/etc/seccomp_policy/crash_dump." ABI_STRING ".policy";
624
Josh Gao70adac62018-02-22 11:38:33 -0800625static pid_t seccomp_fork_impl(void (*prejail)()) {
Josh Gao6f9eeec2018-09-12 13:55:47 -0700626 std::string policy;
627 if (!android::base::ReadFileToString(kDebuggerdSeccompPolicy, &policy)) {
628 PLOG(FATAL) << "failed to read policy file";
629 }
630
631 // Allow a bunch of syscalls used by the tests.
632 policy += "\nclone: 1";
633 policy += "\nsigaltstack: 1";
634 policy += "\nnanosleep: 1";
Christopher Ferrisab606682019-09-17 15:31:47 -0700635 policy += "\ngetrlimit: 1";
636 policy += "\nugetrlimit: 1";
Josh Gao6f9eeec2018-09-12 13:55:47 -0700637
638 FILE* tmp_file = tmpfile();
639 if (!tmp_file) {
640 PLOG(FATAL) << "tmpfile failed";
641 }
642
Christopher Ferris172b0a02019-09-18 17:48:30 -0700643 unique_fd tmp_fd(TEMP_FAILURE_RETRY(dup(fileno(tmp_file))));
Josh Gao6f9eeec2018-09-12 13:55:47 -0700644 if (!android::base::WriteStringToFd(policy, tmp_fd.get())) {
645 PLOG(FATAL) << "failed to write policy to tmpfile";
646 }
647
648 if (lseek(tmp_fd.get(), 0, SEEK_SET) != 0) {
649 PLOG(FATAL) << "failed to seek tmp_fd";
Josh Gaoe04ca272018-01-16 15:38:17 -0800650 }
651
652 ScopedMinijail jail{minijail_new()};
653 if (!jail) {
654 LOG(FATAL) << "failed to create minijail";
655 }
656
657 minijail_no_new_privs(jail.get());
658 minijail_log_seccomp_filter_failures(jail.get());
659 minijail_use_seccomp_filter(jail.get());
Josh Gao6f9eeec2018-09-12 13:55:47 -0700660 minijail_parse_seccomp_filters_from_fd(jail.get(), tmp_fd.release());
Josh Gaoe04ca272018-01-16 15:38:17 -0800661
662 pid_t result = fork();
663 if (result == -1) {
664 return result;
665 } else if (result != 0) {
666 return result;
667 }
668
669 // Spawn and detach a thread that spins forever.
670 std::atomic<bool> thread_ready(false);
671 std::thread thread([&jail, &thread_ready]() {
672 minijail_enter(jail.get());
673 thread_ready = true;
674 for (;;)
675 ;
676 });
677 thread.detach();
678
679 while (!thread_ready) {
680 continue;
681 }
682
Josh Gao70adac62018-02-22 11:38:33 -0800683 if (prejail) {
684 prejail();
685 }
686
Josh Gaoe04ca272018-01-16 15:38:17 -0800687 minijail_enter(jail.get());
688 return result;
689}
690
Josh Gao70adac62018-02-22 11:38:33 -0800691static pid_t seccomp_fork() {
692 return seccomp_fork_impl(nullptr);
693}
694
Josh Gaoe04ca272018-01-16 15:38:17 -0800695TEST_F(CrasherTest, seccomp_crash) {
696 int intercept_result;
697 unique_fd output_fd;
698
699 StartProcess([]() { abort(); }, &seccomp_fork);
700
701 StartIntercept(&output_fd);
702 FinishCrasher();
703 AssertDeath(SIGABRT);
704 FinishIntercept(&intercept_result);
705 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
706
707 std::string result;
708 ConsumeFd(std::move(output_fd), &result);
709 ASSERT_BACKTRACE_FRAME(result, "abort");
710}
711
Josh Gao70adac62018-02-22 11:38:33 -0800712static pid_t seccomp_fork_rlimit() {
713 return seccomp_fork_impl([]() {
714 struct rlimit rlim = {
715 .rlim_cur = 512 * 1024 * 1024,
716 .rlim_max = 512 * 1024 * 1024,
717 };
718
719 if (setrlimit(RLIMIT_AS, &rlim) != 0) {
720 raise(SIGINT);
721 }
722 });
723}
724
725TEST_F(CrasherTest, seccomp_crash_oom) {
726 int intercept_result;
727 unique_fd output_fd;
728
729 StartProcess(
730 []() {
731 std::vector<void*> vec;
732 for (int i = 0; i < 512; ++i) {
733 char* buf = static_cast<char*>(malloc(1024 * 1024));
734 if (!buf) {
735 abort();
736 }
737 memset(buf, 0xff, 1024 * 1024);
738 vec.push_back(buf);
739 }
740 },
741 &seccomp_fork_rlimit);
742
743 StartIntercept(&output_fd);
744 FinishCrasher();
745 AssertDeath(SIGABRT);
746 FinishIntercept(&intercept_result);
747 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
748
749 // We can't actually generate a backtrace, just make sure that the process terminates.
750}
751
Josh Gaoe04ca272018-01-16 15:38:17 -0800752__attribute__((noinline)) extern "C" bool raise_debugger_signal(DebuggerdDumpType dump_type) {
753 siginfo_t siginfo;
754 siginfo.si_code = SI_QUEUE;
755 siginfo.si_pid = getpid();
756 siginfo.si_uid = getuid();
757
758 if (dump_type != kDebuggerdNativeBacktrace && dump_type != kDebuggerdTombstone) {
759 PLOG(FATAL) << "invalid dump type";
760 }
761
762 siginfo.si_value.sival_int = dump_type == kDebuggerdNativeBacktrace;
763
Josh Gaoa48b41b2019-12-13 14:11:04 -0800764 if (syscall(__NR_rt_tgsigqueueinfo, getpid(), gettid(), BIONIC_SIGNAL_DEBUGGER, &siginfo) != 0) {
Josh Gaoe04ca272018-01-16 15:38:17 -0800765 PLOG(ERROR) << "libdebuggerd_client: failed to send signal to self";
766 return false;
767 }
768
769 return true;
770}
771
772TEST_F(CrasherTest, seccomp_tombstone) {
773 int intercept_result;
774 unique_fd output_fd;
775
776 static const auto dump_type = kDebuggerdTombstone;
777 StartProcess(
778 []() {
779 raise_debugger_signal(dump_type);
780 _exit(0);
781 },
782 &seccomp_fork);
783
784 StartIntercept(&output_fd, dump_type);
785 FinishCrasher();
786 AssertDeath(0);
787 FinishIntercept(&intercept_result);
788 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
789
790 std::string result;
791 ConsumeFd(std::move(output_fd), &result);
792 ASSERT_BACKTRACE_FRAME(result, "raise_debugger_signal");
793}
794
Josh Gao6f9eeec2018-09-12 13:55:47 -0700795extern "C" void foo() {
796 LOG(INFO) << "foo";
797 std::this_thread::sleep_for(1s);
798}
799
800extern "C" void bar() {
801 LOG(INFO) << "bar";
802 std::this_thread::sleep_for(1s);
803}
804
Josh Gaoe04ca272018-01-16 15:38:17 -0800805TEST_F(CrasherTest, seccomp_backtrace) {
806 int intercept_result;
807 unique_fd output_fd;
808
809 static const auto dump_type = kDebuggerdNativeBacktrace;
810 StartProcess(
811 []() {
Josh Gao6f9eeec2018-09-12 13:55:47 -0700812 std::thread a(foo);
813 std::thread b(bar);
814
815 std::this_thread::sleep_for(100ms);
816
Josh Gaoe04ca272018-01-16 15:38:17 -0800817 raise_debugger_signal(dump_type);
818 _exit(0);
819 },
820 &seccomp_fork);
821
822 StartIntercept(&output_fd, dump_type);
823 FinishCrasher();
824 AssertDeath(0);
825 FinishIntercept(&intercept_result);
826 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
827
828 std::string result;
829 ConsumeFd(std::move(output_fd), &result);
830 ASSERT_BACKTRACE_FRAME(result, "raise_debugger_signal");
Josh Gao6f9eeec2018-09-12 13:55:47 -0700831 ASSERT_BACKTRACE_FRAME(result, "foo");
832 ASSERT_BACKTRACE_FRAME(result, "bar");
Josh Gaoe04ca272018-01-16 15:38:17 -0800833}
834
835TEST_F(CrasherTest, seccomp_crash_logcat) {
836 StartProcess([]() { abort(); }, &seccomp_fork);
837 FinishCrasher();
838
839 // Make sure we don't get SIGSYS when trying to dump a crash to logcat.
840 AssertDeath(SIGABRT);
841}
842
Josh Gaofd13bf02017-08-18 15:37:26 -0700843TEST_F(CrasherTest, competing_tracer) {
844 int intercept_result;
845 unique_fd output_fd;
846 StartProcess([]() {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700847 raise(SIGABRT);
Josh Gaofd13bf02017-08-18 15:37:26 -0700848 });
849
850 StartIntercept(&output_fd);
Josh Gaofd13bf02017-08-18 15:37:26 -0700851
852 ASSERT_EQ(0, ptrace(PTRACE_SEIZE, crasher_pid, 0, 0));
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700853 FinishCrasher();
Josh Gaofd13bf02017-08-18 15:37:26 -0700854
855 int status;
Christopher Ferris172b0a02019-09-18 17:48:30 -0700856 ASSERT_EQ(crasher_pid, TEMP_FAILURE_RETRY(waitpid(crasher_pid, &status, 0)));
Josh Gaofd13bf02017-08-18 15:37:26 -0700857 ASSERT_TRUE(WIFSTOPPED(status));
858 ASSERT_EQ(SIGABRT, WSTOPSIG(status));
859
860 ASSERT_EQ(0, ptrace(PTRACE_CONT, crasher_pid, 0, SIGABRT));
861 FinishIntercept(&intercept_result);
862 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
863
864 std::string result;
865 ConsumeFd(std::move(output_fd), &result);
866 std::string regex = R"(failed to attach to thread \d+, already traced by )";
867 regex += std::to_string(gettid());
868 regex += R"( \(.+debuggerd_test)";
869 ASSERT_MATCH(result, regex.c_str());
870
Christopher Ferris172b0a02019-09-18 17:48:30 -0700871 ASSERT_EQ(crasher_pid, TEMP_FAILURE_RETRY(waitpid(crasher_pid, &status, 0)));
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700872 ASSERT_TRUE(WIFSTOPPED(status));
873 ASSERT_EQ(SIGABRT, WSTOPSIG(status));
874
Josh Gaofd13bf02017-08-18 15:37:26 -0700875 ASSERT_EQ(0, ptrace(PTRACE_DETACH, crasher_pid, 0, SIGABRT));
876 AssertDeath(SIGABRT);
877}
878
Josh Gaobf06a402018-08-27 16:34:01 -0700879TEST_F(CrasherTest, fdsan_warning_abort_message) {
880 int intercept_result;
881 unique_fd output_fd;
882
883 StartProcess([]() {
884 android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_WARN_ONCE);
Christopher Ferris172b0a02019-09-18 17:48:30 -0700885 unique_fd fd(TEMP_FAILURE_RETRY(open("/dev/null", O_RDONLY | O_CLOEXEC)));
Josh Gaobf06a402018-08-27 16:34:01 -0700886 if (fd == -1) {
887 abort();
888 }
889 close(fd.get());
890 _exit(0);
891 });
892
893 StartIntercept(&output_fd);
894 FinishCrasher();
895 AssertDeath(0);
896 FinishIntercept(&intercept_result);
897 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
898
899 std::string result;
900 ConsumeFd(std::move(output_fd), &result);
901 ASSERT_MATCH(result, "Abort message: 'attempted to close");
902}
903
Josh Gaoc3c8c022017-02-13 16:36:18 -0800904TEST(crash_dump, zombie) {
905 pid_t forkpid = fork();
906
Josh Gaoc3c8c022017-02-13 16:36:18 -0800907 pid_t rc;
908 int status;
909
910 if (forkpid == 0) {
911 errno = 0;
912 rc = waitpid(-1, &status, WNOHANG | __WALL | __WNOTHREAD);
913 if (rc != -1 || errno != ECHILD) {
914 errx(2, "first waitpid returned %d (%s), expected failure with ECHILD", rc, strerror(errno));
915 }
916
Josh Gaoa48b41b2019-12-13 14:11:04 -0800917 raise(BIONIC_SIGNAL_DEBUGGER);
Josh Gaoc3c8c022017-02-13 16:36:18 -0800918
919 errno = 0;
Christopher Ferris172b0a02019-09-18 17:48:30 -0700920 rc = TEMP_FAILURE_RETRY(waitpid(-1, &status, __WALL | __WNOTHREAD));
Josh Gaoc3c8c022017-02-13 16:36:18 -0800921 if (rc != -1 || errno != ECHILD) {
922 errx(2, "second waitpid returned %d (%s), expected failure with ECHILD", rc, strerror(errno));
923 }
924 _exit(0);
925 } else {
Christopher Ferris172b0a02019-09-18 17:48:30 -0700926 rc = TEMP_FAILURE_RETRY(waitpid(forkpid, &status, 0));
Josh Gaoc3c8c022017-02-13 16:36:18 -0800927 ASSERT_EQ(forkpid, rc);
928 ASSERT_TRUE(WIFEXITED(status));
929 ASSERT_EQ(0, WEXITSTATUS(status));
930 }
931}
Josh Gao352a8452017-03-30 16:46:21 -0700932
933TEST(tombstoned, no_notify) {
934 // Do this a few times.
935 for (int i = 0; i < 3; ++i) {
936 pid_t pid = 123'456'789 + i;
937
938 unique_fd intercept_fd, output_fd;
Narayan Kamathca5e9082017-06-02 15:42:06 +0100939 InterceptStatus status;
940 tombstoned_intercept(pid, &intercept_fd, &output_fd, &status, kDebuggerdTombstone);
941 ASSERT_EQ(InterceptStatus::kRegistered, status);
Josh Gao352a8452017-03-30 16:46:21 -0700942
943 {
944 unique_fd tombstoned_socket, input_fd;
Narayan Kamatha73df602017-05-24 15:07:25 +0100945 ASSERT_TRUE(tombstoned_connect(pid, &tombstoned_socket, &input_fd, kDebuggerdTombstone));
Josh Gao352a8452017-03-30 16:46:21 -0700946 ASSERT_TRUE(android::base::WriteFully(input_fd.get(), &pid, sizeof(pid)));
947 }
948
949 pid_t read_pid;
950 ASSERT_TRUE(android::base::ReadFully(output_fd.get(), &read_pid, sizeof(read_pid)));
951 ASSERT_EQ(read_pid, pid);
952 }
953}
954
955TEST(tombstoned, stress) {
956 // Spawn threads to simultaneously do a bunch of failing dumps and a bunch of successful dumps.
957 static constexpr int kDumpCount = 100;
958
959 std::atomic<bool> start(false);
960 std::vector<std::thread> threads;
961 threads.emplace_back([&start]() {
962 while (!start) {
963 continue;
964 }
965
966 // Use a way out of range pid, to avoid stomping on an actual process.
967 pid_t pid_base = 1'000'000;
968
969 for (int dump = 0; dump < kDumpCount; ++dump) {
970 pid_t pid = pid_base + dump;
971
972 unique_fd intercept_fd, output_fd;
Narayan Kamathca5e9082017-06-02 15:42:06 +0100973 InterceptStatus status;
974 tombstoned_intercept(pid, &intercept_fd, &output_fd, &status, kDebuggerdTombstone);
975 ASSERT_EQ(InterceptStatus::kRegistered, status);
Josh Gao352a8452017-03-30 16:46:21 -0700976
977 // Pretend to crash, and then immediately close the socket.
978 unique_fd sockfd(socket_local_client(kTombstonedCrashSocketName,
979 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_SEQPACKET));
980 if (sockfd == -1) {
981 FAIL() << "failed to connect to tombstoned: " << strerror(errno);
982 }
983 TombstonedCrashPacket packet = {};
984 packet.packet_type = CrashPacketType::kDumpRequest;
985 packet.packet.dump_request.pid = pid;
986 if (TEMP_FAILURE_RETRY(write(sockfd, &packet, sizeof(packet))) != sizeof(packet)) {
987 FAIL() << "failed to write to tombstoned: " << strerror(errno);
988 }
989
990 continue;
991 }
992 });
993
994 threads.emplace_back([&start]() {
995 while (!start) {
996 continue;
997 }
998
999 // Use a way out of range pid, to avoid stomping on an actual process.
1000 pid_t pid_base = 2'000'000;
1001
1002 for (int dump = 0; dump < kDumpCount; ++dump) {
1003 pid_t pid = pid_base + dump;
1004
1005 unique_fd intercept_fd, output_fd;
Narayan Kamathca5e9082017-06-02 15:42:06 +01001006 InterceptStatus status;
1007 tombstoned_intercept(pid, &intercept_fd, &output_fd, &status, kDebuggerdTombstone);
1008 ASSERT_EQ(InterceptStatus::kRegistered, status);
Josh Gao352a8452017-03-30 16:46:21 -07001009
1010 {
1011 unique_fd tombstoned_socket, input_fd;
Narayan Kamatha73df602017-05-24 15:07:25 +01001012 ASSERT_TRUE(tombstoned_connect(pid, &tombstoned_socket, &input_fd, kDebuggerdTombstone));
Josh Gao352a8452017-03-30 16:46:21 -07001013 ASSERT_TRUE(android::base::WriteFully(input_fd.get(), &pid, sizeof(pid)));
1014 tombstoned_notify_completion(tombstoned_socket.get());
1015 }
1016
1017 // TODO: Fix the race that requires this sleep.
1018 std::this_thread::sleep_for(50ms);
1019
1020 pid_t read_pid;
1021 ASSERT_TRUE(android::base::ReadFully(output_fd.get(), &read_pid, sizeof(read_pid)));
1022 ASSERT_EQ(read_pid, pid);
1023 }
1024 });
1025
1026 start = true;
1027
1028 for (std::thread& thread : threads) {
1029 thread.join();
1030 }
1031}
Narayan Kamathca5e9082017-06-02 15:42:06 +01001032
1033TEST(tombstoned, java_trace_intercept_smoke) {
1034 // Using a "real" PID is a little dangerous here - if the test fails
1035 // or crashes, we might end up getting a bogus / unreliable stack
1036 // trace.
1037 const pid_t self = getpid();
1038
1039 unique_fd intercept_fd, output_fd;
1040 InterceptStatus status;
1041 tombstoned_intercept(self, &intercept_fd, &output_fd, &status, kDebuggerdJavaBacktrace);
1042 ASSERT_EQ(InterceptStatus::kRegistered, status);
1043
1044 // First connect to tombstoned requesting a native backtrace. This
1045 // should result in a "regular" FD and not the installed intercept.
1046 const char native[] = "native";
1047 unique_fd tombstoned_socket, input_fd;
1048 ASSERT_TRUE(tombstoned_connect(self, &tombstoned_socket, &input_fd, kDebuggerdNativeBacktrace));
1049 ASSERT_TRUE(android::base::WriteFully(input_fd.get(), native, sizeof(native)));
1050 tombstoned_notify_completion(tombstoned_socket.get());
1051
1052 // Then, connect to tombstoned asking for a java backtrace. This *should*
1053 // trigger the intercept.
1054 const char java[] = "java";
1055 ASSERT_TRUE(tombstoned_connect(self, &tombstoned_socket, &input_fd, kDebuggerdJavaBacktrace));
1056 ASSERT_TRUE(android::base::WriteFully(input_fd.get(), java, sizeof(java)));
1057 tombstoned_notify_completion(tombstoned_socket.get());
1058
1059 char outbuf[sizeof(java)];
1060 ASSERT_TRUE(android::base::ReadFully(output_fd.get(), outbuf, sizeof(outbuf)));
1061 ASSERT_STREQ("java", outbuf);
1062}
1063
1064TEST(tombstoned, multiple_intercepts) {
1065 const pid_t fake_pid = 1'234'567;
1066 unique_fd intercept_fd, output_fd;
1067 InterceptStatus status;
1068 tombstoned_intercept(fake_pid, &intercept_fd, &output_fd, &status, kDebuggerdJavaBacktrace);
1069 ASSERT_EQ(InterceptStatus::kRegistered, status);
1070
1071 unique_fd intercept_fd_2, output_fd_2;
1072 tombstoned_intercept(fake_pid, &intercept_fd_2, &output_fd_2, &status, kDebuggerdNativeBacktrace);
1073 ASSERT_EQ(InterceptStatus::kFailedAlreadyRegistered, status);
1074}
1075
1076TEST(tombstoned, intercept_any) {
1077 const pid_t fake_pid = 1'234'567;
1078
1079 unique_fd intercept_fd, output_fd;
1080 InterceptStatus status;
1081 tombstoned_intercept(fake_pid, &intercept_fd, &output_fd, &status, kDebuggerdNativeBacktrace);
1082 ASSERT_EQ(InterceptStatus::kRegistered, status);
1083
1084 const char any[] = "any";
1085 unique_fd tombstoned_socket, input_fd;
1086 ASSERT_TRUE(tombstoned_connect(fake_pid, &tombstoned_socket, &input_fd, kDebuggerdAnyIntercept));
1087 ASSERT_TRUE(android::base::WriteFully(input_fd.get(), any, sizeof(any)));
1088 tombstoned_notify_completion(tombstoned_socket.get());
1089
1090 char outbuf[sizeof(any)];
1091 ASSERT_TRUE(android::base::ReadFully(output_fd.get(), outbuf, sizeof(outbuf)));
1092 ASSERT_STREQ("any", outbuf);
1093}
Josh Gao2b22ae12018-09-12 14:51:03 -07001094
1095TEST(tombstoned, interceptless_backtrace) {
1096 // Generate 50 backtraces, and then check to see that we haven't created 50 new tombstones.
1097 auto get_tombstone_timestamps = []() -> std::map<int, time_t> {
1098 std::map<int, time_t> result;
1099 for (int i = 0; i < 99; ++i) {
1100 std::string path = android::base::StringPrintf("/data/tombstones/tombstone_%02d", i);
1101 struct stat st;
1102 if (stat(path.c_str(), &st) == 0) {
1103 result[i] = st.st_mtim.tv_sec;
1104 }
1105 }
1106 return result;
1107 };
1108
1109 auto before = get_tombstone_timestamps();
1110 for (int i = 0; i < 50; ++i) {
1111 raise_debugger_signal(kDebuggerdNativeBacktrace);
1112 }
1113 auto after = get_tombstone_timestamps();
1114
1115 int diff = 0;
1116 for (int i = 0; i < 99; ++i) {
1117 if (after.count(i) == 0) {
1118 continue;
1119 }
1120 if (before.count(i) == 0) {
1121 ++diff;
1122 continue;
1123 }
1124 if (before[i] != after[i]) {
1125 ++diff;
1126 }
1127 }
1128
1129 // We can't be sure that nothing's crash looping in the background.
1130 // This should be good enough, though...
1131 ASSERT_LT(diff, 10) << "too many new tombstones; is something crashing in the background?";
1132}
Christopher Ferris481e8372019-07-15 17:13:24 -07001133
1134static __attribute__((__noinline__)) void overflow_stack(void* p) {
1135 void* buf[1];
1136 buf[0] = p;
1137 static volatile void* global = buf;
1138 if (global) {
1139 global = buf;
1140 overflow_stack(&buf);
1141 }
1142}
1143
1144TEST_F(CrasherTest, stack_overflow) {
1145 int intercept_result;
1146 unique_fd output_fd;
1147 StartProcess([]() { overflow_stack(nullptr); });
1148
1149 StartIntercept(&output_fd);
1150 FinishCrasher();
1151 AssertDeath(SIGSEGV);
1152 FinishIntercept(&intercept_result);
1153
1154 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
1155
1156 std::string result;
1157 ConsumeFd(std::move(output_fd), &result);
1158 ASSERT_MATCH(result, R"(Cause: stack pointer[^\n]*stack overflow.\n)");
1159}