blob: 831a50d1b5451daae6785b5c006cf5fd15f1980c [file] [log] [blame]
Josh Gao97271922019-11-06 13:15:00 -08001/*
2 * Copyright (C) 2019 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 <inttypes.h>
30
31#include <array>
32#include <mutex>
33#include <vector>
34
Josh Gao6f088662020-01-28 15:13:47 -080035#include <android/fdsan.h>
Josh Gao75962502020-01-28 13:24:33 -080036#include <bionic/fdtrack.h>
Josh Gao97271922019-11-06 13:15:00 -080037
38#include <android-base/no_destructor.h>
39#include <android-base/thread_annotations.h>
40#include <async_safe/log.h>
41#include <bionic/reserved_signals.h>
42#include <unwindstack/LocalUnwinder.h>
43
44struct FdEntry {
45 std::mutex mutex;
46 std::vector<unwindstack::LocalFrameData> backtrace GUARDED_BY(mutex);
47};
48
49extern "C" void fdtrack_dump();
50static void fd_hook(android_fdtrack_event* event);
51
52// Backtraces for the first 4k file descriptors ought to be enough to diagnose an fd leak.
53static constexpr size_t kFdTableSize = 4096;
54static constexpr size_t kStackDepth = 10;
55
56static bool installed = false;
57static std::array<FdEntry, kFdTableSize> stack_traces;
58static unwindstack::LocalUnwinder& Unwinder() {
59 static android::base::NoDestructor<unwindstack::LocalUnwinder> unwinder;
60 return *unwinder.get();
61}
62
63__attribute__((constructor)) static void ctor() {
64 signal(BIONIC_SIGNAL_FDTRACK, [](int) { fdtrack_dump(); });
65 if (Unwinder().Init()) {
66 android_fdtrack_hook_t expected = nullptr;
67 installed = android_fdtrack_compare_exchange_hook(&expected, &fd_hook);
68 }
69}
70
71__attribute__((destructor)) static void dtor() {
72 if (installed) {
73 android_fdtrack_hook_t expected = &fd_hook;
74 android_fdtrack_compare_exchange_hook(&expected, nullptr);
75 }
76}
77
78FdEntry* GetFdEntry(int fd) {
79 if (fd >= 0 && fd < static_cast<int>(kFdTableSize)) {
80 return &stack_traces[fd];
81 }
82 return nullptr;
83}
84
85static void fd_hook(android_fdtrack_event* event) {
86 if (event->type == ANDROID_FDTRACK_EVENT_TYPE_CREATE) {
87 if (FdEntry* entry = GetFdEntry(event->fd); entry) {
88 std::lock_guard<std::mutex> lock(entry->mutex);
89 entry->backtrace.clear();
90 Unwinder().Unwind(&entry->backtrace, kStackDepth);
91 }
92 } else if (event->type == ANDROID_FDTRACK_EVENT_TYPE_CLOSE) {
93 if (FdEntry* entry = GetFdEntry(event->fd); entry) {
94 std::lock_guard<std::mutex> lock(entry->mutex);
95 entry->backtrace.clear();
96 }
97 }
98}
99
100void fdtrack_dump() {
101 if (!installed) {
102 async_safe_format_log(ANDROID_LOG_INFO, "fdtrack", "fdtrack not installed");
103 } else {
104 async_safe_format_log(ANDROID_LOG_INFO, "fdtrack", "fdtrack dumping...");
105 }
106
107 bool prev = android_fdtrack_set_enabled(false);
108 for (int fd = 0; fd < static_cast<int>(stack_traces.size()); ++fd) {
109 FdEntry* entry = GetFdEntry(fd);
110 if (!entry) {
111 continue;
112 }
113
Josh Gao50955c42020-01-28 14:10:19 -0800114 if (!entry->mutex.try_lock()) {
115 async_safe_format_log(ANDROID_LOG_WARN, "fdtrack", "fd %d locked, skipping", fd);
116 continue;
117 }
118
Josh Gao97271922019-11-06 13:15:00 -0800119 if (entry->backtrace.empty()) {
120 continue;
121 }
122
Josh Gao6f088662020-01-28 15:13:47 -0800123 uint64_t fdsan_owner = android_fdsan_get_owner_tag(fd);
124
125 if (fdsan_owner != 0) {
126 async_safe_format_log(ANDROID_LOG_INFO, "fdtrack", "fd %d: (owner = %#" PRIx64 ")", fd,
127 fdsan_owner);
128 } else {
129 async_safe_format_log(ANDROID_LOG_INFO, "fdtrack", "fd %d: (unowned)", fd);
130 }
131
Josh Gao97271922019-11-06 13:15:00 -0800132 const size_t frame_skip = 2;
133 for (size_t i = frame_skip; i < entry->backtrace.size(); ++i) {
134 auto& frame = entry->backtrace[i];
135 async_safe_format_log(ANDROID_LOG_INFO, "fdtrack", " %zu: %s+%" PRIu64, i - frame_skip,
136 frame.function_name.c_str(), frame.function_offset);
137 }
Josh Gao50955c42020-01-28 14:10:19 -0800138
139 entry->mutex.unlock();
Josh Gao97271922019-11-06 13:15:00 -0800140 }
141 android_fdtrack_set_enabled(prev);
142}