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