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