Josh Gao | 9727192 | 2019-11-06 13:15:00 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 <gtest/gtest.h> |
| 18 | |
| 19 | #include <dirent.h> |
| 20 | #include <err.h> |
| 21 | #include <errno.h> |
| 22 | #include <fcntl.h> |
Josh Gao | 9d51240 | 2020-04-29 16:34:37 -0700 | [diff] [blame] | 23 | #include <netinet/in.h> |
Josh Gao | 9727192 | 2019-11-06 13:15:00 -0800 | [diff] [blame] | 24 | #include <stdlib.h> |
Josh Gao | a38331d | 2020-04-29 17:06:14 -0700 | [diff] [blame] | 25 | #include <sys/epoll.h> |
Josh Gao | 7de4124 | 2020-04-29 17:08:46 -0700 | [diff] [blame] | 26 | #include <sys/eventfd.h> |
Josh Gao | 9727192 | 2019-11-06 13:15:00 -0800 | [diff] [blame] | 27 | #include <sys/types.h> |
| 28 | #include <unistd.h> |
| 29 | |
| 30 | #if defined(__BIONIC__) |
Josh Gao | 3de1915 | 2021-02-22 18:09:48 -0800 | [diff] [blame^] | 31 | #include <sys/pidfd.h> |
| 32 | |
Josh Gao | 7596250 | 2020-01-28 13:24:33 -0800 | [diff] [blame] | 33 | #include "platform/bionic/fdtrack.h" |
Josh Gao | 9413ae7 | 2020-04-30 16:03:44 -0700 | [diff] [blame] | 34 | #include "platform/bionic/reserved_signals.h" |
Josh Gao | 9727192 | 2019-11-06 13:15:00 -0800 | [diff] [blame] | 35 | #endif |
| 36 | |
| 37 | #include <vector> |
| 38 | |
Josh Gao | 9d51240 | 2020-04-29 16:34:37 -0700 | [diff] [blame] | 39 | #include <android-base/cmsg.h> |
Josh Gao | 9413ae7 | 2020-04-30 16:03:44 -0700 | [diff] [blame] | 40 | #include <android-base/logging.h> |
Josh Gao | 9727192 | 2019-11-06 13:15:00 -0800 | [diff] [blame] | 41 | #include <android-base/unique_fd.h> |
| 42 | |
Josh Gao | 9d51240 | 2020-04-29 16:34:37 -0700 | [diff] [blame] | 43 | using android::base::ReceiveFileDescriptors; |
| 44 | using android::base::SendFileDescriptors; |
| 45 | using android::base::unique_fd; |
| 46 | |
Josh Gao | 9727192 | 2019-11-06 13:15:00 -0800 | [diff] [blame] | 47 | #if defined(__BIONIC__) |
Josh Gao | 9413ae7 | 2020-04-30 16:03:44 -0700 | [diff] [blame] | 48 | void DumpEvent(std::vector<android_fdtrack_event>* events, size_t index) { |
| 49 | auto& event = (*events)[index]; |
| 50 | if (event.type == ANDROID_FDTRACK_EVENT_TYPE_CREATE) { |
| 51 | fprintf(stderr, " event %zu: fd %d created by %s\n", index, event.fd, |
| 52 | event.data.create.function_name); |
| 53 | } else if (event.type == ANDROID_FDTRACK_EVENT_TYPE_CLOSE) { |
| 54 | fprintf(stderr, " event %zu: fd %d closed\n", index, event.fd); |
| 55 | } else { |
| 56 | errx(1, "unexpected fdtrack event type: %d", event.type); |
| 57 | } |
| 58 | } |
| 59 | |
Josh Gao | 9727192 | 2019-11-06 13:15:00 -0800 | [diff] [blame] | 60 | std::vector<android_fdtrack_event> FdtrackRun(void (*func)()) { |
| 61 | // Each bionic test is run in separate process, so we can safely use a static here. |
| 62 | static std::vector<android_fdtrack_event> events; |
| 63 | events.clear(); |
| 64 | |
| 65 | android_fdtrack_hook_t previous = nullptr; |
Josh Gao | 3de1915 | 2021-02-22 18:09:48 -0800 | [diff] [blame^] | 66 | android_fdtrack_hook_t hook = [](android_fdtrack_event* event) { events.push_back(*event); }; |
Josh Gao | 9727192 | 2019-11-06 13:15:00 -0800 | [diff] [blame] | 67 | |
| 68 | if (!android_fdtrack_compare_exchange_hook(&previous, hook)) { |
| 69 | errx(1, "failed to exchange hook: previous hook was %p", previous); |
| 70 | } |
| 71 | |
| 72 | if (previous) { |
| 73 | errx(1, "hook was already registered?"); |
| 74 | abort(); |
| 75 | } |
| 76 | |
| 77 | func(); |
| 78 | |
| 79 | if (!android_fdtrack_compare_exchange_hook(&hook, nullptr)) { |
| 80 | errx(1, "failed to reset hook"); |
| 81 | } |
| 82 | |
Josh Gao | 9413ae7 | 2020-04-30 16:03:44 -0700 | [diff] [blame] | 83 | // Filter out temporary fds created and closed as a result of the call. |
| 84 | // (e.g. accept creating a socket to tell netd about the newly accepted socket) |
| 85 | size_t i = 0; |
| 86 | while (i + 1 < events.size()) { |
| 87 | auto& event = events[i]; |
| 88 | if (event.type == ANDROID_FDTRACK_EVENT_TYPE_CREATE) { |
| 89 | for (size_t j = i + 1; j < events.size(); ++j) { |
| 90 | if (event.fd == events[j].fd) { |
| 91 | if (events[j].type == ANDROID_FDTRACK_EVENT_TYPE_CREATE) { |
| 92 | fprintf(stderr, "error: multiple create events for the same fd:\n"); |
| 93 | DumpEvent(&events, i); |
| 94 | DumpEvent(&events, j); |
| 95 | exit(1); |
| 96 | } |
| 97 | |
| 98 | events.erase(events.begin() + j); |
| 99 | events.erase(events.begin() + i); |
| 100 | continue; |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | ++i; |
| 105 | } |
| 106 | |
Josh Gao | 9727192 | 2019-11-06 13:15:00 -0800 | [diff] [blame] | 107 | return std::move(events); |
| 108 | } |
Josh Gao | 9727192 | 2019-11-06 13:15:00 -0800 | [diff] [blame] | 109 | |
Josh Gao | 9d51240 | 2020-04-29 16:34:37 -0700 | [diff] [blame] | 110 | const char* FdtrackEventTypeToName(android_fdtrack_event_type event_type) { |
| 111 | switch (event_type) { |
| 112 | case ANDROID_FDTRACK_EVENT_TYPE_CREATE: |
| 113 | return "created"; |
| 114 | case ANDROID_FDTRACK_EVENT_TYPE_CLOSE: |
| 115 | return "closed"; |
| 116 | } |
Josh Gao | 9727192 | 2019-11-06 13:15:00 -0800 | [diff] [blame] | 117 | } |
Josh Gao | 9d51240 | 2020-04-29 16:34:37 -0700 | [diff] [blame] | 118 | #endif |
Josh Gao | 9727192 | 2019-11-06 13:15:00 -0800 | [diff] [blame] | 119 | |
| 120 | TEST(fdtrack, close) { |
| 121 | #if defined(__BIONIC__) |
| 122 | static int fd = open("/dev/null", O_WRONLY | O_CLOEXEC); |
| 123 | ASSERT_NE(-1, fd); |
| 124 | |
| 125 | auto events = FdtrackRun([]() { close(fd); }); |
| 126 | ASSERT_EQ(1U, events.size()); |
| 127 | ASSERT_EQ(fd, events[0].fd); |
| 128 | ASSERT_EQ(ANDROID_FDTRACK_EVENT_TYPE_CLOSE, events[0].type); |
| 129 | #endif |
| 130 | } |
| 131 | |
| 132 | TEST(fdtrack, enable_disable) { |
| 133 | #if defined(__BIONIC__) |
| 134 | static int fd1 = -1; |
| 135 | static int fd2 = -1; |
| 136 | static int fd3 = -1; |
| 137 | |
| 138 | auto events = FdtrackRun([]() { |
| 139 | if (!android_fdtrack_get_enabled()) { |
| 140 | errx(1, "fdtrack is disabled"); |
| 141 | } |
| 142 | fd1 = open("/dev/null", O_WRONLY | O_CLOEXEC); |
| 143 | android_fdtrack_set_enabled(false); |
| 144 | fd2 = open("/dev/null", O_WRONLY | O_CLOEXEC); |
| 145 | android_fdtrack_set_enabled(true); |
| 146 | fd3 = open("/dev/null", O_WRONLY | O_CLOEXEC); |
| 147 | }); |
| 148 | |
| 149 | if (fd1 == -1 || fd2 == -1 || fd3 == -1) { |
| 150 | errx(1, "failed to open /dev/null"); |
| 151 | } |
| 152 | |
| 153 | ASSERT_EQ(2U, events.size()); |
| 154 | |
| 155 | ASSERT_EQ(fd1, events[0].fd); |
| 156 | ASSERT_EQ(ANDROID_FDTRACK_EVENT_TYPE_CREATE, events[0].type); |
| 157 | ASSERT_STREQ("open", events[0].data.create.function_name); |
| 158 | |
| 159 | ASSERT_EQ(fd3, events[1].fd); |
| 160 | ASSERT_EQ(ANDROID_FDTRACK_EVENT_TYPE_CREATE, events[1].type); |
| 161 | ASSERT_STREQ("open", events[1].data.create.function_name); |
| 162 | #endif |
| 163 | } |
Josh Gao | 9d51240 | 2020-04-29 16:34:37 -0700 | [diff] [blame] | 164 | |
| 165 | struct require_semicolon; |
| 166 | |
| 167 | #if defined(__BIONIC__) |
Josh Gao | 1fad528 | 2020-04-29 17:00:13 -0700 | [diff] [blame] | 168 | void SetFdResult(std::vector<int>* output, int fd) { |
| 169 | output->push_back(fd); |
| 170 | } |
| 171 | |
| 172 | void SetFdResult(std::vector<int>* output, std::vector<int> fds) { |
| 173 | *output = fds; |
| 174 | } |
| 175 | |
| 176 | #define FDTRACK_TEST_NAME(test_name, fdtrack_name, expression) \ |
| 177 | TEST(fdtrack, test_name) { \ |
| 178 | static std::vector<int> expected_fds; \ |
| 179 | auto events = FdtrackRun([]() { SetFdResult(&expected_fds, expression); }); \ |
| 180 | for (auto& fd : expected_fds) { \ |
Josh Gao | 3de1915 | 2021-02-22 18:09:48 -0800 | [diff] [blame^] | 181 | ASSERT_NE(-1, fd) << strerror(errno); \ |
Josh Gao | 1fad528 | 2020-04-29 17:00:13 -0700 | [diff] [blame] | 182 | } \ |
| 183 | if (events.size() != expected_fds.size()) { \ |
| 184 | fprintf(stderr, "too many events received: expected %zu, got %zu:\n", expected_fds.size(), \ |
| 185 | events.size()); \ |
| 186 | for (size_t i = 0; i < events.size(); ++i) { \ |
Josh Gao | 9413ae7 | 2020-04-30 16:03:44 -0700 | [diff] [blame] | 187 | DumpEvent(&events, i); \ |
Josh Gao | 1fad528 | 2020-04-29 17:00:13 -0700 | [diff] [blame] | 188 | } \ |
| 189 | FAIL(); \ |
| 190 | return; \ |
| 191 | } \ |
| 192 | for (auto& event : events) { \ |
| 193 | ASSERT_NE(expected_fds.end(), \ |
| 194 | std::find(expected_fds.begin(), expected_fds.end(), events[0].fd)); \ |
| 195 | ASSERT_EQ(ANDROID_FDTRACK_EVENT_TYPE_CREATE, event.type); \ |
| 196 | ASSERT_STREQ(fdtrack_name, event.data.create.function_name); \ |
| 197 | } \ |
| 198 | } \ |
Josh Gao | 9d51240 | 2020-04-29 16:34:37 -0700 | [diff] [blame] | 199 | struct require_semicolon |
| 200 | #else |
| 201 | #define FDTRACK_TEST_NAME(name, fdtrack_name, expression) \ |
| 202 | TEST(fdtrack, name) {} \ |
| 203 | struct require_semicolon |
| 204 | #endif |
| 205 | |
| 206 | #define FDTRACK_TEST(name, expression) FDTRACK_TEST_NAME(name, #name, expression) |
| 207 | |
| 208 | // clang-format misformats statement expressions pretty badly here: |
| 209 | // clang-format off |
| 210 | FDTRACK_TEST(open, open("/dev/null", O_WRONLY | O_CLOEXEC)); |
| 211 | FDTRACK_TEST(openat, openat(AT_EMPTY_PATH, "/dev/null", O_WRONLY | O_CLOEXEC)); |
| 212 | FDTRACK_TEST(socket, socket(AF_UNIX, SOCK_STREAM, 0)); |
| 213 | |
Josh Gao | 3de1915 | 2021-02-22 18:09:48 -0800 | [diff] [blame^] | 214 | FDTRACK_TEST(pidfd_open, ({ |
| 215 | int rc = pidfd_open(getpid(), 0); |
| 216 | if (rc == -1) { |
| 217 | ASSERT_EQ(ENOSYS, errno); |
| 218 | GTEST_SKIP() << "pidfd_open not available"; |
| 219 | } |
| 220 | rc; |
| 221 | })); |
| 222 | |
| 223 | FDTRACK_TEST(pidfd_getfd, ({ |
| 224 | android_fdtrack_set_enabled(false); |
| 225 | int pidfd_self = pidfd_open(getpid(), 0); |
| 226 | if (pidfd_self == -1) { |
| 227 | ASSERT_EQ(ENOSYS, errno); |
| 228 | GTEST_SKIP() << "pidfd_open not available"; |
| 229 | } |
| 230 | android_fdtrack_set_enabled(true); |
| 231 | |
| 232 | int rc = pidfd_getfd(pidfd_self, STDIN_FILENO, 0); |
| 233 | if (rc == -1) { |
| 234 | ASSERT_EQ(ENOSYS, errno); |
| 235 | GTEST_SKIP() << "pidfd_getfd not available"; |
| 236 | } |
| 237 | |
| 238 | android_fdtrack_set_enabled(false); |
| 239 | close(pidfd_self); |
| 240 | android_fdtrack_set_enabled(true); |
| 241 | |
| 242 | rc; |
| 243 | })); |
| 244 | |
Josh Gao | 9d51240 | 2020-04-29 16:34:37 -0700 | [diff] [blame] | 245 | FDTRACK_TEST(dup, dup(STDOUT_FILENO)); |
| 246 | FDTRACK_TEST(dup2, dup2(STDOUT_FILENO, STDERR_FILENO)); |
| 247 | FDTRACK_TEST(dup3, dup3(STDOUT_FILENO, STDERR_FILENO, 0)); |
| 248 | FDTRACK_TEST_NAME(fcntl_F_DUPFD, "F_DUPFD", fcntl(STDOUT_FILENO, F_DUPFD, 0)); |
| 249 | FDTRACK_TEST_NAME(fcntl_F_DUPFD_CLOEXEC, "F_DUPFD_CLOEXEC", fcntl(STDOUT_FILENO, F_DUPFD_CLOEXEC, 0)); |
| 250 | |
Josh Gao | 1fad528 | 2020-04-29 17:00:13 -0700 | [diff] [blame] | 251 | FDTRACK_TEST(pipe, ({ |
| 252 | std::vector<int> fds = { -1, -1}; |
| 253 | if (pipe(fds.data()) != 0) { |
| 254 | err(1, "pipe failed"); |
| 255 | } |
| 256 | fds; |
| 257 | })); |
| 258 | |
| 259 | FDTRACK_TEST(pipe2, ({ |
| 260 | std::vector<int> fds = { -1, -1}; |
| 261 | if (pipe2(fds.data(), O_CLOEXEC) != 0) { |
| 262 | err(1, "pipe failed"); |
| 263 | } |
| 264 | fds; |
| 265 | })); |
| 266 | |
Josh Gao | b107eab | 2020-04-29 17:17:56 -0700 | [diff] [blame] | 267 | FDTRACK_TEST(socketpair, ({ |
| 268 | std::vector<int> fds = { -1, -1}; |
| 269 | if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds.data()) != 0) { |
| 270 | err(1, "socketpair failed"); |
| 271 | } |
| 272 | fds; |
| 273 | })); |
| 274 | |
Josh Gao | a38331d | 2020-04-29 17:06:14 -0700 | [diff] [blame] | 275 | FDTRACK_TEST(epoll_create, epoll_create(1)); |
| 276 | FDTRACK_TEST(epoll_create1, epoll_create1(0)); |
| 277 | |
Josh Gao | 7de4124 | 2020-04-29 17:08:46 -0700 | [diff] [blame] | 278 | FDTRACK_TEST(eventfd, eventfd(0, 0)); |
| 279 | |
Josh Gao | 9413ae7 | 2020-04-30 16:03:44 -0700 | [diff] [blame] | 280 | #if defined(__BIONIC__) |
| 281 | static int CreateListener() { |
Josh Gao | 9d51240 | 2020-04-29 16:34:37 -0700 | [diff] [blame] | 282 | android_fdtrack_set_enabled(false); |
| 283 | int listener = socket(AF_INET, SOCK_STREAM, 0); |
Josh Gao | 9413ae7 | 2020-04-30 16:03:44 -0700 | [diff] [blame] | 284 | CHECK_NE(-1, listener); |
Josh Gao | 9d51240 | 2020-04-29 16:34:37 -0700 | [diff] [blame] | 285 | |
| 286 | sockaddr_in addr = { |
| 287 | .sin_family = AF_INET, |
| 288 | .sin_port = 0, |
| 289 | .sin_addr = {htonl(INADDR_LOOPBACK)}, |
| 290 | }; |
| 291 | socklen_t addrlen = sizeof(addr); |
| 292 | |
Josh Gao | 9413ae7 | 2020-04-30 16:03:44 -0700 | [diff] [blame] | 293 | CHECK_NE(-1, bind(listener, reinterpret_cast<sockaddr*>(&addr), addrlen)) << strerror(errno); |
| 294 | CHECK_NE(-1, getsockname(listener, reinterpret_cast<sockaddr*>(&addr), &addrlen)); |
| 295 | CHECK_EQ(static_cast<size_t>(addrlen), sizeof(addr)); |
| 296 | CHECK_NE(-1, listen(listener, 1)); |
Josh Gao | 9d51240 | 2020-04-29 16:34:37 -0700 | [diff] [blame] | 297 | |
| 298 | int connector = socket(AF_INET, SOCK_STREAM, 0); |
Josh Gao | 9413ae7 | 2020-04-30 16:03:44 -0700 | [diff] [blame] | 299 | CHECK_NE(-1, connector); |
| 300 | CHECK_NE(-1, connect(connector, reinterpret_cast<sockaddr*>(&addr), addrlen)); |
Josh Gao | 9d51240 | 2020-04-29 16:34:37 -0700 | [diff] [blame] | 301 | android_fdtrack_set_enabled(true); |
Josh Gao | 9413ae7 | 2020-04-30 16:03:44 -0700 | [diff] [blame] | 302 | |
| 303 | return listener; |
| 304 | } |
Josh Gao | 9d51240 | 2020-04-29 16:34:37 -0700 | [diff] [blame] | 305 | #endif |
| 306 | |
Josh Gao | 9413ae7 | 2020-04-30 16:03:44 -0700 | [diff] [blame] | 307 | FDTRACK_TEST_NAME(accept, "accept4", accept(CreateListener(), nullptr, nullptr)); |
| 308 | FDTRACK_TEST(accept4, accept4(CreateListener(), nullptr, nullptr, 0)); |
| 309 | |
Josh Gao | 9d51240 | 2020-04-29 16:34:37 -0700 | [diff] [blame] | 310 | FDTRACK_TEST(recvmsg, ({ |
| 311 | android_fdtrack_set_enabled(false); |
| 312 | int sockets[2]; |
| 313 | ASSERT_NE(-1, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, sockets)); |
| 314 | ASSERT_EQ(3, SendFileDescriptors(sockets[0], "foo", 3, STDIN_FILENO)); |
| 315 | android_fdtrack_set_enabled(true); |
| 316 | |
| 317 | char buf[4]; |
| 318 | unique_fd received_fd; |
| 319 | ASSERT_EQ(3, ReceiveFileDescriptors(sockets[1], buf, sizeof(buf), &received_fd)); |
| 320 | received_fd.release(); |
| 321 | })); |
Josh Gao | 4129113 | 2020-05-07 19:43:38 -0700 | [diff] [blame] | 322 | |
| 323 | FDTRACK_TEST_NAME(vfork, "open", ({ |
| 324 | int fd = open("/dev/null", O_RDONLY); |
| 325 | |
| 326 | pid_t rc = vfork(); |
| 327 | ASSERT_NE(-1, rc); |
| 328 | |
| 329 | if (rc == 0) { |
| 330 | close(fd); |
| 331 | _exit(0); |
| 332 | } |
| 333 | |
| 334 | int status; |
| 335 | pid_t wait_result = waitpid(rc, &status, 0); |
| 336 | ASSERT_EQ(wait_result, rc); |
| 337 | ASSERT_TRUE(WIFEXITED(status)); |
| 338 | ASSERT_EQ(0, WEXITSTATUS(status)); |
| 339 | |
| 340 | fd; |
| 341 | })); |
Josh Gao | 9d51240 | 2020-04-29 16:34:37 -0700 | [diff] [blame] | 342 | // clang-format on |