Path: blob/main/contrib/llvm-project/compiler-rt/lib/lsan/lsan_posix.cpp
35233 views
//=-- lsan_posix.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// Standalone LSan RTL code common to POSIX-like systems.10//11//===---------------------------------------------------------------------===//1213#include "sanitizer_common/sanitizer_platform.h"1415#if SANITIZER_POSIX16# include <pthread.h>1718# include "lsan.h"19# include "lsan_allocator.h"20# include "lsan_thread.h"21# include "sanitizer_common/sanitizer_stacktrace.h"22# include "sanitizer_common/sanitizer_tls_get_addr.h"2324namespace __lsan {2526ThreadContext::ThreadContext(int tid) : ThreadContextLsanBase(tid) {}2728struct OnStartedArgs {29uptr stack_begin;30uptr stack_end;31uptr cache_begin;32uptr cache_end;33uptr tls_begin;34uptr tls_end;35DTLS *dtls;36};3738void ThreadContext::OnStarted(void *arg) {39ThreadContextLsanBase::OnStarted(arg);40auto args = reinterpret_cast<const OnStartedArgs *>(arg);41stack_begin_ = args->stack_begin;42stack_end_ = args->stack_end;43tls_begin_ = args->tls_begin;44tls_end_ = args->tls_end;45cache_begin_ = args->cache_begin;46cache_end_ = args->cache_end;47dtls_ = args->dtls;48}4950void ThreadStart(u32 tid, tid_t os_id, ThreadType thread_type) {51OnStartedArgs args;52uptr stack_size = 0;53uptr tls_size = 0;54GetThreadStackAndTls(tid == kMainTid, &args.stack_begin, &stack_size,55&args.tls_begin, &tls_size);56args.stack_end = args.stack_begin + stack_size;57args.tls_end = args.tls_begin + tls_size;58GetAllocatorCacheRange(&args.cache_begin, &args.cache_end);59args.dtls = DTLS_Get();60ThreadContextLsanBase::ThreadStart(tid, os_id, thread_type, &args);61}6263bool GetThreadRangesLocked(tid_t os_id, uptr *stack_begin, uptr *stack_end,64uptr *tls_begin, uptr *tls_end, uptr *cache_begin,65uptr *cache_end, DTLS **dtls) {66ThreadContext *context = static_cast<ThreadContext *>(67GetLsanThreadRegistryLocked()->FindThreadContextByOsIDLocked(os_id));68if (!context)69return false;70*stack_begin = context->stack_begin();71*stack_end = context->stack_end();72*tls_begin = context->tls_begin();73*tls_end = context->tls_end();74*cache_begin = context->cache_begin();75*cache_end = context->cache_end();76*dtls = context->dtls();77return true;78}7980void InitializeMainThread() {81u32 tid = ThreadCreate(kMainTid, true);82CHECK_EQ(tid, kMainTid);83ThreadStart(tid, GetTid());84}8586static void OnStackUnwind(const SignalContext &sig, const void *,87BufferedStackTrace *stack) {88stack->Unwind(StackTrace::GetNextInstructionPc(sig.pc), sig.bp, sig.context,89common_flags()->fast_unwind_on_fatal);90}9192void LsanOnDeadlySignal(int signo, void *siginfo, void *context) {93HandleDeadlySignal(siginfo, context, GetCurrentThreadId(), &OnStackUnwind,94nullptr);95}9697void InstallAtExitCheckLeaks() {98if (common_flags()->detect_leaks && common_flags()->leak_check_at_exit)99Atexit(DoLeakCheck);100}101102static void BeforeFork() {103LockGlobal();104LockThreads();105LockAllocator();106StackDepotLockBeforeFork();107}108109static void AfterFork(bool fork_child) {110StackDepotUnlockAfterFork(fork_child);111UnlockAllocator();112UnlockThreads();113UnlockGlobal();114}115116void InstallAtForkHandler() {117# if SANITIZER_SOLARIS || SANITIZER_NETBSD || SANITIZER_APPLE118return; // FIXME: Implement FutexWait.119# endif120pthread_atfork(121&BeforeFork, []() { AfterFork(/* fork_child= */ false); },122[]() { AfterFork(/* fork_child= */ true); });123}124125} // namespace __lsan126127#endif // SANITIZER_POSIX128129130