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