1 //=-- lsan_common_linux.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 // This file is a part of LeakSanitizer.
11 // Implementation of common leak checking functionality. Linux-specific code.
13 //===----------------------------------------------------------------------===//
15 #include "sanitizer_common/sanitizer_platform.h"
16 #include "lsan_common.h"
18 #if CAN_SANITIZE_LEAKS && SANITIZER_LINUX
21 #include "sanitizer_common/sanitizer_common.h"
22 #include "sanitizer_common/sanitizer_linux.h"
23 #include "sanitizer_common/sanitizer_stackdepot.h"
27 static 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.
30 static char linker_placeholder
[2 * sizeof(LoadedModule
)] ALIGNED(64);
31 static LoadedModule
*linker
= 0;
33 static bool IsLinker(const char* full_name
) {
34 return LibraryNameIs(full_name
, kLinkerName
);
37 void 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
);
46 Report("LeakSanitizer: Dynamic linker not found. "
47 "TLS will not be handled correctly.\n");
48 else if (num_matches
> 1)
49 Report("LeakSanitizer: Multiple modules match \"%s\". "
50 "TLS will not be handled correctly.\n", kLinkerName
);
54 static int ProcessGlobalRegionsCallback(struct dl_phdr_info
*info
, size_t size
,
56 Frontier
*frontier
= reinterpret_cast<Frontier
*>(data
);
57 for (uptr j
= 0; j
< info
->dlpi_phnum
; j
++) {
58 const ElfW(Phdr
) *phdr
= &(info
->dlpi_phdr
[j
]);
59 // We're looking for .data and .bss sections, which reside in writeable,
61 if (!(phdr
->p_flags
& PF_W
) || (phdr
->p_type
!= PT_LOAD
) ||
64 uptr begin
= info
->dlpi_addr
+ phdr
->p_vaddr
;
65 uptr end
= begin
+ phdr
->p_memsz
;
66 uptr allocator_begin
= 0, allocator_end
= 0;
67 GetAllocatorGlobalRange(&allocator_begin
, &allocator_end
);
68 if (begin
<= allocator_begin
&& allocator_begin
< end
) {
69 CHECK_LE(allocator_begin
, allocator_end
);
70 CHECK_LT(allocator_end
, end
);
71 if (begin
< allocator_begin
)
72 ScanRangeForPointers(begin
, allocator_begin
, frontier
, "GLOBAL",
74 if (allocator_end
< end
)
75 ScanRangeForPointers(allocator_end
, end
, frontier
, "GLOBAL",
78 ScanRangeForPointers(begin
, end
, frontier
, "GLOBAL", kReachable
);
84 // Scans global variables for heap pointers.
85 void ProcessGlobalRegions(Frontier
*frontier
) {
86 // FIXME: dl_iterate_phdr acquires a linker lock, so we run a risk of
87 // deadlocking by running this under StopTheWorld. However, the lock is
88 // reentrant, so we should be able to fix this by acquiring the lock before
89 // suspending threads.
90 dl_iterate_phdr(ProcessGlobalRegionsCallback
, frontier
);
93 static uptr
GetCallerPC(u32 stack_id
) {
96 const uptr
*trace
= StackDepotGet(stack_id
, &size
);
97 // The top frame is our malloc/calloc/etc. The next frame is the caller.
103 // ForEachChunk callback. Identifies unreachable chunks which must be treated as
104 // reachable. Marks them as reachable and adds them to the frontier.
105 static void ProcessPlatformSpecificAllocationsCb(uptr chunk
, void *arg
) {
107 chunk
= GetUserBegin(chunk
);
108 LsanMetadata
m(chunk
);
109 if (m
.allocated() && m
.tag() != kReachable
) {
110 if (linker
->containsAddress(GetCallerPC(m
.stack_trace_id()))) {
111 m
.set_tag(kReachable
);
112 reinterpret_cast<Frontier
*>(arg
)->push_back(chunk
);
117 // Handles dynamically allocated TLS blocks by treating all chunks allocated
118 // from ld-linux.so as reachable.
119 void ProcessPlatformSpecificAllocations(Frontier
*frontier
) {
120 if (!flags()->use_tls
) return;
122 ForEachChunk(ProcessPlatformSpecificAllocationsCb
, frontier
);
125 } // namespace __lsan
126 #endif // CAN_SANITIZE_LEAKS && SANITIZER_LINUX