lsan_posix.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 "lsan.h"
  16. #include "lsan_allocator.h"
  17. #include "sanitizer_common/sanitizer_stacktrace.h"
  18. #include "sanitizer_common/sanitizer_tls_get_addr.h"
  19. namespace __lsan {
  20. ThreadContext::ThreadContext(int tid) : ThreadContextLsanBase(tid) {}
  21. struct OnStartedArgs {
  22. uptr stack_begin;
  23. uptr stack_end;
  24. uptr cache_begin;
  25. uptr cache_end;
  26. uptr tls_begin;
  27. uptr tls_end;
  28. DTLS *dtls;
  29. };
  30. void ThreadContext::OnStarted(void *arg) {
  31. auto args = reinterpret_cast<const OnStartedArgs *>(arg);
  32. stack_begin_ = args->stack_begin;
  33. stack_end_ = args->stack_end;
  34. tls_begin_ = args->tls_begin;
  35. tls_end_ = args->tls_end;
  36. cache_begin_ = args->cache_begin;
  37. cache_end_ = args->cache_end;
  38. dtls_ = args->dtls;
  39. }
  40. void ThreadStart(u32 tid, tid_t os_id, ThreadType thread_type) {
  41. OnStartedArgs args;
  42. uptr stack_size = 0;
  43. uptr tls_size = 0;
  44. GetThreadStackAndTls(tid == kMainTid, &args.stack_begin, &stack_size,
  45. &args.tls_begin, &tls_size);
  46. args.stack_end = args.stack_begin + stack_size;
  47. args.tls_end = args.tls_begin + tls_size;
  48. GetAllocatorCacheRange(&args.cache_begin, &args.cache_end);
  49. args.dtls = DTLS_Get();
  50. ThreadContextLsanBase::ThreadStart(tid, os_id, thread_type, &args);
  51. }
  52. bool GetThreadRangesLocked(tid_t os_id, uptr *stack_begin, uptr *stack_end,
  53. uptr *tls_begin, uptr *tls_end, uptr *cache_begin,
  54. uptr *cache_end, DTLS **dtls) {
  55. ThreadContext *context = static_cast<ThreadContext *>(
  56. GetThreadRegistryLocked()->FindThreadContextByOsIDLocked(os_id));
  57. if (!context)
  58. return false;
  59. *stack_begin = context->stack_begin();
  60. *stack_end = context->stack_end();
  61. *tls_begin = context->tls_begin();
  62. *tls_end = context->tls_end();
  63. *cache_begin = context->cache_begin();
  64. *cache_end = context->cache_end();
  65. *dtls = context->dtls();
  66. return true;
  67. }
  68. void InitializeMainThread() {
  69. u32 tid = ThreadCreate(kMainTid, true);
  70. CHECK_EQ(tid, kMainTid);
  71. ThreadStart(tid, GetTid());
  72. }
  73. static void OnStackUnwind(const SignalContext &sig, const void *,
  74. BufferedStackTrace *stack) {
  75. stack->Unwind(StackTrace::GetNextInstructionPc(sig.pc), sig.bp, sig.context,
  76. common_flags()->fast_unwind_on_fatal);
  77. }
  78. void LsanOnDeadlySignal(int signo, void *siginfo, void *context) {
  79. HandleDeadlySignal(siginfo, context, GetCurrentThread(), &OnStackUnwind,
  80. nullptr);
  81. }
  82. } // namespace __lsan
  83. #endif // SANITIZER_POSIX