blob: 57b9d37428ddd8448495ac7d330d3bbe74c31170 [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>
Josh Gaoad8f02d2020-01-28 13:54:00 -080030#include <stdint.h>
Josh Gao97271922019-11-06 13:15:00 -080031
32#include <array>
33#include <mutex>
Christopher Ferris459eecb2022-01-07 13:38:10 -080034#include <string>
35#include <string_view>
Josh Gao1cb36812021-03-11 21:11:37 -080036#include <thread>
37#include <utility>
Josh Gao97271922019-11-06 13:15:00 -080038#include <vector>
39
Josh Gao6f088662020-01-28 15:13:47 -080040#include <android/fdsan.h>
Josh Gao1cb36812021-03-11 21:11:37 -080041#include <android/set_abort_message.h>
Josh Gao75962502020-01-28 13:24:33 -080042#include <bionic/fdtrack.h>
Josh Gao97271922019-11-06 13:15:00 -080043
44#include <android-base/no_destructor.h>
45#include <android-base/thread_annotations.h>
46#include <async_safe/log.h>
47#include <bionic/reserved_signals.h>
Christopher Ferrise2f58a32023-05-17 16:04:38 -070048
49#include <unwindstack/AndroidUnwinder.h>
Josh Gao97271922019-11-06 13:15:00 -080050
51struct FdEntry {
52 std::mutex mutex;
Christopher Ferris459eecb2022-01-07 13:38:10 -080053 std::vector<unwindstack::FrameData> backtrace GUARDED_BY(mutex);
Josh Gao97271922019-11-06 13:15:00 -080054};
55
56extern "C" void fdtrack_dump();
Josh Gao1cb36812021-03-11 21:11:37 -080057extern "C" void fdtrack_dump_fatal();
Josh Gaoad8f02d2020-01-28 13:54:00 -080058
59using fdtrack_callback_t = bool (*)(int fd, const char* const* function_names,
60 const uint64_t* function_offsets, size_t count, void* arg);
61extern "C" void fdtrack_iterate(fdtrack_callback_t callback, void* arg);
62
Josh Gao97271922019-11-06 13:15:00 -080063static void fd_hook(android_fdtrack_event* event);
64
65// Backtraces for the first 4k file descriptors ought to be enough to diagnose an fd leak.
66static constexpr size_t kFdTableSize = 4096;
Josh Gao55b91af2020-06-02 15:54:32 -070067
Christopher Ferris459eecb2022-01-07 13:38:10 -080068// Only unwind up to 32 frames outside of libfdtrack.so.
69static constexpr size_t kStackDepth = 32;
70
Josh Gao97271922019-11-06 13:15:00 -080071static bool installed = false;
Josh Gao38d00b82020-04-21 17:05:32 -070072static std::array<FdEntry, kFdTableSize> stack_traces [[clang::no_destroy]];
Christopher Ferrise2f58a32023-05-17 16:04:38 -070073static unwindstack::AndroidLocalUnwinder& Unwinder() {
74 // Skip any initial frames from libfdtrack.so.
75 // Also ignore frames from ART (http://b/236197847) because we'd rather spend
76 // our precious few frames on the actual Java calling code rather than the
77 // implementation of JNI!
78 static android::base::NoDestructor<unwindstack::AndroidLocalUnwinder> unwinder(
79 std::vector<std::string>{"libfdtrack.so", "libart.so"});
80 return *unwinder.get();
Josh Gao97271922019-11-06 13:15:00 -080081}
82
83__attribute__((constructor)) static void ctor() {
Josh Gaoad8f02d2020-01-28 13:54:00 -080084 for (auto& entry : stack_traces) {
85 entry.backtrace.reserve(kStackDepth);
86 }
87
Josh Gao1cb36812021-03-11 21:11:37 -080088 struct sigaction sa = {};
89 sa.sa_sigaction = [](int, siginfo_t* siginfo, void*) {
90 if (siginfo->si_code == SI_QUEUE && siginfo->si_int == 1) {
91 fdtrack_dump_fatal();
92 } else {
93 fdtrack_dump();
94 }
95 };
96 sa.sa_flags = SA_SIGINFO | SA_ONSTACK;
97 sigaction(BIONIC_SIGNAL_FDTRACK, &sa, nullptr);
98
Christopher Ferrise2f58a32023-05-17 16:04:38 -070099 unwindstack::ErrorData error;
100 if (Unwinder().Initialize(error)) {
Josh Gao97271922019-11-06 13:15:00 -0800101 android_fdtrack_hook_t expected = nullptr;
102 installed = android_fdtrack_compare_exchange_hook(&expected, &fd_hook);
103 }
Josh Gaodcc97c02020-12-09 14:01:13 -0800104
105 android_fdtrack_set_globally_enabled(true);
Josh Gao97271922019-11-06 13:15:00 -0800106}
107
108__attribute__((destructor)) static void dtor() {
109 if (installed) {
110 android_fdtrack_hook_t expected = &fd_hook;
111 android_fdtrack_compare_exchange_hook(&expected, nullptr);
112 }
113}
114
115FdEntry* GetFdEntry(int fd) {
116 if (fd >= 0 && fd < static_cast<int>(kFdTableSize)) {
117 return &stack_traces[fd];
118 }
119 return nullptr;
120}
121
122static void fd_hook(android_fdtrack_event* event) {
123 if (event->type == ANDROID_FDTRACK_EVENT_TYPE_CREATE) {
124 if (FdEntry* entry = GetFdEntry(event->fd); entry) {
125 std::lock_guard<std::mutex> lock(entry->mutex);
126 entry->backtrace.clear();
Christopher Ferris459eecb2022-01-07 13:38:10 -0800127
Christopher Ferrise2f58a32023-05-17 16:04:38 -0700128 unwindstack::AndroidUnwinderData data(kStackDepth);
129 if (Unwinder().Unwind(data)) {
130 entry->backtrace = std::move(data.frames);
131 }
Josh Gao97271922019-11-06 13:15:00 -0800132 }
133 } else if (event->type == ANDROID_FDTRACK_EVENT_TYPE_CLOSE) {
134 if (FdEntry* entry = GetFdEntry(event->fd); entry) {
135 std::lock_guard<std::mutex> lock(entry->mutex);
136 entry->backtrace.clear();
137 }
138 }
139}
140
Josh Gaoad8f02d2020-01-28 13:54:00 -0800141void fdtrack_iterate(fdtrack_callback_t callback, void* arg) {
Josh Gao97271922019-11-06 13:15:00 -0800142 bool prev = android_fdtrack_set_enabled(false);
Josh Gaoad8f02d2020-01-28 13:54:00 -0800143
Josh Gao97271922019-11-06 13:15:00 -0800144 for (int fd = 0; fd < static_cast<int>(stack_traces.size()); ++fd) {
Josh Gaoad8f02d2020-01-28 13:54:00 -0800145 const char* function_names[kStackDepth];
146 uint64_t function_offsets[kStackDepth];
Josh Gao97271922019-11-06 13:15:00 -0800147 FdEntry* entry = GetFdEntry(fd);
148 if (!entry) {
149 continue;
150 }
151
Josh Gao50955c42020-01-28 14:10:19 -0800152 if (!entry->mutex.try_lock()) {
153 async_safe_format_log(ANDROID_LOG_WARN, "fdtrack", "fd %d locked, skipping", fd);
154 continue;
155 }
156
Josh Gao97271922019-11-06 13:15:00 -0800157 if (entry->backtrace.empty()) {
Josh Gaoad8f02d2020-01-28 13:54:00 -0800158 entry->mutex.unlock();
159 continue;
160 } else if (entry->backtrace.size() < 2) {
161 async_safe_format_log(ANDROID_LOG_WARN, "fdtrack", "fd %d missing frames: size = %zu", fd,
162 entry->backtrace.size());
163
164 entry->mutex.unlock();
Josh Gao97271922019-11-06 13:15:00 -0800165 continue;
166 }
167
Christopher Ferris459eecb2022-01-07 13:38:10 -0800168 for (size_t i = 0; i < entry->backtrace.size(); ++i) {
169 function_names[i] = entry->backtrace[i].function_name.c_str();
170 function_offsets[i] = entry->backtrace[i].function_offset;
Josh Gao97271922019-11-06 13:15:00 -0800171 }
Josh Gao50955c42020-01-28 14:10:19 -0800172
Christopher Ferris459eecb2022-01-07 13:38:10 -0800173 bool should_continue =
174 callback(fd, function_names, function_offsets, entry->backtrace.size(), arg);
Josh Gaoad8f02d2020-01-28 13:54:00 -0800175
Josh Gao50955c42020-01-28 14:10:19 -0800176 entry->mutex.unlock();
Josh Gaoad8f02d2020-01-28 13:54:00 -0800177
178 if (!should_continue) {
179 break;
180 }
Josh Gao97271922019-11-06 13:15:00 -0800181 }
Josh Gaoad8f02d2020-01-28 13:54:00 -0800182
Josh Gao97271922019-11-06 13:15:00 -0800183 android_fdtrack_set_enabled(prev);
184}
Josh Gaoad8f02d2020-01-28 13:54:00 -0800185
Josh Gao1cb36812021-03-11 21:11:37 -0800186static size_t hash_stack(const char* const* function_names, const uint64_t* function_offsets,
187 size_t stack_depth) {
188 size_t hash = 0;
189 for (size_t i = 0; i < stack_depth; ++i) {
190 // To future maintainers: if a libc++ update ever makes this invalid, replace this with +.
191 hash = std::__hash_combine(hash, std::hash<std::string_view>()(function_names[i]));
192 hash = std::__hash_combine(hash, std::hash<uint64_t>()(function_offsets[i]));
193 }
194 return hash;
195}
196
197static void fdtrack_dump_impl(bool fatal) {
Josh Gaoad8f02d2020-01-28 13:54:00 -0800198 if (!installed) {
199 async_safe_format_log(ANDROID_LOG_INFO, "fdtrack", "fdtrack not installed");
200 } else {
201 async_safe_format_log(ANDROID_LOG_INFO, "fdtrack", "fdtrack dumping...");
202 }
203
Josh Gao1cb36812021-03-11 21:11:37 -0800204 // If we're aborting, identify the most common stack in the hopes that it's the culprit,
205 // and emit that in the abort message so crash reporting can separate different fd leaks out.
206 // This is horrible and quadratic, but we need to avoid allocation since this can happen in
207 // response to a signal generated asynchronously. We're only going to dump 1k fds by default,
208 // and we're about to blow up the entire system, so this isn't too expensive.
209 struct StackInfo {
210 size_t hash = 0;
211 size_t count = 0;
212
213 size_t stack_depth = 0;
Christopher Ferris459eecb2022-01-07 13:38:10 -0800214 const char* function_names[kStackDepth];
215 uint64_t function_offsets[kStackDepth];
Josh Gao1cb36812021-03-11 21:11:37 -0800216 };
217 struct StackList {
218 size_t count = 0;
219 std::array<StackInfo, 128> data;
220 };
221 static StackList stacks;
222
Josh Gaoad8f02d2020-01-28 13:54:00 -0800223 fdtrack_iterate(
Josh Gao1cb36812021-03-11 21:11:37 -0800224 [](int fd, const char* const* function_names, const uint64_t* function_offsets,
225 size_t stack_depth, void* stacks_ptr) {
226 auto stacks = static_cast<StackList*>(stacks_ptr);
Josh Gaoad8f02d2020-01-28 13:54:00 -0800227 uint64_t fdsan_owner = android_fdsan_get_owner_tag(fd);
228 if (fdsan_owner != 0) {
Yuxian Xu3a5ddd72020-04-09 10:35:37 +0800229 async_safe_format_log(ANDROID_LOG_INFO, "fdtrack", "fd %d: (owner = 0x%" PRIx64 ")", fd,
Josh Gaoad8f02d2020-01-28 13:54:00 -0800230 fdsan_owner);
231 } else {
232 async_safe_format_log(ANDROID_LOG_INFO, "fdtrack", "fd %d: (unowned)", fd);
233 }
234
Josh Gao1cb36812021-03-11 21:11:37 -0800235 for (size_t i = 0; i < stack_depth; ++i) {
Josh Gaoad8f02d2020-01-28 13:54:00 -0800236 async_safe_format_log(ANDROID_LOG_INFO, "fdtrack", " %zu: %s+%" PRIu64, i,
237 function_names[i], function_offsets[i]);
238 }
239
Josh Gao1cb36812021-03-11 21:11:37 -0800240 if (stacks) {
241 size_t hash = hash_stack(function_names, function_offsets, stack_depth);
242 bool found_stack = false;
243 for (size_t i = 0; i < stacks->count; ++i) {
244 if (stacks->data[i].hash == hash) {
245 ++stacks->data[i].count;
246 found_stack = true;
247 break;
248 }
249 }
250
251 if (!found_stack) {
252 if (stacks->count < stacks->data.size()) {
253 auto& stack = stacks->data[stacks->count++];
254 stack.hash = hash;
255 stack.count = 1;
256 stack.stack_depth = stack_depth;
257 for (size_t i = 0; i < stack_depth; ++i) {
258 stack.function_names[i] = function_names[i];
259 stack.function_offsets[i] = function_offsets[i];
260 }
261 }
262 }
263 }
264
Josh Gaoad8f02d2020-01-28 13:54:00 -0800265 return true;
266 },
Josh Gao1cb36812021-03-11 21:11:37 -0800267 fatal ? &stacks : nullptr);
268
269 if (fatal) {
270 // Find the most common stack.
271 size_t max = 0;
272 StackInfo* stack = nullptr;
273 for (size_t i = 0; i < stacks.count; ++i) {
274 if (stacks.data[i].count > max) {
275 stack = &stacks.data[i];
276 max = stack->count;
277 }
278 }
279
280 static char buf[1024];
281
282 if (!stack) {
283 async_safe_format_buffer(buf, sizeof(buf),
284 "aborting due to fd leak: failed to find most common stack");
285 } else {
286 char* p = buf;
287 p += async_safe_format_buffer(buf, sizeof(buf),
288 "aborting due to fd leak: most common stack =\n");
289
290 for (size_t i = 0; i < stack->stack_depth; ++i) {
291 ssize_t bytes_left = buf + sizeof(buf) - p;
292 if (bytes_left > 0) {
293 p += async_safe_format_buffer(p, buf + sizeof(buf) - p, " %zu: %s+%" PRIu64 "\n", i,
294 stack->function_names[i], stack->function_offsets[i]);
295 }
296 }
297 }
298
299 android_set_abort_message(buf);
300
301 // Abort on a different thread to avoid ART dumping runtime stacks.
302 std::thread([]() { abort(); }).join();
303 }
304}
305
306void fdtrack_dump() {
307 fdtrack_dump_impl(false);
308}
309
310void fdtrack_dump_fatal() {
311 fdtrack_dump_impl(true);
Josh Gaoad8f02d2020-01-28 13:54:00 -0800312}