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