1*7c3d14c8STreehugger Robot //=-- lsan_common_linux.cc ------------------------------------------------===//
2*7c3d14c8STreehugger Robot //
3*7c3d14c8STreehugger Robot // The LLVM Compiler Infrastructure
4*7c3d14c8STreehugger Robot //
5*7c3d14c8STreehugger Robot // This file is distributed under the University of Illinois Open Source
6*7c3d14c8STreehugger Robot // License. See LICENSE.TXT for details.
7*7c3d14c8STreehugger Robot //
8*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
9*7c3d14c8STreehugger Robot //
10*7c3d14c8STreehugger Robot // This file is a part of LeakSanitizer.
11*7c3d14c8STreehugger Robot // Implementation of common leak checking functionality. Linux-specific code.
12*7c3d14c8STreehugger Robot //
13*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
14*7c3d14c8STreehugger Robot
15*7c3d14c8STreehugger Robot #include "sanitizer_common/sanitizer_platform.h"
16*7c3d14c8STreehugger Robot #include "lsan_common.h"
17*7c3d14c8STreehugger Robot
18*7c3d14c8STreehugger Robot #if CAN_SANITIZE_LEAKS && SANITIZER_LINUX
19*7c3d14c8STreehugger Robot #include <link.h>
20*7c3d14c8STreehugger Robot
21*7c3d14c8STreehugger Robot #include "sanitizer_common/sanitizer_common.h"
22*7c3d14c8STreehugger Robot #include "sanitizer_common/sanitizer_flags.h"
23*7c3d14c8STreehugger Robot #include "sanitizer_common/sanitizer_linux.h"
24*7c3d14c8STreehugger Robot #include "sanitizer_common/sanitizer_stackdepot.h"
25*7c3d14c8STreehugger Robot
26*7c3d14c8STreehugger Robot namespace __lsan {
27*7c3d14c8STreehugger Robot
28*7c3d14c8STreehugger Robot static const char kLinkerName[] = "ld";
29*7c3d14c8STreehugger Robot
30*7c3d14c8STreehugger Robot static char linker_placeholder[sizeof(LoadedModule)] ALIGNED(64);
31*7c3d14c8STreehugger Robot static LoadedModule *linker = nullptr;
32*7c3d14c8STreehugger Robot
IsLinker(const char * full_name)33*7c3d14c8STreehugger Robot static bool IsLinker(const char* full_name) {
34*7c3d14c8STreehugger Robot return LibraryNameIs(full_name, kLinkerName);
35*7c3d14c8STreehugger Robot }
36*7c3d14c8STreehugger Robot
InitializePlatformSpecificModules()37*7c3d14c8STreehugger Robot void InitializePlatformSpecificModules() {
38*7c3d14c8STreehugger Robot ListOfModules modules;
39*7c3d14c8STreehugger Robot modules.init();
40*7c3d14c8STreehugger Robot for (LoadedModule &module : modules) {
41*7c3d14c8STreehugger Robot if (!IsLinker(module.full_name())) continue;
42*7c3d14c8STreehugger Robot if (linker == nullptr) {
43*7c3d14c8STreehugger Robot linker = reinterpret_cast<LoadedModule *>(linker_placeholder);
44*7c3d14c8STreehugger Robot *linker = module;
45*7c3d14c8STreehugger Robot module = LoadedModule();
46*7c3d14c8STreehugger Robot } else {
47*7c3d14c8STreehugger Robot VReport(1, "LeakSanitizer: Multiple modules match \"%s\". "
48*7c3d14c8STreehugger Robot "TLS will not be handled correctly.\n", kLinkerName);
49*7c3d14c8STreehugger Robot linker->clear();
50*7c3d14c8STreehugger Robot linker = nullptr;
51*7c3d14c8STreehugger Robot return;
52*7c3d14c8STreehugger Robot }
53*7c3d14c8STreehugger Robot }
54*7c3d14c8STreehugger Robot VReport(1, "LeakSanitizer: Dynamic linker not found. "
55*7c3d14c8STreehugger Robot "TLS will not be handled correctly.\n");
56*7c3d14c8STreehugger Robot }
57*7c3d14c8STreehugger Robot
ProcessGlobalRegionsCallback(struct dl_phdr_info * info,size_t size,void * data)58*7c3d14c8STreehugger Robot static int ProcessGlobalRegionsCallback(struct dl_phdr_info *info, size_t size,
59*7c3d14c8STreehugger Robot void *data) {
60*7c3d14c8STreehugger Robot Frontier *frontier = reinterpret_cast<Frontier *>(data);
61*7c3d14c8STreehugger Robot for (uptr j = 0; j < info->dlpi_phnum; j++) {
62*7c3d14c8STreehugger Robot const ElfW(Phdr) *phdr = &(info->dlpi_phdr[j]);
63*7c3d14c8STreehugger Robot // We're looking for .data and .bss sections, which reside in writeable,
64*7c3d14c8STreehugger Robot // loadable segments.
65*7c3d14c8STreehugger Robot if (!(phdr->p_flags & PF_W) || (phdr->p_type != PT_LOAD) ||
66*7c3d14c8STreehugger Robot (phdr->p_memsz == 0))
67*7c3d14c8STreehugger Robot continue;
68*7c3d14c8STreehugger Robot uptr begin = info->dlpi_addr + phdr->p_vaddr;
69*7c3d14c8STreehugger Robot uptr end = begin + phdr->p_memsz;
70*7c3d14c8STreehugger Robot uptr allocator_begin = 0, allocator_end = 0;
71*7c3d14c8STreehugger Robot GetAllocatorGlobalRange(&allocator_begin, &allocator_end);
72*7c3d14c8STreehugger Robot if (begin <= allocator_begin && allocator_begin < end) {
73*7c3d14c8STreehugger Robot CHECK_LE(allocator_begin, allocator_end);
74*7c3d14c8STreehugger Robot CHECK_LT(allocator_end, end);
75*7c3d14c8STreehugger Robot if (begin < allocator_begin)
76*7c3d14c8STreehugger Robot ScanRangeForPointers(begin, allocator_begin, frontier, "GLOBAL",
77*7c3d14c8STreehugger Robot kReachable);
78*7c3d14c8STreehugger Robot if (allocator_end < end)
79*7c3d14c8STreehugger Robot ScanRangeForPointers(allocator_end, end, frontier, "GLOBAL",
80*7c3d14c8STreehugger Robot kReachable);
81*7c3d14c8STreehugger Robot } else {
82*7c3d14c8STreehugger Robot ScanRangeForPointers(begin, end, frontier, "GLOBAL", kReachable);
83*7c3d14c8STreehugger Robot }
84*7c3d14c8STreehugger Robot }
85*7c3d14c8STreehugger Robot return 0;
86*7c3d14c8STreehugger Robot }
87*7c3d14c8STreehugger Robot
88*7c3d14c8STreehugger Robot // Scans global variables for heap pointers.
ProcessGlobalRegions(Frontier * frontier)89*7c3d14c8STreehugger Robot void ProcessGlobalRegions(Frontier *frontier) {
90*7c3d14c8STreehugger Robot if (!flags()->use_globals) return;
91*7c3d14c8STreehugger Robot dl_iterate_phdr(ProcessGlobalRegionsCallback, frontier);
92*7c3d14c8STreehugger Robot }
93*7c3d14c8STreehugger Robot
GetCallerPC(u32 stack_id,StackDepotReverseMap * map)94*7c3d14c8STreehugger Robot static uptr GetCallerPC(u32 stack_id, StackDepotReverseMap *map) {
95*7c3d14c8STreehugger Robot CHECK(stack_id);
96*7c3d14c8STreehugger Robot StackTrace stack = map->Get(stack_id);
97*7c3d14c8STreehugger Robot // The top frame is our malloc/calloc/etc. The next frame is the caller.
98*7c3d14c8STreehugger Robot if (stack.size >= 2)
99*7c3d14c8STreehugger Robot return stack.trace[1];
100*7c3d14c8STreehugger Robot return 0;
101*7c3d14c8STreehugger Robot }
102*7c3d14c8STreehugger Robot
103*7c3d14c8STreehugger Robot struct ProcessPlatformAllocParam {
104*7c3d14c8STreehugger Robot Frontier *frontier;
105*7c3d14c8STreehugger Robot StackDepotReverseMap *stack_depot_reverse_map;
106*7c3d14c8STreehugger Robot bool skip_linker_allocations;
107*7c3d14c8STreehugger Robot };
108*7c3d14c8STreehugger Robot
109*7c3d14c8STreehugger Robot // ForEachChunk callback. Identifies unreachable chunks which must be treated as
110*7c3d14c8STreehugger Robot // reachable. Marks them as reachable and adds them to the frontier.
ProcessPlatformSpecificAllocationsCb(uptr chunk,void * arg)111*7c3d14c8STreehugger Robot static void ProcessPlatformSpecificAllocationsCb(uptr chunk, void *arg) {
112*7c3d14c8STreehugger Robot CHECK(arg);
113*7c3d14c8STreehugger Robot ProcessPlatformAllocParam *param =
114*7c3d14c8STreehugger Robot reinterpret_cast<ProcessPlatformAllocParam *>(arg);
115*7c3d14c8STreehugger Robot chunk = GetUserBegin(chunk);
116*7c3d14c8STreehugger Robot LsanMetadata m(chunk);
117*7c3d14c8STreehugger Robot if (m.allocated() && m.tag() != kReachable && m.tag() != kIgnored) {
118*7c3d14c8STreehugger Robot u32 stack_id = m.stack_trace_id();
119*7c3d14c8STreehugger Robot uptr caller_pc = 0;
120*7c3d14c8STreehugger Robot if (stack_id > 0)
121*7c3d14c8STreehugger Robot caller_pc = GetCallerPC(stack_id, param->stack_depot_reverse_map);
122*7c3d14c8STreehugger Robot // If caller_pc is unknown, this chunk may be allocated in a coroutine. Mark
123*7c3d14c8STreehugger Robot // it as reachable, as we can't properly report its allocation stack anyway.
124*7c3d14c8STreehugger Robot if (caller_pc == 0 || (param->skip_linker_allocations &&
125*7c3d14c8STreehugger Robot linker->containsAddress(caller_pc))) {
126*7c3d14c8STreehugger Robot m.set_tag(kReachable);
127*7c3d14c8STreehugger Robot param->frontier->push_back(chunk);
128*7c3d14c8STreehugger Robot }
129*7c3d14c8STreehugger Robot }
130*7c3d14c8STreehugger Robot }
131*7c3d14c8STreehugger Robot
132*7c3d14c8STreehugger Robot // Handles dynamically allocated TLS blocks by treating all chunks allocated
133*7c3d14c8STreehugger Robot // from ld-linux.so as reachable.
134*7c3d14c8STreehugger Robot // Dynamic TLS blocks contain the TLS variables of dynamically loaded modules.
135*7c3d14c8STreehugger Robot // They are allocated with a __libc_memalign() call in allocate_and_init()
136*7c3d14c8STreehugger Robot // (elf/dl-tls.c). Glibc won't tell us the address ranges occupied by those
137*7c3d14c8STreehugger Robot // blocks, but we can make sure they come from our own allocator by intercepting
138*7c3d14c8STreehugger Robot // __libc_memalign(). On top of that, there is no easy way to reach them. Their
139*7c3d14c8STreehugger Robot // addresses are stored in a dynamically allocated array (the DTV) which is
140*7c3d14c8STreehugger Robot // referenced from the static TLS. Unfortunately, we can't just rely on the DTV
141*7c3d14c8STreehugger Robot // being reachable from the static TLS, and the dynamic TLS being reachable from
142*7c3d14c8STreehugger Robot // the DTV. This is because the initial DTV is allocated before our interception
143*7c3d14c8STreehugger Robot // mechanism kicks in, and thus we don't recognize it as allocated memory. We
144*7c3d14c8STreehugger Robot // can't special-case it either, since we don't know its size.
145*7c3d14c8STreehugger Robot // Our solution is to include in the root set all allocations made from
146*7c3d14c8STreehugger Robot // ld-linux.so (which is where allocate_and_init() is implemented). This is
147*7c3d14c8STreehugger Robot // guaranteed to include all dynamic TLS blocks (and possibly other allocations
148*7c3d14c8STreehugger Robot // which we don't care about).
ProcessPlatformSpecificAllocations(Frontier * frontier)149*7c3d14c8STreehugger Robot void ProcessPlatformSpecificAllocations(Frontier *frontier) {
150*7c3d14c8STreehugger Robot StackDepotReverseMap stack_depot_reverse_map;
151*7c3d14c8STreehugger Robot ProcessPlatformAllocParam arg;
152*7c3d14c8STreehugger Robot arg.frontier = frontier;
153*7c3d14c8STreehugger Robot arg.stack_depot_reverse_map = &stack_depot_reverse_map;
154*7c3d14c8STreehugger Robot arg.skip_linker_allocations =
155*7c3d14c8STreehugger Robot flags()->use_tls && flags()->use_ld_allocations && linker != nullptr;
156*7c3d14c8STreehugger Robot ForEachChunk(ProcessPlatformSpecificAllocationsCb, &arg);
157*7c3d14c8STreehugger Robot }
158*7c3d14c8STreehugger Robot
159*7c3d14c8STreehugger Robot struct DoStopTheWorldParam {
160*7c3d14c8STreehugger Robot StopTheWorldCallback callback;
161*7c3d14c8STreehugger Robot void *argument;
162*7c3d14c8STreehugger Robot };
163*7c3d14c8STreehugger Robot
DoStopTheWorldCallback(struct dl_phdr_info * info,size_t size,void * data)164*7c3d14c8STreehugger Robot static int DoStopTheWorldCallback(struct dl_phdr_info *info, size_t size,
165*7c3d14c8STreehugger Robot void *data) {
166*7c3d14c8STreehugger Robot DoStopTheWorldParam *param = reinterpret_cast<DoStopTheWorldParam *>(data);
167*7c3d14c8STreehugger Robot StopTheWorld(param->callback, param->argument);
168*7c3d14c8STreehugger Robot return 1;
169*7c3d14c8STreehugger Robot }
170*7c3d14c8STreehugger Robot
171*7c3d14c8STreehugger Robot // LSan calls dl_iterate_phdr() from the tracer task. This may deadlock: if one
172*7c3d14c8STreehugger Robot // of the threads is frozen while holding the libdl lock, the tracer will hang
173*7c3d14c8STreehugger Robot // in dl_iterate_phdr() forever.
174*7c3d14c8STreehugger Robot // Luckily, (a) the lock is reentrant and (b) libc can't distinguish between the
175*7c3d14c8STreehugger Robot // tracer task and the thread that spawned it. Thus, if we run the tracer task
176*7c3d14c8STreehugger Robot // while holding the libdl lock in the parent thread, we can safely reenter it
177*7c3d14c8STreehugger Robot // in the tracer. The solution is to run stoptheworld from a dl_iterate_phdr()
178*7c3d14c8STreehugger Robot // callback in the parent thread.
DoStopTheWorld(StopTheWorldCallback callback,void * argument)179*7c3d14c8STreehugger Robot void DoStopTheWorld(StopTheWorldCallback callback, void *argument) {
180*7c3d14c8STreehugger Robot DoStopTheWorldParam param = {callback, argument};
181*7c3d14c8STreehugger Robot dl_iterate_phdr(DoStopTheWorldCallback, ¶m);
182*7c3d14c8STreehugger Robot }
183*7c3d14c8STreehugger Robot
184*7c3d14c8STreehugger Robot } // namespace __lsan
185*7c3d14c8STreehugger Robot
186*7c3d14c8STreehugger Robot #endif // CAN_SANITIZE_LEAKS && SANITIZER_LINUX
187