blob: ab95768de67b028c4344774d7bcdfa5c0fdca509 [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
Christopher Ferris35da2882021-02-17 15:39:06 -080017#include <dirent.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070018#include <err.h>
19#include <fcntl.h>
Elliott Hughes03b283a2021-01-15 11:34:26 -080020#include <malloc.h>
Josh Gaocdea7502017-11-01 15:00:40 -070021#include <stdlib.h>
Josh Gao502cfd22017-02-17 01:39:15 -080022#include <sys/capability.h>
Peter Collingbournefe8997a2020-07-20 15:08:52 -070023#include <sys/mman.h>
Josh Gaofca7ca32017-01-23 12:05:35 -080024#include <sys/prctl.h>
Josh Gaofd13bf02017-08-18 15:37:26 -070025#include <sys/ptrace.h>
Josh Gao70adac62018-02-22 11:38:33 -080026#include <sys/resource.h>
Dan Albertc38057a2017-10-11 11:35:40 -070027#include <sys/syscall.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070028#include <sys/types.h>
Josh Gaofd13bf02017-08-18 15:37:26 -070029#include <unistd.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070030
31#include <chrono>
32#include <regex>
33#include <thread>
34
Josh Gaobf06a402018-08-27 16:34:01 -070035#include <android/fdsan.h>
Josh Gao502cfd22017-02-17 01:39:15 -080036#include <android/set_abort_message.h>
Peter Collingbournef8622522020-04-07 14:07:32 -070037#include <bionic/mte.h>
Josh Gaoa48b41b2019-12-13 14:11:04 -080038#include <bionic/reserved_signals.h>
Josh Gao502cfd22017-02-17 01:39:15 -080039
Josh Gao5f87bbd2019-01-09 17:01:49 -080040#include <android-base/cmsg.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070041#include <android-base/file.h>
42#include <android-base/logging.h>
Josh Gao2e7b8e22017-05-04 17:12:57 -070043#include <android-base/macros.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070044#include <android-base/parseint.h>
45#include <android-base/properties.h>
Josh Gao2b22ae12018-09-12 14:51:03 -070046#include <android-base/stringprintf.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070047#include <android-base/strings.h>
Josh Gao30171a82017-04-27 19:48:44 -070048#include <android-base/test_utils.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070049#include <android-base/unique_fd.h>
50#include <cutils/sockets.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070051#include <gtest/gtest.h>
52
Josh Gaoe04ca272018-01-16 15:38:17 -080053#include <libminijail.h>
54#include <scoped_minijail.h>
55
Narayan Kamath2d377cd2017-05-10 10:58:59 +010056#include "debuggerd/handler.h"
57#include "protocol.h"
58#include "tombstoned/tombstoned.h"
59#include "util.h"
60
Josh Gaocbe70cb2016-10-18 18:17:52 -070061using namespace std::chrono_literals;
Josh Gao5f87bbd2019-01-09 17:01:49 -080062
63using android::base::SendFileDescriptors;
Josh Gaocbe70cb2016-10-18 18:17:52 -070064using android::base::unique_fd;
65
66#if defined(__LP64__)
Josh Gaocbe70cb2016-10-18 18:17:52 -070067#define ARCH_SUFFIX "64"
68#else
Josh Gaocbe70cb2016-10-18 18:17:52 -070069#define ARCH_SUFFIX ""
70#endif
71
72constexpr char kWaitForGdbKey[] = "debug.debuggerd.wait_for_gdb";
73
74#define TIMEOUT(seconds, expr) \
75 [&]() { \
76 struct sigaction old_sigaction; \
77 struct sigaction new_sigaction = {}; \
78 new_sigaction.sa_handler = [](int) {}; \
79 if (sigaction(SIGALRM, &new_sigaction, &new_sigaction) != 0) { \
80 err(1, "sigaction failed"); \
81 } \
82 alarm(seconds); \
83 auto value = expr; \
84 int saved_errno = errno; \
85 if (sigaction(SIGALRM, &old_sigaction, nullptr) != 0) { \
86 err(1, "sigaction failed"); \
87 } \
88 alarm(0); \
89 errno = saved_errno; \
90 return value; \
91 }()
92
Chih-Hung Hsieh3249b3a2018-05-11 16:01:21 -070093// Backtrace frame dump could contain:
94// #01 pc 0001cded /data/tmp/debuggerd_test32 (raise_debugger_signal+80)
95// or
96// #01 pc 00022a09 /data/tmp/debuggerd_test32 (offset 0x12000) (raise_debugger_signal+80)
Josh Gaoe04ca272018-01-16 15:38:17 -080097#define ASSERT_BACKTRACE_FRAME(result, frame_name) \
Chih-Hung Hsieh3249b3a2018-05-11 16:01:21 -070098 ASSERT_MATCH(result, \
99 R"(#\d\d pc [0-9a-f]+\s+ \S+ (\(offset 0x[0-9a-f]+\) )?\()" frame_name R"(\+)");
Jaesung Chung58778e12017-06-15 18:20:34 +0900100
Narayan Kamatha73df602017-05-24 15:07:25 +0100101static void tombstoned_intercept(pid_t target_pid, unique_fd* intercept_fd, unique_fd* output_fd,
Narayan Kamathca5e9082017-06-02 15:42:06 +0100102 InterceptStatus* status, DebuggerdDumpType intercept_type) {
Josh Gao460b3362017-03-30 16:40:47 -0700103 intercept_fd->reset(socket_local_client(kTombstonedInterceptSocketName,
104 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_SEQPACKET));
105 if (intercept_fd->get() == -1) {
106 FAIL() << "failed to contact tombstoned: " << strerror(errno);
107 }
108
Nick Desaulniers67d52aa2019-10-07 23:28:15 -0700109 InterceptRequest req = {
110 .dump_type = intercept_type,
111 .pid = target_pid,
112 };
Josh Gao460b3362017-03-30 16:40:47 -0700113
114 unique_fd output_pipe_write;
115 if (!Pipe(output_fd, &output_pipe_write)) {
116 FAIL() << "failed to create output pipe: " << strerror(errno);
117 }
118
119 std::string pipe_size_str;
120 int pipe_buffer_size;
121 if (!android::base::ReadFileToString("/proc/sys/fs/pipe-max-size", &pipe_size_str)) {
122 FAIL() << "failed to read /proc/sys/fs/pipe-max-size: " << strerror(errno);
123 }
124
125 pipe_size_str = android::base::Trim(pipe_size_str);
126
127 if (!android::base::ParseInt(pipe_size_str.c_str(), &pipe_buffer_size, 0)) {
128 FAIL() << "failed to parse pipe max size";
129 }
130
131 if (fcntl(output_fd->get(), F_SETPIPE_SZ, pipe_buffer_size) != pipe_buffer_size) {
132 FAIL() << "failed to set pipe size: " << strerror(errno);
133 }
134
Josh Gao5675f3c2017-06-01 12:19:53 -0700135 ASSERT_GE(pipe_buffer_size, 1024 * 1024);
136
Josh Gao5f87bbd2019-01-09 17:01:49 -0800137 ssize_t rc = SendFileDescriptors(intercept_fd->get(), &req, sizeof(req), output_pipe_write.get());
138 output_pipe_write.reset();
139 if (rc != sizeof(req)) {
Josh Gao460b3362017-03-30 16:40:47 -0700140 FAIL() << "failed to send output fd to tombstoned: " << strerror(errno);
141 }
142
143 InterceptResponse response;
Josh Gao5f87bbd2019-01-09 17:01:49 -0800144 rc = TEMP_FAILURE_RETRY(read(intercept_fd->get(), &response, sizeof(response)));
Josh Gao460b3362017-03-30 16:40:47 -0700145 if (rc == -1) {
146 FAIL() << "failed to read response from tombstoned: " << strerror(errno);
147 } else if (rc == 0) {
148 FAIL() << "failed to read response from tombstoned (EOF)";
149 } else if (rc != sizeof(response)) {
150 FAIL() << "received packet of unexpected length from tombstoned: expected " << sizeof(response)
151 << ", received " << rc;
152 }
153
Narayan Kamathca5e9082017-06-02 15:42:06 +0100154 *status = response.status;
Josh Gao460b3362017-03-30 16:40:47 -0700155}
156
Josh Gaocbe70cb2016-10-18 18:17:52 -0700157class CrasherTest : public ::testing::Test {
158 public:
159 pid_t crasher_pid = -1;
160 bool previous_wait_for_gdb;
161 unique_fd crasher_pipe;
162 unique_fd intercept_fd;
163
164 CrasherTest();
165 ~CrasherTest();
166
Narayan Kamatha73df602017-05-24 15:07:25 +0100167 void StartIntercept(unique_fd* output_fd, DebuggerdDumpType intercept_type = kDebuggerdTombstone);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700168
169 // Returns -1 if we fail to read a response from tombstoned, otherwise the received return code.
170 void FinishIntercept(int* result);
171
Josh Gao2e7b8e22017-05-04 17:12:57 -0700172 void StartProcess(std::function<void()> function, std::function<pid_t()> forker = fork);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700173 void StartCrasher(const std::string& crash_type);
174 void FinishCrasher();
175 void AssertDeath(int signo);
Peter Collingbourne10e428d2020-07-17 14:49:31 -0700176
177 static void Trap(void* ptr);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700178};
179
180CrasherTest::CrasherTest() {
181 previous_wait_for_gdb = android::base::GetBoolProperty(kWaitForGdbKey, false);
182 android::base::SetProperty(kWaitForGdbKey, "0");
183}
184
185CrasherTest::~CrasherTest() {
186 if (crasher_pid != -1) {
187 kill(crasher_pid, SIGKILL);
188 int status;
Christopher Ferris172b0a02019-09-18 17:48:30 -0700189 TEMP_FAILURE_RETRY(waitpid(crasher_pid, &status, WUNTRACED));
Josh Gaocbe70cb2016-10-18 18:17:52 -0700190 }
191
192 android::base::SetProperty(kWaitForGdbKey, previous_wait_for_gdb ? "1" : "0");
193}
194
Narayan Kamatha73df602017-05-24 15:07:25 +0100195void CrasherTest::StartIntercept(unique_fd* output_fd, DebuggerdDumpType intercept_type) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700196 if (crasher_pid == -1) {
197 FAIL() << "crasher hasn't been started";
198 }
199
Narayan Kamathca5e9082017-06-02 15:42:06 +0100200 InterceptStatus status;
201 tombstoned_intercept(crasher_pid, &this->intercept_fd, output_fd, &status, intercept_type);
202 ASSERT_EQ(InterceptStatus::kRegistered, status);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700203}
204
205void CrasherTest::FinishIntercept(int* result) {
206 InterceptResponse response;
207
Christopher Ferris11555f02019-09-20 14:18:55 -0700208 ssize_t rc = TIMEOUT(30, read(intercept_fd.get(), &response, sizeof(response)));
Josh Gaocbe70cb2016-10-18 18:17:52 -0700209 if (rc == -1) {
210 FAIL() << "failed to read response from tombstoned: " << strerror(errno);
211 } else if (rc == 0) {
212 *result = -1;
213 } else if (rc != sizeof(response)) {
214 FAIL() << "received packet of unexpected length from tombstoned: expected " << sizeof(response)
215 << ", received " << rc;
216 } else {
Josh Gao460b3362017-03-30 16:40:47 -0700217 *result = response.status == InterceptStatus::kStarted ? 1 : 0;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700218 }
219}
220
Josh Gao2e7b8e22017-05-04 17:12:57 -0700221void CrasherTest::StartProcess(std::function<void()> function, std::function<pid_t()> forker) {
Josh Gaofca7ca32017-01-23 12:05:35 -0800222 unique_fd read_pipe;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700223 unique_fd crasher_read_pipe;
224 if (!Pipe(&crasher_read_pipe, &crasher_pipe)) {
225 FAIL() << "failed to create pipe: " << strerror(errno);
226 }
227
Josh Gao2e7b8e22017-05-04 17:12:57 -0700228 crasher_pid = forker();
Josh Gaocbe70cb2016-10-18 18:17:52 -0700229 if (crasher_pid == -1) {
230 FAIL() << "fork failed: " << strerror(errno);
231 } else if (crasher_pid == 0) {
Josh Gao502cfd22017-02-17 01:39:15 -0800232 char dummy;
233 crasher_pipe.reset();
234 TEMP_FAILURE_RETRY(read(crasher_read_pipe.get(), &dummy, 1));
Josh Gaofca7ca32017-01-23 12:05:35 -0800235 function();
236 _exit(0);
237 }
238}
239
Josh Gaocbe70cb2016-10-18 18:17:52 -0700240void CrasherTest::FinishCrasher() {
241 if (crasher_pipe == -1) {
242 FAIL() << "crasher pipe uninitialized";
243 }
244
Christopher Ferris172b0a02019-09-18 17:48:30 -0700245 ssize_t rc = TEMP_FAILURE_RETRY(write(crasher_pipe.get(), "\n", 1));
Josh Gaocbe70cb2016-10-18 18:17:52 -0700246 if (rc == -1) {
247 FAIL() << "failed to write to crasher pipe: " << strerror(errno);
248 } else if (rc == 0) {
249 FAIL() << "crasher pipe was closed";
250 }
251}
252
253void CrasherTest::AssertDeath(int signo) {
254 int status;
Christopher Ferris11555f02019-09-20 14:18:55 -0700255 pid_t pid = TIMEOUT(30, waitpid(crasher_pid, &status, 0));
Josh Gaocbe70cb2016-10-18 18:17:52 -0700256 if (pid != crasher_pid) {
Christopher Ferrisafc0ff72019-06-26 15:08:51 -0700257 printf("failed to wait for crasher (expected pid %d, return value %d): %s\n", crasher_pid, pid,
258 strerror(errno));
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700259 sleep(100);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700260 FAIL() << "failed to wait for crasher: " << strerror(errno);
261 }
262
Josh Gaoe06f2a42017-04-27 16:50:38 -0700263 if (signo == 0) {
264 ASSERT_TRUE(WIFEXITED(status));
265 ASSERT_EQ(0, WEXITSTATUS(signo));
266 } else {
267 ASSERT_FALSE(WIFEXITED(status));
268 ASSERT_TRUE(WIFSIGNALED(status)) << "crasher didn't terminate via a signal";
269 ASSERT_EQ(signo, WTERMSIG(status));
Josh Gaocbe70cb2016-10-18 18:17:52 -0700270 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700271 crasher_pid = -1;
272}
273
274static void ConsumeFd(unique_fd fd, std::string* output) {
275 constexpr size_t read_length = PAGE_SIZE;
276 std::string result;
277
278 while (true) {
279 size_t offset = result.size();
280 result.resize(result.size() + PAGE_SIZE);
281 ssize_t rc = TEMP_FAILURE_RETRY(read(fd.get(), &result[offset], read_length));
282 if (rc == -1) {
283 FAIL() << "read failed: " << strerror(errno);
284 } else if (rc == 0) {
285 result.resize(result.size() - PAGE_SIZE);
286 break;
287 }
288
289 result.resize(result.size() - PAGE_SIZE + rc);
290 }
291
292 *output = std::move(result);
293}
294
295TEST_F(CrasherTest, smoke) {
296 int intercept_result;
297 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800298 StartProcess([]() {
299 *reinterpret_cast<volatile char*>(0xdead) = '1';
300 });
301
Josh Gaocbe70cb2016-10-18 18:17:52 -0700302 StartIntercept(&output_fd);
303 FinishCrasher();
304 AssertDeath(SIGSEGV);
305 FinishIntercept(&intercept_result);
306
307 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
308
309 std::string result;
310 ConsumeFd(std::move(output_fd), &result);
311 ASSERT_MATCH(result, R"(signal 11 \(SIGSEGV\), code 1 \(SEGV_MAPERR\), fault addr 0xdead)");
Peter Collingbourne864f15d2020-09-14 20:27:36 -0700312
313 if (mte_supported()) {
314 // Test that the default TAGGED_ADDR_CTRL value is set.
Peter Collingbourne939d0742021-02-02 19:00:45 -0800315 ASSERT_MATCH(result, R"(tagged_addr_ctrl: 000000000007fff3)");
Peter Collingbourne864f15d2020-09-14 20:27:36 -0700316 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700317}
318
Peter Collingbournef03af882020-03-20 18:09:00 -0700319TEST_F(CrasherTest, tagged_fault_addr) {
320#if !defined(__aarch64__)
321 GTEST_SKIP() << "Requires aarch64";
322#endif
323 int intercept_result;
324 unique_fd output_fd;
325 StartProcess([]() {
326 *reinterpret_cast<volatile char*>(0x100000000000dead) = '1';
327 });
328
329 StartIntercept(&output_fd);
330 FinishCrasher();
331 AssertDeath(SIGSEGV);
332 FinishIntercept(&intercept_result);
333
334 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
335
336 std::string result;
337 ConsumeFd(std::move(output_fd), &result);
338
339 // The address can either be tagged (new kernels) or untagged (old kernels).
340 ASSERT_MATCH(
341 result,
342 R"(signal 11 \(SIGSEGV\), code 1 \(SEGV_MAPERR\), fault addr (0x100000000000dead|0xdead))");
343}
344
Peter Collingbourne10e428d2020-07-17 14:49:31 -0700345// Marked as weak to prevent the compiler from removing the malloc in the caller. In theory, the
346// compiler could still clobber the argument register before trapping, but that's unlikely.
347__attribute__((weak)) void CrasherTest::Trap(void* ptr ATTRIBUTE_UNUSED) {
348 __builtin_trap();
349}
350
351TEST_F(CrasherTest, heap_addr_in_register) {
352#if defined(__i386__)
353 GTEST_SKIP() << "architecture does not pass arguments in registers";
354#endif
355 int intercept_result;
356 unique_fd output_fd;
357 StartProcess([]() {
358 // Crash with a heap pointer in the first argument register.
359 Trap(malloc(1));
360 });
361
362 StartIntercept(&output_fd);
363 FinishCrasher();
364 int status;
365 ASSERT_EQ(crasher_pid, TIMEOUT(30, waitpid(crasher_pid, &status, 0)));
366 ASSERT_TRUE(WIFSIGNALED(status)) << "crasher didn't terminate via a signal";
367 // Don't test the signal number because different architectures use different signals for
368 // __builtin_trap().
369 FinishIntercept(&intercept_result);
370
371 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
372
373 std::string result;
374 ConsumeFd(std::move(output_fd), &result);
375
376#if defined(__aarch64__)
Peter Collingbourne0ea08c22021-02-05 14:59:08 -0800377 ASSERT_MATCH(result, "memory near x0 \\(\\[anon:");
Peter Collingbourne10e428d2020-07-17 14:49:31 -0700378#elif defined(__arm__)
Peter Collingbourne0ea08c22021-02-05 14:59:08 -0800379 ASSERT_MATCH(result, "memory near r0 \\(\\[anon:");
Peter Collingbourne10e428d2020-07-17 14:49:31 -0700380#elif defined(__x86_64__)
Peter Collingbourne0ea08c22021-02-05 14:59:08 -0800381 ASSERT_MATCH(result, "memory near rdi \\(\\[anon:");
Peter Collingbourne10e428d2020-07-17 14:49:31 -0700382#else
383 ASSERT_TRUE(false) << "unsupported architecture";
384#endif
385}
386
Peter Collingbournecd278072020-12-21 14:08:38 -0800387#if defined(__aarch64__)
Peter Collingbournef8622522020-04-07 14:07:32 -0700388static void SetTagCheckingLevelSync() {
Elliott Hughes03b283a2021-01-15 11:34:26 -0800389 if (mallopt(M_BIONIC_SET_HEAP_TAGGING_LEVEL, M_HEAP_TAGGING_LEVEL_SYNC) == 0) {
Peter Collingbournef8622522020-04-07 14:07:32 -0700390 abort();
391 }
392}
393#endif
394
Peter Collingbournebb4b49c2021-01-06 21:02:19 -0800395struct SizeParamCrasherTest : CrasherTest, testing::WithParamInterface<size_t> {};
396
397INSTANTIATE_TEST_SUITE_P(Sizes, SizeParamCrasherTest, testing::Values(16, 131072));
398
399TEST_P(SizeParamCrasherTest, mte_uaf) {
Peter Collingbournecd278072020-12-21 14:08:38 -0800400#if defined(__aarch64__)
Peter Collingbournef8622522020-04-07 14:07:32 -0700401 if (!mte_supported()) {
402 GTEST_SKIP() << "Requires MTE";
403 }
404
405 int intercept_result;
406 unique_fd output_fd;
Peter Collingbournebb4b49c2021-01-06 21:02:19 -0800407 StartProcess([&]() {
Peter Collingbournef8622522020-04-07 14:07:32 -0700408 SetTagCheckingLevelSync();
Peter Collingbournebb4b49c2021-01-06 21:02:19 -0800409 volatile int* p = (volatile int*)malloc(GetParam());
Peter Collingbournef8622522020-04-07 14:07:32 -0700410 free((void *)p);
411 p[0] = 42;
412 });
413
414 StartIntercept(&output_fd);
415 FinishCrasher();
416 AssertDeath(SIGSEGV);
417 FinishIntercept(&intercept_result);
418
419 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
420
421 std::string result;
422 ConsumeFd(std::move(output_fd), &result);
423
Peter Collingbournebb4b49c2021-01-06 21:02:19 -0800424 ASSERT_MATCH(result, R"(signal 11 \(SIGSEGV\))");
425 ASSERT_MATCH(result, R"(Cause: \[MTE\]: Use After Free, 0 bytes into a )" +
426 std::to_string(GetParam()) + R"(-byte allocation.*
Peter Collingbournebbe69052020-05-08 10:11:19 -0700427
428allocated by thread .*
429 #00 pc)");
430 ASSERT_MATCH(result, R"(deallocated by thread .*
431 #00 pc)");
Peter Collingbournef8622522020-04-07 14:07:32 -0700432#else
Peter Collingbournecd278072020-12-21 14:08:38 -0800433 GTEST_SKIP() << "Requires aarch64";
Peter Collingbournef8622522020-04-07 14:07:32 -0700434#endif
435}
436
Peter Collingbournebb4b49c2021-01-06 21:02:19 -0800437TEST_P(SizeParamCrasherTest, mte_overflow) {
Peter Collingbournecd278072020-12-21 14:08:38 -0800438#if defined(__aarch64__)
Peter Collingbournef8622522020-04-07 14:07:32 -0700439 if (!mte_supported()) {
440 GTEST_SKIP() << "Requires MTE";
441 }
442
443 int intercept_result;
444 unique_fd output_fd;
Peter Collingbournebb4b49c2021-01-06 21:02:19 -0800445 StartProcess([&]() {
Peter Collingbournef8622522020-04-07 14:07:32 -0700446 SetTagCheckingLevelSync();
Peter Collingbournebb4b49c2021-01-06 21:02:19 -0800447 volatile char* p = (volatile char*)malloc(GetParam());
448 p[GetParam()] = 42;
Peter Collingbournef8622522020-04-07 14:07:32 -0700449 });
450
451 StartIntercept(&output_fd);
452 FinishCrasher();
453 AssertDeath(SIGSEGV);
454 FinishIntercept(&intercept_result);
455
456 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
457
458 std::string result;
459 ConsumeFd(std::move(output_fd), &result);
460
461 ASSERT_MATCH(result, R"(signal 11 \(SIGSEGV\))");
Peter Collingbournebb4b49c2021-01-06 21:02:19 -0800462 ASSERT_MATCH(result, R"(Cause: \[MTE\]: Buffer Overflow, 0 bytes right of a )" +
463 std::to_string(GetParam()) + R"(-byte allocation.*
Peter Collingbournebbe69052020-05-08 10:11:19 -0700464
465allocated by thread .*
466 #00 pc)");
Peter Collingbournef8622522020-04-07 14:07:32 -0700467#else
Peter Collingbournecd278072020-12-21 14:08:38 -0800468 GTEST_SKIP() << "Requires aarch64";
Peter Collingbournef8622522020-04-07 14:07:32 -0700469#endif
470}
471
Peter Collingbournebb4b49c2021-01-06 21:02:19 -0800472TEST_P(SizeParamCrasherTest, mte_underflow) {
Peter Collingbournecd278072020-12-21 14:08:38 -0800473#if defined(__aarch64__)
Peter Collingbournef8622522020-04-07 14:07:32 -0700474 if (!mte_supported()) {
475 GTEST_SKIP() << "Requires MTE";
476 }
477
478 int intercept_result;
479 unique_fd output_fd;
Peter Collingbournebb4b49c2021-01-06 21:02:19 -0800480 StartProcess([&]() {
Peter Collingbournef8622522020-04-07 14:07:32 -0700481 SetTagCheckingLevelSync();
Peter Collingbournebb4b49c2021-01-06 21:02:19 -0800482 volatile int* p = (volatile int*)malloc(GetParam());
Peter Collingbournef8622522020-04-07 14:07:32 -0700483 p[-1] = 42;
484 });
485
486 StartIntercept(&output_fd);
487 FinishCrasher();
488 AssertDeath(SIGSEGV);
489 FinishIntercept(&intercept_result);
490
491 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
492
493 std::string result;
494 ConsumeFd(std::move(output_fd), &result);
495
496 ASSERT_MATCH(result, R"(signal 11 \(SIGSEGV\), code 9 \(SEGV_MTESERR\))");
Peter Collingbournebb4b49c2021-01-06 21:02:19 -0800497 ASSERT_MATCH(result, R"(Cause: \[MTE\]: Buffer Underflow, 4 bytes left of a )" +
498 std::to_string(GetParam()) + R"(-byte allocation.*
Peter Collingbournebbe69052020-05-08 10:11:19 -0700499
500allocated by thread .*
501 #00 pc)");
Peter Collingbournef8622522020-04-07 14:07:32 -0700502#else
Peter Collingbournecd278072020-12-21 14:08:38 -0800503 GTEST_SKIP() << "Requires aarch64";
Peter Collingbournef8622522020-04-07 14:07:32 -0700504#endif
505}
506
507TEST_F(CrasherTest, mte_multiple_causes) {
Peter Collingbournecd278072020-12-21 14:08:38 -0800508#if defined(__aarch64__)
Peter Collingbournef8622522020-04-07 14:07:32 -0700509 if (!mte_supported()) {
510 GTEST_SKIP() << "Requires MTE";
511 }
512
513 int intercept_result;
514 unique_fd output_fd;
515 StartProcess([]() {
516 SetTagCheckingLevelSync();
517
518 // Make two allocations with the same tag and close to one another. Check for both properties
519 // with a bounds check -- this relies on the fact that only if the allocations have the same tag
520 // would they be measured as closer than 128 bytes to each other. Otherwise they would be about
521 // (some non-zero value << 56) apart.
522 //
523 // The out-of-bounds access will be considered either an overflow of one or an underflow of the
524 // other.
525 std::set<uintptr_t> allocs;
526 for (int i = 0; i != 4096; ++i) {
527 uintptr_t alloc = reinterpret_cast<uintptr_t>(malloc(16));
528 auto it = allocs.insert(alloc).first;
529 if (it != allocs.begin() && *std::prev(it) + 128 > alloc) {
530 *reinterpret_cast<int*>(*std::prev(it) + 16) = 42;
531 }
532 if (std::next(it) != allocs.end() && alloc + 128 > *std::next(it)) {
533 *reinterpret_cast<int*>(alloc + 16) = 42;
534 }
535 }
536 });
537
538 StartIntercept(&output_fd);
539 FinishCrasher();
540 AssertDeath(SIGSEGV);
541 FinishIntercept(&intercept_result);
542
543 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
544
545 std::string result;
546 ConsumeFd(std::move(output_fd), &result);
547
548 ASSERT_MATCH(result, R"(signal 11 \(SIGSEGV\))");
549 ASSERT_MATCH(
550 result,
551 R"(Note: multiple potential causes for this crash were detected, listing them in decreasing order of probability.)");
552
553 // Adjacent untracked allocations may cause us to see the wrong underflow here (or only
554 // overflows), so we can't match explicitly for an underflow message.
555 ASSERT_MATCH(result, R"(Cause: \[MTE\]: Buffer Overflow, 0 bytes right of a 16-byte allocation)");
556#else
Peter Collingbournecd278072020-12-21 14:08:38 -0800557 GTEST_SKIP() << "Requires aarch64";
Peter Collingbournef8622522020-04-07 14:07:32 -0700558#endif
559}
560
Peter Collingbournecd278072020-12-21 14:08:38 -0800561#if defined(__aarch64__)
Peter Collingbournefe8997a2020-07-20 15:08:52 -0700562static uintptr_t CreateTagMapping() {
563 uintptr_t mapping =
564 reinterpret_cast<uintptr_t>(mmap(nullptr, getpagesize(), PROT_READ | PROT_WRITE | PROT_MTE,
565 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0));
566 if (reinterpret_cast<void*>(mapping) == MAP_FAILED) {
567 return 0;
568 }
569 __asm__ __volatile__(".arch_extension mte; stg %0, [%0]"
570 :
571 : "r"(mapping + (1ULL << 56))
572 : "memory");
573 return mapping;
574}
575#endif
576
577TEST_F(CrasherTest, mte_tag_dump) {
Peter Collingbournecd278072020-12-21 14:08:38 -0800578#if defined(__aarch64__)
Peter Collingbournefe8997a2020-07-20 15:08:52 -0700579 if (!mte_supported()) {
580 GTEST_SKIP() << "Requires MTE";
581 }
582
583 int intercept_result;
584 unique_fd output_fd;
585 StartProcess([&]() {
586 SetTagCheckingLevelSync();
587 Trap(reinterpret_cast<void *>(CreateTagMapping()));
588 });
589
590 StartIntercept(&output_fd);
591 FinishCrasher();
592 AssertDeath(SIGTRAP);
593 FinishIntercept(&intercept_result);
594
595 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
596
597 std::string result;
598 ConsumeFd(std::move(output_fd), &result);
599
600 ASSERT_MATCH(result, R"(memory near x0:
601.*
602.*
603 01.............0 0000000000000000 0000000000000000 ................
604 00.............0)");
605#else
Peter Collingbournecd278072020-12-21 14:08:38 -0800606 GTEST_SKIP() << "Requires aarch64";
Peter Collingbournefe8997a2020-07-20 15:08:52 -0700607#endif
608}
609
Josh Gaocdea7502017-11-01 15:00:40 -0700610TEST_F(CrasherTest, LD_PRELOAD) {
611 int intercept_result;
612 unique_fd output_fd;
613 StartProcess([]() {
614 setenv("LD_PRELOAD", "nonexistent.so", 1);
615 *reinterpret_cast<volatile char*>(0xdead) = '1';
616 });
617
618 StartIntercept(&output_fd);
619 FinishCrasher();
620 AssertDeath(SIGSEGV);
621 FinishIntercept(&intercept_result);
622
623 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
624
625 std::string result;
626 ConsumeFd(std::move(output_fd), &result);
627 ASSERT_MATCH(result, R"(signal 11 \(SIGSEGV\), code 1 \(SEGV_MAPERR\), fault addr 0xdead)");
628}
629
Josh Gaocbe70cb2016-10-18 18:17:52 -0700630TEST_F(CrasherTest, abort) {
631 int intercept_result;
632 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800633 StartProcess([]() {
634 abort();
635 });
Josh Gaocbe70cb2016-10-18 18:17:52 -0700636 StartIntercept(&output_fd);
637 FinishCrasher();
638 AssertDeath(SIGABRT);
639 FinishIntercept(&intercept_result);
640
641 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
642
643 std::string result;
644 ConsumeFd(std::move(output_fd), &result);
Andreas Gampe26cbafb2017-06-22 20:14:43 -0700645 ASSERT_BACKTRACE_FRAME(result, "abort");
Josh Gaocbe70cb2016-10-18 18:17:52 -0700646}
647
648TEST_F(CrasherTest, signal) {
649 int intercept_result;
650 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800651 StartProcess([]() {
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700652 while (true) {
653 sleep(1);
654 }
Josh Gao502cfd22017-02-17 01:39:15 -0800655 });
Josh Gaocbe70cb2016-10-18 18:17:52 -0700656 StartIntercept(&output_fd);
Josh Gao2b2ae0c2017-08-21 14:31:17 -0700657 FinishCrasher();
Josh Gaocbe70cb2016-10-18 18:17:52 -0700658 ASSERT_EQ(0, kill(crasher_pid, SIGSEGV));
659
660 AssertDeath(SIGSEGV);
661 FinishIntercept(&intercept_result);
662
663 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
664
665 std::string result;
666 ConsumeFd(std::move(output_fd), &result);
Elliott Hughes89722702018-05-02 10:47:00 -0700667 ASSERT_MATCH(
668 result,
669 R"(signal 11 \(SIGSEGV\), code 0 \(SI_USER from pid \d+, uid \d+\), fault addr --------)");
Josh Gaocbe70cb2016-10-18 18:17:52 -0700670 ASSERT_MATCH(result, R"(backtrace:)");
671}
672
673TEST_F(CrasherTest, abort_message) {
674 int intercept_result;
675 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800676 StartProcess([]() {
Josh Gao1cc7bd82018-02-13 13:16:17 -0800677 // Arrived at experimentally;
678 // logd truncates at 4062.
679 // strlen("Abort message: ''") is 17.
680 // That's 4045, but we also want a NUL.
681 char buf[4045 + 1];
682 memset(buf, 'x', sizeof(buf));
683 buf[sizeof(buf) - 1] = '\0';
684 android_set_abort_message(buf);
Josh Gao502cfd22017-02-17 01:39:15 -0800685 abort();
686 });
Josh Gaocbe70cb2016-10-18 18:17:52 -0700687 StartIntercept(&output_fd);
688 FinishCrasher();
689 AssertDeath(SIGABRT);
690 FinishIntercept(&intercept_result);
691
692 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
693
694 std::string result;
695 ConsumeFd(std::move(output_fd), &result);
Josh Gao1cc7bd82018-02-13 13:16:17 -0800696 ASSERT_MATCH(result, R"(Abort message: 'x{4045}')");
Josh Gaocbe70cb2016-10-18 18:17:52 -0700697}
698
Josh Gaoe06f2a42017-04-27 16:50:38 -0700699TEST_F(CrasherTest, abort_message_backtrace) {
700 int intercept_result;
701 unique_fd output_fd;
702 StartProcess([]() {
703 android_set_abort_message("not actually aborting");
Josh Gaoa48b41b2019-12-13 14:11:04 -0800704 raise(BIONIC_SIGNAL_DEBUGGER);
Josh Gaoe06f2a42017-04-27 16:50:38 -0700705 exit(0);
706 });
707 StartIntercept(&output_fd);
708 FinishCrasher();
709 AssertDeath(0);
710 FinishIntercept(&intercept_result);
711
712 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
713
714 std::string result;
715 ConsumeFd(std::move(output_fd), &result);
716 ASSERT_NOT_MATCH(result, R"(Abort message:)");
717}
718
Josh Gaocbe70cb2016-10-18 18:17:52 -0700719TEST_F(CrasherTest, intercept_timeout) {
720 int intercept_result;
721 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800722 StartProcess([]() {
723 abort();
724 });
Josh Gaocbe70cb2016-10-18 18:17:52 -0700725 StartIntercept(&output_fd);
726
727 // Don't let crasher finish until we timeout.
728 FinishIntercept(&intercept_result);
729
730 ASSERT_NE(1, intercept_result) << "tombstoned reported success? (intercept_result = "
731 << intercept_result << ")";
732
733 FinishCrasher();
734 AssertDeath(SIGABRT);
735}
736
737TEST_F(CrasherTest, wait_for_gdb) {
738 if (!android::base::SetProperty(kWaitForGdbKey, "1")) {
739 FAIL() << "failed to enable wait_for_gdb";
740 }
741 sleep(1);
742
Josh Gao502cfd22017-02-17 01:39:15 -0800743 StartProcess([]() {
744 abort();
745 });
Josh Gaocbe70cb2016-10-18 18:17:52 -0700746 FinishCrasher();
747
748 int status;
Christopher Ferris172b0a02019-09-18 17:48:30 -0700749 ASSERT_EQ(crasher_pid, TEMP_FAILURE_RETRY(waitpid(crasher_pid, &status, WUNTRACED)));
Josh Gaocbe70cb2016-10-18 18:17:52 -0700750 ASSERT_TRUE(WIFSTOPPED(status));
751 ASSERT_EQ(SIGSTOP, WSTOPSIG(status));
752
753 ASSERT_EQ(0, kill(crasher_pid, SIGCONT));
754
755 AssertDeath(SIGABRT);
756}
757
Josh Gaocbe70cb2016-10-18 18:17:52 -0700758TEST_F(CrasherTest, backtrace) {
759 std::string result;
760 int intercept_result;
761 unique_fd output_fd;
Josh Gao502cfd22017-02-17 01:39:15 -0800762
763 StartProcess([]() {
764 abort();
765 });
Narayan Kamatha73df602017-05-24 15:07:25 +0100766 StartIntercept(&output_fd, kDebuggerdNativeBacktrace);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700767
768 std::this_thread::sleep_for(500ms);
769
770 sigval val;
771 val.sival_int = 1;
Josh Gaoa48b41b2019-12-13 14:11:04 -0800772 ASSERT_EQ(0, sigqueue(crasher_pid, BIONIC_SIGNAL_DEBUGGER, val)) << strerror(errno);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700773 FinishIntercept(&intercept_result);
774 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
775 ConsumeFd(std::move(output_fd), &result);
Jaesung Chung58778e12017-06-15 18:20:34 +0900776 ASSERT_BACKTRACE_FRAME(result, "read");
Josh Gaocbe70cb2016-10-18 18:17:52 -0700777
778 int status;
779 ASSERT_EQ(0, waitpid(crasher_pid, &status, WNOHANG | WUNTRACED));
780
781 StartIntercept(&output_fd);
782 FinishCrasher();
783 AssertDeath(SIGABRT);
784 FinishIntercept(&intercept_result);
785 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
786 ConsumeFd(std::move(output_fd), &result);
Andreas Gampe26cbafb2017-06-22 20:14:43 -0700787 ASSERT_BACKTRACE_FRAME(result, "abort");
Josh Gaocbe70cb2016-10-18 18:17:52 -0700788}
Josh Gaofca7ca32017-01-23 12:05:35 -0800789
790TEST_F(CrasherTest, PR_SET_DUMPABLE_0_crash) {
Josh Gao502cfd22017-02-17 01:39:15 -0800791 int intercept_result;
792 unique_fd output_fd;
Josh Gaofca7ca32017-01-23 12:05:35 -0800793 StartProcess([]() {
794 prctl(PR_SET_DUMPABLE, 0);
Josh Gao502cfd22017-02-17 01:39:15 -0800795 abort();
Josh Gaofca7ca32017-01-23 12:05:35 -0800796 });
Josh Gao502cfd22017-02-17 01:39:15 -0800797
798 StartIntercept(&output_fd);
799 FinishCrasher();
800 AssertDeath(SIGABRT);
801 FinishIntercept(&intercept_result);
802
803 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
804
805 std::string result;
806 ConsumeFd(std::move(output_fd), &result);
Andreas Gampe26cbafb2017-06-22 20:14:43 -0700807 ASSERT_BACKTRACE_FRAME(result, "abort");
Josh Gaofca7ca32017-01-23 12:05:35 -0800808}
809
Josh Gao502cfd22017-02-17 01:39:15 -0800810TEST_F(CrasherTest, capabilities) {
811 ASSERT_EQ(0U, getuid()) << "capability test requires root";
812
Josh Gaofca7ca32017-01-23 12:05:35 -0800813 StartProcess([]() {
Josh Gao502cfd22017-02-17 01:39:15 -0800814 if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) != 0) {
815 err(1, "failed to set PR_SET_KEEPCAPS");
816 }
817
818 if (setresuid(1, 1, 1) != 0) {
819 err(1, "setresuid failed");
820 }
821
822 __user_cap_header_struct capheader;
823 __user_cap_data_struct capdata[2];
824 memset(&capheader, 0, sizeof(capheader));
825 memset(&capdata, 0, sizeof(capdata));
826
827 capheader.version = _LINUX_CAPABILITY_VERSION_3;
828 capheader.pid = 0;
829
830 // Turn on every third capability.
831 static_assert(CAP_LAST_CAP > 33, "CAP_LAST_CAP <= 32");
832 for (int i = 0; i < CAP_LAST_CAP; i += 3) {
833 capdata[CAP_TO_INDEX(i)].permitted |= CAP_TO_MASK(i);
834 capdata[CAP_TO_INDEX(i)].effective |= CAP_TO_MASK(i);
835 }
836
837 // Make sure CAP_SYS_PTRACE is off.
838 capdata[CAP_TO_INDEX(CAP_SYS_PTRACE)].permitted &= ~(CAP_TO_MASK(CAP_SYS_PTRACE));
839 capdata[CAP_TO_INDEX(CAP_SYS_PTRACE)].effective &= ~(CAP_TO_MASK(CAP_SYS_PTRACE));
840
841 if (capset(&capheader, &capdata[0]) != 0) {
842 err(1, "capset failed");
843 }
844
845 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) != 0) {
846 err(1, "failed to drop ambient capabilities");
847 }
848
Josh Gaoa5199a92017-04-03 13:18:34 -0700849 pthread_setname_np(pthread_self(), "thread_name");
Josh Gao502cfd22017-02-17 01:39:15 -0800850 raise(SIGSYS);
Josh Gaofca7ca32017-01-23 12:05:35 -0800851 });
Josh Gao502cfd22017-02-17 01:39:15 -0800852
853 unique_fd output_fd;
854 StartIntercept(&output_fd);
855 FinishCrasher();
856 AssertDeath(SIGSYS);
857
858 std::string result;
859 int intercept_result;
860 FinishIntercept(&intercept_result);
861 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
862 ConsumeFd(std::move(output_fd), &result);
Josh Gaoa5199a92017-04-03 13:18:34 -0700863 ASSERT_MATCH(result, R"(name: thread_name\s+>>> .+debuggerd_test(32|64) <<<)");
Jaesung Chung58778e12017-06-15 18:20:34 +0900864 ASSERT_BACKTRACE_FRAME(result, "tgkill");
Josh Gaofca7ca32017-01-23 12:05:35 -0800865}
Josh Gaoc3c8c022017-02-13 16:36:18 -0800866
Josh Gao2e7b8e22017-05-04 17:12:57 -0700867TEST_F(CrasherTest, fake_pid) {
868 int intercept_result;
869 unique_fd output_fd;
870
871 // Prime the getpid/gettid caches.
872 UNUSED(getpid());
873 UNUSED(gettid());
874
875 std::function<pid_t()> clone_fn = []() {
876 return syscall(__NR_clone, SIGCHLD, nullptr, nullptr, nullptr, nullptr);
877 };
878 StartProcess(
879 []() {
880 ASSERT_NE(getpid(), syscall(__NR_getpid));
881 ASSERT_NE(gettid(), syscall(__NR_gettid));
882 raise(SIGSEGV);
883 },
884 clone_fn);
885
886 StartIntercept(&output_fd);
887 FinishCrasher();
888 AssertDeath(SIGSEGV);
889 FinishIntercept(&intercept_result);
890
891 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
892
893 std::string result;
894 ConsumeFd(std::move(output_fd), &result);
Jaesung Chung58778e12017-06-15 18:20:34 +0900895 ASSERT_BACKTRACE_FRAME(result, "tgkill");
Josh Gao2e7b8e22017-05-04 17:12:57 -0700896}
897
Josh Gaoe04ca272018-01-16 15:38:17 -0800898static const char* const kDebuggerdSeccompPolicy =
899 "/system/etc/seccomp_policy/crash_dump." ABI_STRING ".policy";
900
Josh Gao70adac62018-02-22 11:38:33 -0800901static pid_t seccomp_fork_impl(void (*prejail)()) {
Josh Gao6f9eeec2018-09-12 13:55:47 -0700902 std::string policy;
903 if (!android::base::ReadFileToString(kDebuggerdSeccompPolicy, &policy)) {
904 PLOG(FATAL) << "failed to read policy file";
905 }
906
907 // Allow a bunch of syscalls used by the tests.
908 policy += "\nclone: 1";
909 policy += "\nsigaltstack: 1";
910 policy += "\nnanosleep: 1";
Christopher Ferrisab606682019-09-17 15:31:47 -0700911 policy += "\ngetrlimit: 1";
912 policy += "\nugetrlimit: 1";
Josh Gao6f9eeec2018-09-12 13:55:47 -0700913
914 FILE* tmp_file = tmpfile();
915 if (!tmp_file) {
916 PLOG(FATAL) << "tmpfile failed";
917 }
918
Christopher Ferris172b0a02019-09-18 17:48:30 -0700919 unique_fd tmp_fd(TEMP_FAILURE_RETRY(dup(fileno(tmp_file))));
Josh Gao6f9eeec2018-09-12 13:55:47 -0700920 if (!android::base::WriteStringToFd(policy, tmp_fd.get())) {
921 PLOG(FATAL) << "failed to write policy to tmpfile";
922 }
923
924 if (lseek(tmp_fd.get(), 0, SEEK_SET) != 0) {
925 PLOG(FATAL) << "failed to seek tmp_fd";
Josh Gaoe04ca272018-01-16 15:38:17 -0800926 }
927
928 ScopedMinijail jail{minijail_new()};
929 if (!jail) {
930 LOG(FATAL) << "failed to create minijail";
931 }
932
933 minijail_no_new_privs(jail.get());
934 minijail_log_seccomp_filter_failures(jail.get());
935 minijail_use_seccomp_filter(jail.get());
Josh Gao6f9eeec2018-09-12 13:55:47 -0700936 minijail_parse_seccomp_filters_from_fd(jail.get(), tmp_fd.release());
Josh Gaoe04ca272018-01-16 15:38:17 -0800937
938 pid_t result = fork();
939 if (result == -1) {
940 return result;
941 } else if (result != 0) {
942 return result;
943 }
944
945 // Spawn and detach a thread that spins forever.
946 std::atomic<bool> thread_ready(false);
947 std::thread thread([&jail, &thread_ready]() {
948 minijail_enter(jail.get());
949 thread_ready = true;
950 for (;;)
951 ;
952 });
953 thread.detach();
954
955 while (!thread_ready) {
956 continue;
957 }
958
Josh Gao70adac62018-02-22 11:38:33 -0800959 if (prejail) {
960 prejail();
961 }
962
Josh Gaoe04ca272018-01-16 15:38:17 -0800963 minijail_enter(jail.get());
964 return result;
965}
966
Josh Gao70adac62018-02-22 11:38:33 -0800967static pid_t seccomp_fork() {
968 return seccomp_fork_impl(nullptr);
969}
970
Josh Gaoe04ca272018-01-16 15:38:17 -0800971TEST_F(CrasherTest, seccomp_crash) {
972 int intercept_result;
973 unique_fd output_fd;
974
975 StartProcess([]() { abort(); }, &seccomp_fork);
976
977 StartIntercept(&output_fd);
978 FinishCrasher();
979 AssertDeath(SIGABRT);
980 FinishIntercept(&intercept_result);
981 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
982
983 std::string result;
984 ConsumeFd(std::move(output_fd), &result);
985 ASSERT_BACKTRACE_FRAME(result, "abort");
986}
987
Josh Gao70adac62018-02-22 11:38:33 -0800988static pid_t seccomp_fork_rlimit() {
989 return seccomp_fork_impl([]() {
990 struct rlimit rlim = {
991 .rlim_cur = 512 * 1024 * 1024,
992 .rlim_max = 512 * 1024 * 1024,
993 };
994
995 if (setrlimit(RLIMIT_AS, &rlim) != 0) {
996 raise(SIGINT);
997 }
998 });
999}
1000
1001TEST_F(CrasherTest, seccomp_crash_oom) {
1002 int intercept_result;
1003 unique_fd output_fd;
1004
1005 StartProcess(
1006 []() {
1007 std::vector<void*> vec;
1008 for (int i = 0; i < 512; ++i) {
1009 char* buf = static_cast<char*>(malloc(1024 * 1024));
1010 if (!buf) {
1011 abort();
1012 }
1013 memset(buf, 0xff, 1024 * 1024);
1014 vec.push_back(buf);
1015 }
1016 },
1017 &seccomp_fork_rlimit);
1018
1019 StartIntercept(&output_fd);
1020 FinishCrasher();
1021 AssertDeath(SIGABRT);
1022 FinishIntercept(&intercept_result);
1023 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
1024
1025 // We can't actually generate a backtrace, just make sure that the process terminates.
1026}
1027
Josh Gaoe04ca272018-01-16 15:38:17 -08001028__attribute__((noinline)) extern "C" bool raise_debugger_signal(DebuggerdDumpType dump_type) {
1029 siginfo_t siginfo;
1030 siginfo.si_code = SI_QUEUE;
1031 siginfo.si_pid = getpid();
1032 siginfo.si_uid = getuid();
1033
1034 if (dump_type != kDebuggerdNativeBacktrace && dump_type != kDebuggerdTombstone) {
1035 PLOG(FATAL) << "invalid dump type";
1036 }
1037
1038 siginfo.si_value.sival_int = dump_type == kDebuggerdNativeBacktrace;
1039
Josh Gaoa48b41b2019-12-13 14:11:04 -08001040 if (syscall(__NR_rt_tgsigqueueinfo, getpid(), gettid(), BIONIC_SIGNAL_DEBUGGER, &siginfo) != 0) {
Josh Gaoe04ca272018-01-16 15:38:17 -08001041 PLOG(ERROR) << "libdebuggerd_client: failed to send signal to self";
1042 return false;
1043 }
1044
1045 return true;
1046}
1047
1048TEST_F(CrasherTest, seccomp_tombstone) {
1049 int intercept_result;
1050 unique_fd output_fd;
1051
1052 static const auto dump_type = kDebuggerdTombstone;
1053 StartProcess(
1054 []() {
1055 raise_debugger_signal(dump_type);
1056 _exit(0);
1057 },
1058 &seccomp_fork);
1059
1060 StartIntercept(&output_fd, dump_type);
1061 FinishCrasher();
1062 AssertDeath(0);
1063 FinishIntercept(&intercept_result);
1064 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
1065
1066 std::string result;
1067 ConsumeFd(std::move(output_fd), &result);
1068 ASSERT_BACKTRACE_FRAME(result, "raise_debugger_signal");
1069}
1070
Josh Gao6f9eeec2018-09-12 13:55:47 -07001071extern "C" void foo() {
1072 LOG(INFO) << "foo";
1073 std::this_thread::sleep_for(1s);
1074}
1075
1076extern "C" void bar() {
1077 LOG(INFO) << "bar";
1078 std::this_thread::sleep_for(1s);
1079}
1080
Josh Gaoe04ca272018-01-16 15:38:17 -08001081TEST_F(CrasherTest, seccomp_backtrace) {
1082 int intercept_result;
1083 unique_fd output_fd;
1084
1085 static const auto dump_type = kDebuggerdNativeBacktrace;
1086 StartProcess(
1087 []() {
Josh Gao6f9eeec2018-09-12 13:55:47 -07001088 std::thread a(foo);
1089 std::thread b(bar);
1090
1091 std::this_thread::sleep_for(100ms);
1092
Josh Gaoe04ca272018-01-16 15:38:17 -08001093 raise_debugger_signal(dump_type);
1094 _exit(0);
1095 },
1096 &seccomp_fork);
1097
1098 StartIntercept(&output_fd, dump_type);
1099 FinishCrasher();
1100 AssertDeath(0);
1101 FinishIntercept(&intercept_result);
1102 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
1103
1104 std::string result;
1105 ConsumeFd(std::move(output_fd), &result);
1106 ASSERT_BACKTRACE_FRAME(result, "raise_debugger_signal");
Josh Gao6f9eeec2018-09-12 13:55:47 -07001107 ASSERT_BACKTRACE_FRAME(result, "foo");
1108 ASSERT_BACKTRACE_FRAME(result, "bar");
Josh Gaoe04ca272018-01-16 15:38:17 -08001109}
1110
1111TEST_F(CrasherTest, seccomp_crash_logcat) {
1112 StartProcess([]() { abort(); }, &seccomp_fork);
1113 FinishCrasher();
1114
1115 // Make sure we don't get SIGSYS when trying to dump a crash to logcat.
1116 AssertDeath(SIGABRT);
1117}
1118
Josh Gaofd13bf02017-08-18 15:37:26 -07001119TEST_F(CrasherTest, competing_tracer) {
1120 int intercept_result;
1121 unique_fd output_fd;
1122 StartProcess([]() {
Josh Gao2b2ae0c2017-08-21 14:31:17 -07001123 raise(SIGABRT);
Josh Gaofd13bf02017-08-18 15:37:26 -07001124 });
1125
1126 StartIntercept(&output_fd);
Josh Gaofd13bf02017-08-18 15:37:26 -07001127
1128 ASSERT_EQ(0, ptrace(PTRACE_SEIZE, crasher_pid, 0, 0));
Josh Gao2b2ae0c2017-08-21 14:31:17 -07001129 FinishCrasher();
Josh Gaofd13bf02017-08-18 15:37:26 -07001130
1131 int status;
Christopher Ferris172b0a02019-09-18 17:48:30 -07001132 ASSERT_EQ(crasher_pid, TEMP_FAILURE_RETRY(waitpid(crasher_pid, &status, 0)));
Josh Gaofd13bf02017-08-18 15:37:26 -07001133 ASSERT_TRUE(WIFSTOPPED(status));
1134 ASSERT_EQ(SIGABRT, WSTOPSIG(status));
1135
1136 ASSERT_EQ(0, ptrace(PTRACE_CONT, crasher_pid, 0, SIGABRT));
1137 FinishIntercept(&intercept_result);
1138 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
1139
1140 std::string result;
1141 ConsumeFd(std::move(output_fd), &result);
1142 std::string regex = R"(failed to attach to thread \d+, already traced by )";
1143 regex += std::to_string(gettid());
1144 regex += R"( \(.+debuggerd_test)";
1145 ASSERT_MATCH(result, regex.c_str());
1146
Christopher Ferris172b0a02019-09-18 17:48:30 -07001147 ASSERT_EQ(crasher_pid, TEMP_FAILURE_RETRY(waitpid(crasher_pid, &status, 0)));
Josh Gao2b2ae0c2017-08-21 14:31:17 -07001148 ASSERT_TRUE(WIFSTOPPED(status));
1149 ASSERT_EQ(SIGABRT, WSTOPSIG(status));
1150
Josh Gaofd13bf02017-08-18 15:37:26 -07001151 ASSERT_EQ(0, ptrace(PTRACE_DETACH, crasher_pid, 0, SIGABRT));
1152 AssertDeath(SIGABRT);
1153}
1154
Josh Gaobf06a402018-08-27 16:34:01 -07001155TEST_F(CrasherTest, fdsan_warning_abort_message) {
1156 int intercept_result;
1157 unique_fd output_fd;
1158
1159 StartProcess([]() {
1160 android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_WARN_ONCE);
Christopher Ferris172b0a02019-09-18 17:48:30 -07001161 unique_fd fd(TEMP_FAILURE_RETRY(open("/dev/null", O_RDONLY | O_CLOEXEC)));
Josh Gaobf06a402018-08-27 16:34:01 -07001162 if (fd == -1) {
1163 abort();
1164 }
1165 close(fd.get());
1166 _exit(0);
1167 });
1168
1169 StartIntercept(&output_fd);
1170 FinishCrasher();
1171 AssertDeath(0);
1172 FinishIntercept(&intercept_result);
1173 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
1174
1175 std::string result;
1176 ConsumeFd(std::move(output_fd), &result);
1177 ASSERT_MATCH(result, "Abort message: 'attempted to close");
1178}
1179
Josh Gaoc3c8c022017-02-13 16:36:18 -08001180TEST(crash_dump, zombie) {
1181 pid_t forkpid = fork();
1182
Josh Gaoc3c8c022017-02-13 16:36:18 -08001183 pid_t rc;
1184 int status;
1185
1186 if (forkpid == 0) {
1187 errno = 0;
1188 rc = waitpid(-1, &status, WNOHANG | __WALL | __WNOTHREAD);
1189 if (rc != -1 || errno != ECHILD) {
1190 errx(2, "first waitpid returned %d (%s), expected failure with ECHILD", rc, strerror(errno));
1191 }
1192
Josh Gaoa48b41b2019-12-13 14:11:04 -08001193 raise(BIONIC_SIGNAL_DEBUGGER);
Josh Gaoc3c8c022017-02-13 16:36:18 -08001194
1195 errno = 0;
Christopher Ferris172b0a02019-09-18 17:48:30 -07001196 rc = TEMP_FAILURE_RETRY(waitpid(-1, &status, __WALL | __WNOTHREAD));
Josh Gaoc3c8c022017-02-13 16:36:18 -08001197 if (rc != -1 || errno != ECHILD) {
1198 errx(2, "second waitpid returned %d (%s), expected failure with ECHILD", rc, strerror(errno));
1199 }
1200 _exit(0);
1201 } else {
Christopher Ferris172b0a02019-09-18 17:48:30 -07001202 rc = TEMP_FAILURE_RETRY(waitpid(forkpid, &status, 0));
Josh Gaoc3c8c022017-02-13 16:36:18 -08001203 ASSERT_EQ(forkpid, rc);
1204 ASSERT_TRUE(WIFEXITED(status));
1205 ASSERT_EQ(0, WEXITSTATUS(status));
1206 }
1207}
Josh Gao352a8452017-03-30 16:46:21 -07001208
1209TEST(tombstoned, no_notify) {
1210 // Do this a few times.
1211 for (int i = 0; i < 3; ++i) {
1212 pid_t pid = 123'456'789 + i;
1213
1214 unique_fd intercept_fd, output_fd;
Narayan Kamathca5e9082017-06-02 15:42:06 +01001215 InterceptStatus status;
1216 tombstoned_intercept(pid, &intercept_fd, &output_fd, &status, kDebuggerdTombstone);
1217 ASSERT_EQ(InterceptStatus::kRegistered, status);
Josh Gao352a8452017-03-30 16:46:21 -07001218
1219 {
1220 unique_fd tombstoned_socket, input_fd;
Narayan Kamatha73df602017-05-24 15:07:25 +01001221 ASSERT_TRUE(tombstoned_connect(pid, &tombstoned_socket, &input_fd, kDebuggerdTombstone));
Josh Gao352a8452017-03-30 16:46:21 -07001222 ASSERT_TRUE(android::base::WriteFully(input_fd.get(), &pid, sizeof(pid)));
1223 }
1224
1225 pid_t read_pid;
1226 ASSERT_TRUE(android::base::ReadFully(output_fd.get(), &read_pid, sizeof(read_pid)));
1227 ASSERT_EQ(read_pid, pid);
1228 }
1229}
1230
1231TEST(tombstoned, stress) {
1232 // Spawn threads to simultaneously do a bunch of failing dumps and a bunch of successful dumps.
1233 static constexpr int kDumpCount = 100;
1234
1235 std::atomic<bool> start(false);
1236 std::vector<std::thread> threads;
1237 threads.emplace_back([&start]() {
1238 while (!start) {
1239 continue;
1240 }
1241
1242 // Use a way out of range pid, to avoid stomping on an actual process.
1243 pid_t pid_base = 1'000'000;
1244
1245 for (int dump = 0; dump < kDumpCount; ++dump) {
1246 pid_t pid = pid_base + dump;
1247
1248 unique_fd intercept_fd, output_fd;
Narayan Kamathca5e9082017-06-02 15:42:06 +01001249 InterceptStatus status;
1250 tombstoned_intercept(pid, &intercept_fd, &output_fd, &status, kDebuggerdTombstone);
1251 ASSERT_EQ(InterceptStatus::kRegistered, status);
Josh Gao352a8452017-03-30 16:46:21 -07001252
1253 // Pretend to crash, and then immediately close the socket.
1254 unique_fd sockfd(socket_local_client(kTombstonedCrashSocketName,
1255 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_SEQPACKET));
1256 if (sockfd == -1) {
1257 FAIL() << "failed to connect to tombstoned: " << strerror(errno);
1258 }
1259 TombstonedCrashPacket packet = {};
1260 packet.packet_type = CrashPacketType::kDumpRequest;
1261 packet.packet.dump_request.pid = pid;
1262 if (TEMP_FAILURE_RETRY(write(sockfd, &packet, sizeof(packet))) != sizeof(packet)) {
1263 FAIL() << "failed to write to tombstoned: " << strerror(errno);
1264 }
1265
1266 continue;
1267 }
1268 });
1269
1270 threads.emplace_back([&start]() {
1271 while (!start) {
1272 continue;
1273 }
1274
1275 // Use a way out of range pid, to avoid stomping on an actual process.
1276 pid_t pid_base = 2'000'000;
1277
1278 for (int dump = 0; dump < kDumpCount; ++dump) {
1279 pid_t pid = pid_base + dump;
1280
1281 unique_fd intercept_fd, output_fd;
Narayan Kamathca5e9082017-06-02 15:42:06 +01001282 InterceptStatus status;
1283 tombstoned_intercept(pid, &intercept_fd, &output_fd, &status, kDebuggerdTombstone);
1284 ASSERT_EQ(InterceptStatus::kRegistered, status);
Josh Gao352a8452017-03-30 16:46:21 -07001285
1286 {
1287 unique_fd tombstoned_socket, input_fd;
Narayan Kamatha73df602017-05-24 15:07:25 +01001288 ASSERT_TRUE(tombstoned_connect(pid, &tombstoned_socket, &input_fd, kDebuggerdTombstone));
Josh Gao352a8452017-03-30 16:46:21 -07001289 ASSERT_TRUE(android::base::WriteFully(input_fd.get(), &pid, sizeof(pid)));
1290 tombstoned_notify_completion(tombstoned_socket.get());
1291 }
1292
1293 // TODO: Fix the race that requires this sleep.
1294 std::this_thread::sleep_for(50ms);
1295
1296 pid_t read_pid;
1297 ASSERT_TRUE(android::base::ReadFully(output_fd.get(), &read_pid, sizeof(read_pid)));
1298 ASSERT_EQ(read_pid, pid);
1299 }
1300 });
1301
1302 start = true;
1303
1304 for (std::thread& thread : threads) {
1305 thread.join();
1306 }
1307}
Narayan Kamathca5e9082017-06-02 15:42:06 +01001308
1309TEST(tombstoned, java_trace_intercept_smoke) {
1310 // Using a "real" PID is a little dangerous here - if the test fails
1311 // or crashes, we might end up getting a bogus / unreliable stack
1312 // trace.
1313 const pid_t self = getpid();
1314
1315 unique_fd intercept_fd, output_fd;
1316 InterceptStatus status;
1317 tombstoned_intercept(self, &intercept_fd, &output_fd, &status, kDebuggerdJavaBacktrace);
1318 ASSERT_EQ(InterceptStatus::kRegistered, status);
1319
Josh Gao76e1e302021-01-26 15:53:11 -08001320 // First connect to tombstoned requesting a native tombstone. This
Narayan Kamathca5e9082017-06-02 15:42:06 +01001321 // should result in a "regular" FD and not the installed intercept.
1322 const char native[] = "native";
1323 unique_fd tombstoned_socket, input_fd;
Josh Gao76e1e302021-01-26 15:53:11 -08001324 ASSERT_TRUE(tombstoned_connect(self, &tombstoned_socket, &input_fd, kDebuggerdTombstone));
Narayan Kamathca5e9082017-06-02 15:42:06 +01001325 ASSERT_TRUE(android::base::WriteFully(input_fd.get(), native, sizeof(native)));
1326 tombstoned_notify_completion(tombstoned_socket.get());
1327
1328 // Then, connect to tombstoned asking for a java backtrace. This *should*
1329 // trigger the intercept.
1330 const char java[] = "java";
1331 ASSERT_TRUE(tombstoned_connect(self, &tombstoned_socket, &input_fd, kDebuggerdJavaBacktrace));
1332 ASSERT_TRUE(android::base::WriteFully(input_fd.get(), java, sizeof(java)));
1333 tombstoned_notify_completion(tombstoned_socket.get());
1334
1335 char outbuf[sizeof(java)];
1336 ASSERT_TRUE(android::base::ReadFully(output_fd.get(), outbuf, sizeof(outbuf)));
1337 ASSERT_STREQ("java", outbuf);
1338}
1339
1340TEST(tombstoned, multiple_intercepts) {
1341 const pid_t fake_pid = 1'234'567;
1342 unique_fd intercept_fd, output_fd;
1343 InterceptStatus status;
1344 tombstoned_intercept(fake_pid, &intercept_fd, &output_fd, &status, kDebuggerdJavaBacktrace);
1345 ASSERT_EQ(InterceptStatus::kRegistered, status);
1346
1347 unique_fd intercept_fd_2, output_fd_2;
1348 tombstoned_intercept(fake_pid, &intercept_fd_2, &output_fd_2, &status, kDebuggerdNativeBacktrace);
1349 ASSERT_EQ(InterceptStatus::kFailedAlreadyRegistered, status);
1350}
1351
1352TEST(tombstoned, intercept_any) {
1353 const pid_t fake_pid = 1'234'567;
1354
1355 unique_fd intercept_fd, output_fd;
1356 InterceptStatus status;
1357 tombstoned_intercept(fake_pid, &intercept_fd, &output_fd, &status, kDebuggerdNativeBacktrace);
1358 ASSERT_EQ(InterceptStatus::kRegistered, status);
1359
1360 const char any[] = "any";
1361 unique_fd tombstoned_socket, input_fd;
1362 ASSERT_TRUE(tombstoned_connect(fake_pid, &tombstoned_socket, &input_fd, kDebuggerdAnyIntercept));
1363 ASSERT_TRUE(android::base::WriteFully(input_fd.get(), any, sizeof(any)));
1364 tombstoned_notify_completion(tombstoned_socket.get());
1365
1366 char outbuf[sizeof(any)];
1367 ASSERT_TRUE(android::base::ReadFully(output_fd.get(), outbuf, sizeof(outbuf)));
1368 ASSERT_STREQ("any", outbuf);
1369}
Josh Gao2b22ae12018-09-12 14:51:03 -07001370
1371TEST(tombstoned, interceptless_backtrace) {
1372 // Generate 50 backtraces, and then check to see that we haven't created 50 new tombstones.
1373 auto get_tombstone_timestamps = []() -> std::map<int, time_t> {
1374 std::map<int, time_t> result;
1375 for (int i = 0; i < 99; ++i) {
1376 std::string path = android::base::StringPrintf("/data/tombstones/tombstone_%02d", i);
1377 struct stat st;
1378 if (stat(path.c_str(), &st) == 0) {
1379 result[i] = st.st_mtim.tv_sec;
1380 }
1381 }
1382 return result;
1383 };
1384
1385 auto before = get_tombstone_timestamps();
1386 for (int i = 0; i < 50; ++i) {
1387 raise_debugger_signal(kDebuggerdNativeBacktrace);
1388 }
1389 auto after = get_tombstone_timestamps();
1390
1391 int diff = 0;
1392 for (int i = 0; i < 99; ++i) {
1393 if (after.count(i) == 0) {
1394 continue;
1395 }
1396 if (before.count(i) == 0) {
1397 ++diff;
1398 continue;
1399 }
1400 if (before[i] != after[i]) {
1401 ++diff;
1402 }
1403 }
1404
1405 // We can't be sure that nothing's crash looping in the background.
1406 // This should be good enough, though...
1407 ASSERT_LT(diff, 10) << "too many new tombstones; is something crashing in the background?";
1408}
Christopher Ferris481e8372019-07-15 17:13:24 -07001409
1410static __attribute__((__noinline__)) void overflow_stack(void* p) {
1411 void* buf[1];
1412 buf[0] = p;
1413 static volatile void* global = buf;
1414 if (global) {
1415 global = buf;
1416 overflow_stack(&buf);
1417 }
1418}
1419
1420TEST_F(CrasherTest, stack_overflow) {
1421 int intercept_result;
1422 unique_fd output_fd;
1423 StartProcess([]() { overflow_stack(nullptr); });
1424
1425 StartIntercept(&output_fd);
1426 FinishCrasher();
1427 AssertDeath(SIGSEGV);
1428 FinishIntercept(&intercept_result);
1429
1430 ASSERT_EQ(1, intercept_result) << "tombstoned reported failure";
1431
1432 std::string result;
1433 ConsumeFd(std::move(output_fd), &result);
1434 ASSERT_MATCH(result, R"(Cause: stack pointer[^\n]*stack overflow.\n)");
1435}
Josh Gao76e1e302021-01-26 15:53:11 -08001436
1437TEST(tombstoned, proto) {
1438 const pid_t self = getpid();
1439 unique_fd tombstoned_socket, text_fd, proto_fd;
1440 ASSERT_TRUE(
1441 tombstoned_connect(self, &tombstoned_socket, &text_fd, &proto_fd, kDebuggerdTombstoneProto));
1442
1443 tombstoned_notify_completion(tombstoned_socket.get());
1444
1445 ASSERT_NE(-1, text_fd.get());
1446 ASSERT_NE(-1, proto_fd.get());
1447
1448 struct stat text_st;
1449 ASSERT_EQ(0, fstat(text_fd.get(), &text_st));
1450
1451 // Give tombstoned some time to link the files into place.
1452 std::this_thread::sleep_for(100ms);
1453
1454 // Find the tombstone.
Christopher Ferris35da2882021-02-17 15:39:06 -08001455 std::optional<std::string> tombstone_file;
1456 std::unique_ptr<DIR, decltype(&closedir)> dir_h(opendir("/data/tombstones"), closedir);
1457 ASSERT_TRUE(dir_h != nullptr);
1458 std::regex tombstone_re("tombstone_\\d+");
1459 dirent* entry;
1460 while ((entry = readdir(dir_h.get())) != nullptr) {
1461 if (!std::regex_match(entry->d_name, tombstone_re)) {
1462 continue;
1463 }
1464 std::string path = android::base::StringPrintf("/data/tombstones/%s", entry->d_name);
Josh Gao76e1e302021-01-26 15:53:11 -08001465
1466 struct stat st;
1467 if (TEMP_FAILURE_RETRY(stat(path.c_str(), &st)) != 0) {
1468 continue;
1469 }
1470
1471 if (st.st_dev == text_st.st_dev && st.st_ino == text_st.st_ino) {
Christopher Ferris35da2882021-02-17 15:39:06 -08001472 tombstone_file = path;
Josh Gao76e1e302021-01-26 15:53:11 -08001473 break;
1474 }
1475 }
1476
Christopher Ferris35da2882021-02-17 15:39:06 -08001477 ASSERT_TRUE(tombstone_file);
1478 std::string proto_path = tombstone_file.value() + ".pb";
Josh Gao76e1e302021-01-26 15:53:11 -08001479
1480 struct stat proto_fd_st;
1481 struct stat proto_file_st;
1482 ASSERT_EQ(0, fstat(proto_fd.get(), &proto_fd_st));
1483 ASSERT_EQ(0, stat(proto_path.c_str(), &proto_file_st));
1484
1485 ASSERT_EQ(proto_fd_st.st_dev, proto_file_st.st_dev);
1486 ASSERT_EQ(proto_fd_st.st_ino, proto_file_st.st_ino);
1487}
1488
1489TEST(tombstoned, proto_intercept) {
1490 const pid_t self = getpid();
1491 unique_fd intercept_fd, output_fd;
1492 InterceptStatus status;
1493
1494 tombstoned_intercept(self, &intercept_fd, &output_fd, &status, kDebuggerdTombstone);
1495 ASSERT_EQ(InterceptStatus::kRegistered, status);
1496
1497 unique_fd tombstoned_socket, text_fd, proto_fd;
1498 ASSERT_TRUE(
1499 tombstoned_connect(self, &tombstoned_socket, &text_fd, &proto_fd, kDebuggerdTombstoneProto));
1500 ASSERT_TRUE(android::base::WriteStringToFd("foo", text_fd.get()));
1501 tombstoned_notify_completion(tombstoned_socket.get());
1502
1503 text_fd.reset();
1504
1505 std::string output;
1506 ASSERT_TRUE(android::base::ReadFdToString(output_fd, &output));
1507 ASSERT_EQ("foo", output);
1508}