| 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> | 
|  | 19 | #include <unistd.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 | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 22 | #include <sys/types.h> | 
|  | 23 |  | 
|  | 24 | #include <chrono> | 
|  | 25 | #include <regex> | 
|  | 26 | #include <thread> | 
|  | 27 |  | 
| Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 28 | #include <android/set_abort_message.h> | 
|  | 29 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 30 | #include <android-base/file.h> | 
|  | 31 | #include <android-base/logging.h> | 
| Josh Gao | 2e7b8e2 | 2017-05-04 17:12:57 -0700 | [diff] [blame] | 32 | #include <android-base/macros.h> | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 33 | #include <android-base/parseint.h> | 
|  | 34 | #include <android-base/properties.h> | 
|  | 35 | #include <android-base/strings.h> | 
|  | 36 | #include <android-base/unique_fd.h> | 
|  | 37 | #include <cutils/sockets.h> | 
|  | 38 | #include <debuggerd/handler.h> | 
|  | 39 | #include <debuggerd/protocol.h> | 
| Josh Gao | 352a845 | 2017-03-30 16:46:21 -0700 | [diff] [blame] | 40 | #include <debuggerd/tombstoned.h> | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 41 | #include <debuggerd/util.h> | 
|  | 42 | #include <gtest/gtest.h> | 
|  | 43 |  | 
|  | 44 | using namespace std::chrono_literals; | 
|  | 45 | using android::base::unique_fd; | 
|  | 46 |  | 
|  | 47 | #if defined(__LP64__) | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 48 | #define ARCH_SUFFIX "64" | 
|  | 49 | #else | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 50 | #define ARCH_SUFFIX "" | 
|  | 51 | #endif | 
|  | 52 |  | 
|  | 53 | constexpr char kWaitForGdbKey[] = "debug.debuggerd.wait_for_gdb"; | 
|  | 54 |  | 
|  | 55 | #define TIMEOUT(seconds, expr)                                     \ | 
|  | 56 | [&]() {                                                          \ | 
|  | 57 | struct sigaction old_sigaction;                                \ | 
|  | 58 | struct sigaction new_sigaction = {};                           \ | 
|  | 59 | new_sigaction.sa_handler = [](int) {};                         \ | 
|  | 60 | if (sigaction(SIGALRM, &new_sigaction, &new_sigaction) != 0) { \ | 
|  | 61 | err(1, "sigaction failed");                                  \ | 
|  | 62 | }                                                              \ | 
|  | 63 | alarm(seconds);                                                \ | 
|  | 64 | auto value = expr;                                             \ | 
|  | 65 | int saved_errno = errno;                                       \ | 
|  | 66 | if (sigaction(SIGALRM, &old_sigaction, nullptr) != 0) {        \ | 
|  | 67 | err(1, "sigaction failed");                                  \ | 
|  | 68 | }                                                              \ | 
|  | 69 | alarm(0);                                                      \ | 
|  | 70 | errno = saved_errno;                                           \ | 
|  | 71 | return value;                                                  \ | 
|  | 72 | }() | 
|  | 73 |  | 
|  | 74 | #define ASSERT_MATCH(str, pattern)                                              \ | 
|  | 75 | do {                                                                          \ | 
|  | 76 | std::regex r((pattern));                                                    \ | 
|  | 77 | if (!std::regex_search((str), r)) {                                         \ | 
|  | 78 | FAIL() << "regex mismatch: expected " << (pattern) << " in: \n" << (str); \ | 
|  | 79 | }                                                                           \ | 
|  | 80 | } while (0) | 
|  | 81 |  | 
| Josh Gao | e06f2a4 | 2017-04-27 16:50:38 -0700 | [diff] [blame] | 82 | #define ASSERT_NOT_MATCH(str, pattern)                                                      \ | 
|  | 83 | do {                                                                                      \ | 
|  | 84 | std::regex r((pattern));                                                                \ | 
|  | 85 | if (std::regex_search((str), r)) {                                                      \ | 
|  | 86 | FAIL() << "regex mismatch: expected to not find " << (pattern) << " in: \n" << (str); \ | 
|  | 87 | }                                                                                       \ | 
|  | 88 | } while (0) | 
|  | 89 |  | 
| Josh Gao | 460b336 | 2017-03-30 16:40:47 -0700 | [diff] [blame] | 90 | static void tombstoned_intercept(pid_t target_pid, unique_fd* intercept_fd, unique_fd* output_fd) { | 
|  | 91 | intercept_fd->reset(socket_local_client(kTombstonedInterceptSocketName, | 
|  | 92 | ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_SEQPACKET)); | 
|  | 93 | if (intercept_fd->get() == -1) { | 
|  | 94 | FAIL() << "failed to contact tombstoned: " << strerror(errno); | 
|  | 95 | } | 
|  | 96 |  | 
|  | 97 | InterceptRequest req = {.pid = target_pid}; | 
|  | 98 |  | 
|  | 99 | unique_fd output_pipe_write; | 
|  | 100 | if (!Pipe(output_fd, &output_pipe_write)) { | 
|  | 101 | FAIL() << "failed to create output pipe: " << strerror(errno); | 
|  | 102 | } | 
|  | 103 |  | 
|  | 104 | std::string pipe_size_str; | 
|  | 105 | int pipe_buffer_size; | 
|  | 106 | if (!android::base::ReadFileToString("/proc/sys/fs/pipe-max-size", &pipe_size_str)) { | 
|  | 107 | FAIL() << "failed to read /proc/sys/fs/pipe-max-size: " << strerror(errno); | 
|  | 108 | } | 
|  | 109 |  | 
|  | 110 | pipe_size_str = android::base::Trim(pipe_size_str); | 
|  | 111 |  | 
|  | 112 | if (!android::base::ParseInt(pipe_size_str.c_str(), &pipe_buffer_size, 0)) { | 
|  | 113 | FAIL() << "failed to parse pipe max size"; | 
|  | 114 | } | 
|  | 115 |  | 
|  | 116 | if (fcntl(output_fd->get(), F_SETPIPE_SZ, pipe_buffer_size) != pipe_buffer_size) { | 
|  | 117 | FAIL() << "failed to set pipe size: " << strerror(errno); | 
|  | 118 | } | 
|  | 119 |  | 
|  | 120 | if (send_fd(intercept_fd->get(), &req, sizeof(req), std::move(output_pipe_write)) != sizeof(req)) { | 
|  | 121 | FAIL() << "failed to send output fd to tombstoned: " << strerror(errno); | 
|  | 122 | } | 
|  | 123 |  | 
|  | 124 | InterceptResponse response; | 
|  | 125 | ssize_t rc = TEMP_FAILURE_RETRY(read(intercept_fd->get(), &response, sizeof(response))); | 
|  | 126 | if (rc == -1) { | 
|  | 127 | FAIL() << "failed to read response from tombstoned: " << strerror(errno); | 
|  | 128 | } else if (rc == 0) { | 
|  | 129 | FAIL() << "failed to read response from tombstoned (EOF)"; | 
|  | 130 | } else if (rc != sizeof(response)) { | 
|  | 131 | FAIL() << "received packet of unexpected length from tombstoned: expected " << sizeof(response) | 
|  | 132 | << ", received " << rc; | 
|  | 133 | } | 
|  | 134 |  | 
|  | 135 | ASSERT_EQ(InterceptStatus::kRegistered, response.status); | 
|  | 136 | } | 
|  | 137 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 138 | class CrasherTest : public ::testing::Test { | 
|  | 139 | public: | 
|  | 140 | pid_t crasher_pid = -1; | 
|  | 141 | bool previous_wait_for_gdb; | 
|  | 142 | unique_fd crasher_pipe; | 
|  | 143 | unique_fd intercept_fd; | 
|  | 144 |  | 
|  | 145 | CrasherTest(); | 
|  | 146 | ~CrasherTest(); | 
|  | 147 |  | 
|  | 148 | void StartIntercept(unique_fd* output_fd); | 
|  | 149 |  | 
|  | 150 | // Returns -1 if we fail to read a response from tombstoned, otherwise the received return code. | 
|  | 151 | void FinishIntercept(int* result); | 
|  | 152 |  | 
| Josh Gao | 2e7b8e2 | 2017-05-04 17:12:57 -0700 | [diff] [blame] | 153 | void StartProcess(std::function<void()> function, std::function<pid_t()> forker = fork); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 154 | void StartCrasher(const std::string& crash_type); | 
|  | 155 | void FinishCrasher(); | 
|  | 156 | void AssertDeath(int signo); | 
|  | 157 | }; | 
|  | 158 |  | 
|  | 159 | CrasherTest::CrasherTest() { | 
|  | 160 | previous_wait_for_gdb = android::base::GetBoolProperty(kWaitForGdbKey, false); | 
|  | 161 | android::base::SetProperty(kWaitForGdbKey, "0"); | 
|  | 162 | } | 
|  | 163 |  | 
|  | 164 | CrasherTest::~CrasherTest() { | 
|  | 165 | if (crasher_pid != -1) { | 
|  | 166 | kill(crasher_pid, SIGKILL); | 
|  | 167 | int status; | 
|  | 168 | waitpid(crasher_pid, &status, WUNTRACED); | 
|  | 169 | } | 
|  | 170 |  | 
|  | 171 | android::base::SetProperty(kWaitForGdbKey, previous_wait_for_gdb ? "1" : "0"); | 
|  | 172 | } | 
|  | 173 |  | 
|  | 174 | void CrasherTest::StartIntercept(unique_fd* output_fd) { | 
|  | 175 | if (crasher_pid == -1) { | 
|  | 176 | FAIL() << "crasher hasn't been started"; | 
|  | 177 | } | 
|  | 178 |  | 
| Josh Gao | 460b336 | 2017-03-30 16:40:47 -0700 | [diff] [blame] | 179 | tombstoned_intercept(crasher_pid, &this->intercept_fd, output_fd); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 180 | } | 
|  | 181 |  | 
|  | 182 | void CrasherTest::FinishIntercept(int* result) { | 
|  | 183 | InterceptResponse response; | 
|  | 184 |  | 
|  | 185 | // Timeout for tombstoned intercept is 10 seconds. | 
|  | 186 | ssize_t rc = TIMEOUT(20, read(intercept_fd.get(), &response, sizeof(response))); | 
|  | 187 | if (rc == -1) { | 
|  | 188 | FAIL() << "failed to read response from tombstoned: " << strerror(errno); | 
|  | 189 | } else if (rc == 0) { | 
|  | 190 | *result = -1; | 
|  | 191 | } else if (rc != sizeof(response)) { | 
|  | 192 | FAIL() << "received packet of unexpected length from tombstoned: expected " << sizeof(response) | 
|  | 193 | << ", received " << rc; | 
|  | 194 | } else { | 
| Josh Gao | 460b336 | 2017-03-30 16:40:47 -0700 | [diff] [blame] | 195 | *result = response.status == InterceptStatus::kStarted ? 1 : 0; | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 196 | } | 
|  | 197 | } | 
|  | 198 |  | 
| Josh Gao | 2e7b8e2 | 2017-05-04 17:12:57 -0700 | [diff] [blame] | 199 | void CrasherTest::StartProcess(std::function<void()> function, std::function<pid_t()> forker) { | 
| Josh Gao | fca7ca3 | 2017-01-23 12:05:35 -0800 | [diff] [blame] | 200 | unique_fd read_pipe; | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 201 | unique_fd crasher_read_pipe; | 
|  | 202 | if (!Pipe(&crasher_read_pipe, &crasher_pipe)) { | 
|  | 203 | FAIL() << "failed to create pipe: " << strerror(errno); | 
|  | 204 | } | 
|  | 205 |  | 
| Josh Gao | 2e7b8e2 | 2017-05-04 17:12:57 -0700 | [diff] [blame] | 206 | crasher_pid = forker(); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 207 | if (crasher_pid == -1) { | 
|  | 208 | FAIL() << "fork failed: " << strerror(errno); | 
|  | 209 | } else if (crasher_pid == 0) { | 
| Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 210 | char dummy; | 
|  | 211 | crasher_pipe.reset(); | 
|  | 212 | TEMP_FAILURE_RETRY(read(crasher_read_pipe.get(), &dummy, 1)); | 
| Josh Gao | fca7ca3 | 2017-01-23 12:05:35 -0800 | [diff] [blame] | 213 | function(); | 
|  | 214 | _exit(0); | 
|  | 215 | } | 
|  | 216 | } | 
|  | 217 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 218 | void CrasherTest::FinishCrasher() { | 
|  | 219 | if (crasher_pipe == -1) { | 
|  | 220 | FAIL() << "crasher pipe uninitialized"; | 
|  | 221 | } | 
|  | 222 |  | 
|  | 223 | ssize_t rc = write(crasher_pipe.get(), "\n", 1); | 
|  | 224 | if (rc == -1) { | 
|  | 225 | FAIL() << "failed to write to crasher pipe: " << strerror(errno); | 
|  | 226 | } else if (rc == 0) { | 
|  | 227 | FAIL() << "crasher pipe was closed"; | 
|  | 228 | } | 
|  | 229 | } | 
|  | 230 |  | 
|  | 231 | void CrasherTest::AssertDeath(int signo) { | 
|  | 232 | int status; | 
|  | 233 | pid_t pid = TIMEOUT(5, waitpid(crasher_pid, &status, 0)); | 
|  | 234 | if (pid != crasher_pid) { | 
|  | 235 | FAIL() << "failed to wait for crasher: " << strerror(errno); | 
|  | 236 | } | 
|  | 237 |  | 
| Josh Gao | e06f2a4 | 2017-04-27 16:50:38 -0700 | [diff] [blame] | 238 | if (signo == 0) { | 
|  | 239 | ASSERT_TRUE(WIFEXITED(status)); | 
|  | 240 | ASSERT_EQ(0, WEXITSTATUS(signo)); | 
|  | 241 | } else { | 
|  | 242 | ASSERT_FALSE(WIFEXITED(status)); | 
|  | 243 | ASSERT_TRUE(WIFSIGNALED(status)) << "crasher didn't terminate via a signal"; | 
|  | 244 | ASSERT_EQ(signo, WTERMSIG(status)); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 245 | } | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 246 | crasher_pid = -1; | 
|  | 247 | } | 
|  | 248 |  | 
|  | 249 | static void ConsumeFd(unique_fd fd, std::string* output) { | 
|  | 250 | constexpr size_t read_length = PAGE_SIZE; | 
|  | 251 | std::string result; | 
|  | 252 |  | 
|  | 253 | while (true) { | 
|  | 254 | size_t offset = result.size(); | 
|  | 255 | result.resize(result.size() + PAGE_SIZE); | 
|  | 256 | ssize_t rc = TEMP_FAILURE_RETRY(read(fd.get(), &result[offset], read_length)); | 
|  | 257 | if (rc == -1) { | 
|  | 258 | FAIL() << "read failed: " << strerror(errno); | 
|  | 259 | } else if (rc == 0) { | 
|  | 260 | result.resize(result.size() - PAGE_SIZE); | 
|  | 261 | break; | 
|  | 262 | } | 
|  | 263 |  | 
|  | 264 | result.resize(result.size() - PAGE_SIZE + rc); | 
|  | 265 | } | 
|  | 266 |  | 
|  | 267 | *output = std::move(result); | 
|  | 268 | } | 
|  | 269 |  | 
|  | 270 | TEST_F(CrasherTest, smoke) { | 
|  | 271 | int intercept_result; | 
|  | 272 | unique_fd output_fd; | 
| Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 273 | StartProcess([]() { | 
|  | 274 | *reinterpret_cast<volatile char*>(0xdead) = '1'; | 
|  | 275 | }); | 
|  | 276 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 277 | StartIntercept(&output_fd); | 
|  | 278 | FinishCrasher(); | 
|  | 279 | AssertDeath(SIGSEGV); | 
|  | 280 | FinishIntercept(&intercept_result); | 
|  | 281 |  | 
|  | 282 | ASSERT_EQ(1, intercept_result) << "tombstoned reported failure"; | 
|  | 283 |  | 
|  | 284 | std::string result; | 
|  | 285 | ConsumeFd(std::move(output_fd), &result); | 
|  | 286 | ASSERT_MATCH(result, R"(signal 11 \(SIGSEGV\), code 1 \(SEGV_MAPERR\), fault addr 0xdead)"); | 
|  | 287 | } | 
|  | 288 |  | 
|  | 289 | TEST_F(CrasherTest, abort) { | 
|  | 290 | int intercept_result; | 
|  | 291 | unique_fd output_fd; | 
| Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 292 | StartProcess([]() { | 
|  | 293 | abort(); | 
|  | 294 | }); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 295 | StartIntercept(&output_fd); | 
|  | 296 | FinishCrasher(); | 
|  | 297 | AssertDeath(SIGABRT); | 
|  | 298 | FinishIntercept(&intercept_result); | 
|  | 299 |  | 
|  | 300 | ASSERT_EQ(1, intercept_result) << "tombstoned reported failure"; | 
|  | 301 |  | 
|  | 302 | std::string result; | 
|  | 303 | ConsumeFd(std::move(output_fd), &result); | 
|  | 304 | ASSERT_MATCH(result, R"(#00 pc [0-9a-f]+\s+ /system/lib)" ARCH_SUFFIX R"(/libc.so \(tgkill)"); | 
|  | 305 | } | 
|  | 306 |  | 
|  | 307 | TEST_F(CrasherTest, signal) { | 
|  | 308 | int intercept_result; | 
|  | 309 | unique_fd output_fd; | 
| Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 310 | StartProcess([]() { | 
|  | 311 | abort(); | 
|  | 312 | }); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 313 | StartIntercept(&output_fd); | 
|  | 314 |  | 
|  | 315 | // Wait for a bit, or we might end up killing the process before the signal | 
|  | 316 | // handler even gets a chance to be registered. | 
|  | 317 | std::this_thread::sleep_for(100ms); | 
|  | 318 | ASSERT_EQ(0, kill(crasher_pid, SIGSEGV)); | 
|  | 319 |  | 
|  | 320 | AssertDeath(SIGSEGV); | 
|  | 321 | FinishIntercept(&intercept_result); | 
|  | 322 |  | 
|  | 323 | ASSERT_EQ(1, intercept_result) << "tombstoned reported failure"; | 
|  | 324 |  | 
|  | 325 | std::string result; | 
|  | 326 | ConsumeFd(std::move(output_fd), &result); | 
|  | 327 | ASSERT_MATCH(result, R"(signal 11 \(SIGSEGV\), code 0 \(SI_USER\), fault addr --------)"); | 
|  | 328 | ASSERT_MATCH(result, R"(backtrace:)"); | 
|  | 329 | } | 
|  | 330 |  | 
|  | 331 | TEST_F(CrasherTest, abort_message) { | 
|  | 332 | int intercept_result; | 
|  | 333 | unique_fd output_fd; | 
| Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 334 | StartProcess([]() { | 
|  | 335 | android_set_abort_message("abort message goes here"); | 
|  | 336 | abort(); | 
|  | 337 | }); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 338 | StartIntercept(&output_fd); | 
|  | 339 | FinishCrasher(); | 
|  | 340 | AssertDeath(SIGABRT); | 
|  | 341 | FinishIntercept(&intercept_result); | 
|  | 342 |  | 
|  | 343 | ASSERT_EQ(1, intercept_result) << "tombstoned reported failure"; | 
|  | 344 |  | 
|  | 345 | std::string result; | 
|  | 346 | ConsumeFd(std::move(output_fd), &result); | 
| Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 347 | ASSERT_MATCH(result, R"(Abort message: 'abort message goes here')"); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 348 | } | 
|  | 349 |  | 
| Josh Gao | e06f2a4 | 2017-04-27 16:50:38 -0700 | [diff] [blame] | 350 | TEST_F(CrasherTest, abort_message_backtrace) { | 
|  | 351 | int intercept_result; | 
|  | 352 | unique_fd output_fd; | 
|  | 353 | StartProcess([]() { | 
|  | 354 | android_set_abort_message("not actually aborting"); | 
|  | 355 | raise(DEBUGGER_SIGNAL); | 
|  | 356 | exit(0); | 
|  | 357 | }); | 
|  | 358 | StartIntercept(&output_fd); | 
|  | 359 | FinishCrasher(); | 
|  | 360 | AssertDeath(0); | 
|  | 361 | FinishIntercept(&intercept_result); | 
|  | 362 |  | 
|  | 363 | ASSERT_EQ(1, intercept_result) << "tombstoned reported failure"; | 
|  | 364 |  | 
|  | 365 | std::string result; | 
|  | 366 | ConsumeFd(std::move(output_fd), &result); | 
|  | 367 | ASSERT_NOT_MATCH(result, R"(Abort message:)"); | 
|  | 368 | } | 
|  | 369 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 370 | TEST_F(CrasherTest, intercept_timeout) { | 
|  | 371 | int intercept_result; | 
|  | 372 | unique_fd output_fd; | 
| Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 373 | StartProcess([]() { | 
|  | 374 | abort(); | 
|  | 375 | }); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 376 | StartIntercept(&output_fd); | 
|  | 377 |  | 
|  | 378 | // Don't let crasher finish until we timeout. | 
|  | 379 | FinishIntercept(&intercept_result); | 
|  | 380 |  | 
|  | 381 | ASSERT_NE(1, intercept_result) << "tombstoned reported success? (intercept_result = " | 
|  | 382 | << intercept_result << ")"; | 
|  | 383 |  | 
|  | 384 | FinishCrasher(); | 
|  | 385 | AssertDeath(SIGABRT); | 
|  | 386 | } | 
|  | 387 |  | 
|  | 388 | TEST_F(CrasherTest, wait_for_gdb) { | 
|  | 389 | if (!android::base::SetProperty(kWaitForGdbKey, "1")) { | 
|  | 390 | FAIL() << "failed to enable wait_for_gdb"; | 
|  | 391 | } | 
|  | 392 | sleep(1); | 
|  | 393 |  | 
| Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 394 | StartProcess([]() { | 
|  | 395 | abort(); | 
|  | 396 | }); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 397 | FinishCrasher(); | 
|  | 398 |  | 
|  | 399 | int status; | 
|  | 400 | ASSERT_EQ(crasher_pid, waitpid(crasher_pid, &status, WUNTRACED)); | 
|  | 401 | ASSERT_TRUE(WIFSTOPPED(status)); | 
|  | 402 | ASSERT_EQ(SIGSTOP, WSTOPSIG(status)); | 
|  | 403 |  | 
|  | 404 | ASSERT_EQ(0, kill(crasher_pid, SIGCONT)); | 
|  | 405 |  | 
|  | 406 | AssertDeath(SIGABRT); | 
|  | 407 | } | 
|  | 408 |  | 
| Josh Gao | 7c6e313 | 2017-01-22 17:59:02 -0800 | [diff] [blame] | 409 | // wait_for_gdb shouldn't trigger on manually sent signals. | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 410 | TEST_F(CrasherTest, wait_for_gdb_signal) { | 
|  | 411 | if (!android::base::SetProperty(kWaitForGdbKey, "1")) { | 
|  | 412 | FAIL() << "failed to enable wait_for_gdb"; | 
|  | 413 | } | 
|  | 414 |  | 
| Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 415 | StartProcess([]() { | 
|  | 416 | abort(); | 
|  | 417 | }); | 
| Josh Gao | 7c6e313 | 2017-01-22 17:59:02 -0800 | [diff] [blame] | 418 | ASSERT_EQ(0, kill(crasher_pid, SIGSEGV)) << strerror(errno); | 
|  | 419 | AssertDeath(SIGSEGV); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 420 | } | 
|  | 421 |  | 
|  | 422 | TEST_F(CrasherTest, backtrace) { | 
|  | 423 | std::string result; | 
|  | 424 | int intercept_result; | 
|  | 425 | unique_fd output_fd; | 
| Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 426 |  | 
|  | 427 | StartProcess([]() { | 
|  | 428 | abort(); | 
|  | 429 | }); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 430 | StartIntercept(&output_fd); | 
|  | 431 |  | 
|  | 432 | std::this_thread::sleep_for(500ms); | 
|  | 433 |  | 
|  | 434 | sigval val; | 
|  | 435 | val.sival_int = 1; | 
|  | 436 | ASSERT_EQ(0, sigqueue(crasher_pid, DEBUGGER_SIGNAL, val)) << strerror(errno); | 
|  | 437 | FinishIntercept(&intercept_result); | 
|  | 438 | ASSERT_EQ(1, intercept_result) << "tombstoned reported failure"; | 
|  | 439 | ConsumeFd(std::move(output_fd), &result); | 
|  | 440 | ASSERT_MATCH(result, R"(#00 pc [0-9a-f]+  /system/lib)" ARCH_SUFFIX R"(/libc.so \(read\+)"); | 
|  | 441 |  | 
|  | 442 | int status; | 
|  | 443 | ASSERT_EQ(0, waitpid(crasher_pid, &status, WNOHANG | WUNTRACED)); | 
|  | 444 |  | 
|  | 445 | StartIntercept(&output_fd); | 
|  | 446 | FinishCrasher(); | 
|  | 447 | AssertDeath(SIGABRT); | 
|  | 448 | FinishIntercept(&intercept_result); | 
|  | 449 | ASSERT_EQ(1, intercept_result) << "tombstoned reported failure"; | 
|  | 450 | ConsumeFd(std::move(output_fd), &result); | 
|  | 451 | ASSERT_MATCH(result, R"(#00 pc [0-9a-f]+\s+ /system/lib)" ARCH_SUFFIX R"(/libc.so \(tgkill)"); | 
|  | 452 | } | 
| Josh Gao | fca7ca3 | 2017-01-23 12:05:35 -0800 | [diff] [blame] | 453 |  | 
|  | 454 | TEST_F(CrasherTest, PR_SET_DUMPABLE_0_crash) { | 
| Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 455 | int intercept_result; | 
|  | 456 | unique_fd output_fd; | 
| Josh Gao | fca7ca3 | 2017-01-23 12:05:35 -0800 | [diff] [blame] | 457 | StartProcess([]() { | 
|  | 458 | prctl(PR_SET_DUMPABLE, 0); | 
| Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 459 | abort(); | 
| Josh Gao | fca7ca3 | 2017-01-23 12:05:35 -0800 | [diff] [blame] | 460 | }); | 
| Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 461 |  | 
|  | 462 | StartIntercept(&output_fd); | 
|  | 463 | FinishCrasher(); | 
|  | 464 | AssertDeath(SIGABRT); | 
|  | 465 | FinishIntercept(&intercept_result); | 
|  | 466 |  | 
|  | 467 | ASSERT_EQ(1, intercept_result) << "tombstoned reported failure"; | 
|  | 468 |  | 
|  | 469 | std::string result; | 
|  | 470 | ConsumeFd(std::move(output_fd), &result); | 
|  | 471 | ASSERT_MATCH(result, R"(#00 pc [0-9a-f]+\s+ /system/lib)" ARCH_SUFFIX R"(/libc.so \(tgkill)"); | 
| Josh Gao | fca7ca3 | 2017-01-23 12:05:35 -0800 | [diff] [blame] | 472 | } | 
|  | 473 |  | 
| Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 474 | TEST_F(CrasherTest, capabilities) { | 
|  | 475 | ASSERT_EQ(0U, getuid()) << "capability test requires root"; | 
|  | 476 |  | 
| Josh Gao | fca7ca3 | 2017-01-23 12:05:35 -0800 | [diff] [blame] | 477 | StartProcess([]() { | 
| Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 478 | if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) != 0) { | 
|  | 479 | err(1, "failed to set PR_SET_KEEPCAPS"); | 
|  | 480 | } | 
|  | 481 |  | 
|  | 482 | if (setresuid(1, 1, 1) != 0) { | 
|  | 483 | err(1, "setresuid failed"); | 
|  | 484 | } | 
|  | 485 |  | 
|  | 486 | __user_cap_header_struct capheader; | 
|  | 487 | __user_cap_data_struct capdata[2]; | 
|  | 488 | memset(&capheader, 0, sizeof(capheader)); | 
|  | 489 | memset(&capdata, 0, sizeof(capdata)); | 
|  | 490 |  | 
|  | 491 | capheader.version = _LINUX_CAPABILITY_VERSION_3; | 
|  | 492 | capheader.pid = 0; | 
|  | 493 |  | 
|  | 494 | // Turn on every third capability. | 
|  | 495 | static_assert(CAP_LAST_CAP > 33, "CAP_LAST_CAP <= 32"); | 
|  | 496 | for (int i = 0; i < CAP_LAST_CAP; i += 3) { | 
|  | 497 | capdata[CAP_TO_INDEX(i)].permitted |= CAP_TO_MASK(i); | 
|  | 498 | capdata[CAP_TO_INDEX(i)].effective |= CAP_TO_MASK(i); | 
|  | 499 | } | 
|  | 500 |  | 
|  | 501 | // Make sure CAP_SYS_PTRACE is off. | 
|  | 502 | capdata[CAP_TO_INDEX(CAP_SYS_PTRACE)].permitted &= ~(CAP_TO_MASK(CAP_SYS_PTRACE)); | 
|  | 503 | capdata[CAP_TO_INDEX(CAP_SYS_PTRACE)].effective &= ~(CAP_TO_MASK(CAP_SYS_PTRACE)); | 
|  | 504 |  | 
|  | 505 | if (capset(&capheader, &capdata[0]) != 0) { | 
|  | 506 | err(1, "capset failed"); | 
|  | 507 | } | 
|  | 508 |  | 
|  | 509 | if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) != 0) { | 
|  | 510 | err(1, "failed to drop ambient capabilities"); | 
|  | 511 | } | 
|  | 512 |  | 
| Josh Gao | a5199a9 | 2017-04-03 13:18:34 -0700 | [diff] [blame] | 513 | pthread_setname_np(pthread_self(), "thread_name"); | 
| Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 514 | raise(SIGSYS); | 
| Josh Gao | fca7ca3 | 2017-01-23 12:05:35 -0800 | [diff] [blame] | 515 | }); | 
| Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 516 |  | 
|  | 517 | unique_fd output_fd; | 
|  | 518 | StartIntercept(&output_fd); | 
|  | 519 | FinishCrasher(); | 
|  | 520 | AssertDeath(SIGSYS); | 
|  | 521 |  | 
|  | 522 | std::string result; | 
|  | 523 | int intercept_result; | 
|  | 524 | FinishIntercept(&intercept_result); | 
|  | 525 | ASSERT_EQ(1, intercept_result) << "tombstoned reported failure"; | 
|  | 526 | ConsumeFd(std::move(output_fd), &result); | 
| Josh Gao | a5199a9 | 2017-04-03 13:18:34 -0700 | [diff] [blame] | 527 | ASSERT_MATCH(result, R"(name: thread_name\s+>>> .+debuggerd_test(32|64) <<<)"); | 
| Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 528 | ASSERT_MATCH(result, R"(#00 pc [0-9a-f]+\s+ /system/lib)" ARCH_SUFFIX R"(/libc.so \(tgkill)"); | 
| Josh Gao | fca7ca3 | 2017-01-23 12:05:35 -0800 | [diff] [blame] | 529 | } | 
| Josh Gao | c3c8c02 | 2017-02-13 16:36:18 -0800 | [diff] [blame] | 530 |  | 
| Josh Gao | 2e7b8e2 | 2017-05-04 17:12:57 -0700 | [diff] [blame] | 531 | TEST_F(CrasherTest, fake_pid) { | 
|  | 532 | int intercept_result; | 
|  | 533 | unique_fd output_fd; | 
|  | 534 |  | 
|  | 535 | // Prime the getpid/gettid caches. | 
|  | 536 | UNUSED(getpid()); | 
|  | 537 | UNUSED(gettid()); | 
|  | 538 |  | 
|  | 539 | std::function<pid_t()> clone_fn = []() { | 
|  | 540 | return syscall(__NR_clone, SIGCHLD, nullptr, nullptr, nullptr, nullptr); | 
|  | 541 | }; | 
|  | 542 | StartProcess( | 
|  | 543 | []() { | 
|  | 544 | ASSERT_NE(getpid(), syscall(__NR_getpid)); | 
|  | 545 | ASSERT_NE(gettid(), syscall(__NR_gettid)); | 
|  | 546 | raise(SIGSEGV); | 
|  | 547 | }, | 
|  | 548 | clone_fn); | 
|  | 549 |  | 
|  | 550 | StartIntercept(&output_fd); | 
|  | 551 | FinishCrasher(); | 
|  | 552 | AssertDeath(SIGSEGV); | 
|  | 553 | FinishIntercept(&intercept_result); | 
|  | 554 |  | 
|  | 555 | ASSERT_EQ(1, intercept_result) << "tombstoned reported failure"; | 
|  | 556 |  | 
|  | 557 | std::string result; | 
|  | 558 | ConsumeFd(std::move(output_fd), &result); | 
|  | 559 | ASSERT_MATCH(result, R"(#00 pc [0-9a-f]+\s+ /system/lib)" ARCH_SUFFIX R"(/libc.so \(tgkill)"); | 
|  | 560 | } | 
|  | 561 |  | 
| Josh Gao | c3c8c02 | 2017-02-13 16:36:18 -0800 | [diff] [blame] | 562 | TEST(crash_dump, zombie) { | 
|  | 563 | pid_t forkpid = fork(); | 
|  | 564 |  | 
| Josh Gao | c3c8c02 | 2017-02-13 16:36:18 -0800 | [diff] [blame] | 565 | pid_t rc; | 
|  | 566 | int status; | 
|  | 567 |  | 
|  | 568 | if (forkpid == 0) { | 
|  | 569 | errno = 0; | 
|  | 570 | rc = waitpid(-1, &status, WNOHANG | __WALL | __WNOTHREAD); | 
|  | 571 | if (rc != -1 || errno != ECHILD) { | 
|  | 572 | errx(2, "first waitpid returned %d (%s), expected failure with ECHILD", rc, strerror(errno)); | 
|  | 573 | } | 
|  | 574 |  | 
|  | 575 | raise(DEBUGGER_SIGNAL); | 
|  | 576 |  | 
|  | 577 | errno = 0; | 
|  | 578 | rc = waitpid(-1, &status, __WALL | __WNOTHREAD); | 
|  | 579 | if (rc != -1 || errno != ECHILD) { | 
|  | 580 | errx(2, "second waitpid returned %d (%s), expected failure with ECHILD", rc, strerror(errno)); | 
|  | 581 | } | 
|  | 582 | _exit(0); | 
|  | 583 | } else { | 
|  | 584 | rc = waitpid(forkpid, &status, 0); | 
|  | 585 | ASSERT_EQ(forkpid, rc); | 
|  | 586 | ASSERT_TRUE(WIFEXITED(status)); | 
|  | 587 | ASSERT_EQ(0, WEXITSTATUS(status)); | 
|  | 588 | } | 
|  | 589 | } | 
| Josh Gao | 352a845 | 2017-03-30 16:46:21 -0700 | [diff] [blame] | 590 |  | 
|  | 591 | TEST(tombstoned, no_notify) { | 
|  | 592 | // Do this a few times. | 
|  | 593 | for (int i = 0; i < 3; ++i) { | 
|  | 594 | pid_t pid = 123'456'789 + i; | 
|  | 595 |  | 
|  | 596 | unique_fd intercept_fd, output_fd; | 
|  | 597 | tombstoned_intercept(pid, &intercept_fd, &output_fd); | 
|  | 598 |  | 
|  | 599 | { | 
|  | 600 | unique_fd tombstoned_socket, input_fd; | 
|  | 601 | ASSERT_TRUE(tombstoned_connect(pid, &tombstoned_socket, &input_fd)); | 
|  | 602 | ASSERT_TRUE(android::base::WriteFully(input_fd.get(), &pid, sizeof(pid))); | 
|  | 603 | } | 
|  | 604 |  | 
|  | 605 | pid_t read_pid; | 
|  | 606 | ASSERT_TRUE(android::base::ReadFully(output_fd.get(), &read_pid, sizeof(read_pid))); | 
|  | 607 | ASSERT_EQ(read_pid, pid); | 
|  | 608 | } | 
|  | 609 | } | 
|  | 610 |  | 
|  | 611 | TEST(tombstoned, stress) { | 
|  | 612 | // Spawn threads to simultaneously do a bunch of failing dumps and a bunch of successful dumps. | 
|  | 613 | static constexpr int kDumpCount = 100; | 
|  | 614 |  | 
|  | 615 | std::atomic<bool> start(false); | 
|  | 616 | std::vector<std::thread> threads; | 
|  | 617 | threads.emplace_back([&start]() { | 
|  | 618 | while (!start) { | 
|  | 619 | continue; | 
|  | 620 | } | 
|  | 621 |  | 
|  | 622 | // Use a way out of range pid, to avoid stomping on an actual process. | 
|  | 623 | pid_t pid_base = 1'000'000; | 
|  | 624 |  | 
|  | 625 | for (int dump = 0; dump < kDumpCount; ++dump) { | 
|  | 626 | pid_t pid = pid_base + dump; | 
|  | 627 |  | 
|  | 628 | unique_fd intercept_fd, output_fd; | 
|  | 629 | tombstoned_intercept(pid, &intercept_fd, &output_fd); | 
|  | 630 |  | 
|  | 631 | // Pretend to crash, and then immediately close the socket. | 
|  | 632 | unique_fd sockfd(socket_local_client(kTombstonedCrashSocketName, | 
|  | 633 | ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_SEQPACKET)); | 
|  | 634 | if (sockfd == -1) { | 
|  | 635 | FAIL() << "failed to connect to tombstoned: " << strerror(errno); | 
|  | 636 | } | 
|  | 637 | TombstonedCrashPacket packet = {}; | 
|  | 638 | packet.packet_type = CrashPacketType::kDumpRequest; | 
|  | 639 | packet.packet.dump_request.pid = pid; | 
|  | 640 | if (TEMP_FAILURE_RETRY(write(sockfd, &packet, sizeof(packet))) != sizeof(packet)) { | 
|  | 641 | FAIL() << "failed to write to tombstoned: " << strerror(errno); | 
|  | 642 | } | 
|  | 643 |  | 
|  | 644 | continue; | 
|  | 645 | } | 
|  | 646 | }); | 
|  | 647 |  | 
|  | 648 | threads.emplace_back([&start]() { | 
|  | 649 | while (!start) { | 
|  | 650 | continue; | 
|  | 651 | } | 
|  | 652 |  | 
|  | 653 | // Use a way out of range pid, to avoid stomping on an actual process. | 
|  | 654 | pid_t pid_base = 2'000'000; | 
|  | 655 |  | 
|  | 656 | for (int dump = 0; dump < kDumpCount; ++dump) { | 
|  | 657 | pid_t pid = pid_base + dump; | 
|  | 658 |  | 
|  | 659 | unique_fd intercept_fd, output_fd; | 
|  | 660 | tombstoned_intercept(pid, &intercept_fd, &output_fd); | 
|  | 661 |  | 
|  | 662 | { | 
|  | 663 | unique_fd tombstoned_socket, input_fd; | 
|  | 664 | ASSERT_TRUE(tombstoned_connect(pid, &tombstoned_socket, &input_fd)); | 
|  | 665 | ASSERT_TRUE(android::base::WriteFully(input_fd.get(), &pid, sizeof(pid))); | 
|  | 666 | tombstoned_notify_completion(tombstoned_socket.get()); | 
|  | 667 | } | 
|  | 668 |  | 
|  | 669 | // TODO: Fix the race that requires this sleep. | 
|  | 670 | std::this_thread::sleep_for(50ms); | 
|  | 671 |  | 
|  | 672 | pid_t read_pid; | 
|  | 673 | ASSERT_TRUE(android::base::ReadFully(output_fd.get(), &read_pid, sizeof(read_pid))); | 
|  | 674 | ASSERT_EQ(read_pid, pid); | 
|  | 675 | } | 
|  | 676 | }); | 
|  | 677 |  | 
|  | 678 | start = true; | 
|  | 679 |  | 
|  | 680 | for (std::thread& thread : threads) { | 
|  | 681 | thread.join(); | 
|  | 682 | } | 
|  | 683 | } |