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