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