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