Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 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" |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 16 | #include "lsan_common.h" |
| 17 | |
Sergey Matveev | 9bdf780 | 2013-05-21 14:12:11 +0000 | [diff] [blame] | 18 | #if CAN_SANITIZE_LEAKS && SANITIZER_LINUX |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 19 | #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 | |
| 25 | namespace __lsan { |
| 26 | |
| 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; |
| 32 | |
| 33 | static bool IsLinker(const char* full_name) { |
| 34 | return LibraryNameIs(full_name, kLinkerName); |
| 35 | } |
| 36 | |
| 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); |
| 43 | return; |
| 44 | } |
| 45 | if (num_matches == 0) |
Sergey Matveev | 8e66cf5 | 2013-05-24 15:36:30 +0000 | [diff] [blame] | 46 | Report("LeakSanitizer: Dynamic linker not found. " |
| 47 | "TLS will not be handled correctly.\n"); |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 48 | else if (num_matches > 1) |
Sergey Matveev | 8e66cf5 | 2013-05-24 15:36:30 +0000 | [diff] [blame] | 49 | Report("LeakSanitizer: Multiple modules match \"%s\". " |
| 50 | "TLS will not be handled correctly.\n", kLinkerName); |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 51 | linker = 0; |
| 52 | } |
| 53 | |
| 54 | static int ProcessGlobalRegionsCallback(struct dl_phdr_info *info, size_t size, |
| 55 | void *data) { |
Sergey Matveev | ac78d00 | 2013-06-24 08:34:50 +0000 | [diff] [blame] | 56 | Frontier *frontier = reinterpret_cast<Frontier *>(data); |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 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, |
| 60 | // loadable segments. |
| 61 | if (!(phdr->p_flags & PF_W) || (phdr->p_type != PT_LOAD) || |
| 62 | (phdr->p_memsz == 0)) |
| 63 | continue; |
| 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", |
| 73 | kReachable); |
| 74 | if (allocator_end < end) |
| 75 | ScanRangeForPointers(allocator_end, end, frontier, "GLOBAL", |
| 76 | kReachable); |
| 77 | } else { |
| 78 | ScanRangeForPointers(begin, end, frontier, "GLOBAL", kReachable); |
| 79 | } |
| 80 | } |
| 81 | return 0; |
| 82 | } |
| 83 | |
Sergey Matveev | ac78d00 | 2013-06-24 08:34:50 +0000 | [diff] [blame] | 84 | // Scans global variables for heap pointers. |
Alexey Samsonov | dbeb48d | 2013-06-14 10:07:56 +0000 | [diff] [blame] | 85 | void ProcessGlobalRegions(Frontier *frontier) { |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 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); |
| 91 | } |
| 92 | |
Sergey Matveev | 384a448 | 2013-08-26 13:24:43 +0000 | [diff] [blame] | 93 | static uptr GetCallerPC(u32 stack_id, StackDepotReverseMap *map) { |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 94 | CHECK(stack_id); |
| 95 | uptr size = 0; |
Sergey Matveev | 384a448 | 2013-08-26 13:24:43 +0000 | [diff] [blame] | 96 | const uptr *trace = map->Get(stack_id, &size); |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 97 | // The top frame is our malloc/calloc/etc. The next frame is the caller. |
Sergey Matveev | 0550a3f | 2013-06-06 14:19:36 +0000 | [diff] [blame] | 98 | if (size >= 2) |
| 99 | return trace[1]; |
| 100 | return 0; |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Sergey Matveev | 384a448 | 2013-08-26 13:24:43 +0000 | [diff] [blame] | 103 | struct ProcessPlatformAllocParam { |
| 104 | Frontier *frontier; |
| 105 | StackDepotReverseMap *stack_depot_reverse_map; |
| 106 | }; |
| 107 | |
Sergey Matveev | ac78d00 | 2013-06-24 08:34:50 +0000 | [diff] [blame] | 108 | // ForEachChunk callback. Identifies unreachable chunks which must be treated as |
| 109 | // reachable. Marks them as reachable and adds them to the frontier. |
| 110 | static void ProcessPlatformSpecificAllocationsCb(uptr chunk, void *arg) { |
| 111 | CHECK(arg); |
Sergey Matveev | 384a448 | 2013-08-26 13:24:43 +0000 | [diff] [blame] | 112 | ProcessPlatformAllocParam *param = |
| 113 | reinterpret_cast<ProcessPlatformAllocParam *>(arg); |
Sergey Matveev | ac78d00 | 2013-06-24 08:34:50 +0000 | [diff] [blame] | 114 | chunk = GetUserBegin(chunk); |
| 115 | LsanMetadata m(chunk); |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 116 | if (m.allocated() && m.tag() != kReachable) { |
Evgeniy Stepanov | 96a575f | 2013-09-12 08:16:28 +0000 | [diff] [blame] | 117 | u32 stack_id = m.stack_trace_id(); |
Alexey Samsonov | 2946f13 | 2013-09-30 10:57:56 +0000 | [diff] [blame] | 118 | uptr caller_pc = 0; |
| 119 | if (stack_id > 0) |
| 120 | caller_pc = GetCallerPC(stack_id, param->stack_depot_reverse_map); |
| 121 | // If caller_pc is unknown, this chunk may be allocated in a coroutine. Mark |
| 122 | // it as reachable, as we can't properly report its allocation stack anyway. |
| 123 | if (caller_pc == 0 || linker->containsAddress(caller_pc)) { |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 124 | m.set_tag(kReachable); |
Sergey Matveev | 384a448 | 2013-08-26 13:24:43 +0000 | [diff] [blame] | 125 | param->frontier->push_back(chunk); |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
Sergey Matveev | ac78d00 | 2013-06-24 08:34:50 +0000 | [diff] [blame] | 130 | // Handles dynamically allocated TLS blocks by treating all chunks allocated |
| 131 | // from ld-linux.so as reachable. |
Alexey Samsonov | dbeb48d | 2013-06-14 10:07:56 +0000 | [diff] [blame] | 132 | void ProcessPlatformSpecificAllocations(Frontier *frontier) { |
Sergey Matveev | ebe3a36 | 2013-05-27 11:41:46 +0000 | [diff] [blame] | 133 | if (!flags()->use_tls) return; |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 134 | if (!linker) return; |
Sergey Matveev | 384a448 | 2013-08-26 13:24:43 +0000 | [diff] [blame] | 135 | StackDepotReverseMap stack_depot_reverse_map; |
| 136 | ProcessPlatformAllocParam arg = {frontier, &stack_depot_reverse_map}; |
| 137 | ForEachChunk(ProcessPlatformSpecificAllocationsCb, &arg); |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | } // namespace __lsan |
Sergey Matveev | 9bdf780 | 2013-05-21 14:12:11 +0000 | [diff] [blame] | 141 | #endif // CAN_SANITIZE_LEAKS && SANITIZER_LINUX |