| 1 | //===-- sanitizer_dl.cpp --------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file has helper functions that depend on libc's dynamic loading |
| 10 | // introspection. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "sanitizer_dl.h" |
| 15 | |
| 16 | #include "sanitizer_common/sanitizer_platform.h" |
| 17 | |
| 18 | #if SANITIZER_GLIBC |
| 19 | # include <dlfcn.h> |
| 20 | #endif |
| 21 | #if SANITIZER_GLIBC && !SANITIZER_GO |
| 22 | # include <resolv.h> |
| 23 | #endif |
| 24 | |
| 25 | namespace __sanitizer { |
| 26 | extern const char *SanitizerToolName; |
| 27 | |
| 28 | const char *DladdrSelfFName(void) { |
| 29 | // go-tsan can't link libdl before it was merged into glibc 2.34. |
| 30 | #if SANITIZER_GLIBC && !SANITIZER_GO |
| 31 | Dl_info info; |
| 32 | int ret = dladdr(address: (void *)&SanitizerToolName, info: &info); |
| 33 | if (ret) { |
| 34 | return info.dli_fname; |
| 35 | } |
| 36 | #endif |
| 37 | |
| 38 | return nullptr; |
| 39 | } |
| 40 | |
| 41 | char* (void* ld, char* addr) { |
| 42 | // go-tsan can't link libdl before it was merged into glibc 2.34. |
| 43 | #if SANITIZER_GLIBC && !SANITIZER_GO |
| 44 | Dl_info info; |
| 45 | if (dladdr(address: ld, info: &info) && info.dli_fbase) |
| 46 | addr = (char*)info.dli_fbase; |
| 47 | #endif // SANITIZER_GLIBC |
| 48 | return addr; |
| 49 | } |
| 50 | |
| 51 | void ClearDlerror() { |
| 52 | #if SANITIZER_GLIBC && !SANITIZER_GO |
| 53 | // Starting with glibc 2.34 |
| 54 | // (https://sourceware.org/git/?p=glibc.git;h=fada9018199c), failed dlfcn |
| 55 | // calls store a per-thread error struct in TLS (__libc_dlerror_result) |
| 56 | // instead of a pthread_key_t, and free it in __libc_thread_freeres() after |
| 57 | // pthread TSD destructors have already unregistered the thread from the |
| 58 | // sanitizer. Clear dlerror() while the thread is still tracked so that LSan |
| 59 | // does not report the pending error buffer as a leak if a leak check runs |
| 60 | // before __libc_thread_freeres(). |
| 61 | dlerror(); |
| 62 | dlerror(); |
| 63 | // Similarly, glibc's resolver allocates per-thread nameserver state in _res |
| 64 | // (such as _res._u._ext.nsaddrs) and frees it in res_thread_freeres() during |
| 65 | // __libc_thread_freeres(), after pthread TSD destructors have already |
| 66 | // unregistered the thread. Free it here and reset _res.options (matching |
| 67 | // glibc's res_thread_freeres) so LSan does not report it as a leak and any |
| 68 | // subsequent TSD destructor using the resolver re-initializes _res cleanly. |
| 69 | if (_res.nscount > 0) { |
| 70 | res_nclose(&_res); |
| 71 | _res.options = 0; |
| 72 | } |
| 73 | #endif |
| 74 | } |
| 75 | |
| 76 | } // namespace __sanitizer |
| 77 | |