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