| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 1 | /* | 
 | 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 Gao | cdea750 | 2017-11-01 15:00:40 -0700 | [diff] [blame] | 19 | #include <stdlib.h> | 
| Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 20 | #include <sys/capability.h> | 
| Josh Gao | fca7ca3 | 2017-01-23 12:05:35 -0800 | [diff] [blame] | 21 | #include <sys/prctl.h> | 
| Josh Gao | fd13bf0 | 2017-08-18 15:37:26 -0700 | [diff] [blame] | 22 | #include <sys/ptrace.h> | 
| Josh Gao | 70adac6 | 2018-02-22 11:38:33 -0800 | [diff] [blame] | 23 | #include <sys/resource.h> | 
| Dan Albert | c38057a | 2017-10-11 11:35:40 -0700 | [diff] [blame] | 24 | #include <sys/syscall.h> | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 25 | #include <sys/types.h> | 
| Josh Gao | fd13bf0 | 2017-08-18 15:37:26 -0700 | [diff] [blame] | 26 | #include <unistd.h> | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 27 |  | 
 | 28 | #include <chrono> | 
 | 29 | #include <regex> | 
 | 30 | #include <thread> | 
 | 31 |  | 
| Josh Gao | bf06a40 | 2018-08-27 16:34:01 -0700 | [diff] [blame] | 32 | #include <android/fdsan.h> | 
| Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 33 | #include <android/set_abort_message.h> | 
| Josh Gao | a48b41b | 2019-12-13 14:11:04 -0800 | [diff] [blame] | 34 | #include <bionic/reserved_signals.h> | 
| Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 35 |  | 
| Josh Gao | 5f87bbd | 2019-01-09 17:01:49 -0800 | [diff] [blame] | 36 | #include <android-base/cmsg.h> | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 37 | #include <android-base/file.h> | 
 | 38 | #include <android-base/logging.h> | 
| Josh Gao | 2e7b8e2 | 2017-05-04 17:12:57 -0700 | [diff] [blame] | 39 | #include <android-base/macros.h> | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 40 | #include <android-base/parseint.h> | 
 | 41 | #include <android-base/properties.h> | 
| Josh Gao | 2b22ae1 | 2018-09-12 14:51:03 -0700 | [diff] [blame] | 42 | #include <android-base/stringprintf.h> | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 43 | #include <android-base/strings.h> | 
| Josh Gao | 30171a8 | 2017-04-27 19:48:44 -0700 | [diff] [blame] | 44 | #include <android-base/test_utils.h> | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 45 | #include <android-base/unique_fd.h> | 
 | 46 | #include <cutils/sockets.h> | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 47 | #include <gtest/gtest.h> | 
 | 48 |  | 
| Josh Gao | e04ca27 | 2018-01-16 15:38:17 -0800 | [diff] [blame] | 49 | #include <libminijail.h> | 
 | 50 | #include <scoped_minijail.h> | 
 | 51 |  | 
| Narayan Kamath | 2d377cd | 2017-05-10 10:58:59 +0100 | [diff] [blame] | 52 | #include "debuggerd/handler.h" | 
 | 53 | #include "protocol.h" | 
 | 54 | #include "tombstoned/tombstoned.h" | 
 | 55 | #include "util.h" | 
 | 56 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 57 | using namespace std::chrono_literals; | 
| Josh Gao | 5f87bbd | 2019-01-09 17:01:49 -0800 | [diff] [blame] | 58 |  | 
 | 59 | using android::base::SendFileDescriptors; | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 60 | using android::base::unique_fd; | 
 | 61 |  | 
 | 62 | #if defined(__LP64__) | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 63 | #define ARCH_SUFFIX "64" | 
 | 64 | #else | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 65 | #define ARCH_SUFFIX "" | 
 | 66 | #endif | 
 | 67 |  | 
 | 68 | constexpr 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 Hsieh | 3249b3a | 2018-05-11 16:01:21 -0700 | [diff] [blame] | 89 | // 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 Gao | e04ca27 | 2018-01-16 15:38:17 -0800 | [diff] [blame] | 93 | #define ASSERT_BACKTRACE_FRAME(result, frame_name) \ | 
| Chih-Hung Hsieh | 3249b3a | 2018-05-11 16:01:21 -0700 | [diff] [blame] | 94 |   ASSERT_MATCH(result,                             \ | 
 | 95 |                R"(#\d\d pc [0-9a-f]+\s+ \S+ (\(offset 0x[0-9a-f]+\) )?\()" frame_name R"(\+)"); | 
| Jaesung Chung | 58778e1 | 2017-06-15 18:20:34 +0900 | [diff] [blame] | 96 |  | 
| Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 97 | static void tombstoned_intercept(pid_t target_pid, unique_fd* intercept_fd, unique_fd* output_fd, | 
| Narayan Kamath | ca5e908 | 2017-06-02 15:42:06 +0100 | [diff] [blame] | 98 |                                  InterceptStatus* status, DebuggerdDumpType intercept_type) { | 
| Josh Gao | 460b336 | 2017-03-30 16:40:47 -0700 | [diff] [blame] | 99 |   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 Desaulniers | 67d52aa | 2019-10-07 23:28:15 -0700 | [diff] [blame] | 105 |   InterceptRequest req = { | 
 | 106 |       .dump_type = intercept_type, | 
 | 107 |       .pid = target_pid, | 
 | 108 |   }; | 
| Josh Gao | 460b336 | 2017-03-30 16:40:47 -0700 | [diff] [blame] | 109 |  | 
 | 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 Gao | 5675f3c | 2017-06-01 12:19:53 -0700 | [diff] [blame] | 131 |   ASSERT_GE(pipe_buffer_size, 1024 * 1024); | 
 | 132 |  | 
| Josh Gao | 5f87bbd | 2019-01-09 17:01:49 -0800 | [diff] [blame] | 133 |   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 Gao | 460b336 | 2017-03-30 16:40:47 -0700 | [diff] [blame] | 136 |     FAIL() << "failed to send output fd to tombstoned: " << strerror(errno); | 
 | 137 |   } | 
 | 138 |  | 
 | 139 |   InterceptResponse response; | 
| Josh Gao | 5f87bbd | 2019-01-09 17:01:49 -0800 | [diff] [blame] | 140 |   rc = TEMP_FAILURE_RETRY(read(intercept_fd->get(), &response, sizeof(response))); | 
| Josh Gao | 460b336 | 2017-03-30 16:40:47 -0700 | [diff] [blame] | 141 |   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 Kamath | ca5e908 | 2017-06-02 15:42:06 +0100 | [diff] [blame] | 150 |   *status = response.status; | 
| Josh Gao | 460b336 | 2017-03-30 16:40:47 -0700 | [diff] [blame] | 151 | } | 
 | 152 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 153 | class 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 Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 163 |   void StartIntercept(unique_fd* output_fd, DebuggerdDumpType intercept_type = kDebuggerdTombstone); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 164 |  | 
 | 165 |   // Returns -1 if we fail to read a response from tombstoned, otherwise the received return code. | 
 | 166 |   void FinishIntercept(int* result); | 
 | 167 |  | 
| Josh Gao | 2e7b8e2 | 2017-05-04 17:12:57 -0700 | [diff] [blame] | 168 |   void StartProcess(std::function<void()> function, std::function<pid_t()> forker = fork); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 169 |   void StartCrasher(const std::string& crash_type); | 
 | 170 |   void FinishCrasher(); | 
 | 171 |   void AssertDeath(int signo); | 
 | 172 | }; | 
 | 173 |  | 
 | 174 | CrasherTest::CrasherTest() { | 
 | 175 |   previous_wait_for_gdb = android::base::GetBoolProperty(kWaitForGdbKey, false); | 
 | 176 |   android::base::SetProperty(kWaitForGdbKey, "0"); | 
 | 177 | } | 
 | 178 |  | 
 | 179 | CrasherTest::~CrasherTest() { | 
 | 180 |   if (crasher_pid != -1) { | 
 | 181 |     kill(crasher_pid, SIGKILL); | 
 | 182 |     int status; | 
| Christopher Ferris | 172b0a0 | 2019-09-18 17:48:30 -0700 | [diff] [blame] | 183 |     TEMP_FAILURE_RETRY(waitpid(crasher_pid, &status, WUNTRACED)); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 184 |   } | 
 | 185 |  | 
 | 186 |   android::base::SetProperty(kWaitForGdbKey, previous_wait_for_gdb ? "1" : "0"); | 
 | 187 | } | 
 | 188 |  | 
| Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 189 | void CrasherTest::StartIntercept(unique_fd* output_fd, DebuggerdDumpType intercept_type) { | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 190 |   if (crasher_pid == -1) { | 
 | 191 |     FAIL() << "crasher hasn't been started"; | 
 | 192 |   } | 
 | 193 |  | 
| Narayan Kamath | ca5e908 | 2017-06-02 15:42:06 +0100 | [diff] [blame] | 194 |   InterceptStatus status; | 
 | 195 |   tombstoned_intercept(crasher_pid, &this->intercept_fd, output_fd, &status, intercept_type); | 
 | 196 |   ASSERT_EQ(InterceptStatus::kRegistered, status); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 197 | } | 
 | 198 |  | 
 | 199 | void CrasherTest::FinishIntercept(int* result) { | 
 | 200 |   InterceptResponse response; | 
 | 201 |  | 
| Christopher Ferris | 11555f0 | 2019-09-20 14:18:55 -0700 | [diff] [blame] | 202 |   ssize_t rc = TIMEOUT(30, read(intercept_fd.get(), &response, sizeof(response))); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 203 |   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 Gao | 460b336 | 2017-03-30 16:40:47 -0700 | [diff] [blame] | 211 |     *result = response.status == InterceptStatus::kStarted ? 1 : 0; | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 212 |   } | 
 | 213 | } | 
 | 214 |  | 
| Josh Gao | 2e7b8e2 | 2017-05-04 17:12:57 -0700 | [diff] [blame] | 215 | void CrasherTest::StartProcess(std::function<void()> function, std::function<pid_t()> forker) { | 
| Josh Gao | fca7ca3 | 2017-01-23 12:05:35 -0800 | [diff] [blame] | 216 |   unique_fd read_pipe; | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 217 |   unique_fd crasher_read_pipe; | 
 | 218 |   if (!Pipe(&crasher_read_pipe, &crasher_pipe)) { | 
 | 219 |     FAIL() << "failed to create pipe: " << strerror(errno); | 
 | 220 |   } | 
 | 221 |  | 
| Josh Gao | 2e7b8e2 | 2017-05-04 17:12:57 -0700 | [diff] [blame] | 222 |   crasher_pid = forker(); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 223 |   if (crasher_pid == -1) { | 
 | 224 |     FAIL() << "fork failed: " << strerror(errno); | 
 | 225 |   } else if (crasher_pid == 0) { | 
| Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 226 |     char dummy; | 
 | 227 |     crasher_pipe.reset(); | 
 | 228 |     TEMP_FAILURE_RETRY(read(crasher_read_pipe.get(), &dummy, 1)); | 
| Josh Gao | fca7ca3 | 2017-01-23 12:05:35 -0800 | [diff] [blame] | 229 |     function(); | 
 | 230 |     _exit(0); | 
 | 231 |   } | 
 | 232 | } | 
 | 233 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 234 | void CrasherTest::FinishCrasher() { | 
 | 235 |   if (crasher_pipe == -1) { | 
 | 236 |     FAIL() << "crasher pipe uninitialized"; | 
 | 237 |   } | 
 | 238 |  | 
| Christopher Ferris | 172b0a0 | 2019-09-18 17:48:30 -0700 | [diff] [blame] | 239 |   ssize_t rc = TEMP_FAILURE_RETRY(write(crasher_pipe.get(), "\n", 1)); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 240 |   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 |  | 
 | 247 | void CrasherTest::AssertDeath(int signo) { | 
 | 248 |   int status; | 
| Christopher Ferris | 11555f0 | 2019-09-20 14:18:55 -0700 | [diff] [blame] | 249 |   pid_t pid = TIMEOUT(30, waitpid(crasher_pid, &status, 0)); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 250 |   if (pid != crasher_pid) { | 
| Christopher Ferris | afc0ff7 | 2019-06-26 15:08:51 -0700 | [diff] [blame] | 251 |     printf("failed to wait for crasher (expected pid %d, return value %d): %s\n", crasher_pid, pid, | 
 | 252 |            strerror(errno)); | 
| Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 253 |     sleep(100); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 254 |     FAIL() << "failed to wait for crasher: " << strerror(errno); | 
 | 255 |   } | 
 | 256 |  | 
| Josh Gao | e06f2a4 | 2017-04-27 16:50:38 -0700 | [diff] [blame] | 257 |   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 Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 264 |   } | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 265 |   crasher_pid = -1; | 
 | 266 | } | 
 | 267 |  | 
 | 268 | static 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 |  | 
 | 289 | TEST_F(CrasherTest, smoke) { | 
 | 290 |   int intercept_result; | 
 | 291 |   unique_fd output_fd; | 
| Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 292 |   StartProcess([]() { | 
 | 293 |     *reinterpret_cast<volatile char*>(0xdead) = '1'; | 
 | 294 |   }); | 
 | 295 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 296 |   StartIntercept(&output_fd); | 
 | 297 |   FinishCrasher(); | 
 | 298 |   AssertDeath(SIGSEGV); | 
 | 299 |   FinishIntercept(&intercept_result); | 
 | 300 |  | 
 | 301 |   ASSERT_EQ(1, intercept_result) << "tombstoned reported failure"; | 
 | 302 |  | 
 | 303 |   std::string result; | 
 | 304 |   ConsumeFd(std::move(output_fd), &result); | 
 | 305 |   ASSERT_MATCH(result, R"(signal 11 \(SIGSEGV\), code 1 \(SEGV_MAPERR\), fault addr 0xdead)"); | 
 | 306 | } | 
 | 307 |  | 
| Josh Gao | cdea750 | 2017-11-01 15:00:40 -0700 | [diff] [blame] | 308 | TEST_F(CrasherTest, LD_PRELOAD) { | 
 | 309 |   int intercept_result; | 
 | 310 |   unique_fd output_fd; | 
 | 311 |   StartProcess([]() { | 
 | 312 |     setenv("LD_PRELOAD", "nonexistent.so", 1); | 
 | 313 |     *reinterpret_cast<volatile char*>(0xdead) = '1'; | 
 | 314 |   }); | 
 | 315 |  | 
 | 316 |   StartIntercept(&output_fd); | 
 | 317 |   FinishCrasher(); | 
 | 318 |   AssertDeath(SIGSEGV); | 
 | 319 |   FinishIntercept(&intercept_result); | 
 | 320 |  | 
 | 321 |   ASSERT_EQ(1, intercept_result) << "tombstoned reported failure"; | 
 | 322 |  | 
 | 323 |   std::string result; | 
 | 324 |   ConsumeFd(std::move(output_fd), &result); | 
 | 325 |   ASSERT_MATCH(result, R"(signal 11 \(SIGSEGV\), code 1 \(SEGV_MAPERR\), fault addr 0xdead)"); | 
 | 326 | } | 
 | 327 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 328 | TEST_F(CrasherTest, abort) { | 
 | 329 |   int intercept_result; | 
 | 330 |   unique_fd output_fd; | 
| Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 331 |   StartProcess([]() { | 
 | 332 |     abort(); | 
 | 333 |   }); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 334 |   StartIntercept(&output_fd); | 
 | 335 |   FinishCrasher(); | 
 | 336 |   AssertDeath(SIGABRT); | 
 | 337 |   FinishIntercept(&intercept_result); | 
 | 338 |  | 
 | 339 |   ASSERT_EQ(1, intercept_result) << "tombstoned reported failure"; | 
 | 340 |  | 
 | 341 |   std::string result; | 
 | 342 |   ConsumeFd(std::move(output_fd), &result); | 
| Andreas Gampe | 26cbafb | 2017-06-22 20:14:43 -0700 | [diff] [blame] | 343 |   ASSERT_BACKTRACE_FRAME(result, "abort"); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 344 | } | 
 | 345 |  | 
 | 346 | TEST_F(CrasherTest, signal) { | 
 | 347 |   int intercept_result; | 
 | 348 |   unique_fd output_fd; | 
| Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 349 |   StartProcess([]() { | 
| Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 350 |     while (true) { | 
 | 351 |       sleep(1); | 
 | 352 |     } | 
| Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 353 |   }); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 354 |   StartIntercept(&output_fd); | 
| Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 355 |   FinishCrasher(); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 356 |   ASSERT_EQ(0, kill(crasher_pid, SIGSEGV)); | 
 | 357 |  | 
 | 358 |   AssertDeath(SIGSEGV); | 
 | 359 |   FinishIntercept(&intercept_result); | 
 | 360 |  | 
 | 361 |   ASSERT_EQ(1, intercept_result) << "tombstoned reported failure"; | 
 | 362 |  | 
 | 363 |   std::string result; | 
 | 364 |   ConsumeFd(std::move(output_fd), &result); | 
| Elliott Hughes | 8972270 | 2018-05-02 10:47:00 -0700 | [diff] [blame] | 365 |   ASSERT_MATCH( | 
 | 366 |       result, | 
 | 367 |       R"(signal 11 \(SIGSEGV\), code 0 \(SI_USER from pid \d+, uid \d+\), fault addr --------)"); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 368 |   ASSERT_MATCH(result, R"(backtrace:)"); | 
 | 369 | } | 
 | 370 |  | 
 | 371 | TEST_F(CrasherTest, abort_message) { | 
 | 372 |   int intercept_result; | 
 | 373 |   unique_fd output_fd; | 
| Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 374 |   StartProcess([]() { | 
| Josh Gao | 1cc7bd8 | 2018-02-13 13:16:17 -0800 | [diff] [blame] | 375 |     // Arrived at experimentally; | 
 | 376 |     // logd truncates at 4062. | 
 | 377 |     // strlen("Abort message: ''") is 17. | 
 | 378 |     // That's 4045, but we also want a NUL. | 
 | 379 |     char buf[4045 + 1]; | 
 | 380 |     memset(buf, 'x', sizeof(buf)); | 
 | 381 |     buf[sizeof(buf) - 1] = '\0'; | 
 | 382 |     android_set_abort_message(buf); | 
| Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 383 |     abort(); | 
 | 384 |   }); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 385 |   StartIntercept(&output_fd); | 
 | 386 |   FinishCrasher(); | 
 | 387 |   AssertDeath(SIGABRT); | 
 | 388 |   FinishIntercept(&intercept_result); | 
 | 389 |  | 
 | 390 |   ASSERT_EQ(1, intercept_result) << "tombstoned reported failure"; | 
 | 391 |  | 
 | 392 |   std::string result; | 
 | 393 |   ConsumeFd(std::move(output_fd), &result); | 
| Josh Gao | 1cc7bd8 | 2018-02-13 13:16:17 -0800 | [diff] [blame] | 394 |   ASSERT_MATCH(result, R"(Abort message: 'x{4045}')"); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 395 | } | 
 | 396 |  | 
| Josh Gao | e06f2a4 | 2017-04-27 16:50:38 -0700 | [diff] [blame] | 397 | TEST_F(CrasherTest, abort_message_backtrace) { | 
 | 398 |   int intercept_result; | 
 | 399 |   unique_fd output_fd; | 
 | 400 |   StartProcess([]() { | 
 | 401 |     android_set_abort_message("not actually aborting"); | 
| Josh Gao | a48b41b | 2019-12-13 14:11:04 -0800 | [diff] [blame] | 402 |     raise(BIONIC_SIGNAL_DEBUGGER); | 
| Josh Gao | e06f2a4 | 2017-04-27 16:50:38 -0700 | [diff] [blame] | 403 |     exit(0); | 
 | 404 |   }); | 
 | 405 |   StartIntercept(&output_fd); | 
 | 406 |   FinishCrasher(); | 
 | 407 |   AssertDeath(0); | 
 | 408 |   FinishIntercept(&intercept_result); | 
 | 409 |  | 
 | 410 |   ASSERT_EQ(1, intercept_result) << "tombstoned reported failure"; | 
 | 411 |  | 
 | 412 |   std::string result; | 
 | 413 |   ConsumeFd(std::move(output_fd), &result); | 
 | 414 |   ASSERT_NOT_MATCH(result, R"(Abort message:)"); | 
 | 415 | } | 
 | 416 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 417 | TEST_F(CrasherTest, intercept_timeout) { | 
 | 418 |   int intercept_result; | 
 | 419 |   unique_fd output_fd; | 
| Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 420 |   StartProcess([]() { | 
 | 421 |     abort(); | 
 | 422 |   }); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 423 |   StartIntercept(&output_fd); | 
 | 424 |  | 
 | 425 |   // Don't let crasher finish until we timeout. | 
 | 426 |   FinishIntercept(&intercept_result); | 
 | 427 |  | 
 | 428 |   ASSERT_NE(1, intercept_result) << "tombstoned reported success? (intercept_result = " | 
 | 429 |                                  << intercept_result << ")"; | 
 | 430 |  | 
 | 431 |   FinishCrasher(); | 
 | 432 |   AssertDeath(SIGABRT); | 
 | 433 | } | 
 | 434 |  | 
 | 435 | TEST_F(CrasherTest, wait_for_gdb) { | 
 | 436 |   if (!android::base::SetProperty(kWaitForGdbKey, "1")) { | 
 | 437 |     FAIL() << "failed to enable wait_for_gdb"; | 
 | 438 |   } | 
 | 439 |   sleep(1); | 
 | 440 |  | 
| Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 441 |   StartProcess([]() { | 
 | 442 |     abort(); | 
 | 443 |   }); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 444 |   FinishCrasher(); | 
 | 445 |  | 
 | 446 |   int status; | 
| Christopher Ferris | 172b0a0 | 2019-09-18 17:48:30 -0700 | [diff] [blame] | 447 |   ASSERT_EQ(crasher_pid, TEMP_FAILURE_RETRY(waitpid(crasher_pid, &status, WUNTRACED))); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 448 |   ASSERT_TRUE(WIFSTOPPED(status)); | 
 | 449 |   ASSERT_EQ(SIGSTOP, WSTOPSIG(status)); | 
 | 450 |  | 
 | 451 |   ASSERT_EQ(0, kill(crasher_pid, SIGCONT)); | 
 | 452 |  | 
 | 453 |   AssertDeath(SIGABRT); | 
 | 454 | } | 
 | 455 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 456 | TEST_F(CrasherTest, backtrace) { | 
 | 457 |   std::string result; | 
 | 458 |   int intercept_result; | 
 | 459 |   unique_fd output_fd; | 
| Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 460 |  | 
 | 461 |   StartProcess([]() { | 
 | 462 |     abort(); | 
 | 463 |   }); | 
| Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 464 |   StartIntercept(&output_fd, kDebuggerdNativeBacktrace); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 465 |  | 
 | 466 |   std::this_thread::sleep_for(500ms); | 
 | 467 |  | 
 | 468 |   sigval val; | 
 | 469 |   val.sival_int = 1; | 
| Josh Gao | a48b41b | 2019-12-13 14:11:04 -0800 | [diff] [blame] | 470 |   ASSERT_EQ(0, sigqueue(crasher_pid, BIONIC_SIGNAL_DEBUGGER, val)) << strerror(errno); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 471 |   FinishIntercept(&intercept_result); | 
 | 472 |   ASSERT_EQ(1, intercept_result) << "tombstoned reported failure"; | 
 | 473 |   ConsumeFd(std::move(output_fd), &result); | 
| Jaesung Chung | 58778e1 | 2017-06-15 18:20:34 +0900 | [diff] [blame] | 474 |   ASSERT_BACKTRACE_FRAME(result, "read"); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 475 |  | 
 | 476 |   int status; | 
 | 477 |   ASSERT_EQ(0, waitpid(crasher_pid, &status, WNOHANG | WUNTRACED)); | 
 | 478 |  | 
 | 479 |   StartIntercept(&output_fd); | 
 | 480 |   FinishCrasher(); | 
 | 481 |   AssertDeath(SIGABRT); | 
 | 482 |   FinishIntercept(&intercept_result); | 
 | 483 |   ASSERT_EQ(1, intercept_result) << "tombstoned reported failure"; | 
 | 484 |   ConsumeFd(std::move(output_fd), &result); | 
| Andreas Gampe | 26cbafb | 2017-06-22 20:14:43 -0700 | [diff] [blame] | 485 |   ASSERT_BACKTRACE_FRAME(result, "abort"); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 486 | } | 
| Josh Gao | fca7ca3 | 2017-01-23 12:05:35 -0800 | [diff] [blame] | 487 |  | 
 | 488 | TEST_F(CrasherTest, PR_SET_DUMPABLE_0_crash) { | 
| Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 489 |   int intercept_result; | 
 | 490 |   unique_fd output_fd; | 
| Josh Gao | fca7ca3 | 2017-01-23 12:05:35 -0800 | [diff] [blame] | 491 |   StartProcess([]() { | 
 | 492 |     prctl(PR_SET_DUMPABLE, 0); | 
| Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 493 |     abort(); | 
| Josh Gao | fca7ca3 | 2017-01-23 12:05:35 -0800 | [diff] [blame] | 494 |   }); | 
| Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 495 |  | 
 | 496 |   StartIntercept(&output_fd); | 
 | 497 |   FinishCrasher(); | 
 | 498 |   AssertDeath(SIGABRT); | 
 | 499 |   FinishIntercept(&intercept_result); | 
 | 500 |  | 
 | 501 |   ASSERT_EQ(1, intercept_result) << "tombstoned reported failure"; | 
 | 502 |  | 
 | 503 |   std::string result; | 
 | 504 |   ConsumeFd(std::move(output_fd), &result); | 
| Andreas Gampe | 26cbafb | 2017-06-22 20:14:43 -0700 | [diff] [blame] | 505 |   ASSERT_BACKTRACE_FRAME(result, "abort"); | 
| Josh Gao | fca7ca3 | 2017-01-23 12:05:35 -0800 | [diff] [blame] | 506 | } | 
 | 507 |  | 
| Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 508 | TEST_F(CrasherTest, capabilities) { | 
 | 509 |   ASSERT_EQ(0U, getuid()) << "capability test requires root"; | 
 | 510 |  | 
| Josh Gao | fca7ca3 | 2017-01-23 12:05:35 -0800 | [diff] [blame] | 511 |   StartProcess([]() { | 
| Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 512 |     if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) != 0) { | 
 | 513 |       err(1, "failed to set PR_SET_KEEPCAPS"); | 
 | 514 |     } | 
 | 515 |  | 
 | 516 |     if (setresuid(1, 1, 1) != 0) { | 
 | 517 |       err(1, "setresuid failed"); | 
 | 518 |     } | 
 | 519 |  | 
 | 520 |     __user_cap_header_struct capheader; | 
 | 521 |     __user_cap_data_struct capdata[2]; | 
 | 522 |     memset(&capheader, 0, sizeof(capheader)); | 
 | 523 |     memset(&capdata, 0, sizeof(capdata)); | 
 | 524 |  | 
 | 525 |     capheader.version = _LINUX_CAPABILITY_VERSION_3; | 
 | 526 |     capheader.pid = 0; | 
 | 527 |  | 
 | 528 |     // Turn on every third capability. | 
 | 529 |     static_assert(CAP_LAST_CAP > 33, "CAP_LAST_CAP <= 32"); | 
 | 530 |     for (int i = 0; i < CAP_LAST_CAP; i += 3) { | 
 | 531 |       capdata[CAP_TO_INDEX(i)].permitted |= CAP_TO_MASK(i); | 
 | 532 |       capdata[CAP_TO_INDEX(i)].effective |= CAP_TO_MASK(i); | 
 | 533 |     } | 
 | 534 |  | 
 | 535 |     // Make sure CAP_SYS_PTRACE is off. | 
 | 536 |     capdata[CAP_TO_INDEX(CAP_SYS_PTRACE)].permitted &= ~(CAP_TO_MASK(CAP_SYS_PTRACE)); | 
 | 537 |     capdata[CAP_TO_INDEX(CAP_SYS_PTRACE)].effective &= ~(CAP_TO_MASK(CAP_SYS_PTRACE)); | 
 | 538 |  | 
 | 539 |     if (capset(&capheader, &capdata[0]) != 0) { | 
 | 540 |       err(1, "capset failed"); | 
 | 541 |     } | 
 | 542 |  | 
 | 543 |     if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) != 0) { | 
 | 544 |       err(1, "failed to drop ambient capabilities"); | 
 | 545 |     } | 
 | 546 |  | 
| Josh Gao | a5199a9 | 2017-04-03 13:18:34 -0700 | [diff] [blame] | 547 |     pthread_setname_np(pthread_self(), "thread_name"); | 
| Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 548 |     raise(SIGSYS); | 
| Josh Gao | fca7ca3 | 2017-01-23 12:05:35 -0800 | [diff] [blame] | 549 |   }); | 
| Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 550 |  | 
 | 551 |   unique_fd output_fd; | 
 | 552 |   StartIntercept(&output_fd); | 
 | 553 |   FinishCrasher(); | 
 | 554 |   AssertDeath(SIGSYS); | 
 | 555 |  | 
 | 556 |   std::string result; | 
 | 557 |   int intercept_result; | 
 | 558 |   FinishIntercept(&intercept_result); | 
 | 559 |   ASSERT_EQ(1, intercept_result) << "tombstoned reported failure"; | 
 | 560 |   ConsumeFd(std::move(output_fd), &result); | 
| Josh Gao | a5199a9 | 2017-04-03 13:18:34 -0700 | [diff] [blame] | 561 |   ASSERT_MATCH(result, R"(name: thread_name\s+>>> .+debuggerd_test(32|64) <<<)"); | 
| Jaesung Chung | 58778e1 | 2017-06-15 18:20:34 +0900 | [diff] [blame] | 562 |   ASSERT_BACKTRACE_FRAME(result, "tgkill"); | 
| Josh Gao | fca7ca3 | 2017-01-23 12:05:35 -0800 | [diff] [blame] | 563 | } | 
| Josh Gao | c3c8c02 | 2017-02-13 16:36:18 -0800 | [diff] [blame] | 564 |  | 
| Josh Gao | 2e7b8e2 | 2017-05-04 17:12:57 -0700 | [diff] [blame] | 565 | TEST_F(CrasherTest, fake_pid) { | 
 | 566 |   int intercept_result; | 
 | 567 |   unique_fd output_fd; | 
 | 568 |  | 
 | 569 |   // Prime the getpid/gettid caches. | 
 | 570 |   UNUSED(getpid()); | 
 | 571 |   UNUSED(gettid()); | 
 | 572 |  | 
 | 573 |   std::function<pid_t()> clone_fn = []() { | 
 | 574 |     return syscall(__NR_clone, SIGCHLD, nullptr, nullptr, nullptr, nullptr); | 
 | 575 |   }; | 
 | 576 |   StartProcess( | 
 | 577 |       []() { | 
 | 578 |         ASSERT_NE(getpid(), syscall(__NR_getpid)); | 
 | 579 |         ASSERT_NE(gettid(), syscall(__NR_gettid)); | 
 | 580 |         raise(SIGSEGV); | 
 | 581 |       }, | 
 | 582 |       clone_fn); | 
 | 583 |  | 
 | 584 |   StartIntercept(&output_fd); | 
 | 585 |   FinishCrasher(); | 
 | 586 |   AssertDeath(SIGSEGV); | 
 | 587 |   FinishIntercept(&intercept_result); | 
 | 588 |  | 
 | 589 |   ASSERT_EQ(1, intercept_result) << "tombstoned reported failure"; | 
 | 590 |  | 
 | 591 |   std::string result; | 
 | 592 |   ConsumeFd(std::move(output_fd), &result); | 
| Jaesung Chung | 58778e1 | 2017-06-15 18:20:34 +0900 | [diff] [blame] | 593 |   ASSERT_BACKTRACE_FRAME(result, "tgkill"); | 
| Josh Gao | 2e7b8e2 | 2017-05-04 17:12:57 -0700 | [diff] [blame] | 594 | } | 
 | 595 |  | 
| Josh Gao | e04ca27 | 2018-01-16 15:38:17 -0800 | [diff] [blame] | 596 | static const char* const kDebuggerdSeccompPolicy = | 
 | 597 |     "/system/etc/seccomp_policy/crash_dump." ABI_STRING ".policy"; | 
 | 598 |  | 
| Josh Gao | 70adac6 | 2018-02-22 11:38:33 -0800 | [diff] [blame] | 599 | static pid_t seccomp_fork_impl(void (*prejail)()) { | 
| Josh Gao | 6f9eeec | 2018-09-12 13:55:47 -0700 | [diff] [blame] | 600 |   std::string policy; | 
 | 601 |   if (!android::base::ReadFileToString(kDebuggerdSeccompPolicy, &policy)) { | 
 | 602 |     PLOG(FATAL) << "failed to read policy file"; | 
 | 603 |   } | 
 | 604 |  | 
 | 605 |   // Allow a bunch of syscalls used by the tests. | 
 | 606 |   policy += "\nclone: 1"; | 
 | 607 |   policy += "\nsigaltstack: 1"; | 
 | 608 |   policy += "\nnanosleep: 1"; | 
| Christopher Ferris | ab60668 | 2019-09-17 15:31:47 -0700 | [diff] [blame] | 609 |   policy += "\ngetrlimit: 1"; | 
 | 610 |   policy += "\nugetrlimit: 1"; | 
| Josh Gao | 6f9eeec | 2018-09-12 13:55:47 -0700 | [diff] [blame] | 611 |  | 
 | 612 |   FILE* tmp_file = tmpfile(); | 
 | 613 |   if (!tmp_file) { | 
 | 614 |     PLOG(FATAL) << "tmpfile failed"; | 
 | 615 |   } | 
 | 616 |  | 
| Christopher Ferris | 172b0a0 | 2019-09-18 17:48:30 -0700 | [diff] [blame] | 617 |   unique_fd tmp_fd(TEMP_FAILURE_RETRY(dup(fileno(tmp_file)))); | 
| Josh Gao | 6f9eeec | 2018-09-12 13:55:47 -0700 | [diff] [blame] | 618 |   if (!android::base::WriteStringToFd(policy, tmp_fd.get())) { | 
 | 619 |     PLOG(FATAL) << "failed to write policy to tmpfile"; | 
 | 620 |   } | 
 | 621 |  | 
 | 622 |   if (lseek(tmp_fd.get(), 0, SEEK_SET) != 0) { | 
 | 623 |     PLOG(FATAL) << "failed to seek tmp_fd"; | 
| Josh Gao | e04ca27 | 2018-01-16 15:38:17 -0800 | [diff] [blame] | 624 |   } | 
 | 625 |  | 
 | 626 |   ScopedMinijail jail{minijail_new()}; | 
 | 627 |   if (!jail) { | 
 | 628 |     LOG(FATAL) << "failed to create minijail"; | 
 | 629 |   } | 
 | 630 |  | 
 | 631 |   minijail_no_new_privs(jail.get()); | 
 | 632 |   minijail_log_seccomp_filter_failures(jail.get()); | 
 | 633 |   minijail_use_seccomp_filter(jail.get()); | 
| Josh Gao | 6f9eeec | 2018-09-12 13:55:47 -0700 | [diff] [blame] | 634 |   minijail_parse_seccomp_filters_from_fd(jail.get(), tmp_fd.release()); | 
| Josh Gao | e04ca27 | 2018-01-16 15:38:17 -0800 | [diff] [blame] | 635 |  | 
 | 636 |   pid_t result = fork(); | 
 | 637 |   if (result == -1) { | 
 | 638 |     return result; | 
 | 639 |   } else if (result != 0) { | 
 | 640 |     return result; | 
 | 641 |   } | 
 | 642 |  | 
 | 643 |   // Spawn and detach a thread that spins forever. | 
 | 644 |   std::atomic<bool> thread_ready(false); | 
 | 645 |   std::thread thread([&jail, &thread_ready]() { | 
 | 646 |     minijail_enter(jail.get()); | 
 | 647 |     thread_ready = true; | 
 | 648 |     for (;;) | 
 | 649 |       ; | 
 | 650 |   }); | 
 | 651 |   thread.detach(); | 
 | 652 |  | 
 | 653 |   while (!thread_ready) { | 
 | 654 |     continue; | 
 | 655 |   } | 
 | 656 |  | 
| Josh Gao | 70adac6 | 2018-02-22 11:38:33 -0800 | [diff] [blame] | 657 |   if (prejail) { | 
 | 658 |     prejail(); | 
 | 659 |   } | 
 | 660 |  | 
| Josh Gao | e04ca27 | 2018-01-16 15:38:17 -0800 | [diff] [blame] | 661 |   minijail_enter(jail.get()); | 
 | 662 |   return result; | 
 | 663 | } | 
 | 664 |  | 
| Josh Gao | 70adac6 | 2018-02-22 11:38:33 -0800 | [diff] [blame] | 665 | static pid_t seccomp_fork() { | 
 | 666 |   return seccomp_fork_impl(nullptr); | 
 | 667 | } | 
 | 668 |  | 
| Josh Gao | e04ca27 | 2018-01-16 15:38:17 -0800 | [diff] [blame] | 669 | TEST_F(CrasherTest, seccomp_crash) { | 
 | 670 |   int intercept_result; | 
 | 671 |   unique_fd output_fd; | 
 | 672 |  | 
 | 673 |   StartProcess([]() { abort(); }, &seccomp_fork); | 
 | 674 |  | 
 | 675 |   StartIntercept(&output_fd); | 
 | 676 |   FinishCrasher(); | 
 | 677 |   AssertDeath(SIGABRT); | 
 | 678 |   FinishIntercept(&intercept_result); | 
 | 679 |   ASSERT_EQ(1, intercept_result) << "tombstoned reported failure"; | 
 | 680 |  | 
 | 681 |   std::string result; | 
 | 682 |   ConsumeFd(std::move(output_fd), &result); | 
 | 683 |   ASSERT_BACKTRACE_FRAME(result, "abort"); | 
 | 684 | } | 
 | 685 |  | 
| Josh Gao | 70adac6 | 2018-02-22 11:38:33 -0800 | [diff] [blame] | 686 | static pid_t seccomp_fork_rlimit() { | 
 | 687 |   return seccomp_fork_impl([]() { | 
 | 688 |     struct rlimit rlim = { | 
 | 689 |         .rlim_cur = 512 * 1024 * 1024, | 
 | 690 |         .rlim_max = 512 * 1024 * 1024, | 
 | 691 |     }; | 
 | 692 |  | 
 | 693 |     if (setrlimit(RLIMIT_AS, &rlim) != 0) { | 
 | 694 |       raise(SIGINT); | 
 | 695 |     } | 
 | 696 |   }); | 
 | 697 | } | 
 | 698 |  | 
 | 699 | TEST_F(CrasherTest, seccomp_crash_oom) { | 
 | 700 |   int intercept_result; | 
 | 701 |   unique_fd output_fd; | 
 | 702 |  | 
 | 703 |   StartProcess( | 
 | 704 |       []() { | 
 | 705 |         std::vector<void*> vec; | 
 | 706 |         for (int i = 0; i < 512; ++i) { | 
 | 707 |           char* buf = static_cast<char*>(malloc(1024 * 1024)); | 
 | 708 |           if (!buf) { | 
 | 709 |             abort(); | 
 | 710 |           } | 
 | 711 |           memset(buf, 0xff, 1024 * 1024); | 
 | 712 |           vec.push_back(buf); | 
 | 713 |         } | 
 | 714 |       }, | 
 | 715 |       &seccomp_fork_rlimit); | 
 | 716 |  | 
 | 717 |   StartIntercept(&output_fd); | 
 | 718 |   FinishCrasher(); | 
 | 719 |   AssertDeath(SIGABRT); | 
 | 720 |   FinishIntercept(&intercept_result); | 
 | 721 |   ASSERT_EQ(1, intercept_result) << "tombstoned reported failure"; | 
 | 722 |  | 
 | 723 |   // We can't actually generate a backtrace, just make sure that the process terminates. | 
 | 724 | } | 
 | 725 |  | 
| Josh Gao | e04ca27 | 2018-01-16 15:38:17 -0800 | [diff] [blame] | 726 | __attribute__((noinline)) extern "C" bool raise_debugger_signal(DebuggerdDumpType dump_type) { | 
 | 727 |   siginfo_t siginfo; | 
 | 728 |   siginfo.si_code = SI_QUEUE; | 
 | 729 |   siginfo.si_pid = getpid(); | 
 | 730 |   siginfo.si_uid = getuid(); | 
 | 731 |  | 
 | 732 |   if (dump_type != kDebuggerdNativeBacktrace && dump_type != kDebuggerdTombstone) { | 
 | 733 |     PLOG(FATAL) << "invalid dump type"; | 
 | 734 |   } | 
 | 735 |  | 
 | 736 |   siginfo.si_value.sival_int = dump_type == kDebuggerdNativeBacktrace; | 
 | 737 |  | 
| Josh Gao | a48b41b | 2019-12-13 14:11:04 -0800 | [diff] [blame] | 738 |   if (syscall(__NR_rt_tgsigqueueinfo, getpid(), gettid(), BIONIC_SIGNAL_DEBUGGER, &siginfo) != 0) { | 
| Josh Gao | e04ca27 | 2018-01-16 15:38:17 -0800 | [diff] [blame] | 739 |     PLOG(ERROR) << "libdebuggerd_client: failed to send signal to self"; | 
 | 740 |     return false; | 
 | 741 |   } | 
 | 742 |  | 
 | 743 |   return true; | 
 | 744 | } | 
 | 745 |  | 
 | 746 | TEST_F(CrasherTest, seccomp_tombstone) { | 
 | 747 |   int intercept_result; | 
 | 748 |   unique_fd output_fd; | 
 | 749 |  | 
 | 750 |   static const auto dump_type = kDebuggerdTombstone; | 
 | 751 |   StartProcess( | 
 | 752 |       []() { | 
 | 753 |         raise_debugger_signal(dump_type); | 
 | 754 |         _exit(0); | 
 | 755 |       }, | 
 | 756 |       &seccomp_fork); | 
 | 757 |  | 
 | 758 |   StartIntercept(&output_fd, dump_type); | 
 | 759 |   FinishCrasher(); | 
 | 760 |   AssertDeath(0); | 
 | 761 |   FinishIntercept(&intercept_result); | 
 | 762 |   ASSERT_EQ(1, intercept_result) << "tombstoned reported failure"; | 
 | 763 |  | 
 | 764 |   std::string result; | 
 | 765 |   ConsumeFd(std::move(output_fd), &result); | 
 | 766 |   ASSERT_BACKTRACE_FRAME(result, "raise_debugger_signal"); | 
 | 767 | } | 
 | 768 |  | 
| Josh Gao | 6f9eeec | 2018-09-12 13:55:47 -0700 | [diff] [blame] | 769 | extern "C" void foo() { | 
 | 770 |   LOG(INFO) << "foo"; | 
 | 771 |   std::this_thread::sleep_for(1s); | 
 | 772 | } | 
 | 773 |  | 
 | 774 | extern "C" void bar() { | 
 | 775 |   LOG(INFO) << "bar"; | 
 | 776 |   std::this_thread::sleep_for(1s); | 
 | 777 | } | 
 | 778 |  | 
| Josh Gao | e04ca27 | 2018-01-16 15:38:17 -0800 | [diff] [blame] | 779 | TEST_F(CrasherTest, seccomp_backtrace) { | 
 | 780 |   int intercept_result; | 
 | 781 |   unique_fd output_fd; | 
 | 782 |  | 
 | 783 |   static const auto dump_type = kDebuggerdNativeBacktrace; | 
 | 784 |   StartProcess( | 
 | 785 |       []() { | 
| Josh Gao | 6f9eeec | 2018-09-12 13:55:47 -0700 | [diff] [blame] | 786 |         std::thread a(foo); | 
 | 787 |         std::thread b(bar); | 
 | 788 |  | 
 | 789 |         std::this_thread::sleep_for(100ms); | 
 | 790 |  | 
| Josh Gao | e04ca27 | 2018-01-16 15:38:17 -0800 | [diff] [blame] | 791 |         raise_debugger_signal(dump_type); | 
 | 792 |         _exit(0); | 
 | 793 |       }, | 
 | 794 |       &seccomp_fork); | 
 | 795 |  | 
 | 796 |   StartIntercept(&output_fd, dump_type); | 
 | 797 |   FinishCrasher(); | 
 | 798 |   AssertDeath(0); | 
 | 799 |   FinishIntercept(&intercept_result); | 
 | 800 |   ASSERT_EQ(1, intercept_result) << "tombstoned reported failure"; | 
 | 801 |  | 
 | 802 |   std::string result; | 
 | 803 |   ConsumeFd(std::move(output_fd), &result); | 
 | 804 |   ASSERT_BACKTRACE_FRAME(result, "raise_debugger_signal"); | 
| Josh Gao | 6f9eeec | 2018-09-12 13:55:47 -0700 | [diff] [blame] | 805 |   ASSERT_BACKTRACE_FRAME(result, "foo"); | 
 | 806 |   ASSERT_BACKTRACE_FRAME(result, "bar"); | 
| Josh Gao | e04ca27 | 2018-01-16 15:38:17 -0800 | [diff] [blame] | 807 | } | 
 | 808 |  | 
 | 809 | TEST_F(CrasherTest, seccomp_crash_logcat) { | 
 | 810 |   StartProcess([]() { abort(); }, &seccomp_fork); | 
 | 811 |   FinishCrasher(); | 
 | 812 |  | 
 | 813 |   // Make sure we don't get SIGSYS when trying to dump a crash to logcat. | 
 | 814 |   AssertDeath(SIGABRT); | 
 | 815 | } | 
 | 816 |  | 
| Josh Gao | fd13bf0 | 2017-08-18 15:37:26 -0700 | [diff] [blame] | 817 | TEST_F(CrasherTest, competing_tracer) { | 
 | 818 |   int intercept_result; | 
 | 819 |   unique_fd output_fd; | 
 | 820 |   StartProcess([]() { | 
| Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 821 |     raise(SIGABRT); | 
| Josh Gao | fd13bf0 | 2017-08-18 15:37:26 -0700 | [diff] [blame] | 822 |   }); | 
 | 823 |  | 
 | 824 |   StartIntercept(&output_fd); | 
| Josh Gao | fd13bf0 | 2017-08-18 15:37:26 -0700 | [diff] [blame] | 825 |  | 
 | 826 |   ASSERT_EQ(0, ptrace(PTRACE_SEIZE, crasher_pid, 0, 0)); | 
| Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 827 |   FinishCrasher(); | 
| Josh Gao | fd13bf0 | 2017-08-18 15:37:26 -0700 | [diff] [blame] | 828 |  | 
 | 829 |   int status; | 
| Christopher Ferris | 172b0a0 | 2019-09-18 17:48:30 -0700 | [diff] [blame] | 830 |   ASSERT_EQ(crasher_pid, TEMP_FAILURE_RETRY(waitpid(crasher_pid, &status, 0))); | 
| Josh Gao | fd13bf0 | 2017-08-18 15:37:26 -0700 | [diff] [blame] | 831 |   ASSERT_TRUE(WIFSTOPPED(status)); | 
 | 832 |   ASSERT_EQ(SIGABRT, WSTOPSIG(status)); | 
 | 833 |  | 
 | 834 |   ASSERT_EQ(0, ptrace(PTRACE_CONT, crasher_pid, 0, SIGABRT)); | 
 | 835 |   FinishIntercept(&intercept_result); | 
 | 836 |   ASSERT_EQ(1, intercept_result) << "tombstoned reported failure"; | 
 | 837 |  | 
 | 838 |   std::string result; | 
 | 839 |   ConsumeFd(std::move(output_fd), &result); | 
 | 840 |   std::string regex = R"(failed to attach to thread \d+, already traced by )"; | 
 | 841 |   regex += std::to_string(gettid()); | 
 | 842 |   regex += R"( \(.+debuggerd_test)"; | 
 | 843 |   ASSERT_MATCH(result, regex.c_str()); | 
 | 844 |  | 
| Christopher Ferris | 172b0a0 | 2019-09-18 17:48:30 -0700 | [diff] [blame] | 845 |   ASSERT_EQ(crasher_pid, TEMP_FAILURE_RETRY(waitpid(crasher_pid, &status, 0))); | 
| Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 846 |   ASSERT_TRUE(WIFSTOPPED(status)); | 
 | 847 |   ASSERT_EQ(SIGABRT, WSTOPSIG(status)); | 
 | 848 |  | 
| Josh Gao | fd13bf0 | 2017-08-18 15:37:26 -0700 | [diff] [blame] | 849 |   ASSERT_EQ(0, ptrace(PTRACE_DETACH, crasher_pid, 0, SIGABRT)); | 
 | 850 |   AssertDeath(SIGABRT); | 
 | 851 | } | 
 | 852 |  | 
| Josh Gao | bf06a40 | 2018-08-27 16:34:01 -0700 | [diff] [blame] | 853 | TEST_F(CrasherTest, fdsan_warning_abort_message) { | 
 | 854 |   int intercept_result; | 
 | 855 |   unique_fd output_fd; | 
 | 856 |  | 
 | 857 |   StartProcess([]() { | 
 | 858 |     android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_WARN_ONCE); | 
| Christopher Ferris | 172b0a0 | 2019-09-18 17:48:30 -0700 | [diff] [blame] | 859 |     unique_fd fd(TEMP_FAILURE_RETRY(open("/dev/null", O_RDONLY | O_CLOEXEC))); | 
| Josh Gao | bf06a40 | 2018-08-27 16:34:01 -0700 | [diff] [blame] | 860 |     if (fd == -1) { | 
 | 861 |       abort(); | 
 | 862 |     } | 
 | 863 |     close(fd.get()); | 
 | 864 |     _exit(0); | 
 | 865 |   }); | 
 | 866 |  | 
 | 867 |   StartIntercept(&output_fd); | 
 | 868 |   FinishCrasher(); | 
 | 869 |   AssertDeath(0); | 
 | 870 |   FinishIntercept(&intercept_result); | 
 | 871 |   ASSERT_EQ(1, intercept_result) << "tombstoned reported failure"; | 
 | 872 |  | 
 | 873 |   std::string result; | 
 | 874 |   ConsumeFd(std::move(output_fd), &result); | 
 | 875 |   ASSERT_MATCH(result, "Abort message: 'attempted to close"); | 
 | 876 | } | 
 | 877 |  | 
| Josh Gao | c3c8c02 | 2017-02-13 16:36:18 -0800 | [diff] [blame] | 878 | TEST(crash_dump, zombie) { | 
 | 879 |   pid_t forkpid = fork(); | 
 | 880 |  | 
| Josh Gao | c3c8c02 | 2017-02-13 16:36:18 -0800 | [diff] [blame] | 881 |   pid_t rc; | 
 | 882 |   int status; | 
 | 883 |  | 
 | 884 |   if (forkpid == 0) { | 
 | 885 |     errno = 0; | 
 | 886 |     rc = waitpid(-1, &status, WNOHANG | __WALL | __WNOTHREAD); | 
 | 887 |     if (rc != -1 || errno != ECHILD) { | 
 | 888 |       errx(2, "first waitpid returned %d (%s), expected failure with ECHILD", rc, strerror(errno)); | 
 | 889 |     } | 
 | 890 |  | 
| Josh Gao | a48b41b | 2019-12-13 14:11:04 -0800 | [diff] [blame] | 891 |     raise(BIONIC_SIGNAL_DEBUGGER); | 
| Josh Gao | c3c8c02 | 2017-02-13 16:36:18 -0800 | [diff] [blame] | 892 |  | 
 | 893 |     errno = 0; | 
| Christopher Ferris | 172b0a0 | 2019-09-18 17:48:30 -0700 | [diff] [blame] | 894 |     rc = TEMP_FAILURE_RETRY(waitpid(-1, &status, __WALL | __WNOTHREAD)); | 
| Josh Gao | c3c8c02 | 2017-02-13 16:36:18 -0800 | [diff] [blame] | 895 |     if (rc != -1 || errno != ECHILD) { | 
 | 896 |       errx(2, "second waitpid returned %d (%s), expected failure with ECHILD", rc, strerror(errno)); | 
 | 897 |     } | 
 | 898 |     _exit(0); | 
 | 899 |   } else { | 
| Christopher Ferris | 172b0a0 | 2019-09-18 17:48:30 -0700 | [diff] [blame] | 900 |     rc = TEMP_FAILURE_RETRY(waitpid(forkpid, &status, 0)); | 
| Josh Gao | c3c8c02 | 2017-02-13 16:36:18 -0800 | [diff] [blame] | 901 |     ASSERT_EQ(forkpid, rc); | 
 | 902 |     ASSERT_TRUE(WIFEXITED(status)); | 
 | 903 |     ASSERT_EQ(0, WEXITSTATUS(status)); | 
 | 904 |   } | 
 | 905 | } | 
| Josh Gao | 352a845 | 2017-03-30 16:46:21 -0700 | [diff] [blame] | 906 |  | 
 | 907 | TEST(tombstoned, no_notify) { | 
 | 908 |   // Do this a few times. | 
 | 909 |   for (int i = 0; i < 3; ++i) { | 
 | 910 |     pid_t pid = 123'456'789 + i; | 
 | 911 |  | 
 | 912 |     unique_fd intercept_fd, output_fd; | 
| Narayan Kamath | ca5e908 | 2017-06-02 15:42:06 +0100 | [diff] [blame] | 913 |     InterceptStatus status; | 
 | 914 |     tombstoned_intercept(pid, &intercept_fd, &output_fd, &status, kDebuggerdTombstone); | 
 | 915 |     ASSERT_EQ(InterceptStatus::kRegistered, status); | 
| Josh Gao | 352a845 | 2017-03-30 16:46:21 -0700 | [diff] [blame] | 916 |  | 
 | 917 |     { | 
 | 918 |       unique_fd tombstoned_socket, input_fd; | 
| Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 919 |       ASSERT_TRUE(tombstoned_connect(pid, &tombstoned_socket, &input_fd, kDebuggerdTombstone)); | 
| Josh Gao | 352a845 | 2017-03-30 16:46:21 -0700 | [diff] [blame] | 920 |       ASSERT_TRUE(android::base::WriteFully(input_fd.get(), &pid, sizeof(pid))); | 
 | 921 |     } | 
 | 922 |  | 
 | 923 |     pid_t read_pid; | 
 | 924 |     ASSERT_TRUE(android::base::ReadFully(output_fd.get(), &read_pid, sizeof(read_pid))); | 
 | 925 |     ASSERT_EQ(read_pid, pid); | 
 | 926 |   } | 
 | 927 | } | 
 | 928 |  | 
 | 929 | TEST(tombstoned, stress) { | 
 | 930 |   // Spawn threads to simultaneously do a bunch of failing dumps and a bunch of successful dumps. | 
 | 931 |   static constexpr int kDumpCount = 100; | 
 | 932 |  | 
 | 933 |   std::atomic<bool> start(false); | 
 | 934 |   std::vector<std::thread> threads; | 
 | 935 |   threads.emplace_back([&start]() { | 
 | 936 |     while (!start) { | 
 | 937 |       continue; | 
 | 938 |     } | 
 | 939 |  | 
 | 940 |     // Use a way out of range pid, to avoid stomping on an actual process. | 
 | 941 |     pid_t pid_base = 1'000'000; | 
 | 942 |  | 
 | 943 |     for (int dump = 0; dump < kDumpCount; ++dump) { | 
 | 944 |       pid_t pid = pid_base + dump; | 
 | 945 |  | 
 | 946 |       unique_fd intercept_fd, output_fd; | 
| Narayan Kamath | ca5e908 | 2017-06-02 15:42:06 +0100 | [diff] [blame] | 947 |       InterceptStatus status; | 
 | 948 |       tombstoned_intercept(pid, &intercept_fd, &output_fd, &status, kDebuggerdTombstone); | 
 | 949 |       ASSERT_EQ(InterceptStatus::kRegistered, status); | 
| Josh Gao | 352a845 | 2017-03-30 16:46:21 -0700 | [diff] [blame] | 950 |  | 
 | 951 |       // Pretend to crash, and then immediately close the socket. | 
 | 952 |       unique_fd sockfd(socket_local_client(kTombstonedCrashSocketName, | 
 | 953 |                                            ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_SEQPACKET)); | 
 | 954 |       if (sockfd == -1) { | 
 | 955 |         FAIL() << "failed to connect to tombstoned: " << strerror(errno); | 
 | 956 |       } | 
 | 957 |       TombstonedCrashPacket packet = {}; | 
 | 958 |       packet.packet_type = CrashPacketType::kDumpRequest; | 
 | 959 |       packet.packet.dump_request.pid = pid; | 
 | 960 |       if (TEMP_FAILURE_RETRY(write(sockfd, &packet, sizeof(packet))) != sizeof(packet)) { | 
 | 961 |         FAIL() << "failed to write to tombstoned: " << strerror(errno); | 
 | 962 |       } | 
 | 963 |  | 
 | 964 |       continue; | 
 | 965 |     } | 
 | 966 |   }); | 
 | 967 |  | 
 | 968 |   threads.emplace_back([&start]() { | 
 | 969 |     while (!start) { | 
 | 970 |       continue; | 
 | 971 |     } | 
 | 972 |  | 
 | 973 |     // Use a way out of range pid, to avoid stomping on an actual process. | 
 | 974 |     pid_t pid_base = 2'000'000; | 
 | 975 |  | 
 | 976 |     for (int dump = 0; dump < kDumpCount; ++dump) { | 
 | 977 |       pid_t pid = pid_base + dump; | 
 | 978 |  | 
 | 979 |       unique_fd intercept_fd, output_fd; | 
| Narayan Kamath | ca5e908 | 2017-06-02 15:42:06 +0100 | [diff] [blame] | 980 |       InterceptStatus status; | 
 | 981 |       tombstoned_intercept(pid, &intercept_fd, &output_fd, &status, kDebuggerdTombstone); | 
 | 982 |       ASSERT_EQ(InterceptStatus::kRegistered, status); | 
| Josh Gao | 352a845 | 2017-03-30 16:46:21 -0700 | [diff] [blame] | 983 |  | 
 | 984 |       { | 
 | 985 |         unique_fd tombstoned_socket, input_fd; | 
| Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 986 |         ASSERT_TRUE(tombstoned_connect(pid, &tombstoned_socket, &input_fd, kDebuggerdTombstone)); | 
| Josh Gao | 352a845 | 2017-03-30 16:46:21 -0700 | [diff] [blame] | 987 |         ASSERT_TRUE(android::base::WriteFully(input_fd.get(), &pid, sizeof(pid))); | 
 | 988 |         tombstoned_notify_completion(tombstoned_socket.get()); | 
 | 989 |       } | 
 | 990 |  | 
 | 991 |       // TODO: Fix the race that requires this sleep. | 
 | 992 |       std::this_thread::sleep_for(50ms); | 
 | 993 |  | 
 | 994 |       pid_t read_pid; | 
 | 995 |       ASSERT_TRUE(android::base::ReadFully(output_fd.get(), &read_pid, sizeof(read_pid))); | 
 | 996 |       ASSERT_EQ(read_pid, pid); | 
 | 997 |     } | 
 | 998 |   }); | 
 | 999 |  | 
 | 1000 |   start = true; | 
 | 1001 |  | 
 | 1002 |   for (std::thread& thread : threads) { | 
 | 1003 |     thread.join(); | 
 | 1004 |   } | 
 | 1005 | } | 
| Narayan Kamath | ca5e908 | 2017-06-02 15:42:06 +0100 | [diff] [blame] | 1006 |  | 
 | 1007 | TEST(tombstoned, java_trace_intercept_smoke) { | 
 | 1008 |   // Using a "real" PID is a little dangerous here - if the test fails | 
 | 1009 |   // or crashes, we might end up getting a bogus / unreliable stack | 
 | 1010 |   // trace. | 
 | 1011 |   const pid_t self = getpid(); | 
 | 1012 |  | 
 | 1013 |   unique_fd intercept_fd, output_fd; | 
 | 1014 |   InterceptStatus status; | 
 | 1015 |   tombstoned_intercept(self, &intercept_fd, &output_fd, &status, kDebuggerdJavaBacktrace); | 
 | 1016 |   ASSERT_EQ(InterceptStatus::kRegistered, status); | 
 | 1017 |  | 
 | 1018 |   // First connect to tombstoned requesting a native backtrace. This | 
 | 1019 |   // should result in a "regular" FD and not the installed intercept. | 
 | 1020 |   const char native[] = "native"; | 
 | 1021 |   unique_fd tombstoned_socket, input_fd; | 
 | 1022 |   ASSERT_TRUE(tombstoned_connect(self, &tombstoned_socket, &input_fd, kDebuggerdNativeBacktrace)); | 
 | 1023 |   ASSERT_TRUE(android::base::WriteFully(input_fd.get(), native, sizeof(native))); | 
 | 1024 |   tombstoned_notify_completion(tombstoned_socket.get()); | 
 | 1025 |  | 
 | 1026 |   // Then, connect to tombstoned asking for a java backtrace. This *should* | 
 | 1027 |   // trigger the intercept. | 
 | 1028 |   const char java[] = "java"; | 
 | 1029 |   ASSERT_TRUE(tombstoned_connect(self, &tombstoned_socket, &input_fd, kDebuggerdJavaBacktrace)); | 
 | 1030 |   ASSERT_TRUE(android::base::WriteFully(input_fd.get(), java, sizeof(java))); | 
 | 1031 |   tombstoned_notify_completion(tombstoned_socket.get()); | 
 | 1032 |  | 
 | 1033 |   char outbuf[sizeof(java)]; | 
 | 1034 |   ASSERT_TRUE(android::base::ReadFully(output_fd.get(), outbuf, sizeof(outbuf))); | 
 | 1035 |   ASSERT_STREQ("java", outbuf); | 
 | 1036 | } | 
 | 1037 |  | 
 | 1038 | TEST(tombstoned, multiple_intercepts) { | 
 | 1039 |   const pid_t fake_pid = 1'234'567; | 
 | 1040 |   unique_fd intercept_fd, output_fd; | 
 | 1041 |   InterceptStatus status; | 
 | 1042 |   tombstoned_intercept(fake_pid, &intercept_fd, &output_fd, &status, kDebuggerdJavaBacktrace); | 
 | 1043 |   ASSERT_EQ(InterceptStatus::kRegistered, status); | 
 | 1044 |  | 
 | 1045 |   unique_fd intercept_fd_2, output_fd_2; | 
 | 1046 |   tombstoned_intercept(fake_pid, &intercept_fd_2, &output_fd_2, &status, kDebuggerdNativeBacktrace); | 
 | 1047 |   ASSERT_EQ(InterceptStatus::kFailedAlreadyRegistered, status); | 
 | 1048 | } | 
 | 1049 |  | 
 | 1050 | TEST(tombstoned, intercept_any) { | 
 | 1051 |   const pid_t fake_pid = 1'234'567; | 
 | 1052 |  | 
 | 1053 |   unique_fd intercept_fd, output_fd; | 
 | 1054 |   InterceptStatus status; | 
 | 1055 |   tombstoned_intercept(fake_pid, &intercept_fd, &output_fd, &status, kDebuggerdNativeBacktrace); | 
 | 1056 |   ASSERT_EQ(InterceptStatus::kRegistered, status); | 
 | 1057 |  | 
 | 1058 |   const char any[] = "any"; | 
 | 1059 |   unique_fd tombstoned_socket, input_fd; | 
 | 1060 |   ASSERT_TRUE(tombstoned_connect(fake_pid, &tombstoned_socket, &input_fd, kDebuggerdAnyIntercept)); | 
 | 1061 |   ASSERT_TRUE(android::base::WriteFully(input_fd.get(), any, sizeof(any))); | 
 | 1062 |   tombstoned_notify_completion(tombstoned_socket.get()); | 
 | 1063 |  | 
 | 1064 |   char outbuf[sizeof(any)]; | 
 | 1065 |   ASSERT_TRUE(android::base::ReadFully(output_fd.get(), outbuf, sizeof(outbuf))); | 
 | 1066 |   ASSERT_STREQ("any", outbuf); | 
 | 1067 | } | 
| Josh Gao | 2b22ae1 | 2018-09-12 14:51:03 -0700 | [diff] [blame] | 1068 |  | 
 | 1069 | TEST(tombstoned, interceptless_backtrace) { | 
 | 1070 |   // Generate 50 backtraces, and then check to see that we haven't created 50 new tombstones. | 
 | 1071 |   auto get_tombstone_timestamps = []() -> std::map<int, time_t> { | 
 | 1072 |     std::map<int, time_t> result; | 
 | 1073 |     for (int i = 0; i < 99; ++i) { | 
 | 1074 |       std::string path = android::base::StringPrintf("/data/tombstones/tombstone_%02d", i); | 
 | 1075 |       struct stat st; | 
 | 1076 |       if (stat(path.c_str(), &st) == 0) { | 
 | 1077 |         result[i] = st.st_mtim.tv_sec; | 
 | 1078 |       } | 
 | 1079 |     } | 
 | 1080 |     return result; | 
 | 1081 |   }; | 
 | 1082 |  | 
 | 1083 |   auto before = get_tombstone_timestamps(); | 
 | 1084 |   for (int i = 0; i < 50; ++i) { | 
 | 1085 |     raise_debugger_signal(kDebuggerdNativeBacktrace); | 
 | 1086 |   } | 
 | 1087 |   auto after = get_tombstone_timestamps(); | 
 | 1088 |  | 
 | 1089 |   int diff = 0; | 
 | 1090 |   for (int i = 0; i < 99; ++i) { | 
 | 1091 |     if (after.count(i) == 0) { | 
 | 1092 |       continue; | 
 | 1093 |     } | 
 | 1094 |     if (before.count(i) == 0) { | 
 | 1095 |       ++diff; | 
 | 1096 |       continue; | 
 | 1097 |     } | 
 | 1098 |     if (before[i] != after[i]) { | 
 | 1099 |       ++diff; | 
 | 1100 |     } | 
 | 1101 |   } | 
 | 1102 |  | 
 | 1103 |   // We can't be sure that nothing's crash looping in the background. | 
 | 1104 |   // This should be good enough, though... | 
 | 1105 |   ASSERT_LT(diff, 10) << "too many new tombstones; is something crashing in the background?"; | 
 | 1106 | } | 
| Christopher Ferris | 481e837 | 2019-07-15 17:13:24 -0700 | [diff] [blame] | 1107 |  | 
 | 1108 | static __attribute__((__noinline__)) void overflow_stack(void* p) { | 
 | 1109 |   void* buf[1]; | 
 | 1110 |   buf[0] = p; | 
 | 1111 |   static volatile void* global = buf; | 
 | 1112 |   if (global) { | 
 | 1113 |     global = buf; | 
 | 1114 |     overflow_stack(&buf); | 
 | 1115 |   } | 
 | 1116 | } | 
 | 1117 |  | 
 | 1118 | TEST_F(CrasherTest, stack_overflow) { | 
 | 1119 |   int intercept_result; | 
 | 1120 |   unique_fd output_fd; | 
 | 1121 |   StartProcess([]() { overflow_stack(nullptr); }); | 
 | 1122 |  | 
 | 1123 |   StartIntercept(&output_fd); | 
 | 1124 |   FinishCrasher(); | 
 | 1125 |   AssertDeath(SIGSEGV); | 
 | 1126 |   FinishIntercept(&intercept_result); | 
 | 1127 |  | 
 | 1128 |   ASSERT_EQ(1, intercept_result) << "tombstoned reported failure"; | 
 | 1129 |  | 
 | 1130 |   std::string result; | 
 | 1131 |   ConsumeFd(std::move(output_fd), &result); | 
 | 1132 |   ASSERT_MATCH(result, R"(Cause: stack pointer[^\n]*stack overflow.\n)"); | 
 | 1133 | } |