1 //=-- lsan_common_mac.cpp -------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file is a part of LeakSanitizer.
10 // Implementation of common leak checking functionality. Darwin-specific code.
12 //===----------------------------------------------------------------------===//
14 #include "sanitizer_common/sanitizer_platform.h"
15 #include "sanitizer_common/sanitizer_libc.h"
16 #include "lsan_common.h"
18 #if CAN_SANITIZE_LEAKS && SANITIZER_APPLE
20 # include <mach/mach.h>
21 # include <mach/vm_statistics.h>
24 # include "lsan_allocator.h"
25 # include "sanitizer_common/sanitizer_allocator_internal.h"
28 class ThreadContextLsanBase
;
30 enum class SeenRegion
{
35 All
= AllocOnce
| LibDispatch
| Foundation
38 inline SeenRegion
operator|(SeenRegion left
, SeenRegion right
) {
39 return static_cast<SeenRegion
>(static_cast<int>(left
) |
40 static_cast<int>(right
));
43 inline SeenRegion
&operator|=(SeenRegion
&left
, const SeenRegion
&right
) {
48 struct RegionScanState
{
49 SeenRegion seen_regions
= SeenRegion::None
;
50 bool in_libdispatch
= false;
55 ThreadContextLsanBase
*current_thread
;
57 } thread_local_data_t
;
59 static pthread_key_t key
;
60 static pthread_once_t key_once
= PTHREAD_ONCE_INIT
;
62 // The main thread destructor requires the current thread,
63 // so we can't destroy it until it's been used and reset.
64 void restore_tid_data(void *ptr
) {
65 thread_local_data_t
*data
= (thread_local_data_t
*)ptr
;
66 if (data
->current_thread
)
67 pthread_setspecific(key
, data
);
70 static void make_tls_key() {
71 CHECK_EQ(pthread_key_create(&key
, restore_tid_data
), 0);
74 static thread_local_data_t
*get_tls_val(bool alloc
) {
75 pthread_once(&key_once
, make_tls_key
);
77 thread_local_data_t
*ptr
= (thread_local_data_t
*)pthread_getspecific(key
);
78 if (ptr
== NULL
&& alloc
) {
79 ptr
= (thread_local_data_t
*)InternalAlloc(sizeof(*ptr
));
80 ptr
->disable_counter
= 0;
81 ptr
->current_thread
= nullptr;
82 ptr
->cache
= AllocatorCache();
83 pthread_setspecific(key
, ptr
);
89 bool DisabledInThisThread() {
90 thread_local_data_t
*data
= get_tls_val(false);
91 return data
? data
->disable_counter
> 0 : false;
94 void DisableInThisThread() { ++get_tls_val(true)->disable_counter
; }
96 void EnableInThisThread() {
97 int *disable_counter
= &get_tls_val(true)->disable_counter
;
98 if (*disable_counter
== 0) {
99 DisableCounterUnderflow();
104 ThreadContextLsanBase
*GetCurrentThread() {
105 thread_local_data_t
*data
= get_tls_val(false);
106 return data
? data
->current_thread
: nullptr;
109 void SetCurrentThread(ThreadContextLsanBase
*tctx
) {
110 get_tls_val(true)->current_thread
= tctx
;
113 AllocatorCache
*GetAllocatorCache() { return &get_tls_val(true)->cache
; }
115 LoadedModule
*GetLinker() { return nullptr; }
117 // Required on Linux for initialization of TLS behavior, but should not be
118 // required on Darwin.
119 void InitializePlatformSpecificModules() {}
121 // Sections which can't contain contain global pointers. This list errs on the
122 // side of caution to avoid false positives, at the expense of performance.
124 // Other potentially safe sections include:
125 // __all_image_info, __crash_info, __const, __got, __interpose, __objc_msg_break
127 // Sections which definitely cannot be included here are:
128 // __objc_data, __objc_const, __data, __bss, __common, __thread_data,
129 // __thread_bss, __thread_vars, __objc_opt_rw, __objc_opt_ptrs
130 static const char *kSkippedSecNames
[] = {
131 "__cfstring", "__la_symbol_ptr", "__mod_init_func",
132 "__mod_term_func", "__nl_symbol_ptr", "__objc_classlist",
133 "__objc_classrefs", "__objc_imageinfo", "__objc_nlclslist",
134 "__objc_protolist", "__objc_selrefs", "__objc_superrefs"};
136 // Scans global variables for heap pointers.
137 void ProcessGlobalRegions(Frontier
*frontier
) {
138 for (auto name
: kSkippedSecNames
)
139 CHECK(internal_strnlen(name
, kMaxSegName
+ 1) <= kMaxSegName
);
141 MemoryMappingLayout
memory_mapping(false);
142 InternalMmapVector
<LoadedModule
> modules
;
143 modules
.reserve(128);
144 memory_mapping
.DumpListOfModules(&modules
);
145 for (uptr i
= 0; i
< modules
.size(); ++i
) {
146 // Even when global scanning is disabled, we still need to scan
147 // system libraries for stashed pointers
148 if (!flags()->use_globals
&& modules
[i
].instrumented()) continue;
150 for (const __sanitizer::LoadedModule::AddressRange
&range
:
151 modules
[i
].ranges()) {
152 // Sections storing global variables are writable and non-executable
153 if (range
.executable
|| !range
.writable
) continue;
155 for (auto name
: kSkippedSecNames
) {
156 if (!internal_strcmp(range
.name
, name
)) continue;
159 ScanGlobalRange(range
.beg
, range
.end
, frontier
);
164 void ProcessPlatformSpecificAllocations(Frontier
*frontier
) {
165 vm_address_t address
= 0;
166 kern_return_t err
= KERN_SUCCESS
;
168 InternalMmapVector
<Region
> mapped_regions
;
169 bool use_root_regions
= flags()->use_root_regions
&& HasRootRegions();
171 RegionScanState scan_state
;
172 while (err
== KERN_SUCCESS
) {
175 struct vm_region_submap_info_64 info
;
176 mach_msg_type_number_t count
= VM_REGION_SUBMAP_INFO_COUNT_64
;
177 err
= vm_region_recurse_64(mach_task_self(), &address
, &size
, &depth
,
178 (vm_region_info_t
)&info
, &count
);
180 uptr end_address
= address
+ size
;
181 if (info
.user_tag
== VM_MEMORY_OS_ALLOC_ONCE
) {
182 // libxpc stashes some pointers in the Kernel Alloc Once page,
183 // make sure not to report those as leaks.
184 scan_state
.seen_regions
|= SeenRegion::AllocOnce
;
185 ScanRangeForPointers(address
, end_address
, frontier
, "GLOBAL",
187 } else if (info
.user_tag
== VM_MEMORY_FOUNDATION
) {
188 // Objective-C block trampolines use the Foundation region.
189 scan_state
.seen_regions
|= SeenRegion::Foundation
;
190 ScanRangeForPointers(address
, end_address
, frontier
, "GLOBAL",
192 } else if (info
.user_tag
== VM_MEMORY_LIBDISPATCH
) {
193 // Dispatch continuations use the libdispatch region. Empirically, there
194 // can be more than one region with this tag, so we'll optimistically
195 // assume that they're continguous. Otherwise, we would need to scan every
196 // region to ensure we find them all.
197 scan_state
.in_libdispatch
= true;
198 ScanRangeForPointers(address
, end_address
, frontier
, "GLOBAL",
200 } else if (scan_state
.in_libdispatch
) {
201 scan_state
.seen_regions
|= SeenRegion::LibDispatch
;
202 scan_state
.in_libdispatch
= false;
205 // Recursing over the full memory map is very slow, break out
206 // early if we don't need the full iteration.
207 if (scan_state
.seen_regions
== SeenRegion::All
&& !use_root_regions
) {
211 // This additional root region scan is required on Darwin in order to
212 // detect root regions contained within mmap'd memory regions, because
213 // the Darwin implementation of sanitizer_procmaps traverses images
214 // as loaded by dyld, and not the complete set of all memory regions.
216 // TODO(fjricci) - remove this once sanitizer_procmaps_mac has the same
217 // behavior as sanitizer_procmaps_linux and traverses all memory regions
218 if (use_root_regions
&& (info
.protection
& kProtectionRead
))
219 mapped_regions
.push_back({address
, end_address
});
221 address
= end_address
;
223 ScanRootRegions(frontier
, mapped_regions
);
226 // On darwin, we can intercept _exit gracefully, and return a failing exit code
227 // if required at that point. Calling Die() here is undefined behavior and
228 // causes rare race conditions.
229 void HandleLeaks() {}
231 void LockStuffAndStopTheWorld(StopTheWorldCallback callback
,
232 CheckForLeaksParam
*argument
) {
233 ScopedStopTheWorldLock lock
;
234 StopTheWorld(callback
, argument
);
237 } // namespace __lsan
239 #endif // CAN_SANITIZE_LEAKS && SANITIZER_APPLE