1 //=-- lsan_common_mac.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. Darwin-specific code.
13 //===----------------------------------------------------------------------===//
15 #include "sanitizer_common/sanitizer_platform.h"
16 #include "lsan_common.h"
18 #if CAN_SANITIZE_LEAKS && SANITIZER_MAC
20 #include "sanitizer_common/sanitizer_allocator_internal.h"
21 #include "lsan_allocator.h"
25 #include <mach/mach.h>
31 u32 current_thread_id;
33 } thread_local_data_t;
35 static pthread_key_t key;
36 static pthread_once_t key_once = PTHREAD_ONCE_INIT;
38 // The main thread destructor requires the current thread id,
39 // so we can't destroy it until it's been used and reset to invalid tid
40 void restore_tid_data(void *ptr) {
41 thread_local_data_t *data = (thread_local_data_t *)ptr;
42 if (data->current_thread_id != kInvalidTid)
43 pthread_setspecific(key, data);
46 static void make_tls_key() {
47 CHECK_EQ(pthread_key_create(&key, restore_tid_data), 0);
50 static thread_local_data_t *get_tls_val(bool alloc) {
51 pthread_once(&key_once, make_tls_key);
53 thread_local_data_t *ptr = (thread_local_data_t *)pthread_getspecific(key);
54 if (ptr == NULL && alloc) {
55 ptr = (thread_local_data_t *)InternalAlloc(sizeof(*ptr));
56 ptr->disable_counter = 0;
57 ptr->current_thread_id = kInvalidTid;
58 ptr->cache = AllocatorCache();
59 pthread_setspecific(key, ptr);
65 bool DisabledInThisThread() {
66 thread_local_data_t *data = get_tls_val(false);
67 return data ? data->disable_counter > 0 : false;
70 void DisableInThisThread() { ++get_tls_val(true)->disable_counter; }
72 void EnableInThisThread() {
73 int *disable_counter = &get_tls_val(true)->disable_counter;
74 if (*disable_counter == 0) {
75 DisableCounterUnderflow();
80 u32 GetCurrentThread() {
81 thread_local_data_t *data = get_tls_val(false);
83 return data->current_thread_id;
86 void SetCurrentThread(u32 tid) { get_tls_val(true)->current_thread_id = tid; }
88 AllocatorCache *GetAllocatorCache() { return &get_tls_val(true)->cache; }
90 LoadedModule *GetLinker() { return nullptr; }
92 // Required on Linux for initialization of TLS behavior, but should not be
93 // required on Darwin.
94 void InitializePlatformSpecificModules() {
95 if (flags()->use_tls) {
96 Report("use_tls=1 is not supported on Darwin.\n");
101 // Scans global variables for heap pointers.
102 void ProcessGlobalRegions(Frontier *frontier) {
103 MemoryMappingLayout memory_mapping(false);
104 InternalMmapVector<LoadedModule> modules(/*initial_capacity*/ 128);
105 memory_mapping.DumpListOfModules(&modules);
106 for (uptr i = 0; i < modules.size(); ++i) {
107 // Even when global scanning is disabled, we still need to scan
108 // system libraries for stashed pointers
109 if (!flags()->use_globals && modules[i].instrumented()) continue;
111 for (const __sanitizer::LoadedModule::AddressRange &range :
112 modules[i].ranges()) {
113 // Sections storing global variables are writable and non-executable
114 if (range.executable || !range.writable) continue;
116 ScanGlobalRange(range.beg, range.end, frontier);
121 void ProcessPlatformSpecificAllocations(Frontier *frontier) {
122 mach_port_name_t port;
123 if (task_for_pid(mach_task_self(), internal_getpid(), &port)
130 vm_address_t address = 0;
131 kern_return_t err = KERN_SUCCESS;
132 mach_msg_type_number_t count = VM_REGION_SUBMAP_INFO_COUNT_64;
134 InternalMmapVector<RootRegion> const *root_regions = GetRootRegions();
136 while (err == KERN_SUCCESS) {
137 struct vm_region_submap_info_64 info;
138 err = vm_region_recurse_64(port, &address, &size, &depth,
139 (vm_region_info_t)&info, &count);
141 uptr end_address = address + size;
143 // libxpc stashes some pointers in the Kernel Alloc Once page,
144 // make sure not to report those as leaks.
145 if (info.user_tag == VM_MEMORY_OS_ALLOC_ONCE) {
146 ScanRangeForPointers(address, end_address, frontier, "GLOBAL",
149 // Recursing over the full memory map is very slow, break out
150 // early if we don't need the full iteration.
151 if (!flags()->use_root_regions || !root_regions->size())
155 // This additional root region scan is required on Darwin in order to
156 // detect root regions contained within mmap'd memory regions, because
157 // the Darwin implementation of sanitizer_procmaps traverses images
158 // as loaded by dyld, and not the complete set of all memory regions.
160 // TODO(fjricci) - remove this once sanitizer_procmaps_mac has the same
161 // behavior as sanitizer_procmaps_linux and traverses all memory regions
162 if (flags()->use_root_regions) {
163 for (uptr i = 0; i < root_regions->size(); i++) {
164 ScanRootRegion(frontier, (*root_regions)[i], address, end_address,
169 address = end_address;
173 void DoStopTheWorld(StopTheWorldCallback callback, void *argument) {
174 StopTheWorld(callback, argument);
177 } // namespace __lsan
179 #endif // CAN_SANITIZE_LEAKS && SANITIZER_MAC