1 //===-- sanitizer_libignore.cc --------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "sanitizer_platform.h"
12 #if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_MAC
14 #include "sanitizer_libignore.h"
15 #include "sanitizer_flags.h"
16 #include "sanitizer_posix.h"
17 #include "sanitizer_procmaps.h"
19 namespace __sanitizer {
21 LibIgnore::LibIgnore(LinkerInitialized) {
24 void LibIgnore::AddIgnoredLibrary(const char *name_templ) {
25 BlockingMutexLock lock(&mutex_);
26 if (count_ >= kMaxLibs) {
27 Report("%s: too many ignored libraries (max: %d)\n", SanitizerToolName,
31 Lib *lib = &libs_[count_++];
32 lib->templ = internal_strdup(name_templ);
34 lib->real_name = nullptr;
38 void LibIgnore::OnLibraryLoaded(const char *name) {
39 BlockingMutexLock lock(&mutex_);
40 // Try to match suppressions with symlink target.
41 InternalScopedString buf(kMaxPathLength);
42 if (name && internal_readlink(name, buf.data(), buf.size() - 1) > 0 &&
44 for (uptr i = 0; i < count_; i++) {
46 if (!lib->loaded && (!lib->real_name) &&
47 TemplateMatch(lib->templ, name))
48 lib->real_name = internal_strdup(buf.data());
52 // Scan suppressions list and find newly loaded and unloaded libraries.
53 ListOfModules modules;
55 for (uptr i = 0; i < count_; i++) {
58 for (const auto &mod : modules) {
59 for (const auto &range : mod.ranges()) {
60 if (!range.executable)
62 if (!TemplateMatch(lib->templ, mod.full_name()) &&
64 internal_strcmp(lib->real_name, mod.full_name()) == 0))
67 Report("%s: called_from_lib suppression '%s' is matched against"
68 " 2 libraries: '%s' and '%s'\n",
69 SanitizerToolName, lib->templ, lib->name, mod.full_name());
76 "Matched called_from_lib suppression '%s' against library"
78 lib->templ, mod.full_name());
80 lib->name = internal_strdup(mod.full_name());
81 const uptr idx = atomic_load(&loaded_count_, memory_order_relaxed);
82 code_ranges_[idx].begin = range.beg;
83 code_ranges_[idx].end = range.end;
84 atomic_store(&loaded_count_, idx + 1, memory_order_release);
88 if (lib->loaded && !loaded) {
89 Report("%s: library '%s' that was matched against called_from_lib"
90 " suppression '%s' is unloaded\n",
91 SanitizerToolName, lib->name, lib->templ);
97 void LibIgnore::OnLibraryUnloaded() {
98 OnLibraryLoaded(nullptr);
101 } // namespace __sanitizer
103 #endif // #if SANITIZER_FREEBSD || SANITIZER_LINUX