lsan_posix.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //=-- lsan_posix.cpp -----------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===---------------------------------------------------------------------===//
  8. //
  9. // This file is a part of LeakSanitizer.
  10. // Standalone LSan RTL code common to POSIX-like systems.
  11. //
  12. //===---------------------------------------------------------------------===//
  13. #include "sanitizer_common/sanitizer_platform.h"
  14. #if SANITIZER_POSIX
  15. # include <pthread.h>
  16. # include "lsan.h"
  17. # include "lsan_allocator.h"
  18. # include "lsan_thread.h"
  19. # include "sanitizer_common/sanitizer_stacktrace.h"
  20. # include "sanitizer_common/sanitizer_tls_get_addr.h"
  21. namespace __lsan {
  22. ThreadContext::ThreadContext(int tid) : ThreadContextLsanBase(tid) {}
  23. struct OnStartedArgs {
  24. uptr stack_begin;
  25. uptr stack_end;
  26. uptr cache_begin;
  27. uptr cache_end;
  28. uptr tls_begin;
  29. uptr tls_end;
  30. DTLS *dtls;
  31. };
  32. void ThreadContext::OnStarted(void *arg) {
  33. ThreadContextLsanBase::OnStarted(arg);
  34. auto args = reinterpret_cast<const OnStartedArgs *>(arg);
  35. stack_begin_ = args->stack_begin;
  36. stack_end_ = args->stack_end;
  37. tls_begin_ = args->tls_begin;
  38. tls_end_ = args->tls_end;
  39. cache_begin_ = args->cache_begin;
  40. cache_end_ = args->cache_end;
  41. dtls_ = args->dtls;
  42. }
  43. void ThreadStart(u32 tid, tid_t os_id, ThreadType thread_type) {
  44. OnStartedArgs args;
  45. uptr stack_size = 0;
  46. uptr tls_size = 0;
  47. GetThreadStackAndTls(tid == kMainTid, &args.stack_begin, &stack_size,
  48. &args.tls_begin, &tls_size);
  49. args.stack_end = args.stack_begin + stack_size;
  50. args.tls_end = args.tls_begin + tls_size;
  51. GetAllocatorCacheRange(&args.cache_begin, &args.cache_end);
  52. args.dtls = DTLS_Get();
  53. ThreadContextLsanBase::ThreadStart(tid, os_id, thread_type, &args);
  54. }
  55. bool GetThreadRangesLocked(tid_t os_id, uptr *stack_begin, uptr *stack_end,
  56. uptr *tls_begin, uptr *tls_end, uptr *cache_begin,
  57. uptr *cache_end, DTLS **dtls) {
  58. ThreadContext *context = static_cast<ThreadContext *>(
  59. GetLsanThreadRegistryLocked()->FindThreadContextByOsIDLocked(os_id));
  60. if (!context)
  61. return false;
  62. *stack_begin = context->stack_begin();
  63. *stack_end = context->stack_end();
  64. *tls_begin = context->tls_begin();
  65. *tls_end = context->tls_end();
  66. *cache_begin = context->cache_begin();
  67. *cache_end = context->cache_end();
  68. *dtls = context->dtls();
  69. return true;
  70. }
  71. void InitializeMainThread() {
  72. u32 tid = ThreadCreate(kMainTid, true);
  73. CHECK_EQ(tid, kMainTid);
  74. ThreadStart(tid, GetTid());
  75. }
  76. static void OnStackUnwind(const SignalContext &sig, const void *,
  77. BufferedStackTrace *stack) {
  78. stack->Unwind(StackTrace::GetNextInstructionPc(sig.pc), sig.bp, sig.context,
  79. common_flags()->fast_unwind_on_fatal);
  80. }
  81. void LsanOnDeadlySignal(int signo, void *siginfo, void *context) {
  82. HandleDeadlySignal(siginfo, context, GetCurrentThreadId(), &OnStackUnwind,
  83. nullptr);
  84. }
  85. void InstallAtExitCheckLeaks() {
  86. if (common_flags()->detect_leaks && common_flags()->leak_check_at_exit)
  87. Atexit(DoLeakCheck);
  88. }
  89. static void BeforeFork() {
  90. LockGlobal();
  91. LockThreads();
  92. LockAllocator();
  93. StackDepotLockBeforeFork();
  94. }
  95. static void AfterFork(bool fork_child) {
  96. StackDepotUnlockAfterFork(fork_child);
  97. UnlockAllocator();
  98. UnlockThreads();
  99. UnlockGlobal();
  100. }
  101. void InstallAtForkHandler() {
  102. # if SANITIZER_SOLARIS || SANITIZER_NETBSD || SANITIZER_APPLE
  103. return; // FIXME: Implement FutexWait.
  104. # endif
  105. pthread_atfork(
  106. &BeforeFork, []() { AfterFork(/* fork_child= */ false); },
  107. []() { AfterFork(/* fork_child= */ true); });
  108. }
  109. } // namespace __lsan
  110. #endif // SANITIZER_POSIX