1 //=-- lsan_common_mac.cc --------------------------------------------------===// 2 // 3 // This file is distributed under the University of Illinois Open Source 4 // License. See LICENSE.TXT for details. 5 // 6 //===----------------------------------------------------------------------===// 7 // 8 // This file is a part of LeakSanitizer. 9 // Implementation of common leak checking functionality. Darwin-specific code. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "sanitizer_common/sanitizer_platform.h" 14 #include "lsan_common.h" 15 16 #if CAN_SANITIZE_LEAKS && SANITIZER_MAC 17 18 #include "sanitizer_common/sanitizer_allocator_internal.h" 19 #include "lsan_allocator.h" 20 21 #include <pthread.h> 22 23 #include <mach/mach.h> 24 25 // Only introduced in Mac OS X 10.9. 26 #ifdef VM_MEMORY_OS_ALLOC_ONCE 27 static const int kSanitizerVmMemoryOsAllocOnce = VM_MEMORY_OS_ALLOC_ONCE; 28 #else 29 static const int kSanitizerVmMemoryOsAllocOnce = 73; 30 #endif 31 32 namespace __lsan { 33 34 typedef struct { 35 int disable_counter; 36 u32 current_thread_id; 37 AllocatorCache cache; 38 } thread_local_data_t; 39 40 static pthread_key_t key; 41 static pthread_once_t key_once = PTHREAD_ONCE_INIT; 42 43 // The main thread destructor requires the current thread id, 44 // so we can't destroy it until it's been used and reset to invalid tid 45 void restore_tid_data(void *ptr) { 46 thread_local_data_t *data = (thread_local_data_t *)ptr; 47 if (data->current_thread_id != kInvalidTid) 48 pthread_setspecific(key, data); 49 } 50 51 static void make_tls_key() { 52 CHECK_EQ(pthread_key_create(&key, restore_tid_data), 0); 53 } 54 55 static thread_local_data_t *get_tls_val(bool alloc) { 56 pthread_once(&key_once, make_tls_key); 57 58 thread_local_data_t *ptr = (thread_local_data_t *)pthread_getspecific(key); 59 if (ptr == NULL && alloc) { 60 ptr = (thread_local_data_t *)InternalAlloc(sizeof(*ptr)); 61 ptr->disable_counter = 0; 62 ptr->current_thread_id = kInvalidTid; 63 ptr->cache = AllocatorCache(); 64 pthread_setspecific(key, ptr); 65 } 66 67 return ptr; 68 } 69 70 bool DisabledInThisThread() { 71 thread_local_data_t *data = get_tls_val(false); 72 return data ? data->disable_counter > 0 : false; 73 } 74 75 void DisableInThisThread() { ++get_tls_val(true)->disable_counter; } 76 77 void EnableInThisThread() { 78 int *disable_counter = &get_tls_val(true)->disable_counter; 79 if (*disable_counter == 0) { 80 DisableCounterUnderflow(); 81 } 82 --*disable_counter; 83 } 84 85 u32 GetCurrentThread() { 86 thread_local_data_t *data = get_tls_val(false); 87 return data ? data->current_thread_id : kInvalidTid; 88 } 89 90 void SetCurrentThread(u32 tid) { get_tls_val(true)->current_thread_id = tid; } 91 92 AllocatorCache *GetAllocatorCache() { return &get_tls_val(true)->cache; } 93 94 LoadedModule *GetLinker() { return nullptr; } 95 96 // Required on Linux for initialization of TLS behavior, but should not be 97 // required on Darwin. 98 void InitializePlatformSpecificModules() {} 99 100 // Sections which can't contain contain global pointers. This list errs on the 101 // side of caution to avoid false positives, at the expense of performance. 102 // 103 // Other potentially safe sections include: 104 // __all_image_info, __crash_info, __const, __got, __interpose, __objc_msg_break 105 // 106 // Sections which definitely cannot be included here are: 107 // __objc_data, __objc_const, __data, __bss, __common, __thread_data, 108 // __thread_bss, __thread_vars, __objc_opt_rw, __objc_opt_ptrs 109 static const char *kSkippedSecNames[] = { 110 "__cfstring", "__la_symbol_ptr", "__mod_init_func", 111 "__mod_term_func", "__nl_symbol_ptr", "__objc_classlist", 112 "__objc_classrefs", "__objc_imageinfo", "__objc_nlclslist", 113 "__objc_protolist", "__objc_selrefs", "__objc_superrefs"}; 114 115 // Scans global variables for heap pointers. 116 void ProcessGlobalRegions(Frontier *frontier) { 117 for (auto name : kSkippedSecNames) CHECK(ARRAY_SIZE(name) < kMaxSegName); 118 119 MemoryMappingLayout memory_mapping(false); 120 InternalMmapVector<LoadedModule> modules(/*initial_capacity*/ 128); 121 memory_mapping.DumpListOfModules(&modules); 122 for (uptr i = 0; i < modules.size(); ++i) { 123 // Even when global scanning is disabled, we still need to scan 124 // system libraries for stashed pointers 125 if (!flags()->use_globals && modules[i].instrumented()) continue; 126 127 for (const __sanitizer::LoadedModule::AddressRange &range : 128 modules[i].ranges()) { 129 // Sections storing global variables are writable and non-executable 130 if (range.executable || !range.writable) continue; 131 132 for (auto name : kSkippedSecNames) { 133 if (!internal_strcmp(range.name, name)) continue; 134 } 135 136 ScanGlobalRange(range.beg, range.end, frontier); 137 } 138 } 139 } 140 141 void ProcessPlatformSpecificAllocations(Frontier *frontier) { 142 mach_port_name_t port; 143 if (task_for_pid(mach_task_self(), internal_getpid(), &port) 144 != KERN_SUCCESS) { 145 return; 146 } 147 148 unsigned depth = 1; 149 vm_size_t size = 0; 150 vm_address_t address = 0; 151 kern_return_t err = KERN_SUCCESS; 152 mach_msg_type_number_t count = VM_REGION_SUBMAP_INFO_COUNT_64; 153 154 InternalMmapVector<RootRegion> const *root_regions = GetRootRegions(); 155 156 while (err == KERN_SUCCESS) { 157 struct vm_region_submap_info_64 info; 158 err = vm_region_recurse_64(port, &address, &size, &depth, 159 (vm_region_info_t)&info, &count); 160 161 uptr end_address = address + size; 162 163 // libxpc stashes some pointers in the Kernel Alloc Once page, 164 // make sure not to report those as leaks. 165 if (info.user_tag == kSanitizerVmMemoryOsAllocOnce) { 166 ScanRangeForPointers(address, end_address, frontier, "GLOBAL", 167 kReachable); 168 169 // Recursing over the full memory map is very slow, break out 170 // early if we don't need the full iteration. 171 if (!flags()->use_root_regions || !root_regions->size()) 172 break; 173 } 174 175 // This additional root region scan is required on Darwin in order to 176 // detect root regions contained within mmap'd memory regions, because 177 // the Darwin implementation of sanitizer_procmaps traverses images 178 // as loaded by dyld, and not the complete set of all memory regions. 179 // 180 // TODO(fjricci) - remove this once sanitizer_procmaps_mac has the same 181 // behavior as sanitizer_procmaps_linux and traverses all memory regions 182 if (flags()->use_root_regions) { 183 for (uptr i = 0; i < root_regions->size(); i++) { 184 ScanRootRegion(frontier, (*root_regions)[i], address, end_address, 185 info.protection & kProtectionRead); 186 } 187 } 188 189 address = end_address; 190 } 191 } 192 193 // On darwin, we can intercept _exit gracefully, and return a failing exit code 194 // if required at that point. Calling Die() here is undefined behavior and 195 // causes rare race conditions. 196 void HandleLeaks() {} 197 198 void DoStopTheWorld(StopTheWorldCallback callback, void *argument) { 199 StopTheWorld(callback, argument); 200 } 201 202 } // namespace __lsan 203 204 #endif // CAN_SANITIZE_LEAKS && SANITIZER_MAC 205