Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016, The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <err.h> |
| 18 | #include <fcntl.h> |
Josh Gao | cdea750 | 2017-11-01 15:00:40 -0700 | [diff] [blame] | 19 | #include <stdlib.h> |
Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 20 | #include <sys/capability.h> |
Josh Gao | fca7ca3 | 2017-01-23 12:05:35 -0800 | [diff] [blame] | 21 | #include <sys/prctl.h> |
Josh Gao | fd13bf0 | 2017-08-18 15:37:26 -0700 | [diff] [blame] | 22 | #include <sys/ptrace.h> |
Dan Albert | c38057a | 2017-10-11 11:35:40 -0700 | [diff] [blame] | 23 | #include <sys/syscall.h> |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 24 | #include <sys/types.h> |
Josh Gao | fd13bf0 | 2017-08-18 15:37:26 -0700 | [diff] [blame] | 25 | #include <unistd.h> |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 26 | |
| 27 | #include <chrono> |
| 28 | #include <regex> |
| 29 | #include <thread> |
| 30 | |
Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 31 | #include <android/set_abort_message.h> |
| 32 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 33 | #include <android-base/file.h> |
| 34 | #include <android-base/logging.h> |
Josh Gao | 2e7b8e2 | 2017-05-04 17:12:57 -0700 | [diff] [blame] | 35 | #include <android-base/macros.h> |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 36 | #include <android-base/parseint.h> |
| 37 | #include <android-base/properties.h> |
| 38 | #include <android-base/strings.h> |
Josh Gao | 30171a8 | 2017-04-27 19:48:44 -0700 | [diff] [blame^] | 39 | #include <android-base/test_utils.h> |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 40 | #include <android-base/unique_fd.h> |
| 41 | #include <cutils/sockets.h> |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 42 | #include <gtest/gtest.h> |
| 43 | |
Narayan Kamath | 2d377cd | 2017-05-10 10:58:59 +0100 | [diff] [blame] | 44 | #include "debuggerd/handler.h" |
| 45 | #include "protocol.h" |
| 46 | #include "tombstoned/tombstoned.h" |
| 47 | #include "util.h" |
| 48 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 49 | using namespace std::chrono_literals; |
| 50 | using android::base::unique_fd; |
| 51 | |
| 52 | #if defined(__LP64__) |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 53 | #define ARCH_SUFFIX "64" |
| 54 | #else |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 55 | #define ARCH_SUFFIX "" |
| 56 | #endif |
| 57 | |
| 58 | constexpr char kWaitForGdbKey[] = "debug.debuggerd.wait_for_gdb"; |
| 59 | |
| 60 | #define TIMEOUT(seconds, expr) \ |
| 61 | [&]() { \ |
| 62 | struct sigaction old_sigaction; \ |
| 63 | struct sigaction new_sigaction = {}; \ |
| 64 | new_sigaction.sa_handler = [](int) {}; \ |
| 65 | if (sigaction(SIGALRM, &new_sigaction, &new_sigaction) != 0) { \ |
| 66 | err(1, "sigaction failed"); \ |
| 67 | } \ |
| 68 | alarm(seconds); \ |
| 69 | auto value = expr; \ |
| 70 | int saved_errno = errno; \ |
| 71 | if (sigaction(SIGALRM, &old_sigaction, nullptr) != 0) { \ |
| 72 | err(1, "sigaction failed"); \ |
| 73 | } \ |
| 74 | alarm(0); \ |
| 75 | errno = saved_errno; \ |
| 76 | return value; \ |
| 77 | }() |
| 78 | |
Jaesung Chung | 58778e1 | 2017-06-15 18:20:34 +0900 | [diff] [blame] | 79 | #define ASSERT_BACKTRACE_FRAME(result, frame_name) \ |
| 80 | ASSERT_MATCH(result, R"(#\d\d pc [0-9a-f]+\s+ /system/lib)" ARCH_SUFFIX \ |
| 81 | R"(/libc.so \()" frame_name R"(\+)") |
| 82 | |
Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 83 | static void tombstoned_intercept(pid_t target_pid, unique_fd* intercept_fd, unique_fd* output_fd, |
Narayan Kamath | ca5e908 | 2017-06-02 15:42:06 +0100 | [diff] [blame] | 84 | InterceptStatus* status, DebuggerdDumpType intercept_type) { |
Josh Gao | 460b336 | 2017-03-30 16:40:47 -0700 | [diff] [blame] | 85 | intercept_fd->reset(socket_local_client(kTombstonedInterceptSocketName, |
| 86 | ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_SEQPACKET)); |
| 87 | if (intercept_fd->get() == -1) { |
| 88 | FAIL() << "failed to contact tombstoned: " << strerror(errno); |
| 89 | } |
| 90 | |
Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 91 | InterceptRequest req = {.pid = target_pid, .dump_type = intercept_type}; |
Josh Gao | 460b336 | 2017-03-30 16:40:47 -0700 | [diff] [blame] | 92 | |
| 93 | unique_fd output_pipe_write; |
| 94 | if (!Pipe(output_fd, &output_pipe_write)) { |
| 95 | FAIL() << "failed to create output pipe: " << strerror(errno); |
| 96 | } |
| 97 | |
| 98 | std::string pipe_size_str; |
| 99 | int pipe_buffer_size; |
| 100 | if (!android::base::ReadFileToString("/proc/sys/fs/pipe-max-size", &pipe_size_str)) { |
| 101 | FAIL() << "failed to read /proc/sys/fs/pipe-max-size: " << strerror(errno); |
| 102 | } |
| 103 | |
| 104 | pipe_size_str = android::base::Trim(pipe_size_str); |
| 105 | |
| 106 | if (!android::base::ParseInt(pipe_size_str.c_str(), &pipe_buffer_size, 0)) { |
| 107 | FAIL() << "failed to parse pipe max size"; |
| 108 | } |
| 109 | |
| 110 | if (fcntl(output_fd->get(), F_SETPIPE_SZ, pipe_buffer_size) != pipe_buffer_size) { |
| 111 | FAIL() << "failed to set pipe size: " << strerror(errno); |
| 112 | } |
| 113 | |
Josh Gao | 5675f3c | 2017-06-01 12:19:53 -0700 | [diff] [blame] | 114 | ASSERT_GE(pipe_buffer_size, 1024 * 1024); |
| 115 | |
Josh Gao | 460b336 | 2017-03-30 16:40:47 -0700 | [diff] [blame] | 116 | if (send_fd(intercept_fd->get(), &req, sizeof(req), std::move(output_pipe_write)) != sizeof(req)) { |
| 117 | FAIL() << "failed to send output fd to tombstoned: " << strerror(errno); |
| 118 | } |
| 119 | |
| 120 | InterceptResponse response; |
| 121 | ssize_t rc = TEMP_FAILURE_RETRY(read(intercept_fd->get(), &response, sizeof(response))); |
| 122 | if (rc == -1) { |
| 123 | FAIL() << "failed to read response from tombstoned: " << strerror(errno); |
| 124 | } else if (rc == 0) { |
| 125 | FAIL() << "failed to read response from tombstoned (EOF)"; |
| 126 | } else if (rc != sizeof(response)) { |
| 127 | FAIL() << "received packet of unexpected length from tombstoned: expected " << sizeof(response) |
| 128 | << ", received " << rc; |
| 129 | } |
| 130 | |
Narayan Kamath | ca5e908 | 2017-06-02 15:42:06 +0100 | [diff] [blame] | 131 | *status = response.status; |
Josh Gao | 460b336 | 2017-03-30 16:40:47 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 134 | class CrasherTest : public ::testing::Test { |
| 135 | public: |
| 136 | pid_t crasher_pid = -1; |
| 137 | bool previous_wait_for_gdb; |
| 138 | unique_fd crasher_pipe; |
| 139 | unique_fd intercept_fd; |
| 140 | |
| 141 | CrasherTest(); |
| 142 | ~CrasherTest(); |
| 143 | |
Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 144 | void StartIntercept(unique_fd* output_fd, DebuggerdDumpType intercept_type = kDebuggerdTombstone); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 145 | |
| 146 | // Returns -1 if we fail to read a response from tombstoned, otherwise the received return code. |
| 147 | void FinishIntercept(int* result); |
| 148 | |
Josh Gao | 2e7b8e2 | 2017-05-04 17:12:57 -0700 | [diff] [blame] | 149 | void StartProcess(std::function<void()> function, std::function<pid_t()> forker = fork); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 150 | void StartCrasher(const std::string& crash_type); |
| 151 | void FinishCrasher(); |
| 152 | void AssertDeath(int signo); |
| 153 | }; |
| 154 | |
| 155 | CrasherTest::CrasherTest() { |
| 156 | previous_wait_for_gdb = android::base::GetBoolProperty(kWaitForGdbKey, false); |
| 157 | android::base::SetProperty(kWaitForGdbKey, "0"); |
| 158 | } |
| 159 | |
| 160 | CrasherTest::~CrasherTest() { |
| 161 | if (crasher_pid != -1) { |
| 162 | kill(crasher_pid, SIGKILL); |
| 163 | int status; |
| 164 | waitpid(crasher_pid, &status, WUNTRACED); |
| 165 | } |
| 166 | |
| 167 | android::base::SetProperty(kWaitForGdbKey, previous_wait_for_gdb ? "1" : "0"); |
| 168 | } |
| 169 | |
Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 170 | void CrasherTest::StartIntercept(unique_fd* output_fd, DebuggerdDumpType intercept_type) { |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 171 | if (crasher_pid == -1) { |
| 172 | FAIL() << "crasher hasn't been started"; |
| 173 | } |
| 174 | |
Narayan Kamath | ca5e908 | 2017-06-02 15:42:06 +0100 | [diff] [blame] | 175 | InterceptStatus status; |
| 176 | tombstoned_intercept(crasher_pid, &this->intercept_fd, output_fd, &status, intercept_type); |
| 177 | ASSERT_EQ(InterceptStatus::kRegistered, status); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | void CrasherTest::FinishIntercept(int* result) { |
| 181 | InterceptResponse response; |
| 182 | |
| 183 | // Timeout for tombstoned intercept is 10 seconds. |
| 184 | ssize_t rc = TIMEOUT(20, read(intercept_fd.get(), &response, sizeof(response))); |
| 185 | if (rc == -1) { |
| 186 | FAIL() << "failed to read response from tombstoned: " << strerror(errno); |
| 187 | } else if (rc == 0) { |
| 188 | *result = -1; |
| 189 | } else if (rc != sizeof(response)) { |
| 190 | FAIL() << "received packet of unexpected length from tombstoned: expected " << sizeof(response) |
| 191 | << ", received " << rc; |
| 192 | } else { |
Josh Gao | 460b336 | 2017-03-30 16:40:47 -0700 | [diff] [blame] | 193 | *result = response.status == InterceptStatus::kStarted ? 1 : 0; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
Josh Gao | 2e7b8e2 | 2017-05-04 17:12:57 -0700 | [diff] [blame] | 197 | void CrasherTest::StartProcess(std::function<void()> function, std::function<pid_t()> forker) { |
Josh Gao | fca7ca3 | 2017-01-23 12:05:35 -0800 | [diff] [blame] | 198 | unique_fd read_pipe; |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 199 | unique_fd crasher_read_pipe; |
| 200 | if (!Pipe(&crasher_read_pipe, &crasher_pipe)) { |
| 201 | FAIL() << "failed to create pipe: " << strerror(errno); |
| 202 | } |
| 203 | |
Josh Gao | 2e7b8e2 | 2017-05-04 17:12:57 -0700 | [diff] [blame] | 204 | crasher_pid = forker(); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 205 | if (crasher_pid == -1) { |
| 206 | FAIL() << "fork failed: " << strerror(errno); |
| 207 | } else if (crasher_pid == 0) { |
Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 208 | char dummy; |
| 209 | crasher_pipe.reset(); |
| 210 | TEMP_FAILURE_RETRY(read(crasher_read_pipe.get(), &dummy, 1)); |
Josh Gao | fca7ca3 | 2017-01-23 12:05:35 -0800 | [diff] [blame] | 211 | function(); |
| 212 | _exit(0); |
| 213 | } |
| 214 | } |
| 215 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 216 | void CrasherTest::FinishCrasher() { |
| 217 | if (crasher_pipe == -1) { |
| 218 | FAIL() << "crasher pipe uninitialized"; |
| 219 | } |
| 220 | |
| 221 | ssize_t rc = write(crasher_pipe.get(), "\n", 1); |
| 222 | if (rc == -1) { |
| 223 | FAIL() << "failed to write to crasher pipe: " << strerror(errno); |
| 224 | } else if (rc == 0) { |
| 225 | FAIL() << "crasher pipe was closed"; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | void CrasherTest::AssertDeath(int signo) { |
| 230 | int status; |
| 231 | pid_t pid = TIMEOUT(5, waitpid(crasher_pid, &status, 0)); |
| 232 | if (pid != crasher_pid) { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 233 | printf("failed to wait for crasher (pid %d)\n", crasher_pid); |
| 234 | sleep(100); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 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 | |
Josh Gao | cdea750 | 2017-11-01 15:00:40 -0700 | [diff] [blame] | 289 | TEST_F(CrasherTest, LD_PRELOAD) { |
| 290 | int intercept_result; |
| 291 | unique_fd output_fd; |
| 292 | StartProcess([]() { |
| 293 | setenv("LD_PRELOAD", "nonexistent.so", 1); |
| 294 | *reinterpret_cast<volatile char*>(0xdead) = '1'; |
| 295 | }); |
| 296 | |
| 297 | StartIntercept(&output_fd); |
| 298 | FinishCrasher(); |
| 299 | AssertDeath(SIGSEGV); |
| 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"(signal 11 \(SIGSEGV\), code 1 \(SEGV_MAPERR\), fault addr 0xdead)"); |
| 307 | } |
| 308 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 309 | TEST_F(CrasherTest, abort) { |
| 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 | FinishCrasher(); |
| 317 | AssertDeath(SIGABRT); |
| 318 | FinishIntercept(&intercept_result); |
| 319 | |
| 320 | ASSERT_EQ(1, intercept_result) << "tombstoned reported failure"; |
| 321 | |
| 322 | std::string result; |
| 323 | ConsumeFd(std::move(output_fd), &result); |
Andreas Gampe | 26cbafb | 2017-06-22 20:14:43 -0700 | [diff] [blame] | 324 | ASSERT_BACKTRACE_FRAME(result, "abort"); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | TEST_F(CrasherTest, signal) { |
| 328 | int intercept_result; |
| 329 | unique_fd output_fd; |
Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 330 | StartProcess([]() { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 331 | while (true) { |
| 332 | sleep(1); |
| 333 | } |
Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 334 | }); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 335 | StartIntercept(&output_fd); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 336 | FinishCrasher(); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 337 | ASSERT_EQ(0, kill(crasher_pid, SIGSEGV)); |
| 338 | |
| 339 | AssertDeath(SIGSEGV); |
| 340 | FinishIntercept(&intercept_result); |
| 341 | |
| 342 | ASSERT_EQ(1, intercept_result) << "tombstoned reported failure"; |
| 343 | |
| 344 | std::string result; |
| 345 | ConsumeFd(std::move(output_fd), &result); |
| 346 | ASSERT_MATCH(result, R"(signal 11 \(SIGSEGV\), code 0 \(SI_USER\), fault addr --------)"); |
| 347 | ASSERT_MATCH(result, R"(backtrace:)"); |
| 348 | } |
| 349 | |
| 350 | TEST_F(CrasherTest, abort_message) { |
| 351 | int intercept_result; |
| 352 | unique_fd output_fd; |
Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 353 | StartProcess([]() { |
| 354 | android_set_abort_message("abort message goes here"); |
| 355 | abort(); |
| 356 | }); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 357 | StartIntercept(&output_fd); |
| 358 | FinishCrasher(); |
| 359 | AssertDeath(SIGABRT); |
| 360 | FinishIntercept(&intercept_result); |
| 361 | |
| 362 | ASSERT_EQ(1, intercept_result) << "tombstoned reported failure"; |
| 363 | |
| 364 | std::string result; |
| 365 | ConsumeFd(std::move(output_fd), &result); |
Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 366 | ASSERT_MATCH(result, R"(Abort message: 'abort message goes here')"); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Josh Gao | e06f2a4 | 2017-04-27 16:50:38 -0700 | [diff] [blame] | 369 | TEST_F(CrasherTest, abort_message_backtrace) { |
| 370 | int intercept_result; |
| 371 | unique_fd output_fd; |
| 372 | StartProcess([]() { |
| 373 | android_set_abort_message("not actually aborting"); |
| 374 | raise(DEBUGGER_SIGNAL); |
| 375 | exit(0); |
| 376 | }); |
| 377 | StartIntercept(&output_fd); |
| 378 | FinishCrasher(); |
| 379 | AssertDeath(0); |
| 380 | FinishIntercept(&intercept_result); |
| 381 | |
| 382 | ASSERT_EQ(1, intercept_result) << "tombstoned reported failure"; |
| 383 | |
| 384 | std::string result; |
| 385 | ConsumeFd(std::move(output_fd), &result); |
| 386 | ASSERT_NOT_MATCH(result, R"(Abort message:)"); |
| 387 | } |
| 388 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 389 | TEST_F(CrasherTest, intercept_timeout) { |
| 390 | int intercept_result; |
| 391 | unique_fd output_fd; |
Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 392 | StartProcess([]() { |
| 393 | abort(); |
| 394 | }); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 395 | StartIntercept(&output_fd); |
| 396 | |
| 397 | // Don't let crasher finish until we timeout. |
| 398 | FinishIntercept(&intercept_result); |
| 399 | |
| 400 | ASSERT_NE(1, intercept_result) << "tombstoned reported success? (intercept_result = " |
| 401 | << intercept_result << ")"; |
| 402 | |
| 403 | FinishCrasher(); |
| 404 | AssertDeath(SIGABRT); |
| 405 | } |
| 406 | |
| 407 | TEST_F(CrasherTest, wait_for_gdb) { |
| 408 | if (!android::base::SetProperty(kWaitForGdbKey, "1")) { |
| 409 | FAIL() << "failed to enable wait_for_gdb"; |
| 410 | } |
| 411 | sleep(1); |
| 412 | |
Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 413 | StartProcess([]() { |
| 414 | abort(); |
| 415 | }); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 416 | FinishCrasher(); |
| 417 | |
| 418 | int status; |
| 419 | ASSERT_EQ(crasher_pid, waitpid(crasher_pid, &status, WUNTRACED)); |
| 420 | ASSERT_TRUE(WIFSTOPPED(status)); |
| 421 | ASSERT_EQ(SIGSTOP, WSTOPSIG(status)); |
| 422 | |
| 423 | ASSERT_EQ(0, kill(crasher_pid, SIGCONT)); |
| 424 | |
| 425 | AssertDeath(SIGABRT); |
| 426 | } |
| 427 | |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 428 | TEST_F(CrasherTest, backtrace) { |
| 429 | std::string result; |
| 430 | int intercept_result; |
| 431 | unique_fd output_fd; |
Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 432 | |
| 433 | StartProcess([]() { |
| 434 | abort(); |
| 435 | }); |
Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 436 | StartIntercept(&output_fd, kDebuggerdNativeBacktrace); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 437 | |
| 438 | std::this_thread::sleep_for(500ms); |
| 439 | |
| 440 | sigval val; |
| 441 | val.sival_int = 1; |
| 442 | ASSERT_EQ(0, sigqueue(crasher_pid, DEBUGGER_SIGNAL, val)) << strerror(errno); |
| 443 | FinishIntercept(&intercept_result); |
| 444 | ASSERT_EQ(1, intercept_result) << "tombstoned reported failure"; |
| 445 | ConsumeFd(std::move(output_fd), &result); |
Jaesung Chung | 58778e1 | 2017-06-15 18:20:34 +0900 | [diff] [blame] | 446 | ASSERT_BACKTRACE_FRAME(result, "read"); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 447 | |
| 448 | int status; |
| 449 | ASSERT_EQ(0, waitpid(crasher_pid, &status, WNOHANG | WUNTRACED)); |
| 450 | |
| 451 | StartIntercept(&output_fd); |
| 452 | FinishCrasher(); |
| 453 | AssertDeath(SIGABRT); |
| 454 | FinishIntercept(&intercept_result); |
| 455 | ASSERT_EQ(1, intercept_result) << "tombstoned reported failure"; |
| 456 | ConsumeFd(std::move(output_fd), &result); |
Andreas Gampe | 26cbafb | 2017-06-22 20:14:43 -0700 | [diff] [blame] | 457 | ASSERT_BACKTRACE_FRAME(result, "abort"); |
Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 458 | } |
Josh Gao | fca7ca3 | 2017-01-23 12:05:35 -0800 | [diff] [blame] | 459 | |
| 460 | TEST_F(CrasherTest, PR_SET_DUMPABLE_0_crash) { |
Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 461 | int intercept_result; |
| 462 | unique_fd output_fd; |
Josh Gao | fca7ca3 | 2017-01-23 12:05:35 -0800 | [diff] [blame] | 463 | StartProcess([]() { |
| 464 | prctl(PR_SET_DUMPABLE, 0); |
Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 465 | abort(); |
Josh Gao | fca7ca3 | 2017-01-23 12:05:35 -0800 | [diff] [blame] | 466 | }); |
Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 467 | |
| 468 | StartIntercept(&output_fd); |
| 469 | FinishCrasher(); |
| 470 | AssertDeath(SIGABRT); |
| 471 | FinishIntercept(&intercept_result); |
| 472 | |
| 473 | ASSERT_EQ(1, intercept_result) << "tombstoned reported failure"; |
| 474 | |
| 475 | std::string result; |
| 476 | ConsumeFd(std::move(output_fd), &result); |
Andreas Gampe | 26cbafb | 2017-06-22 20:14:43 -0700 | [diff] [blame] | 477 | ASSERT_BACKTRACE_FRAME(result, "abort"); |
Josh Gao | fca7ca3 | 2017-01-23 12:05:35 -0800 | [diff] [blame] | 478 | } |
| 479 | |
Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 480 | TEST_F(CrasherTest, capabilities) { |
| 481 | ASSERT_EQ(0U, getuid()) << "capability test requires root"; |
| 482 | |
Josh Gao | fca7ca3 | 2017-01-23 12:05:35 -0800 | [diff] [blame] | 483 | StartProcess([]() { |
Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 484 | if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) != 0) { |
| 485 | err(1, "failed to set PR_SET_KEEPCAPS"); |
| 486 | } |
| 487 | |
| 488 | if (setresuid(1, 1, 1) != 0) { |
| 489 | err(1, "setresuid failed"); |
| 490 | } |
| 491 | |
| 492 | __user_cap_header_struct capheader; |
| 493 | __user_cap_data_struct capdata[2]; |
| 494 | memset(&capheader, 0, sizeof(capheader)); |
| 495 | memset(&capdata, 0, sizeof(capdata)); |
| 496 | |
| 497 | capheader.version = _LINUX_CAPABILITY_VERSION_3; |
| 498 | capheader.pid = 0; |
| 499 | |
| 500 | // Turn on every third capability. |
| 501 | static_assert(CAP_LAST_CAP > 33, "CAP_LAST_CAP <= 32"); |
| 502 | for (int i = 0; i < CAP_LAST_CAP; i += 3) { |
| 503 | capdata[CAP_TO_INDEX(i)].permitted |= CAP_TO_MASK(i); |
| 504 | capdata[CAP_TO_INDEX(i)].effective |= CAP_TO_MASK(i); |
| 505 | } |
| 506 | |
| 507 | // Make sure CAP_SYS_PTRACE is off. |
| 508 | capdata[CAP_TO_INDEX(CAP_SYS_PTRACE)].permitted &= ~(CAP_TO_MASK(CAP_SYS_PTRACE)); |
| 509 | capdata[CAP_TO_INDEX(CAP_SYS_PTRACE)].effective &= ~(CAP_TO_MASK(CAP_SYS_PTRACE)); |
| 510 | |
| 511 | if (capset(&capheader, &capdata[0]) != 0) { |
| 512 | err(1, "capset failed"); |
| 513 | } |
| 514 | |
| 515 | if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) != 0) { |
| 516 | err(1, "failed to drop ambient capabilities"); |
| 517 | } |
| 518 | |
Josh Gao | a5199a9 | 2017-04-03 13:18:34 -0700 | [diff] [blame] | 519 | pthread_setname_np(pthread_self(), "thread_name"); |
Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 520 | raise(SIGSYS); |
Josh Gao | fca7ca3 | 2017-01-23 12:05:35 -0800 | [diff] [blame] | 521 | }); |
Josh Gao | 502cfd2 | 2017-02-17 01:39:15 -0800 | [diff] [blame] | 522 | |
| 523 | unique_fd output_fd; |
| 524 | StartIntercept(&output_fd); |
| 525 | FinishCrasher(); |
| 526 | AssertDeath(SIGSYS); |
| 527 | |
| 528 | std::string result; |
| 529 | int intercept_result; |
| 530 | FinishIntercept(&intercept_result); |
| 531 | ASSERT_EQ(1, intercept_result) << "tombstoned reported failure"; |
| 532 | ConsumeFd(std::move(output_fd), &result); |
Josh Gao | a5199a9 | 2017-04-03 13:18:34 -0700 | [diff] [blame] | 533 | ASSERT_MATCH(result, R"(name: thread_name\s+>>> .+debuggerd_test(32|64) <<<)"); |
Jaesung Chung | 58778e1 | 2017-06-15 18:20:34 +0900 | [diff] [blame] | 534 | ASSERT_BACKTRACE_FRAME(result, "tgkill"); |
Josh Gao | fca7ca3 | 2017-01-23 12:05:35 -0800 | [diff] [blame] | 535 | } |
Josh Gao | c3c8c02 | 2017-02-13 16:36:18 -0800 | [diff] [blame] | 536 | |
Josh Gao | 2e7b8e2 | 2017-05-04 17:12:57 -0700 | [diff] [blame] | 537 | TEST_F(CrasherTest, fake_pid) { |
| 538 | int intercept_result; |
| 539 | unique_fd output_fd; |
| 540 | |
| 541 | // Prime the getpid/gettid caches. |
| 542 | UNUSED(getpid()); |
| 543 | UNUSED(gettid()); |
| 544 | |
| 545 | std::function<pid_t()> clone_fn = []() { |
| 546 | return syscall(__NR_clone, SIGCHLD, nullptr, nullptr, nullptr, nullptr); |
| 547 | }; |
| 548 | StartProcess( |
| 549 | []() { |
| 550 | ASSERT_NE(getpid(), syscall(__NR_getpid)); |
| 551 | ASSERT_NE(gettid(), syscall(__NR_gettid)); |
| 552 | raise(SIGSEGV); |
| 553 | }, |
| 554 | clone_fn); |
| 555 | |
| 556 | StartIntercept(&output_fd); |
| 557 | FinishCrasher(); |
| 558 | AssertDeath(SIGSEGV); |
| 559 | FinishIntercept(&intercept_result); |
| 560 | |
| 561 | ASSERT_EQ(1, intercept_result) << "tombstoned reported failure"; |
| 562 | |
| 563 | std::string result; |
| 564 | ConsumeFd(std::move(output_fd), &result); |
Jaesung Chung | 58778e1 | 2017-06-15 18:20:34 +0900 | [diff] [blame] | 565 | ASSERT_BACKTRACE_FRAME(result, "tgkill"); |
Josh Gao | 2e7b8e2 | 2017-05-04 17:12:57 -0700 | [diff] [blame] | 566 | } |
| 567 | |
Josh Gao | fd13bf0 | 2017-08-18 15:37:26 -0700 | [diff] [blame] | 568 | TEST_F(CrasherTest, competing_tracer) { |
| 569 | int intercept_result; |
| 570 | unique_fd output_fd; |
| 571 | StartProcess([]() { |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 572 | raise(SIGABRT); |
Josh Gao | fd13bf0 | 2017-08-18 15:37:26 -0700 | [diff] [blame] | 573 | }); |
| 574 | |
| 575 | StartIntercept(&output_fd); |
Josh Gao | fd13bf0 | 2017-08-18 15:37:26 -0700 | [diff] [blame] | 576 | |
| 577 | ASSERT_EQ(0, ptrace(PTRACE_SEIZE, crasher_pid, 0, 0)); |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 578 | FinishCrasher(); |
Josh Gao | fd13bf0 | 2017-08-18 15:37:26 -0700 | [diff] [blame] | 579 | |
| 580 | int status; |
| 581 | ASSERT_EQ(crasher_pid, waitpid(crasher_pid, &status, 0)); |
| 582 | ASSERT_TRUE(WIFSTOPPED(status)); |
| 583 | ASSERT_EQ(SIGABRT, WSTOPSIG(status)); |
| 584 | |
| 585 | ASSERT_EQ(0, ptrace(PTRACE_CONT, crasher_pid, 0, SIGABRT)); |
| 586 | FinishIntercept(&intercept_result); |
| 587 | ASSERT_EQ(1, intercept_result) << "tombstoned reported failure"; |
| 588 | |
| 589 | std::string result; |
| 590 | ConsumeFd(std::move(output_fd), &result); |
| 591 | std::string regex = R"(failed to attach to thread \d+, already traced by )"; |
| 592 | regex += std::to_string(gettid()); |
| 593 | regex += R"( \(.+debuggerd_test)"; |
| 594 | ASSERT_MATCH(result, regex.c_str()); |
| 595 | |
Josh Gao | 2b2ae0c | 2017-08-21 14:31:17 -0700 | [diff] [blame] | 596 | ASSERT_EQ(crasher_pid, waitpid(crasher_pid, &status, 0)); |
| 597 | ASSERT_TRUE(WIFSTOPPED(status)); |
| 598 | ASSERT_EQ(SIGABRT, WSTOPSIG(status)); |
| 599 | |
Josh Gao | fd13bf0 | 2017-08-18 15:37:26 -0700 | [diff] [blame] | 600 | ASSERT_EQ(0, ptrace(PTRACE_DETACH, crasher_pid, 0, SIGABRT)); |
| 601 | AssertDeath(SIGABRT); |
| 602 | } |
| 603 | |
Josh Gao | c3c8c02 | 2017-02-13 16:36:18 -0800 | [diff] [blame] | 604 | TEST(crash_dump, zombie) { |
| 605 | pid_t forkpid = fork(); |
| 606 | |
Josh Gao | c3c8c02 | 2017-02-13 16:36:18 -0800 | [diff] [blame] | 607 | pid_t rc; |
| 608 | int status; |
| 609 | |
| 610 | if (forkpid == 0) { |
| 611 | errno = 0; |
| 612 | rc = waitpid(-1, &status, WNOHANG | __WALL | __WNOTHREAD); |
| 613 | if (rc != -1 || errno != ECHILD) { |
| 614 | errx(2, "first waitpid returned %d (%s), expected failure with ECHILD", rc, strerror(errno)); |
| 615 | } |
| 616 | |
| 617 | raise(DEBUGGER_SIGNAL); |
| 618 | |
| 619 | errno = 0; |
| 620 | rc = waitpid(-1, &status, __WALL | __WNOTHREAD); |
| 621 | if (rc != -1 || errno != ECHILD) { |
| 622 | errx(2, "second waitpid returned %d (%s), expected failure with ECHILD", rc, strerror(errno)); |
| 623 | } |
| 624 | _exit(0); |
| 625 | } else { |
| 626 | rc = waitpid(forkpid, &status, 0); |
| 627 | ASSERT_EQ(forkpid, rc); |
| 628 | ASSERT_TRUE(WIFEXITED(status)); |
| 629 | ASSERT_EQ(0, WEXITSTATUS(status)); |
| 630 | } |
| 631 | } |
Josh Gao | 352a845 | 2017-03-30 16:46:21 -0700 | [diff] [blame] | 632 | |
| 633 | TEST(tombstoned, no_notify) { |
| 634 | // Do this a few times. |
| 635 | for (int i = 0; i < 3; ++i) { |
| 636 | pid_t pid = 123'456'789 + i; |
| 637 | |
| 638 | unique_fd intercept_fd, output_fd; |
Narayan Kamath | ca5e908 | 2017-06-02 15:42:06 +0100 | [diff] [blame] | 639 | InterceptStatus status; |
| 640 | tombstoned_intercept(pid, &intercept_fd, &output_fd, &status, kDebuggerdTombstone); |
| 641 | ASSERT_EQ(InterceptStatus::kRegistered, status); |
Josh Gao | 352a845 | 2017-03-30 16:46:21 -0700 | [diff] [blame] | 642 | |
| 643 | { |
| 644 | unique_fd tombstoned_socket, input_fd; |
Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 645 | ASSERT_TRUE(tombstoned_connect(pid, &tombstoned_socket, &input_fd, kDebuggerdTombstone)); |
Josh Gao | 352a845 | 2017-03-30 16:46:21 -0700 | [diff] [blame] | 646 | ASSERT_TRUE(android::base::WriteFully(input_fd.get(), &pid, sizeof(pid))); |
| 647 | } |
| 648 | |
| 649 | pid_t read_pid; |
| 650 | ASSERT_TRUE(android::base::ReadFully(output_fd.get(), &read_pid, sizeof(read_pid))); |
| 651 | ASSERT_EQ(read_pid, pid); |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | TEST(tombstoned, stress) { |
| 656 | // Spawn threads to simultaneously do a bunch of failing dumps and a bunch of successful dumps. |
| 657 | static constexpr int kDumpCount = 100; |
| 658 | |
| 659 | std::atomic<bool> start(false); |
| 660 | std::vector<std::thread> threads; |
| 661 | threads.emplace_back([&start]() { |
| 662 | while (!start) { |
| 663 | continue; |
| 664 | } |
| 665 | |
| 666 | // Use a way out of range pid, to avoid stomping on an actual process. |
| 667 | pid_t pid_base = 1'000'000; |
| 668 | |
| 669 | for (int dump = 0; dump < kDumpCount; ++dump) { |
| 670 | pid_t pid = pid_base + dump; |
| 671 | |
| 672 | unique_fd intercept_fd, output_fd; |
Narayan Kamath | ca5e908 | 2017-06-02 15:42:06 +0100 | [diff] [blame] | 673 | InterceptStatus status; |
| 674 | tombstoned_intercept(pid, &intercept_fd, &output_fd, &status, kDebuggerdTombstone); |
| 675 | ASSERT_EQ(InterceptStatus::kRegistered, status); |
Josh Gao | 352a845 | 2017-03-30 16:46:21 -0700 | [diff] [blame] | 676 | |
| 677 | // Pretend to crash, and then immediately close the socket. |
| 678 | unique_fd sockfd(socket_local_client(kTombstonedCrashSocketName, |
| 679 | ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_SEQPACKET)); |
| 680 | if (sockfd == -1) { |
| 681 | FAIL() << "failed to connect to tombstoned: " << strerror(errno); |
| 682 | } |
| 683 | TombstonedCrashPacket packet = {}; |
| 684 | packet.packet_type = CrashPacketType::kDumpRequest; |
| 685 | packet.packet.dump_request.pid = pid; |
| 686 | if (TEMP_FAILURE_RETRY(write(sockfd, &packet, sizeof(packet))) != sizeof(packet)) { |
| 687 | FAIL() << "failed to write to tombstoned: " << strerror(errno); |
| 688 | } |
| 689 | |
| 690 | continue; |
| 691 | } |
| 692 | }); |
| 693 | |
| 694 | threads.emplace_back([&start]() { |
| 695 | while (!start) { |
| 696 | continue; |
| 697 | } |
| 698 | |
| 699 | // Use a way out of range pid, to avoid stomping on an actual process. |
| 700 | pid_t pid_base = 2'000'000; |
| 701 | |
| 702 | for (int dump = 0; dump < kDumpCount; ++dump) { |
| 703 | pid_t pid = pid_base + dump; |
| 704 | |
| 705 | unique_fd intercept_fd, output_fd; |
Narayan Kamath | ca5e908 | 2017-06-02 15:42:06 +0100 | [diff] [blame] | 706 | InterceptStatus status; |
| 707 | tombstoned_intercept(pid, &intercept_fd, &output_fd, &status, kDebuggerdTombstone); |
| 708 | ASSERT_EQ(InterceptStatus::kRegistered, status); |
Josh Gao | 352a845 | 2017-03-30 16:46:21 -0700 | [diff] [blame] | 709 | |
| 710 | { |
| 711 | unique_fd tombstoned_socket, input_fd; |
Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 712 | ASSERT_TRUE(tombstoned_connect(pid, &tombstoned_socket, &input_fd, kDebuggerdTombstone)); |
Josh Gao | 352a845 | 2017-03-30 16:46:21 -0700 | [diff] [blame] | 713 | ASSERT_TRUE(android::base::WriteFully(input_fd.get(), &pid, sizeof(pid))); |
| 714 | tombstoned_notify_completion(tombstoned_socket.get()); |
| 715 | } |
| 716 | |
| 717 | // TODO: Fix the race that requires this sleep. |
| 718 | std::this_thread::sleep_for(50ms); |
| 719 | |
| 720 | pid_t read_pid; |
| 721 | ASSERT_TRUE(android::base::ReadFully(output_fd.get(), &read_pid, sizeof(read_pid))); |
| 722 | ASSERT_EQ(read_pid, pid); |
| 723 | } |
| 724 | }); |
| 725 | |
| 726 | start = true; |
| 727 | |
| 728 | for (std::thread& thread : threads) { |
| 729 | thread.join(); |
| 730 | } |
| 731 | } |
Narayan Kamath | ca5e908 | 2017-06-02 15:42:06 +0100 | [diff] [blame] | 732 | |
| 733 | TEST(tombstoned, java_trace_intercept_smoke) { |
| 734 | // Using a "real" PID is a little dangerous here - if the test fails |
| 735 | // or crashes, we might end up getting a bogus / unreliable stack |
| 736 | // trace. |
| 737 | const pid_t self = getpid(); |
| 738 | |
| 739 | unique_fd intercept_fd, output_fd; |
| 740 | InterceptStatus status; |
| 741 | tombstoned_intercept(self, &intercept_fd, &output_fd, &status, kDebuggerdJavaBacktrace); |
| 742 | ASSERT_EQ(InterceptStatus::kRegistered, status); |
| 743 | |
| 744 | // First connect to tombstoned requesting a native backtrace. This |
| 745 | // should result in a "regular" FD and not the installed intercept. |
| 746 | const char native[] = "native"; |
| 747 | unique_fd tombstoned_socket, input_fd; |
| 748 | ASSERT_TRUE(tombstoned_connect(self, &tombstoned_socket, &input_fd, kDebuggerdNativeBacktrace)); |
| 749 | ASSERT_TRUE(android::base::WriteFully(input_fd.get(), native, sizeof(native))); |
| 750 | tombstoned_notify_completion(tombstoned_socket.get()); |
| 751 | |
| 752 | // Then, connect to tombstoned asking for a java backtrace. This *should* |
| 753 | // trigger the intercept. |
| 754 | const char java[] = "java"; |
| 755 | ASSERT_TRUE(tombstoned_connect(self, &tombstoned_socket, &input_fd, kDebuggerdJavaBacktrace)); |
| 756 | ASSERT_TRUE(android::base::WriteFully(input_fd.get(), java, sizeof(java))); |
| 757 | tombstoned_notify_completion(tombstoned_socket.get()); |
| 758 | |
| 759 | char outbuf[sizeof(java)]; |
| 760 | ASSERT_TRUE(android::base::ReadFully(output_fd.get(), outbuf, sizeof(outbuf))); |
| 761 | ASSERT_STREQ("java", outbuf); |
| 762 | } |
| 763 | |
| 764 | TEST(tombstoned, multiple_intercepts) { |
| 765 | const pid_t fake_pid = 1'234'567; |
| 766 | unique_fd intercept_fd, output_fd; |
| 767 | InterceptStatus status; |
| 768 | tombstoned_intercept(fake_pid, &intercept_fd, &output_fd, &status, kDebuggerdJavaBacktrace); |
| 769 | ASSERT_EQ(InterceptStatus::kRegistered, status); |
| 770 | |
| 771 | unique_fd intercept_fd_2, output_fd_2; |
| 772 | tombstoned_intercept(fake_pid, &intercept_fd_2, &output_fd_2, &status, kDebuggerdNativeBacktrace); |
| 773 | ASSERT_EQ(InterceptStatus::kFailedAlreadyRegistered, status); |
| 774 | } |
| 775 | |
| 776 | TEST(tombstoned, intercept_any) { |
| 777 | const pid_t fake_pid = 1'234'567; |
| 778 | |
| 779 | unique_fd intercept_fd, output_fd; |
| 780 | InterceptStatus status; |
| 781 | tombstoned_intercept(fake_pid, &intercept_fd, &output_fd, &status, kDebuggerdNativeBacktrace); |
| 782 | ASSERT_EQ(InterceptStatus::kRegistered, status); |
| 783 | |
| 784 | const char any[] = "any"; |
| 785 | unique_fd tombstoned_socket, input_fd; |
| 786 | ASSERT_TRUE(tombstoned_connect(fake_pid, &tombstoned_socket, &input_fd, kDebuggerdAnyIntercept)); |
| 787 | ASSERT_TRUE(android::base::WriteFully(input_fd.get(), any, sizeof(any))); |
| 788 | tombstoned_notify_completion(tombstoned_socket.get()); |
| 789 | |
| 790 | char outbuf[sizeof(any)]; |
| 791 | ASSERT_TRUE(android::base::ReadFully(output_fd.get(), outbuf, sizeof(outbuf))); |
| 792 | ASSERT_STREQ("any", outbuf); |
| 793 | } |