xref: /freebsd-src/contrib/llvm-project/compiler-rt/lib/lsan/lsan_common_mac.cpp (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
168d75effSDimitry Andric //=-- lsan_common_mac.cpp -------------------------------------------------===//
268d75effSDimitry Andric //
368d75effSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
468d75effSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
568d75effSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
668d75effSDimitry Andric //
768d75effSDimitry Andric //===----------------------------------------------------------------------===//
868d75effSDimitry Andric //
968d75effSDimitry Andric // This file is a part of LeakSanitizer.
1068d75effSDimitry Andric // Implementation of common leak checking functionality. Darwin-specific code.
1168d75effSDimitry Andric //
1268d75effSDimitry Andric //===----------------------------------------------------------------------===//
1368d75effSDimitry Andric 
1468d75effSDimitry Andric #include "sanitizer_common/sanitizer_platform.h"
1568d75effSDimitry Andric #include "sanitizer_common/sanitizer_libc.h"
1668d75effSDimitry Andric #include "lsan_common.h"
1768d75effSDimitry Andric 
1881ad6265SDimitry Andric #if CAN_SANITIZE_LEAKS && SANITIZER_APPLE
1968d75effSDimitry Andric 
20bdd1243dSDimitry Andric #  include <mach/mach.h>
21bdd1243dSDimitry Andric #  include <mach/vm_statistics.h>
2268d75effSDimitry Andric #  include <pthread.h>
2368d75effSDimitry Andric 
24bdd1243dSDimitry Andric #  include "lsan_allocator.h"
25bdd1243dSDimitry Andric #  include "sanitizer_common/sanitizer_allocator_internal.h"
2668d75effSDimitry Andric namespace __lsan {
2768d75effSDimitry Andric 
28*06c3fb27SDimitry Andric class ThreadContextLsanBase;
29*06c3fb27SDimitry Andric 
30bdd1243dSDimitry Andric enum class SeenRegion {
31bdd1243dSDimitry Andric   None = 0,
32bdd1243dSDimitry Andric   AllocOnce = 1 << 0,
33bdd1243dSDimitry Andric   LibDispatch = 1 << 1,
34bdd1243dSDimitry Andric   Foundation = 1 << 2,
35bdd1243dSDimitry Andric   All = AllocOnce | LibDispatch | Foundation
36bdd1243dSDimitry Andric };
37bdd1243dSDimitry Andric 
operator |(SeenRegion left,SeenRegion right)38bdd1243dSDimitry Andric inline SeenRegion operator|(SeenRegion left, SeenRegion right) {
39bdd1243dSDimitry Andric   return static_cast<SeenRegion>(static_cast<int>(left) |
40bdd1243dSDimitry Andric                                  static_cast<int>(right));
41bdd1243dSDimitry Andric }
42bdd1243dSDimitry Andric 
operator |=(SeenRegion & left,const SeenRegion & right)43bdd1243dSDimitry Andric inline SeenRegion &operator|=(SeenRegion &left, const SeenRegion &right) {
44bdd1243dSDimitry Andric   left = left | right;
45bdd1243dSDimitry Andric   return left;
46bdd1243dSDimitry Andric }
47bdd1243dSDimitry Andric 
48bdd1243dSDimitry Andric struct RegionScanState {
49bdd1243dSDimitry Andric   SeenRegion seen_regions = SeenRegion::None;
50bdd1243dSDimitry Andric   bool in_libdispatch = false;
51bdd1243dSDimitry Andric };
52bdd1243dSDimitry Andric 
5368d75effSDimitry Andric typedef struct {
5468d75effSDimitry Andric   int disable_counter;
55*06c3fb27SDimitry Andric   ThreadContextLsanBase *current_thread;
5668d75effSDimitry Andric   AllocatorCache cache;
5768d75effSDimitry Andric } thread_local_data_t;
5868d75effSDimitry Andric 
5968d75effSDimitry Andric static pthread_key_t key;
6068d75effSDimitry Andric static pthread_once_t key_once = PTHREAD_ONCE_INIT;
6168d75effSDimitry Andric 
62*06c3fb27SDimitry Andric // The main thread destructor requires the current thread,
63*06c3fb27SDimitry Andric // so we can't destroy it until it's been used and reset.
restore_tid_data(void * ptr)6468d75effSDimitry Andric void restore_tid_data(void *ptr) {
6568d75effSDimitry Andric   thread_local_data_t *data = (thread_local_data_t *)ptr;
66*06c3fb27SDimitry Andric   if (data->current_thread)
6768d75effSDimitry Andric     pthread_setspecific(key, data);
6868d75effSDimitry Andric }
6968d75effSDimitry Andric 
make_tls_key()7068d75effSDimitry Andric static void make_tls_key() {
7168d75effSDimitry Andric   CHECK_EQ(pthread_key_create(&key, restore_tid_data), 0);
7268d75effSDimitry Andric }
7368d75effSDimitry Andric 
get_tls_val(bool alloc)7468d75effSDimitry Andric static thread_local_data_t *get_tls_val(bool alloc) {
7568d75effSDimitry Andric   pthread_once(&key_once, make_tls_key);
7668d75effSDimitry Andric 
7768d75effSDimitry Andric   thread_local_data_t *ptr = (thread_local_data_t *)pthread_getspecific(key);
7868d75effSDimitry Andric   if (ptr == NULL && alloc) {
7968d75effSDimitry Andric     ptr = (thread_local_data_t *)InternalAlloc(sizeof(*ptr));
8068d75effSDimitry Andric     ptr->disable_counter = 0;
81*06c3fb27SDimitry Andric     ptr->current_thread = nullptr;
8268d75effSDimitry Andric     ptr->cache = AllocatorCache();
8368d75effSDimitry Andric     pthread_setspecific(key, ptr);
8468d75effSDimitry Andric   }
8568d75effSDimitry Andric 
8668d75effSDimitry Andric   return ptr;
8768d75effSDimitry Andric }
8868d75effSDimitry Andric 
DisabledInThisThread()8968d75effSDimitry Andric bool DisabledInThisThread() {
9068d75effSDimitry Andric   thread_local_data_t *data = get_tls_val(false);
9168d75effSDimitry Andric   return data ? data->disable_counter > 0 : false;
9268d75effSDimitry Andric }
9368d75effSDimitry Andric 
DisableInThisThread()9468d75effSDimitry Andric void DisableInThisThread() { ++get_tls_val(true)->disable_counter; }
9568d75effSDimitry Andric 
EnableInThisThread()9668d75effSDimitry Andric void EnableInThisThread() {
9768d75effSDimitry Andric   int *disable_counter = &get_tls_val(true)->disable_counter;
9868d75effSDimitry Andric   if (*disable_counter == 0) {
9968d75effSDimitry Andric     DisableCounterUnderflow();
10068d75effSDimitry Andric   }
10168d75effSDimitry Andric   --*disable_counter;
10268d75effSDimitry Andric }
10368d75effSDimitry Andric 
GetCurrentThread()104*06c3fb27SDimitry Andric ThreadContextLsanBase *GetCurrentThread() {
10568d75effSDimitry Andric   thread_local_data_t *data = get_tls_val(false);
106*06c3fb27SDimitry Andric   return data ? data->current_thread : nullptr;
10768d75effSDimitry Andric }
10868d75effSDimitry Andric 
SetCurrentThread(ThreadContextLsanBase * tctx)109*06c3fb27SDimitry Andric void SetCurrentThread(ThreadContextLsanBase *tctx) {
110*06c3fb27SDimitry Andric   get_tls_val(true)->current_thread = tctx;
111*06c3fb27SDimitry Andric }
11268d75effSDimitry Andric 
GetAllocatorCache()11368d75effSDimitry Andric AllocatorCache *GetAllocatorCache() { return &get_tls_val(true)->cache; }
11468d75effSDimitry Andric 
GetLinker()11568d75effSDimitry Andric LoadedModule *GetLinker() { return nullptr; }
11668d75effSDimitry Andric 
11768d75effSDimitry Andric // Required on Linux for initialization of TLS behavior, but should not be
11868d75effSDimitry Andric // required on Darwin.
InitializePlatformSpecificModules()11968d75effSDimitry Andric void InitializePlatformSpecificModules() {}
12068d75effSDimitry Andric 
12168d75effSDimitry Andric // Sections which can't contain contain global pointers. This list errs on the
12268d75effSDimitry Andric // side of caution to avoid false positives, at the expense of performance.
12368d75effSDimitry Andric //
12468d75effSDimitry Andric // Other potentially safe sections include:
12568d75effSDimitry Andric // __all_image_info, __crash_info, __const, __got, __interpose, __objc_msg_break
12668d75effSDimitry Andric //
12768d75effSDimitry Andric // Sections which definitely cannot be included here are:
12868d75effSDimitry Andric // __objc_data, __objc_const, __data, __bss, __common, __thread_data,
12968d75effSDimitry Andric // __thread_bss, __thread_vars, __objc_opt_rw, __objc_opt_ptrs
13068d75effSDimitry Andric static const char *kSkippedSecNames[] = {
13168d75effSDimitry Andric     "__cfstring",       "__la_symbol_ptr",  "__mod_init_func",
13268d75effSDimitry Andric     "__mod_term_func",  "__nl_symbol_ptr",  "__objc_classlist",
13368d75effSDimitry Andric     "__objc_classrefs", "__objc_imageinfo", "__objc_nlclslist",
13468d75effSDimitry Andric     "__objc_protolist", "__objc_selrefs",   "__objc_superrefs"};
13568d75effSDimitry Andric 
13668d75effSDimitry Andric // Scans global variables for heap pointers.
ProcessGlobalRegions(Frontier * frontier)13768d75effSDimitry Andric void ProcessGlobalRegions(Frontier *frontier) {
13868d75effSDimitry Andric   for (auto name : kSkippedSecNames)
13968d75effSDimitry Andric     CHECK(internal_strnlen(name, kMaxSegName + 1) <= kMaxSegName);
14068d75effSDimitry Andric 
14168d75effSDimitry Andric   MemoryMappingLayout memory_mapping(false);
14268d75effSDimitry Andric   InternalMmapVector<LoadedModule> modules;
14368d75effSDimitry Andric   modules.reserve(128);
14468d75effSDimitry Andric   memory_mapping.DumpListOfModules(&modules);
14568d75effSDimitry Andric   for (uptr i = 0; i < modules.size(); ++i) {
14668d75effSDimitry Andric     // Even when global scanning is disabled, we still need to scan
14768d75effSDimitry Andric     // system libraries for stashed pointers
14868d75effSDimitry Andric     if (!flags()->use_globals && modules[i].instrumented()) continue;
14968d75effSDimitry Andric 
15068d75effSDimitry Andric     for (const __sanitizer::LoadedModule::AddressRange &range :
15168d75effSDimitry Andric          modules[i].ranges()) {
15268d75effSDimitry Andric       // Sections storing global variables are writable and non-executable
15368d75effSDimitry Andric       if (range.executable || !range.writable) continue;
15468d75effSDimitry Andric 
15568d75effSDimitry Andric       for (auto name : kSkippedSecNames) {
15668d75effSDimitry Andric         if (!internal_strcmp(range.name, name)) continue;
15768d75effSDimitry Andric       }
15868d75effSDimitry Andric 
15968d75effSDimitry Andric       ScanGlobalRange(range.beg, range.end, frontier);
16068d75effSDimitry Andric     }
16168d75effSDimitry Andric   }
16268d75effSDimitry Andric }
16368d75effSDimitry Andric 
ProcessPlatformSpecificAllocations(Frontier * frontier)16468d75effSDimitry Andric void ProcessPlatformSpecificAllocations(Frontier *frontier) {
16568d75effSDimitry Andric   vm_address_t address = 0;
16668d75effSDimitry Andric   kern_return_t err = KERN_SUCCESS;
16768d75effSDimitry Andric 
168*06c3fb27SDimitry Andric   InternalMmapVector<Region> mapped_regions;
169*06c3fb27SDimitry Andric   bool use_root_regions = flags()->use_root_regions && HasRootRegions();
17068d75effSDimitry Andric 
171bdd1243dSDimitry Andric   RegionScanState scan_state;
17268d75effSDimitry Andric   while (err == KERN_SUCCESS) {
17304eeddc0SDimitry Andric     vm_size_t size = 0;
17404eeddc0SDimitry Andric     unsigned depth = 1;
17568d75effSDimitry Andric     struct vm_region_submap_info_64 info;
17604eeddc0SDimitry Andric     mach_msg_type_number_t count = VM_REGION_SUBMAP_INFO_COUNT_64;
17768d75effSDimitry Andric     err = vm_region_recurse_64(mach_task_self(), &address, &size, &depth,
17868d75effSDimitry Andric                                (vm_region_info_t)&info, &count);
17968d75effSDimitry Andric 
18068d75effSDimitry Andric     uptr end_address = address + size;
181bdd1243dSDimitry Andric     if (info.user_tag == VM_MEMORY_OS_ALLOC_ONCE) {
18268d75effSDimitry Andric       // libxpc stashes some pointers in the Kernel Alloc Once page,
18368d75effSDimitry Andric       // make sure not to report those as leaks.
184bdd1243dSDimitry Andric       scan_state.seen_regions |= SeenRegion::AllocOnce;
18568d75effSDimitry Andric       ScanRangeForPointers(address, end_address, frontier, "GLOBAL",
18668d75effSDimitry Andric                            kReachable);
187bdd1243dSDimitry Andric     } else if (info.user_tag == VM_MEMORY_FOUNDATION) {
188bdd1243dSDimitry Andric       // Objective-C block trampolines use the Foundation region.
189bdd1243dSDimitry Andric       scan_state.seen_regions |= SeenRegion::Foundation;
190bdd1243dSDimitry Andric       ScanRangeForPointers(address, end_address, frontier, "GLOBAL",
191bdd1243dSDimitry Andric                            kReachable);
192bdd1243dSDimitry Andric     } else if (info.user_tag == VM_MEMORY_LIBDISPATCH) {
193bdd1243dSDimitry Andric       // Dispatch continuations use the libdispatch region. Empirically, there
194bdd1243dSDimitry Andric       // can be more than one region with this tag, so we'll optimistically
195bdd1243dSDimitry Andric       // assume that they're continguous. Otherwise, we would need to scan every
196bdd1243dSDimitry Andric       // region to ensure we find them all.
197bdd1243dSDimitry Andric       scan_state.in_libdispatch = true;
198bdd1243dSDimitry Andric       ScanRangeForPointers(address, end_address, frontier, "GLOBAL",
199bdd1243dSDimitry Andric                            kReachable);
200bdd1243dSDimitry Andric     } else if (scan_state.in_libdispatch) {
201bdd1243dSDimitry Andric       scan_state.seen_regions |= SeenRegion::LibDispatch;
202bdd1243dSDimitry Andric       scan_state.in_libdispatch = false;
203bdd1243dSDimitry Andric     }
20468d75effSDimitry Andric 
20568d75effSDimitry Andric     // Recursing over the full memory map is very slow, break out
20668d75effSDimitry Andric     // early if we don't need the full iteration.
207*06c3fb27SDimitry Andric     if (scan_state.seen_regions == SeenRegion::All && !use_root_regions) {
20868d75effSDimitry Andric       break;
20968d75effSDimitry Andric     }
21068d75effSDimitry Andric 
21168d75effSDimitry Andric     // This additional root region scan is required on Darwin in order to
21268d75effSDimitry Andric     // detect root regions contained within mmap'd memory regions, because
21368d75effSDimitry Andric     // the Darwin implementation of sanitizer_procmaps traverses images
21468d75effSDimitry Andric     // as loaded by dyld, and not the complete set of all memory regions.
21568d75effSDimitry Andric     //
21668d75effSDimitry Andric     // TODO(fjricci) - remove this once sanitizer_procmaps_mac has the same
21768d75effSDimitry Andric     // behavior as sanitizer_procmaps_linux and traverses all memory regions
218*06c3fb27SDimitry Andric     if (use_root_regions && (info.protection & kProtectionRead))
219*06c3fb27SDimitry Andric       mapped_regions.push_back({address, end_address});
22068d75effSDimitry Andric 
22168d75effSDimitry Andric     address = end_address;
22268d75effSDimitry Andric   }
223*06c3fb27SDimitry Andric   ScanRootRegions(frontier, mapped_regions);
22468d75effSDimitry Andric }
22568d75effSDimitry Andric 
22668d75effSDimitry Andric // On darwin, we can intercept _exit gracefully, and return a failing exit code
22768d75effSDimitry Andric // if required at that point. Calling Die() here is undefined behavior and
22868d75effSDimitry Andric // causes rare race conditions.
HandleLeaks()22968d75effSDimitry Andric void HandleLeaks() {}
23068d75effSDimitry Andric 
LockStuffAndStopTheWorld(StopTheWorldCallback callback,CheckForLeaksParam * argument)2315ffd83dbSDimitry Andric void LockStuffAndStopTheWorld(StopTheWorldCallback callback,
2325ffd83dbSDimitry Andric                               CheckForLeaksParam *argument) {
2330eae32dcSDimitry Andric   ScopedStopTheWorldLock lock;
23468d75effSDimitry Andric   StopTheWorld(callback, argument);
23568d75effSDimitry Andric }
23668d75effSDimitry Andric 
23768d75effSDimitry Andric }  // namespace __lsan
23868d75effSDimitry Andric 
23981ad6265SDimitry Andric #endif // CAN_SANITIZE_LEAKS && SANITIZER_APPLE
240