#include "sanitizer_common/sanitizer_platform.h"
#include "sanitizer_common/sanitizer_libc.h"
#include "lsan_common.h"
#if CAN_SANITIZE_LEAKS && SANITIZER_APPLE
# include <mach/mach.h>
# include <mach/vm_statistics.h>
# include <pthread.h>
# include "lsan_allocator.h"
# include "sanitizer_common/sanitizer_allocator_internal.h"
namespace __lsan {
class ThreadContextLsanBase;
enum class SeenRegion {
None = 0,
AllocOnce = 1 << 0,
LibDispatch = 1 << 1,
Foundation = 1 << 2,
All = AllocOnce | LibDispatch | Foundation
};
inline SeenRegion operator|(SeenRegion left, SeenRegion right) {
return static_cast<SeenRegion>(static_cast<int>(left) |
static_cast<int>(right));
}
inline SeenRegion &operator|=(SeenRegion &left, const SeenRegion &right) {
left = left | right;
return left;
}
struct RegionScanState {
SeenRegion seen_regions = SeenRegion::None;
bool in_libdispatch = false;
};
typedef struct {
int disable_counter;
ThreadContextLsanBase *current_thread;
AllocatorCache cache;
} thread_local_data_t;
static pthread_key_t key;
static pthread_once_t key_once = PTHREAD_ONCE_INIT;
void restore_tid_data(void *ptr) {
thread_local_data_t *data = (thread_local_data_t *)ptr;
if (data->current_thread)
pthread_setspecific(key, data);
}
static void make_tls_key() {
CHECK_EQ(pthread_key_create(&key, restore_tid_data), 0);
}
static thread_local_data_t *get_tls_val(bool alloc) {
pthread_once(&key_once, make_tls_key);
thread_local_data_t *ptr = (thread_local_data_t *)pthread_getspecific(key);
if (ptr == NULL && alloc) {
ptr = (thread_local_data_t *)InternalAlloc(sizeof(*ptr));
ptr->disable_counter = 0;
ptr->current_thread = nullptr;
ptr->cache = AllocatorCache();
pthread_setspecific(key, ptr);
}
return ptr;
}
bool DisabledInThisThread() {
thread_local_data_t *data = get_tls_val(false);
return data ? data->disable_counter > 0 : false;
}
void DisableInThisThread() { ++get_tls_val(true)->disable_counter; }
void EnableInThisThread() {
int *disable_counter = &get_tls_val(true)->disable_counter;
if (*disable_counter == 0) {
DisableCounterUnderflow();
}
--*disable_counter;
}
ThreadContextLsanBase *GetCurrentThread() {
thread_local_data_t *data = get_tls_val(false);
return data ? data->current_thread : nullptr;
}
void SetCurrentThread(ThreadContextLsanBase *tctx) {
get_tls_val(true)->current_thread = tctx;
}
AllocatorCache *GetAllocatorCache() { return &get_tls_val(true)->cache; }
LoadedModule *GetLinker() { return nullptr; }
void InitializePlatformSpecificModules() {}
static const char *kSkippedSecNames[] = {
"__cfstring", "__la_symbol_ptr", "__mod_init_func",
"__mod_term_func", "__nl_symbol_ptr", "__objc_classlist",
"__objc_classrefs", "__objc_imageinfo", "__objc_nlclslist",
"__objc_protolist", "__objc_selrefs", "__objc_superrefs"};
void ProcessGlobalRegions(Frontier *frontier) {
for (auto name : kSkippedSecNames)
CHECK(internal_strnlen(name, kMaxSegName + 1) <= kMaxSegName);
MemoryMappingLayout memory_mapping(false);
InternalMmapVector<LoadedModule> modules;
modules.reserve(128);
memory_mapping.DumpListOfModules(&modules);
for (uptr i = 0; i < modules.size(); ++i) {
if (!flags()->use_globals && modules[i].instrumented()) continue;
for (const __sanitizer::LoadedModule::AddressRange &range :
modules[i].ranges()) {
if (range.executable || !range.writable) continue;
for (auto name : kSkippedSecNames) {
if (!internal_strcmp(range.name, name)) continue;
}
ScanGlobalRange(range.beg, range.end, frontier);
}
}
}
void ProcessPlatformSpecificAllocations(Frontier *frontier) {
vm_address_t address = 0;
kern_return_t err = KERN_SUCCESS;
InternalMmapVector<Region> mapped_regions;
bool use_root_regions = flags()->use_root_regions && HasRootRegions();
RegionScanState scan_state;
while (err == KERN_SUCCESS) {
vm_size_t size = 0;
unsigned depth = 1;
struct vm_region_submap_info_64 info;
mach_msg_type_number_t count = VM_REGION_SUBMAP_INFO_COUNT_64;
err = vm_region_recurse_64(mach_task_self(), &address, &size, &depth,
(vm_region_info_t)&info, &count);
uptr end_address = address + size;
if (info.user_tag == VM_MEMORY_OS_ALLOC_ONCE) {
scan_state.seen_regions |= SeenRegion::AllocOnce;
ScanRangeForPointers(address, end_address, frontier, "GLOBAL",
kReachable);
} else if (info.user_tag == VM_MEMORY_FOUNDATION) {
scan_state.seen_regions |= SeenRegion::Foundation;
ScanRangeForPointers(address, end_address, frontier, "GLOBAL",
kReachable);
} else if (info.user_tag == VM_MEMORY_LIBDISPATCH) {
scan_state.in_libdispatch = true;
ScanRangeForPointers(address, end_address, frontier, "GLOBAL",
kReachable);
} else if (scan_state.in_libdispatch) {
scan_state.seen_regions |= SeenRegion::LibDispatch;
scan_state.in_libdispatch = false;
}
if (scan_state.seen_regions == SeenRegion::All && !use_root_regions) {
break;
}
if (use_root_regions && (info.protection & kProtectionRead))
mapped_regions.push_back({address, end_address});
address = end_address;
}
ScanRootRegions(frontier, mapped_regions);
}
void HandleLeaks() {}
void LockStuffAndStopTheWorld(StopTheWorldCallback callback,
CheckForLeaksParam *argument) {
ScopedStopTheWorldLock lock;
StopTheWorld(callback, argument);
}
}
#endif