Merge changes I8b25accf,I59013f0c,I6c881e5d,I66826f31,I552692ae, ...

* changes:
  fdtrack: add wrapper for socketpair.
  fdtrack: add wrapper for eventfd.
  fdtrack: add wrappers for epoll_create, epoll_create1.
  fdtrack: add wrappers for pipe, pipe2.
  fdtrack: add tests for individual wrappers.
  fdtrack: don't destroy traces.
This commit is contained in:
Josh Gao 2020-04-30 01:54:08 +00:00 committed by Gerrit Code Review
commit 4d0c40be89
9 changed files with 240 additions and 24 deletions

View file

@ -1022,8 +1022,7 @@ cc_library_static {
"bionic/dup.cpp",
"bionic/environ.cpp",
"bionic/error.cpp",
"bionic/eventfd_read.cpp",
"bionic/eventfd_write.cpp",
"bionic/eventfd.cpp",
"bionic/exec.cpp",
"bionic/faccessat.cpp",
"bionic/fchmod.cpp",
@ -1114,6 +1113,7 @@ cc_library_static {
"bionic/signal.cpp",
"bionic/sigprocmask.cpp",
"bionic/sleep.cpp",
"bionic/socketpair.cpp",
"bionic/spawn.cpp",
"bionic/stat.cpp",
"bionic/stdlib_l.cpp",

View file

@ -126,7 +126,7 @@ int __fcntl64:fcntl64(int, int, void*) lp32
int __fcntl:fcntl(int, int, void*) lp64
int flock(int, int) all
int __fchmod:fchmod(int, mode_t) all
int pipe2(int*, int) all
int __pipe2:pipe2(int*, int) all
int __dup:dup(int) all
int __dup3:dup3(int, int, int) all
int fsync(int) all
@ -243,7 +243,7 @@ int __signalfd4:signalfd4(int, const sigset64_t*, size_t, int) all
# sockets
int __socket:socket(int, int, int) arm,lp64
int socketpair(int, int, int, int*) arm,lp64
int __socketpair:socketpair(int, int, int, int*) arm,lp64
int bind(int, struct sockaddr*, socklen_t) arm,lp64
int __connect:connect(int, struct sockaddr*, socklen_t) arm,lp64
int listen(int, int) arm,lp64
@ -267,7 +267,7 @@ int __connect:socketcall:3(int, struct sockaddr*, socklen_t) x86
int listen:socketcall:4(int, int) x86
int getsockname:socketcall:6(int, struct sockaddr*, socklen_t*) x86
int getpeername:socketcall:7(int, struct sockaddr*, socklen_t*) x86
int socketpair:socketcall:8(int, int, int, int*) x86
int __socketpair:socketcall:8(int, int, int, int*) x86
ssize_t __sendto:socketcall:11(int, const void*, size_t, int, const struct sockaddr*, socklen_t) x86
ssize_t recvfrom:socketcall:12(int, void*, size_t, unsigned int, struct sockaddr*, socklen_t*) x86
int shutdown:socketcall:13(int, int) x86
@ -308,11 +308,11 @@ ssize_t tee(int, int, size_t, unsigned int) all
ssize_t splice(int, off64_t*, int, off64_t*, size_t, unsigned int) all
ssize_t vmsplice(int, const struct iovec*, size_t, unsigned int) all
int epoll_create1(int) all
int __epoll_create1:epoll_create1(int) all
int epoll_ctl(int, int op, int, struct epoll_event*) all
int __epoll_pwait:epoll_pwait(int, struct epoll_event*, int, int, const sigset64_t*, size_t) all
int eventfd:eventfd2(unsigned int, int) all
int __eventfd:eventfd2(unsigned int, int) all
void _exit|_Exit:exit_group(int) all
void __exit:exit(int) all

View file

@ -29,6 +29,18 @@
#include <sys/eventfd.h>
#include <unistd.h>
#include "private/bionic_fdtrack.h"
extern "C" int __eventfd(unsigned int initval, int flags);
int eventfd(unsigned int initval, int flags) {
return FDTRACK_CREATE(__eventfd(initval, flags));
}
int eventfd_read(int fd, eventfd_t* value) {
return (read(fd, value, sizeof(*value)) == sizeof(*value)) ? 0 : -1;
}
int eventfd_write(int fd, eventfd_t value) {
return (write(fd, &value, sizeof(value)) == sizeof(value)) ? 0 : -1;
}

View file

@ -29,6 +29,3 @@
#include <sys/eventfd.h>
#include <unistd.h>
int eventfd_write(int fd, eventfd_t value) {
return (write(fd, &value, sizeof(value)) == sizeof(value)) ? 0 : -1;
}

View file

@ -28,6 +28,24 @@
#include <unistd.h>
#include "private/bionic_fdtrack.h"
extern "C" int __pipe2(int pipefd[2], int flags);
int pipe(int pipefd[2]) {
return pipe2(pipefd, 0);
int rc = __pipe2(pipefd, 0);
if (rc == 0) {
FDTRACK_CREATE(pipefd[0]);
FDTRACK_CREATE(pipefd[1]);
}
return rc;
}
int pipe2(int pipefd[2], int flags) {
int rc = __pipe2(pipefd, flags);
if (rc == 0) {
FDTRACK_CREATE(pipefd[0]);
FDTRACK_CREATE(pipefd[1]);
}
return rc;
}

View file

@ -0,0 +1,43 @@
/*
* Copyright (C) 2020 The Android Open Source Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/socket.h>
#include <unistd.h>
#include "private/bionic_fdtrack.h"
extern "C" int __socketpair(int domain, int type, int protocol, int sv[2]);
int socketpair(int domain, int type, int protocol, int sv[2]) {
int rc = __socketpair(domain, type, protocol, sv);
if (rc == 0) {
FDTRACK_CREATE(sv[0]);
FDTRACK_CREATE(sv[1]);
}
return rc;
}

View file

@ -30,7 +30,9 @@
#include <sys/epoll.h>
#include "private/SigSetConverter.h"
#include "private/bionic_fdtrack.h"
extern "C" int __epoll_create1(int flags);
extern "C" int __epoll_pwait(int, epoll_event*, int, int, const sigset64_t*, size_t);
int epoll_create(int size) {
@ -38,7 +40,11 @@ int epoll_create(int size) {
errno = EINVAL;
return -1;
}
return epoll_create1(0);
return FDTRACK_CREATE(__epoll_create1(0));
}
int epoll_create1(int flags) {
return FDTRACK_CREATE(__epoll_create1(flags));
}
int epoll_pwait(int fd, epoll_event* events, int max_events, int timeout, const sigset_t* ss) {

View file

@ -60,7 +60,7 @@ static constexpr size_t kFdTableSize = 4096;
static constexpr size_t kStackDepth = 10;
static bool installed = false;
static std::array<FdEntry, kFdTableSize> stack_traces;
static std::array<FdEntry, kFdTableSize> stack_traces [[clang::no_destroy]];
static unwindstack::LocalUnwinder& Unwinder() {
static android::base::NoDestructor<unwindstack::LocalUnwinder> unwinder;
return *unwinder.get();

View file

@ -20,7 +20,10 @@
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <sys/epoll.h>
#include <sys/eventfd.h>
#include <sys/types.h>
#include <unistd.h>
@ -30,8 +33,13 @@
#include <vector>
#include <android-base/cmsg.h>
#include <android-base/unique_fd.h>
using android::base::ReceiveFileDescriptors;
using android::base::SendFileDescriptors;
using android::base::unique_fd;
#if defined(__BIONIC__)
std::vector<android_fdtrack_event> FdtrackRun(void (*func)()) {
// Each bionic test is run in separate process, so we can safely use a static here.
@ -60,19 +68,16 @@ std::vector<android_fdtrack_event> FdtrackRun(void (*func)()) {
return std::move(events);
}
#endif
TEST(fdtrack, open) {
#if defined(__BIONIC__)
static int fd = -1;
auto events = FdtrackRun([]() { fd = open("/dev/null", O_WRONLY | O_CLOEXEC); });
ASSERT_NE(-1, fd);
ASSERT_EQ(1U, events.size());
ASSERT_EQ(fd, events[0].fd);
ASSERT_EQ(ANDROID_FDTRACK_EVENT_TYPE_CREATE, events[0].type);
ASSERT_STREQ("open", events[0].data.create.function_name);
#endif
const char* FdtrackEventTypeToName(android_fdtrack_event_type event_type) {
switch (event_type) {
case ANDROID_FDTRACK_EVENT_TYPE_CREATE:
return "created";
case ANDROID_FDTRACK_EVENT_TYPE_CLOSE:
return "closed";
}
}
#endif
TEST(fdtrack, close) {
#if defined(__BIONIC__)
@ -118,3 +123,138 @@ TEST(fdtrack, enable_disable) {
ASSERT_STREQ("open", events[1].data.create.function_name);
#endif
}
struct require_semicolon;
#if defined(__BIONIC__)
void SetFdResult(std::vector<int>* output, int fd) {
output->push_back(fd);
}
void SetFdResult(std::vector<int>* output, std::vector<int> fds) {
*output = fds;
}
#define FDTRACK_TEST_NAME(test_name, fdtrack_name, expression) \
TEST(fdtrack, test_name) { \
static std::vector<int> expected_fds; \
auto events = FdtrackRun([]() { SetFdResult(&expected_fds, expression); }); \
for (auto& fd : expected_fds) { \
ASSERT_NE(-1, fd); \
} \
if (events.size() != expected_fds.size()) { \
fprintf(stderr, "too many events received: expected %zu, got %zu:\n", expected_fds.size(), \
events.size()); \
for (size_t i = 0; i < events.size(); ++i) { \
auto& event = events[i]; \
if (event.type == ANDROID_FDTRACK_EVENT_TYPE_CREATE) { \
fprintf(stderr, " event %zu: fd %d created by %s\n", i, event.fd, \
event.data.create.function_name); \
} else if (event.type == ANDROID_FDTRACK_EVENT_TYPE_CLOSE) { \
fprintf(stderr, " event %zu: fd %d closed\n", i, event.fd); \
} else { \
errx(1, "unexpected fdtrack event type: %d", event.type); \
} \
} \
FAIL(); \
return; \
} \
for (auto& event : events) { \
ASSERT_NE(expected_fds.end(), \
std::find(expected_fds.begin(), expected_fds.end(), events[0].fd)); \
ASSERT_EQ(ANDROID_FDTRACK_EVENT_TYPE_CREATE, event.type); \
ASSERT_STREQ(fdtrack_name, event.data.create.function_name); \
} \
} \
struct require_semicolon
#else
#define FDTRACK_TEST_NAME(name, fdtrack_name, expression) \
TEST(fdtrack, name) {} \
struct require_semicolon
#endif
#define FDTRACK_TEST(name, expression) FDTRACK_TEST_NAME(name, #name, expression)
// clang-format misformats statement expressions pretty badly here:
// clang-format off
FDTRACK_TEST(open, open("/dev/null", O_WRONLY | O_CLOEXEC));
FDTRACK_TEST(openat, openat(AT_EMPTY_PATH, "/dev/null", O_WRONLY | O_CLOEXEC));
FDTRACK_TEST(socket, socket(AF_UNIX, SOCK_STREAM, 0));
FDTRACK_TEST(dup, dup(STDOUT_FILENO));
FDTRACK_TEST(dup2, dup2(STDOUT_FILENO, STDERR_FILENO));
FDTRACK_TEST(dup3, dup3(STDOUT_FILENO, STDERR_FILENO, 0));
FDTRACK_TEST_NAME(fcntl_F_DUPFD, "F_DUPFD", fcntl(STDOUT_FILENO, F_DUPFD, 0));
FDTRACK_TEST_NAME(fcntl_F_DUPFD_CLOEXEC, "F_DUPFD_CLOEXEC", fcntl(STDOUT_FILENO, F_DUPFD_CLOEXEC, 0));
FDTRACK_TEST(pipe, ({
std::vector<int> fds = { -1, -1};
if (pipe(fds.data()) != 0) {
err(1, "pipe failed");
}
fds;
}));
FDTRACK_TEST(pipe2, ({
std::vector<int> fds = { -1, -1};
if (pipe2(fds.data(), O_CLOEXEC) != 0) {
err(1, "pipe failed");
}
fds;
}));
FDTRACK_TEST(socketpair, ({
std::vector<int> fds = { -1, -1};
if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds.data()) != 0) {
err(1, "socketpair failed");
}
fds;
}));
FDTRACK_TEST(epoll_create, epoll_create(1));
FDTRACK_TEST(epoll_create1, epoll_create1(0));
FDTRACK_TEST(eventfd, eventfd(0, 0));
#if 0
// Why is this generating an extra socket/close event?
FDTRACK_TEST(accept, ({
android_fdtrack_set_enabled(false);
int listener = socket(AF_INET, SOCK_STREAM, 0);
ASSERT_NE(-1, listener);
sockaddr_in addr = {
.sin_family = AF_INET,
.sin_port = 0,
.sin_addr = {htonl(INADDR_LOOPBACK)},
};
socklen_t addrlen = sizeof(addr);
ASSERT_NE(-1, bind(listener, reinterpret_cast<sockaddr*>(&addr), addrlen)) << strerror(errno);
ASSERT_NE(-1, getsockname(listener, reinterpret_cast<sockaddr*>(&addr), &addrlen));
ASSERT_EQ(static_cast<size_t>(addrlen), sizeof(addr));
ASSERT_NE(-1, listen(listener, 1));
int connector = socket(AF_INET, SOCK_STREAM, 0);
ASSERT_NE(-1, connector);
ASSERT_NE(-1, connect(connector, reinterpret_cast<sockaddr*>(&addr), addrlen));
android_fdtrack_set_enabled(true);
int accepted = accept(listener, nullptr, nullptr);
accepted;
}));
#endif
FDTRACK_TEST(recvmsg, ({
android_fdtrack_set_enabled(false);
int sockets[2];
ASSERT_NE(-1, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, sockets));
ASSERT_EQ(3, SendFileDescriptors(sockets[0], "foo", 3, STDIN_FILENO));
android_fdtrack_set_enabled(true);
char buf[4];
unique_fd received_fd;
ASSERT_EQ(3, ReceiveFileDescriptors(sockets[1], buf, sizeof(buf), &received_fd));
received_fd.release();
}));
// clang-format on