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