blob: 95758bf2a4db87cc44075cc2edc3aec19163086d [file] [log] [blame]
Josh Gao97271922019-11-06 13:15:00 -08001/*
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 Gao9d512402020-04-29 16:34:37 -070023#include <netinet/in.h>
Josh Gao97271922019-11-06 13:15:00 -080024#include <stdlib.h>
Josh Gaoa38331d2020-04-29 17:06:14 -070025#include <sys/epoll.h>
Josh Gao7de41242020-04-29 17:08:46 -070026#include <sys/eventfd.h>
Josh Gao97271922019-11-06 13:15:00 -080027#include <sys/types.h>
28#include <unistd.h>
29
30#if defined(__BIONIC__)
Josh Gao75962502020-01-28 13:24:33 -080031#include "platform/bionic/fdtrack.h"
Josh Gao9413ae72020-04-30 16:03:44 -070032#include "platform/bionic/reserved_signals.h"
Josh Gao97271922019-11-06 13:15:00 -080033#endif
34
35#include <vector>
36
Josh Gao9d512402020-04-29 16:34:37 -070037#include <android-base/cmsg.h>
Josh Gao9413ae72020-04-30 16:03:44 -070038#include <android-base/logging.h>
Josh Gao97271922019-11-06 13:15:00 -080039#include <android-base/unique_fd.h>
40
Josh Gao9d512402020-04-29 16:34:37 -070041using android::base::ReceiveFileDescriptors;
42using android::base::SendFileDescriptors;
43using android::base::unique_fd;
44
Josh Gao97271922019-11-06 13:15:00 -080045#if defined(__BIONIC__)
Josh Gao9413ae72020-04-30 16:03:44 -070046void DumpEvent(std::vector<android_fdtrack_event>* events, size_t index) {
47 auto& event = (*events)[index];
48 if (event.type == ANDROID_FDTRACK_EVENT_TYPE_CREATE) {
49 fprintf(stderr, " event %zu: fd %d created by %s\n", index, event.fd,
50 event.data.create.function_name);
51 } else if (event.type == ANDROID_FDTRACK_EVENT_TYPE_CLOSE) {
52 fprintf(stderr, " event %zu: fd %d closed\n", index, event.fd);
53 } else {
54 errx(1, "unexpected fdtrack event type: %d", event.type);
55 }
56}
57
Josh Gao97271922019-11-06 13:15:00 -080058std::vector<android_fdtrack_event> FdtrackRun(void (*func)()) {
59 // Each bionic test is run in separate process, so we can safely use a static here.
60 static std::vector<android_fdtrack_event> events;
61 events.clear();
62
63 android_fdtrack_hook_t previous = nullptr;
64 android_fdtrack_hook_t hook = [](android_fdtrack_event* event) {
65 events.push_back(*event);
66 };
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 Gao9413ae72020-04-30 16:03:44 -070083 // 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 Gao97271922019-11-06 13:15:00 -0800107 return std::move(events);
108}
Josh Gao97271922019-11-06 13:15:00 -0800109
Josh Gao9d512402020-04-29 16:34:37 -0700110const 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 Gao97271922019-11-06 13:15:00 -0800117}
Josh Gao9d512402020-04-29 16:34:37 -0700118#endif
Josh Gao97271922019-11-06 13:15:00 -0800119
120TEST(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
132TEST(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 Gao9d512402020-04-29 16:34:37 -0700164
165struct require_semicolon;
166
167#if defined(__BIONIC__)
Josh Gao1fad5282020-04-29 17:00:13 -0700168void SetFdResult(std::vector<int>* output, int fd) {
169 output->push_back(fd);
170}
171
172void 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) { \
181 ASSERT_NE(-1, fd); \
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 Gao9413ae72020-04-30 16:03:44 -0700187 DumpEvent(&events, i); \
Josh Gao1fad5282020-04-29 17:00:13 -0700188 } \
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 Gao9d512402020-04-29 16:34:37 -0700199 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
210FDTRACK_TEST(open, open("/dev/null", O_WRONLY | O_CLOEXEC));
211FDTRACK_TEST(openat, openat(AT_EMPTY_PATH, "/dev/null", O_WRONLY | O_CLOEXEC));
212FDTRACK_TEST(socket, socket(AF_UNIX, SOCK_STREAM, 0));
213
214FDTRACK_TEST(dup, dup(STDOUT_FILENO));
215FDTRACK_TEST(dup2, dup2(STDOUT_FILENO, STDERR_FILENO));
216FDTRACK_TEST(dup3, dup3(STDOUT_FILENO, STDERR_FILENO, 0));
217FDTRACK_TEST_NAME(fcntl_F_DUPFD, "F_DUPFD", fcntl(STDOUT_FILENO, F_DUPFD, 0));
218FDTRACK_TEST_NAME(fcntl_F_DUPFD_CLOEXEC, "F_DUPFD_CLOEXEC", fcntl(STDOUT_FILENO, F_DUPFD_CLOEXEC, 0));
219
Josh Gao1fad5282020-04-29 17:00:13 -0700220FDTRACK_TEST(pipe, ({
221 std::vector<int> fds = { -1, -1};
222 if (pipe(fds.data()) != 0) {
223 err(1, "pipe failed");
224 }
225 fds;
226}));
227
228FDTRACK_TEST(pipe2, ({
229 std::vector<int> fds = { -1, -1};
230 if (pipe2(fds.data(), O_CLOEXEC) != 0) {
231 err(1, "pipe failed");
232 }
233 fds;
234}));
235
Josh Gaob107eab2020-04-29 17:17:56 -0700236FDTRACK_TEST(socketpair, ({
237 std::vector<int> fds = { -1, -1};
238 if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds.data()) != 0) {
239 err(1, "socketpair failed");
240 }
241 fds;
242}));
243
Josh Gaoa38331d2020-04-29 17:06:14 -0700244FDTRACK_TEST(epoll_create, epoll_create(1));
245FDTRACK_TEST(epoll_create1, epoll_create1(0));
246
Josh Gao7de41242020-04-29 17:08:46 -0700247FDTRACK_TEST(eventfd, eventfd(0, 0));
248
Josh Gao9413ae72020-04-30 16:03:44 -0700249#if defined(__BIONIC__)
250static int CreateListener() {
Josh Gao9d512402020-04-29 16:34:37 -0700251 android_fdtrack_set_enabled(false);
252 int listener = socket(AF_INET, SOCK_STREAM, 0);
Josh Gao9413ae72020-04-30 16:03:44 -0700253 CHECK_NE(-1, listener);
Josh Gao9d512402020-04-29 16:34:37 -0700254
255 sockaddr_in addr = {
256 .sin_family = AF_INET,
257 .sin_port = 0,
258 .sin_addr = {htonl(INADDR_LOOPBACK)},
259 };
260 socklen_t addrlen = sizeof(addr);
261
Josh Gao9413ae72020-04-30 16:03:44 -0700262 CHECK_NE(-1, bind(listener, reinterpret_cast<sockaddr*>(&addr), addrlen)) << strerror(errno);
263 CHECK_NE(-1, getsockname(listener, reinterpret_cast<sockaddr*>(&addr), &addrlen));
264 CHECK_EQ(static_cast<size_t>(addrlen), sizeof(addr));
265 CHECK_NE(-1, listen(listener, 1));
Josh Gao9d512402020-04-29 16:34:37 -0700266
267 int connector = socket(AF_INET, SOCK_STREAM, 0);
Josh Gao9413ae72020-04-30 16:03:44 -0700268 CHECK_NE(-1, connector);
269 CHECK_NE(-1, connect(connector, reinterpret_cast<sockaddr*>(&addr), addrlen));
Josh Gao9d512402020-04-29 16:34:37 -0700270 android_fdtrack_set_enabled(true);
Josh Gao9413ae72020-04-30 16:03:44 -0700271
272 return listener;
273}
Josh Gao9d512402020-04-29 16:34:37 -0700274#endif
275
Josh Gao9413ae72020-04-30 16:03:44 -0700276FDTRACK_TEST_NAME(accept, "accept4", accept(CreateListener(), nullptr, nullptr));
277FDTRACK_TEST(accept4, accept4(CreateListener(), nullptr, nullptr, 0));
278
Josh Gao9d512402020-04-29 16:34:37 -0700279FDTRACK_TEST(recvmsg, ({
280 android_fdtrack_set_enabled(false);
281 int sockets[2];
282 ASSERT_NE(-1, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, sockets));
283 ASSERT_EQ(3, SendFileDescriptors(sockets[0], "foo", 3, STDIN_FILENO));
284 android_fdtrack_set_enabled(true);
285
286 char buf[4];
287 unique_fd received_fd;
288 ASSERT_EQ(3, ReceiveFileDescriptors(sockets[1], buf, sizeof(buf), &received_fd));
289 received_fd.release();
290}));
Josh Gao41291132020-05-07 19:43:38 -0700291
292FDTRACK_TEST_NAME(vfork, "open", ({
293 int fd = open("/dev/null", O_RDONLY);
294
295 pid_t rc = vfork();
296 ASSERT_NE(-1, rc);
297
298 if (rc == 0) {
299 close(fd);
300 _exit(0);
301 }
302
303 int status;
304 pid_t wait_result = waitpid(rc, &status, 0);
305 ASSERT_EQ(wait_result, rc);
306 ASSERT_TRUE(WIFEXITED(status));
307 ASSERT_EQ(0, WEXITSTATUS(status));
308
309 fd;
310}));
Josh Gao9d512402020-04-29 16:34:37 -0700311// clang-format on