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