]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/lsan/lsan_common_linux.cc
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r304149, and update
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / lsan / lsan_common_linux.cc
1 //=-- lsan_common_linux.cc ------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is a part of LeakSanitizer.
11 // Implementation of common leak checking functionality. Linux-specific code.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "sanitizer_common/sanitizer_platform.h"
16 #include "lsan_common.h"
17
18 #if CAN_SANITIZE_LEAKS && SANITIZER_LINUX
19 #include <link.h>
20
21 #include "sanitizer_common/sanitizer_common.h"
22 #include "sanitizer_common/sanitizer_flags.h"
23 #include "sanitizer_common/sanitizer_linux.h"
24 #include "sanitizer_common/sanitizer_stackdepot.h"
25
26 namespace __lsan {
27
28 static const char kLinkerName[] = "ld";
29
30 static char linker_placeholder[sizeof(LoadedModule)] ALIGNED(64);
31 static LoadedModule *linker = nullptr;
32
33 static bool IsLinker(const char* full_name) {
34   return LibraryNameIs(full_name, kLinkerName);
35 }
36
37 __attribute__((tls_model("initial-exec")))
38 THREADLOCAL int disable_counter;
39 bool DisabledInThisThread() { return disable_counter > 0; }
40 void DisableInThisThread() { disable_counter++; }
41 void EnableInThisThread() {
42   if (disable_counter == 0) {
43     DisableCounterUnderflow();
44   }
45   disable_counter--;
46 }
47
48 void InitializePlatformSpecificModules() {
49   ListOfModules modules;
50   modules.init();
51   for (LoadedModule &module : modules) {
52     if (!IsLinker(module.full_name())) continue;
53     if (linker == nullptr) {
54       linker = reinterpret_cast<LoadedModule *>(linker_placeholder);
55       *linker = module;
56       module = LoadedModule();
57     } else {
58       VReport(1, "LeakSanitizer: Multiple modules match \"%s\". "
59               "TLS will not be handled correctly.\n", kLinkerName);
60       linker->clear();
61       linker = nullptr;
62       return;
63     }
64   }
65   if (linker == nullptr) {
66     VReport(1, "LeakSanitizer: Dynamic linker not found. "
67                "TLS will not be handled correctly.\n");
68   }
69 }
70
71 static int ProcessGlobalRegionsCallback(struct dl_phdr_info *info, size_t size,
72                                         void *data) {
73   Frontier *frontier = reinterpret_cast<Frontier *>(data);
74   for (uptr j = 0; j < info->dlpi_phnum; j++) {
75     const ElfW(Phdr) *phdr = &(info->dlpi_phdr[j]);
76     // We're looking for .data and .bss sections, which reside in writeable,
77     // loadable segments.
78     if (!(phdr->p_flags & PF_W) || (phdr->p_type != PT_LOAD) ||
79         (phdr->p_memsz == 0))
80       continue;
81     uptr begin = info->dlpi_addr + phdr->p_vaddr;
82     uptr end = begin + phdr->p_memsz;
83     ScanGlobalRange(begin, end, frontier);
84   }
85   return 0;
86 }
87
88 // Scans global variables for heap pointers.
89 void ProcessGlobalRegions(Frontier *frontier) {
90   if (!flags()->use_globals) return;
91   dl_iterate_phdr(ProcessGlobalRegionsCallback, frontier);
92 }
93
94 LoadedModule *GetLinker() { return linker; }
95
96 void ProcessPlatformSpecificAllocations(Frontier *frontier) {}
97
98 struct DoStopTheWorldParam {
99   StopTheWorldCallback callback;
100   void *argument;
101 };
102
103 static int DoStopTheWorldCallback(struct dl_phdr_info *info, size_t size,
104                                   void *data) {
105   DoStopTheWorldParam *param = reinterpret_cast<DoStopTheWorldParam *>(data);
106   StopTheWorld(param->callback, param->argument);
107   return 1;
108 }
109
110 // LSan calls dl_iterate_phdr() from the tracer task. This may deadlock: if one
111 // of the threads is frozen while holding the libdl lock, the tracer will hang
112 // in dl_iterate_phdr() forever.
113 // Luckily, (a) the lock is reentrant and (b) libc can't distinguish between the
114 // tracer task and the thread that spawned it. Thus, if we run the tracer task
115 // while holding the libdl lock in the parent thread, we can safely reenter it
116 // in the tracer. The solution is to run stoptheworld from a dl_iterate_phdr()
117 // callback in the parent thread.
118 void DoStopTheWorld(StopTheWorldCallback callback, void *argument) {
119   DoStopTheWorldParam param = {callback, argument};
120   dl_iterate_phdr(DoStopTheWorldCallback, &param);
121 }
122
123 } // namespace __lsan
124
125 #endif // CAN_SANITIZE_LEAKS && SANITIZER_LINUX