blob: 9570810ecd67628ac28c972b2c90956b4c8a80d1 [file] [log] [blame]
Sergey Matveevab0f7442013-05-20 11:06:50 +00001//=-- 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"
Sergey Matveevab0f7442013-05-20 11:06:50 +000016#include "lsan_common.h"
17
Sergey Matveev9bdf7802013-05-21 14:12:11 +000018#if CAN_SANITIZE_LEAKS && SANITIZER_LINUX
Sergey Matveevab0f7442013-05-20 11:06:50 +000019#include <link.h>
20
21#include "sanitizer_common/sanitizer_common.h"
22#include "sanitizer_common/sanitizer_linux.h"
23#include "sanitizer_common/sanitizer_stackdepot.h"
24
25namespace __lsan {
26
27static const char kLinkerName[] = "ld";
28// We request 2 modules matching "ld", so we can print a warning if there's more
29// than one match. But only the first one is actually used.
30static char linker_placeholder[2 * sizeof(LoadedModule)] ALIGNED(64);
31static LoadedModule *linker = 0;
32
33static bool IsLinker(const char* full_name) {
34 return LibraryNameIs(full_name, kLinkerName);
35}
36
37void InitializePlatformSpecificModules() {
38 internal_memset(linker_placeholder, 0, sizeof(linker_placeholder));
39 uptr num_matches = GetListOfModules(
40 reinterpret_cast<LoadedModule *>(linker_placeholder), 2, IsLinker);
41 if (num_matches == 1) {
42 linker = reinterpret_cast<LoadedModule *>(linker_placeholder);
43 return;
44 }
45 if (num_matches == 0)
Sergey Matveev8e66cf52013-05-24 15:36:30 +000046 Report("LeakSanitizer: Dynamic linker not found. "
47 "TLS will not be handled correctly.\n");
Sergey Matveevab0f7442013-05-20 11:06:50 +000048 else if (num_matches > 1)
Sergey Matveev8e66cf52013-05-24 15:36:30 +000049 Report("LeakSanitizer: Multiple modules match \"%s\". "
50 "TLS will not be handled correctly.\n", kLinkerName);
Sergey Matveevab0f7442013-05-20 11:06:50 +000051 linker = 0;
52}
53
54static int ProcessGlobalRegionsCallback(struct dl_phdr_info *info, size_t size,
55 void *data) {
56 InternalVector<uptr> *frontier =
57 reinterpret_cast<InternalVector<uptr> *>(data);
58 for (uptr j = 0; j < info->dlpi_phnum; j++) {
59 const ElfW(Phdr) *phdr = &(info->dlpi_phdr[j]);
60 // We're looking for .data and .bss sections, which reside in writeable,
61 // loadable segments.
62 if (!(phdr->p_flags & PF_W) || (phdr->p_type != PT_LOAD) ||
63 (phdr->p_memsz == 0))
64 continue;
65 uptr begin = info->dlpi_addr + phdr->p_vaddr;
66 uptr end = begin + phdr->p_memsz;
67 uptr allocator_begin = 0, allocator_end = 0;
68 GetAllocatorGlobalRange(&allocator_begin, &allocator_end);
69 if (begin <= allocator_begin && allocator_begin < end) {
70 CHECK_LE(allocator_begin, allocator_end);
71 CHECK_LT(allocator_end, end);
72 if (begin < allocator_begin)
73 ScanRangeForPointers(begin, allocator_begin, frontier, "GLOBAL",
74 kReachable);
75 if (allocator_end < end)
76 ScanRangeForPointers(allocator_end, end, frontier, "GLOBAL",
77 kReachable);
78 } else {
79 ScanRangeForPointers(begin, end, frontier, "GLOBAL", kReachable);
80 }
81 }
82 return 0;
83}
84
85// Scan global variables for heap pointers.
86void ProcessGlobalRegions(InternalVector<uptr> *frontier) {
87 // FIXME: dl_iterate_phdr acquires a linker lock, so we run a risk of
88 // deadlocking by running this under StopTheWorld. However, the lock is
89 // reentrant, so we should be able to fix this by acquiring the lock before
90 // suspending threads.
91 dl_iterate_phdr(ProcessGlobalRegionsCallback, frontier);
92}
93
94static uptr GetCallerPC(u32 stack_id) {
95 CHECK(stack_id);
96 uptr size = 0;
97 const uptr *trace = StackDepotGet(stack_id, &size);
98 // The top frame is our malloc/calloc/etc. The next frame is the caller.
Sergey Matveev0550a3f2013-06-06 14:19:36 +000099 if (size >= 2)
100 return trace[1];
101 return 0;
Sergey Matveevab0f7442013-05-20 11:06:50 +0000102}
103
104void ProcessPlatformSpecificAllocationsCb::operator()(void *p) const {
Sergey Matveevb9c9ce72013-05-20 14:04:56 +0000105 p = GetUserBegin(p);
Sergey Matveevab0f7442013-05-20 11:06:50 +0000106 LsanMetadata m(p);
107 if (m.allocated() && m.tag() != kReachable) {
108 if (linker->containsAddress(GetCallerPC(m.stack_trace_id()))) {
109 m.set_tag(kReachable);
110 frontier_->push_back(reinterpret_cast<uptr>(p));
111 }
112 }
113}
114
115// Handle dynamically allocated TLS blocks by treating all chunks allocated from
116// ld-linux.so as reachable.
117void ProcessPlatformSpecificAllocations(InternalVector<uptr> *frontier) {
Sergey Matveevebe3a362013-05-27 11:41:46 +0000118 if (!flags()->use_tls) return;
Sergey Matveevab0f7442013-05-20 11:06:50 +0000119 if (!linker) return;
120 ForEachChunk(ProcessPlatformSpecificAllocationsCb(frontier));
121}
122
123} // namespace __lsan
Sergey Matveev9bdf7802013-05-21 14:12:11 +0000124#endif // CAN_SANITIZE_LEAKS && SANITIZER_LINUX