Josh Gao | ad8f02d | 2020-01-28 13:54:00 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2020 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include <dlfcn.h> |
| 30 | #include <err.h> |
| 31 | #include <fcntl.h> |
| 32 | #include <stdio.h> |
| 33 | #include <unistd.h> |
| 34 | |
| 35 | #include <map> |
| 36 | #include <vector> |
| 37 | |
| 38 | #include <gtest/gtest.h> |
| 39 | |
| 40 | struct FdtrackFrame { |
| 41 | const char* function_name; |
| 42 | uint64_t function_offset; |
| 43 | }; |
| 44 | |
| 45 | std::map<int, std::vector<FdtrackFrame>> RunFdtrack(std::function<void()> f) { |
| 46 | void* libfdtrack = dlopen("libfdtrack.so", RTLD_NOW); |
| 47 | if (!libfdtrack) { |
| 48 | errx(1, "failed to dlopen libfdtrack.so: %s", dlerror()); |
| 49 | } |
| 50 | |
| 51 | using fdtrack_callback_t = bool (*)(int fd, const char* const* function_names, |
| 52 | const uint64_t* function_offsets, size_t count, void* arg); |
| 53 | auto fdtrack_iterate = reinterpret_cast<void (*)(fdtrack_callback_t, void* arg)>( |
| 54 | dlsym(libfdtrack, "fdtrack_iterate")); |
| 55 | if (!fdtrack_iterate) { |
| 56 | errx(1, "failed to dlsym fdtrack_iterate"); |
| 57 | } |
| 58 | |
| 59 | f(); |
| 60 | |
| 61 | std::map<int, std::vector<FdtrackFrame>> result; |
| 62 | fdtrack_iterate( |
| 63 | [](int fd, const char* const* function_names, const uint64_t* function_offsets, size_t count, |
| 64 | void* arg) { |
| 65 | auto& map = *static_cast<decltype(result)*>(arg); |
| 66 | for (size_t i = 0; i < count; ++i) { |
| 67 | map[fd].push_back(FdtrackFrame{ |
| 68 | .function_name = function_names[i], |
| 69 | .function_offset = function_offsets[i], |
| 70 | }); |
| 71 | } |
| 72 | |
| 73 | return true; |
| 74 | }, |
| 75 | &result); |
| 76 | |
| 77 | return result; |
| 78 | } |
| 79 | |
| 80 | TEST(fdtrack, open) { |
| 81 | static int fd = -1; |
| 82 | auto result = RunFdtrack([]() { fd = open("/dev/null", O_RDONLY | O_CLOEXEC); }); |
| 83 | |
| 84 | ASSERT_NE(-1, fd); |
| 85 | ASSERT_EQ(1, result.size()); |
| 86 | ASSERT_EQ(fd, result.begin()->first); |
| 87 | ASSERT_NE(nullptr, strstr(result.begin()->second.at(0).function_name, "open")); |
| 88 | } |
| 89 | |
| 90 | TEST(fdtrack, close) { |
| 91 | static int fd1 = -1; |
| 92 | static int fd2 = -1; |
| 93 | static int fd3 = -1; |
| 94 | auto result = RunFdtrack([]() { |
| 95 | fd1 = open("/dev/null", O_RDONLY | O_CLOEXEC); |
| 96 | fd2 = open("/dev/null", O_RDONLY | O_CLOEXEC); |
| 97 | fd3 = open("/dev/null", O_RDONLY | O_CLOEXEC); |
| 98 | close(fd2); |
| 99 | }); |
| 100 | |
| 101 | ASSERT_NE(-1, fd1); |
| 102 | ASSERT_NE(-1, fd2); |
| 103 | ASSERT_NE(-1, fd3); |
| 104 | |
| 105 | ASSERT_EQ(2, result.size()); |
| 106 | ASSERT_EQ(1, result.count(fd1)); |
| 107 | ASSERT_EQ(1, result.count(fd3)); |
| 108 | |
| 109 | ASSERT_NE(nullptr, strstr(result[fd1].at(0).function_name, "open")); |
| 110 | ASSERT_NE(nullptr, strstr(result[fd3].at(0).function_name, "open")); |
| 111 | } |