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