xref: /openbsd-src/gnu/llvm/compiler-rt/lib/lsan/lsan_common_mac.cpp (revision 810390e339a5425391477d5d41c78d7cab2424ac)
13cab2bb3Spatrick //=-- lsan_common_mac.cpp -------------------------------------------------===//
23cab2bb3Spatrick //
33cab2bb3Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
43cab2bb3Spatrick // See https://llvm.org/LICENSE.txt for license information.
53cab2bb3Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
63cab2bb3Spatrick //
73cab2bb3Spatrick //===----------------------------------------------------------------------===//
83cab2bb3Spatrick //
93cab2bb3Spatrick // This file is a part of LeakSanitizer.
103cab2bb3Spatrick // Implementation of common leak checking functionality. Darwin-specific code.
113cab2bb3Spatrick //
123cab2bb3Spatrick //===----------------------------------------------------------------------===//
133cab2bb3Spatrick 
143cab2bb3Spatrick #include "sanitizer_common/sanitizer_platform.h"
153cab2bb3Spatrick #include "sanitizer_common/sanitizer_libc.h"
163cab2bb3Spatrick #include "lsan_common.h"
173cab2bb3Spatrick 
18*810390e3Srobert #if CAN_SANITIZE_LEAKS && SANITIZER_APPLE
193cab2bb3Spatrick 
203cab2bb3Spatrick #  include <mach/mach.h>
21*810390e3Srobert #  include <mach/vm_statistics.h>
22*810390e3Srobert #  include <pthread.h>
233cab2bb3Spatrick 
24*810390e3Srobert #  include "lsan_allocator.h"
25*810390e3Srobert #  include "sanitizer_common/sanitizer_allocator_internal.h"
263cab2bb3Spatrick namespace __lsan {
273cab2bb3Spatrick 
28*810390e3Srobert enum class SeenRegion {
29*810390e3Srobert   None = 0,
30*810390e3Srobert   AllocOnce = 1 << 0,
31*810390e3Srobert   LibDispatch = 1 << 1,
32*810390e3Srobert   Foundation = 1 << 2,
33*810390e3Srobert   All = AllocOnce | LibDispatch | Foundation
34*810390e3Srobert };
35*810390e3Srobert 
operator |(SeenRegion left,SeenRegion right)36*810390e3Srobert inline SeenRegion operator|(SeenRegion left, SeenRegion right) {
37*810390e3Srobert   return static_cast<SeenRegion>(static_cast<int>(left) |
38*810390e3Srobert                                  static_cast<int>(right));
39*810390e3Srobert }
40*810390e3Srobert 
operator |=(SeenRegion & left,const SeenRegion & right)41*810390e3Srobert inline SeenRegion &operator|=(SeenRegion &left, const SeenRegion &right) {
42*810390e3Srobert   left = left | right;
43*810390e3Srobert   return left;
44*810390e3Srobert }
45*810390e3Srobert 
46*810390e3Srobert struct RegionScanState {
47*810390e3Srobert   SeenRegion seen_regions = SeenRegion::None;
48*810390e3Srobert   bool in_libdispatch = false;
49*810390e3Srobert };
50*810390e3Srobert 
513cab2bb3Spatrick typedef struct {
523cab2bb3Spatrick   int disable_counter;
533cab2bb3Spatrick   u32 current_thread_id;
543cab2bb3Spatrick   AllocatorCache cache;
553cab2bb3Spatrick } thread_local_data_t;
563cab2bb3Spatrick 
573cab2bb3Spatrick static pthread_key_t key;
583cab2bb3Spatrick static pthread_once_t key_once = PTHREAD_ONCE_INIT;
593cab2bb3Spatrick 
603cab2bb3Spatrick // The main thread destructor requires the current thread id,
613cab2bb3Spatrick // so we can't destroy it until it's been used and reset to invalid tid
restore_tid_data(void * ptr)623cab2bb3Spatrick void restore_tid_data(void *ptr) {
633cab2bb3Spatrick   thread_local_data_t *data = (thread_local_data_t *)ptr;
643cab2bb3Spatrick   if (data->current_thread_id != kInvalidTid)
653cab2bb3Spatrick     pthread_setspecific(key, data);
663cab2bb3Spatrick }
673cab2bb3Spatrick 
make_tls_key()683cab2bb3Spatrick static void make_tls_key() {
693cab2bb3Spatrick   CHECK_EQ(pthread_key_create(&key, restore_tid_data), 0);
703cab2bb3Spatrick }
713cab2bb3Spatrick 
get_tls_val(bool alloc)723cab2bb3Spatrick static thread_local_data_t *get_tls_val(bool alloc) {
733cab2bb3Spatrick   pthread_once(&key_once, make_tls_key);
743cab2bb3Spatrick 
753cab2bb3Spatrick   thread_local_data_t *ptr = (thread_local_data_t *)pthread_getspecific(key);
763cab2bb3Spatrick   if (ptr == NULL && alloc) {
773cab2bb3Spatrick     ptr = (thread_local_data_t *)InternalAlloc(sizeof(*ptr));
783cab2bb3Spatrick     ptr->disable_counter = 0;
793cab2bb3Spatrick     ptr->current_thread_id = kInvalidTid;
803cab2bb3Spatrick     ptr->cache = AllocatorCache();
813cab2bb3Spatrick     pthread_setspecific(key, ptr);
823cab2bb3Spatrick   }
833cab2bb3Spatrick 
843cab2bb3Spatrick   return ptr;
853cab2bb3Spatrick }
863cab2bb3Spatrick 
DisabledInThisThread()873cab2bb3Spatrick bool DisabledInThisThread() {
883cab2bb3Spatrick   thread_local_data_t *data = get_tls_val(false);
893cab2bb3Spatrick   return data ? data->disable_counter > 0 : false;
903cab2bb3Spatrick }
913cab2bb3Spatrick 
DisableInThisThread()923cab2bb3Spatrick void DisableInThisThread() { ++get_tls_val(true)->disable_counter; }
933cab2bb3Spatrick 
EnableInThisThread()943cab2bb3Spatrick void EnableInThisThread() {
953cab2bb3Spatrick   int *disable_counter = &get_tls_val(true)->disable_counter;
963cab2bb3Spatrick   if (*disable_counter == 0) {
973cab2bb3Spatrick     DisableCounterUnderflow();
983cab2bb3Spatrick   }
993cab2bb3Spatrick   --*disable_counter;
1003cab2bb3Spatrick }
1013cab2bb3Spatrick 
GetCurrentThread()1023cab2bb3Spatrick u32 GetCurrentThread() {
1033cab2bb3Spatrick   thread_local_data_t *data = get_tls_val(false);
1043cab2bb3Spatrick   return data ? data->current_thread_id : kInvalidTid;
1053cab2bb3Spatrick }
1063cab2bb3Spatrick 
SetCurrentThread(u32 tid)1073cab2bb3Spatrick void SetCurrentThread(u32 tid) { get_tls_val(true)->current_thread_id = tid; }
1083cab2bb3Spatrick 
GetAllocatorCache()1093cab2bb3Spatrick AllocatorCache *GetAllocatorCache() { return &get_tls_val(true)->cache; }
1103cab2bb3Spatrick 
GetLinker()1113cab2bb3Spatrick LoadedModule *GetLinker() { return nullptr; }
1123cab2bb3Spatrick 
1133cab2bb3Spatrick // Required on Linux for initialization of TLS behavior, but should not be
1143cab2bb3Spatrick // required on Darwin.
InitializePlatformSpecificModules()1153cab2bb3Spatrick void InitializePlatformSpecificModules() {}
1163cab2bb3Spatrick 
1173cab2bb3Spatrick // Sections which can't contain contain global pointers. This list errs on the
1183cab2bb3Spatrick // side of caution to avoid false positives, at the expense of performance.
1193cab2bb3Spatrick //
1203cab2bb3Spatrick // Other potentially safe sections include:
1213cab2bb3Spatrick // __all_image_info, __crash_info, __const, __got, __interpose, __objc_msg_break
1223cab2bb3Spatrick //
1233cab2bb3Spatrick // Sections which definitely cannot be included here are:
1243cab2bb3Spatrick // __objc_data, __objc_const, __data, __bss, __common, __thread_data,
1253cab2bb3Spatrick // __thread_bss, __thread_vars, __objc_opt_rw, __objc_opt_ptrs
1263cab2bb3Spatrick static const char *kSkippedSecNames[] = {
1273cab2bb3Spatrick     "__cfstring",       "__la_symbol_ptr",  "__mod_init_func",
1283cab2bb3Spatrick     "__mod_term_func",  "__nl_symbol_ptr",  "__objc_classlist",
1293cab2bb3Spatrick     "__objc_classrefs", "__objc_imageinfo", "__objc_nlclslist",
1303cab2bb3Spatrick     "__objc_protolist", "__objc_selrefs",   "__objc_superrefs"};
1313cab2bb3Spatrick 
1323cab2bb3Spatrick // Scans global variables for heap pointers.
ProcessGlobalRegions(Frontier * frontier)1333cab2bb3Spatrick void ProcessGlobalRegions(Frontier *frontier) {
1343cab2bb3Spatrick   for (auto name : kSkippedSecNames)
1353cab2bb3Spatrick     CHECK(internal_strnlen(name, kMaxSegName + 1) <= kMaxSegName);
1363cab2bb3Spatrick 
1373cab2bb3Spatrick   MemoryMappingLayout memory_mapping(false);
1383cab2bb3Spatrick   InternalMmapVector<LoadedModule> modules;
1393cab2bb3Spatrick   modules.reserve(128);
1403cab2bb3Spatrick   memory_mapping.DumpListOfModules(&modules);
1413cab2bb3Spatrick   for (uptr i = 0; i < modules.size(); ++i) {
1423cab2bb3Spatrick     // Even when global scanning is disabled, we still need to scan
1433cab2bb3Spatrick     // system libraries for stashed pointers
1443cab2bb3Spatrick     if (!flags()->use_globals && modules[i].instrumented()) continue;
1453cab2bb3Spatrick 
1463cab2bb3Spatrick     for (const __sanitizer::LoadedModule::AddressRange &range :
1473cab2bb3Spatrick          modules[i].ranges()) {
1483cab2bb3Spatrick       // Sections storing global variables are writable and non-executable
1493cab2bb3Spatrick       if (range.executable || !range.writable) continue;
1503cab2bb3Spatrick 
1513cab2bb3Spatrick       for (auto name : kSkippedSecNames) {
1523cab2bb3Spatrick         if (!internal_strcmp(range.name, name)) continue;
1533cab2bb3Spatrick       }
1543cab2bb3Spatrick 
1553cab2bb3Spatrick       ScanGlobalRange(range.beg, range.end, frontier);
1563cab2bb3Spatrick     }
1573cab2bb3Spatrick   }
1583cab2bb3Spatrick }
1593cab2bb3Spatrick 
ProcessPlatformSpecificAllocations(Frontier * frontier)1603cab2bb3Spatrick void ProcessPlatformSpecificAllocations(Frontier *frontier) {
1613cab2bb3Spatrick   vm_address_t address = 0;
1623cab2bb3Spatrick   kern_return_t err = KERN_SUCCESS;
1633cab2bb3Spatrick 
164*810390e3Srobert   InternalMmapVectorNoCtor<RootRegion> const *root_regions = GetRootRegions();
1653cab2bb3Spatrick 
166*810390e3Srobert   RegionScanState scan_state;
1673cab2bb3Spatrick   while (err == KERN_SUCCESS) {
168*810390e3Srobert     vm_size_t size = 0;
169*810390e3Srobert     unsigned depth = 1;
1703cab2bb3Spatrick     struct vm_region_submap_info_64 info;
171*810390e3Srobert     mach_msg_type_number_t count = VM_REGION_SUBMAP_INFO_COUNT_64;
1723cab2bb3Spatrick     err = vm_region_recurse_64(mach_task_self(), &address, &size, &depth,
1733cab2bb3Spatrick                                (vm_region_info_t)&info, &count);
1743cab2bb3Spatrick 
1753cab2bb3Spatrick     uptr end_address = address + size;
176*810390e3Srobert     if (info.user_tag == VM_MEMORY_OS_ALLOC_ONCE) {
1773cab2bb3Spatrick       // libxpc stashes some pointers in the Kernel Alloc Once page,
1783cab2bb3Spatrick       // make sure not to report those as leaks.
179*810390e3Srobert       scan_state.seen_regions |= SeenRegion::AllocOnce;
1803cab2bb3Spatrick       ScanRangeForPointers(address, end_address, frontier, "GLOBAL",
1813cab2bb3Spatrick                            kReachable);
182*810390e3Srobert     } else if (info.user_tag == VM_MEMORY_FOUNDATION) {
183*810390e3Srobert       // Objective-C block trampolines use the Foundation region.
184*810390e3Srobert       scan_state.seen_regions |= SeenRegion::Foundation;
185*810390e3Srobert       ScanRangeForPointers(address, end_address, frontier, "GLOBAL",
186*810390e3Srobert                            kReachable);
187*810390e3Srobert     } else if (info.user_tag == VM_MEMORY_LIBDISPATCH) {
188*810390e3Srobert       // Dispatch continuations use the libdispatch region. Empirically, there
189*810390e3Srobert       // can be more than one region with this tag, so we'll optimistically
190*810390e3Srobert       // assume that they're continguous. Otherwise, we would need to scan every
191*810390e3Srobert       // region to ensure we find them all.
192*810390e3Srobert       scan_state.in_libdispatch = true;
193*810390e3Srobert       ScanRangeForPointers(address, end_address, frontier, "GLOBAL",
194*810390e3Srobert                            kReachable);
195*810390e3Srobert     } else if (scan_state.in_libdispatch) {
196*810390e3Srobert       scan_state.seen_regions |= SeenRegion::LibDispatch;
197*810390e3Srobert       scan_state.in_libdispatch = false;
198*810390e3Srobert     }
1993cab2bb3Spatrick 
2003cab2bb3Spatrick     // Recursing over the full memory map is very slow, break out
2013cab2bb3Spatrick     // early if we don't need the full iteration.
202*810390e3Srobert     if (scan_state.seen_regions == SeenRegion::All &&
203*810390e3Srobert         !(flags()->use_root_regions && root_regions->size() > 0)) {
2043cab2bb3Spatrick       break;
2053cab2bb3Spatrick     }
2063cab2bb3Spatrick 
2073cab2bb3Spatrick     // This additional root region scan is required on Darwin in order to
2083cab2bb3Spatrick     // detect root regions contained within mmap'd memory regions, because
2093cab2bb3Spatrick     // the Darwin implementation of sanitizer_procmaps traverses images
2103cab2bb3Spatrick     // as loaded by dyld, and not the complete set of all memory regions.
2113cab2bb3Spatrick     //
2123cab2bb3Spatrick     // TODO(fjricci) - remove this once sanitizer_procmaps_mac has the same
2133cab2bb3Spatrick     // behavior as sanitizer_procmaps_linux and traverses all memory regions
2143cab2bb3Spatrick     if (flags()->use_root_regions) {
2153cab2bb3Spatrick       for (uptr i = 0; i < root_regions->size(); i++) {
2163cab2bb3Spatrick         ScanRootRegion(frontier, (*root_regions)[i], address, end_address,
2173cab2bb3Spatrick                        info.protection & kProtectionRead);
2183cab2bb3Spatrick       }
2193cab2bb3Spatrick     }
2203cab2bb3Spatrick 
2213cab2bb3Spatrick     address = end_address;
2223cab2bb3Spatrick   }
2233cab2bb3Spatrick }
2243cab2bb3Spatrick 
2253cab2bb3Spatrick // On darwin, we can intercept _exit gracefully, and return a failing exit code
2263cab2bb3Spatrick // if required at that point. Calling Die() here is undefined behavior and
2273cab2bb3Spatrick // causes rare race conditions.
HandleLeaks()2283cab2bb3Spatrick void HandleLeaks() {}
2293cab2bb3Spatrick 
LockStuffAndStopTheWorld(StopTheWorldCallback callback,CheckForLeaksParam * argument)2301f9cb04fSpatrick void LockStuffAndStopTheWorld(StopTheWorldCallback callback,
2311f9cb04fSpatrick                               CheckForLeaksParam *argument) {
232*810390e3Srobert   ScopedStopTheWorldLock lock;
2333cab2bb3Spatrick   StopTheWorld(callback, argument);
2343cab2bb3Spatrick }
2353cab2bb3Spatrick 
2363cab2bb3Spatrick }  // namespace __lsan
2373cab2bb3Spatrick 
238*810390e3Srobert #endif // CAN_SANITIZE_LEAKS && SANITIZER_APPLE
239