Path: blob/main/contrib/llvm-project/compiler-rt/lib/lsan/lsan_mac.cpp
35233 views
//===-- lsan_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, a memory leak checker.9//10// Mac-specific details.11//===----------------------------------------------------------------------===//1213#include "sanitizer_common/sanitizer_platform.h"14#if SANITIZER_APPLE1516#include "interception/interception.h"17#include "lsan.h"18#include "lsan_allocator.h"19#include "lsan_thread.h"2021#include <pthread.h>2223namespace __lsan {24// Support for the following functions from libdispatch on Mac OS:25// dispatch_async_f()26// dispatch_async()27// dispatch_sync_f()28// dispatch_sync()29// dispatch_after_f()30// dispatch_after()31// dispatch_group_async_f()32// dispatch_group_async()33// TODO(glider): libdispatch API contains other functions that we don't support34// yet.35//36// dispatch_sync() and dispatch_sync_f() are synchronous, although chances are37// they can cause jobs to run on a thread different from the current one.38// TODO(glider): if so, we need a test for this (otherwise we should remove39// them).40//41// The following functions use dispatch_barrier_async_f() (which isn't a library42// function but is exported) and are thus supported:43// dispatch_source_set_cancel_handler_f()44// dispatch_source_set_cancel_handler()45// dispatch_source_set_event_handler_f()46// dispatch_source_set_event_handler()47//48// The reference manual for Grand Central Dispatch is available at49// http://developer.apple.com/library/mac/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html50// The implementation details are at51// http://libdispatch.macosforge.org/trac/browser/trunk/src/queue.c5253typedef void *dispatch_group_t;54typedef void *dispatch_queue_t;55typedef void *dispatch_source_t;56typedef u64 dispatch_time_t;57typedef void (*dispatch_function_t)(void *block);58typedef void *(*worker_t)(void *block);5960// A wrapper for the ObjC blocks used to support libdispatch.61typedef struct {62void *block;63dispatch_function_t func;64u32 parent_tid;65} lsan_block_context_t;6667ALWAYS_INLINE68void lsan_register_worker_thread(int parent_tid) {69if (GetCurrentThreadId() == kInvalidTid) {70u32 tid = ThreadCreate(parent_tid, true);71ThreadStart(tid, GetTid());72}73}7475// For use by only those functions that allocated the context via76// alloc_lsan_context().77extern "C" void lsan_dispatch_call_block_and_release(void *block) {78lsan_block_context_t *context = (lsan_block_context_t *)block;79VReport(2,80"lsan_dispatch_call_block_and_release(): "81"context: %p, pthread_self: %p\n",82block, (void*)pthread_self());83lsan_register_worker_thread(context->parent_tid);84// Call the original dispatcher for the block.85context->func(context->block);86lsan_free(context);87}8889} // namespace __lsan9091using namespace __lsan;9293// Wrap |ctxt| and |func| into an lsan_block_context_t.94// The caller retains control of the allocated context.95extern "C" lsan_block_context_t *alloc_lsan_context(void *ctxt,96dispatch_function_t func) {97GET_STACK_TRACE_THREAD;98lsan_block_context_t *lsan_ctxt =99(lsan_block_context_t *)lsan_malloc(sizeof(lsan_block_context_t), stack);100lsan_ctxt->block = ctxt;101lsan_ctxt->func = func;102lsan_ctxt->parent_tid = GetCurrentThreadId();103return lsan_ctxt;104}105106// Define interceptor for dispatch_*_f function with the three most common107// parameters: dispatch_queue_t, context, dispatch_function_t.108#define INTERCEPT_DISPATCH_X_F_3(dispatch_x_f) \109INTERCEPTOR(void, dispatch_x_f, dispatch_queue_t dq, void *ctxt, \110dispatch_function_t func) { \111lsan_block_context_t *lsan_ctxt = alloc_lsan_context(ctxt, func); \112return REAL(dispatch_x_f)(dq, (void *)lsan_ctxt, \113lsan_dispatch_call_block_and_release); \114}115116INTERCEPT_DISPATCH_X_F_3(dispatch_async_f)117INTERCEPT_DISPATCH_X_F_3(dispatch_sync_f)118INTERCEPT_DISPATCH_X_F_3(dispatch_barrier_async_f)119120INTERCEPTOR(void, dispatch_after_f, dispatch_time_t when, dispatch_queue_t dq,121void *ctxt, dispatch_function_t func) {122lsan_block_context_t *lsan_ctxt = alloc_lsan_context(ctxt, func);123return REAL(dispatch_after_f)(when, dq, (void *)lsan_ctxt,124lsan_dispatch_call_block_and_release);125}126127INTERCEPTOR(void, dispatch_group_async_f, dispatch_group_t group,128dispatch_queue_t dq, void *ctxt, dispatch_function_t func) {129lsan_block_context_t *lsan_ctxt = alloc_lsan_context(ctxt, func);130REAL(dispatch_group_async_f)131(group, dq, (void *)lsan_ctxt, lsan_dispatch_call_block_and_release);132}133134#if !defined(MISSING_BLOCKS_SUPPORT)135extern "C" {136void dispatch_async(dispatch_queue_t dq, void (^work)(void));137void dispatch_group_async(dispatch_group_t dg, dispatch_queue_t dq,138void (^work)(void));139void dispatch_after(dispatch_time_t when, dispatch_queue_t queue,140void (^work)(void));141void dispatch_source_set_cancel_handler(dispatch_source_t ds,142void (^work)(void));143void dispatch_source_set_event_handler(dispatch_source_t ds,144void (^work)(void));145}146147# define GET_LSAN_BLOCK(work) \148void (^lsan_block)(void); \149int parent_tid = GetCurrentThreadId(); \150lsan_block = ^(void) { \151lsan_register_worker_thread(parent_tid); \152work(); \153}154155INTERCEPTOR(void, dispatch_async, dispatch_queue_t dq, void (^work)(void)) {156GET_LSAN_BLOCK(work);157REAL(dispatch_async)(dq, lsan_block);158}159160INTERCEPTOR(void, dispatch_group_async, dispatch_group_t dg,161dispatch_queue_t dq, void (^work)(void)) {162GET_LSAN_BLOCK(work);163REAL(dispatch_group_async)(dg, dq, lsan_block);164}165166INTERCEPTOR(void, dispatch_after, dispatch_time_t when, dispatch_queue_t queue,167void (^work)(void)) {168GET_LSAN_BLOCK(work);169REAL(dispatch_after)(when, queue, lsan_block);170}171172INTERCEPTOR(void, dispatch_source_set_cancel_handler, dispatch_source_t ds,173void (^work)(void)) {174if (!work) {175REAL(dispatch_source_set_cancel_handler)(ds, work);176return;177}178GET_LSAN_BLOCK(work);179REAL(dispatch_source_set_cancel_handler)(ds, lsan_block);180}181182INTERCEPTOR(void, dispatch_source_set_event_handler, dispatch_source_t ds,183void (^work)(void)) {184GET_LSAN_BLOCK(work);185REAL(dispatch_source_set_event_handler)(ds, lsan_block);186}187#endif188189#endif // SANITIZER_APPLE190191192