Path: blob/main/contrib/llvm-project/compiler-rt/lib/lsan/lsan_common_mac.cpp
35233 views
//=-- lsan_common_mac.cpp -------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file is a part of LeakSanitizer.9// Implementation of common leak checking functionality. Darwin-specific code.10//11//===----------------------------------------------------------------------===//1213#include "sanitizer_common/sanitizer_platform.h"14#include "sanitizer_common/sanitizer_libc.h"15#include "lsan_common.h"1617#if CAN_SANITIZE_LEAKS && SANITIZER_APPLE1819# include <mach/mach.h>20# include <mach/vm_statistics.h>21# include <pthread.h>2223# include "lsan_allocator.h"24# include "sanitizer_common/sanitizer_allocator_internal.h"25namespace __lsan {2627class ThreadContextLsanBase;2829enum class SeenRegion {30None = 0,31AllocOnce = 1 << 0,32LibDispatch = 1 << 1,33Foundation = 1 << 2,34All = AllocOnce | LibDispatch | Foundation35};3637inline SeenRegion operator|(SeenRegion left, SeenRegion right) {38return static_cast<SeenRegion>(static_cast<int>(left) |39static_cast<int>(right));40}4142inline SeenRegion &operator|=(SeenRegion &left, const SeenRegion &right) {43left = left | right;44return left;45}4647struct RegionScanState {48SeenRegion seen_regions = SeenRegion::None;49bool in_libdispatch = false;50};5152typedef struct {53int disable_counter;54ThreadContextLsanBase *current_thread;55AllocatorCache cache;56} thread_local_data_t;5758static pthread_key_t key;59static pthread_once_t key_once = PTHREAD_ONCE_INIT;6061// The main thread destructor requires the current thread,62// so we can't destroy it until it's been used and reset.63void restore_tid_data(void *ptr) {64thread_local_data_t *data = (thread_local_data_t *)ptr;65if (data->current_thread)66pthread_setspecific(key, data);67}6869static void make_tls_key() {70CHECK_EQ(pthread_key_create(&key, restore_tid_data), 0);71}7273static thread_local_data_t *get_tls_val(bool alloc) {74pthread_once(&key_once, make_tls_key);7576thread_local_data_t *ptr = (thread_local_data_t *)pthread_getspecific(key);77if (ptr == NULL && alloc) {78ptr = (thread_local_data_t *)InternalAlloc(sizeof(*ptr));79ptr->disable_counter = 0;80ptr->current_thread = nullptr;81ptr->cache = AllocatorCache();82pthread_setspecific(key, ptr);83}8485return ptr;86}8788bool DisabledInThisThread() {89thread_local_data_t *data = get_tls_val(false);90return data ? data->disable_counter > 0 : false;91}9293void DisableInThisThread() { ++get_tls_val(true)->disable_counter; }9495void EnableInThisThread() {96int *disable_counter = &get_tls_val(true)->disable_counter;97if (*disable_counter == 0) {98DisableCounterUnderflow();99}100--*disable_counter;101}102103ThreadContextLsanBase *GetCurrentThread() {104thread_local_data_t *data = get_tls_val(false);105return data ? data->current_thread : nullptr;106}107108void SetCurrentThread(ThreadContextLsanBase *tctx) {109get_tls_val(true)->current_thread = tctx;110}111112AllocatorCache *GetAllocatorCache() { return &get_tls_val(true)->cache; }113114LoadedModule *GetLinker() { return nullptr; }115116// Required on Linux for initialization of TLS behavior, but should not be117// required on Darwin.118void InitializePlatformSpecificModules() {}119120// Sections which can't contain contain global pointers. This list errs on the121// side of caution to avoid false positives, at the expense of performance.122//123// Other potentially safe sections include:124// __all_image_info, __crash_info, __const, __got, __interpose, __objc_msg_break125//126// Sections which definitely cannot be included here are:127// __objc_data, __objc_const, __data, __bss, __common, __thread_data,128// __thread_bss, __thread_vars, __objc_opt_rw, __objc_opt_ptrs129static const char *kSkippedSecNames[] = {130"__cfstring", "__la_symbol_ptr", "__mod_init_func",131"__mod_term_func", "__nl_symbol_ptr", "__objc_classlist",132"__objc_classrefs", "__objc_imageinfo", "__objc_nlclslist",133"__objc_protolist", "__objc_selrefs", "__objc_superrefs"};134135// Scans global variables for heap pointers.136void ProcessGlobalRegions(Frontier *frontier) {137for (auto name : kSkippedSecNames)138CHECK(internal_strnlen(name, kMaxSegName + 1) <= kMaxSegName);139140MemoryMappingLayout memory_mapping(false);141InternalMmapVector<LoadedModule> modules;142modules.reserve(128);143memory_mapping.DumpListOfModules(&modules);144for (uptr i = 0; i < modules.size(); ++i) {145// Even when global scanning is disabled, we still need to scan146// system libraries for stashed pointers147if (!flags()->use_globals && modules[i].instrumented()) continue;148149for (const __sanitizer::LoadedModule::AddressRange &range :150modules[i].ranges()) {151// Sections storing global variables are writable and non-executable152if (range.executable || !range.writable) continue;153154for (auto name : kSkippedSecNames) {155if (!internal_strcmp(range.name, name)) continue;156}157158ScanGlobalRange(range.beg, range.end, frontier);159}160}161}162163void ProcessPlatformSpecificAllocations(Frontier *frontier) {164vm_address_t address = 0;165kern_return_t err = KERN_SUCCESS;166167InternalMmapVector<Region> mapped_regions;168bool use_root_regions = flags()->use_root_regions && HasRootRegions();169170RegionScanState scan_state;171while (err == KERN_SUCCESS) {172vm_size_t size = 0;173unsigned depth = 1;174struct vm_region_submap_info_64 info;175mach_msg_type_number_t count = VM_REGION_SUBMAP_INFO_COUNT_64;176err = vm_region_recurse_64(mach_task_self(), &address, &size, &depth,177(vm_region_info_t)&info, &count);178179uptr end_address = address + size;180if (info.user_tag == VM_MEMORY_OS_ALLOC_ONCE) {181// libxpc stashes some pointers in the Kernel Alloc Once page,182// make sure not to report those as leaks.183scan_state.seen_regions |= SeenRegion::AllocOnce;184ScanRangeForPointers(address, end_address, frontier, "GLOBAL",185kReachable);186} else if (info.user_tag == VM_MEMORY_FOUNDATION) {187// Objective-C block trampolines use the Foundation region.188scan_state.seen_regions |= SeenRegion::Foundation;189ScanRangeForPointers(address, end_address, frontier, "GLOBAL",190kReachable);191} else if (info.user_tag == VM_MEMORY_LIBDISPATCH) {192// Dispatch continuations use the libdispatch region. Empirically, there193// can be more than one region with this tag, so we'll optimistically194// assume that they're continguous. Otherwise, we would need to scan every195// region to ensure we find them all.196scan_state.in_libdispatch = true;197ScanRangeForPointers(address, end_address, frontier, "GLOBAL",198kReachable);199} else if (scan_state.in_libdispatch) {200scan_state.seen_regions |= SeenRegion::LibDispatch;201scan_state.in_libdispatch = false;202}203204// Recursing over the full memory map is very slow, break out205// early if we don't need the full iteration.206if (scan_state.seen_regions == SeenRegion::All && !use_root_regions) {207break;208}209210// This additional root region scan is required on Darwin in order to211// detect root regions contained within mmap'd memory regions, because212// the Darwin implementation of sanitizer_procmaps traverses images213// as loaded by dyld, and not the complete set of all memory regions.214//215// TODO(fjricci) - remove this once sanitizer_procmaps_mac has the same216// behavior as sanitizer_procmaps_linux and traverses all memory regions217if (use_root_regions && (info.protection & kProtectionRead))218mapped_regions.push_back({address, end_address});219220address = end_address;221}222ScanRootRegions(frontier, mapped_regions);223}224225// On darwin, we can intercept _exit gracefully, and return a failing exit code226// if required at that point. Calling Die() here is undefined behavior and227// causes rare race conditions.228void HandleLeaks() {}229230void LockStuffAndStopTheWorld(StopTheWorldCallback callback,231CheckForLeaksParam *argument) {232ScopedStopTheWorldLock lock;233StopTheWorld(callback, argument);234}235236} // namespace __lsan237238#endif // CAN_SANITIZE_LEAKS && SANITIZER_APPLE239240241