lsan_fuchsia.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //=-- lsan_fuchsia.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 specific to Fuchsia.
  11. //
  12. //===---------------------------------------------------------------------===//
  13. #include "sanitizer_common/sanitizer_platform.h"
  14. #if SANITIZER_FUCHSIA
  15. #error #include <zircon/sanitizer.h>
  16. #include "lsan.h"
  17. #include "lsan_allocator.h"
  18. using namespace __lsan;
  19. namespace __lsan {
  20. void LsanOnDeadlySignal(int signo, void *siginfo, void *context) {}
  21. ThreadContext::ThreadContext(int tid) : ThreadContextLsanBase(tid) {}
  22. struct OnCreatedArgs {
  23. uptr stack_begin, stack_end;
  24. };
  25. // On Fuchsia, the stack bounds of a new thread are available before
  26. // the thread itself has started running.
  27. void ThreadContext::OnCreated(void *arg) {
  28. // Stack bounds passed through from __sanitizer_before_thread_create_hook
  29. // or InitializeMainThread.
  30. auto args = reinterpret_cast<const OnCreatedArgs *>(arg);
  31. stack_begin_ = args->stack_begin;
  32. stack_end_ = args->stack_end;
  33. }
  34. struct OnStartedArgs {
  35. uptr cache_begin, cache_end;
  36. };
  37. void ThreadContext::OnStarted(void *arg) {
  38. auto args = reinterpret_cast<const OnStartedArgs *>(arg);
  39. cache_begin_ = args->cache_begin;
  40. cache_end_ = args->cache_end;
  41. }
  42. void ThreadStart(u32 tid) {
  43. OnStartedArgs args;
  44. GetAllocatorCacheRange(&args.cache_begin, &args.cache_end);
  45. CHECK_EQ(args.cache_end - args.cache_begin, sizeof(AllocatorCache));
  46. ThreadContextLsanBase::ThreadStart(tid, GetTid(), ThreadType::Regular, &args);
  47. }
  48. void InitializeMainThread() {
  49. OnCreatedArgs args;
  50. __sanitizer::GetThreadStackTopAndBottom(true, &args.stack_end,
  51. &args.stack_begin);
  52. u32 tid = ThreadCreate(kMainTid, true, &args);
  53. CHECK_EQ(tid, 0);
  54. ThreadStart(tid);
  55. }
  56. void GetAllThreadAllocatorCachesLocked(InternalMmapVector<uptr> *caches) {
  57. GetLsanThreadRegistryLocked()->RunCallbackForEachThreadLocked(
  58. [](ThreadContextBase *tctx, void *arg) {
  59. auto ctx = static_cast<ThreadContext *>(tctx);
  60. static_cast<decltype(caches)>(arg)->push_back(ctx->cache_begin());
  61. },
  62. caches);
  63. }
  64. // On Fuchsia, leak detection is done by a special hook after atexit hooks.
  65. // So this doesn't install any atexit hook like on other platforms.
  66. void InstallAtExitCheckLeaks() {}
  67. // ASan defines this to check its `halt_on_error` flag.
  68. bool UseExitcodeOnLeak() { return true; }
  69. } // namespace __lsan
  70. // These are declared (in extern "C") by <zircon/sanitizer.h>.
  71. // The system runtime will call our definitions directly.
  72. // This is called before each thread creation is attempted. So, in
  73. // its first call, the calling thread is the initial and sole thread.
  74. void *__sanitizer_before_thread_create_hook(thrd_t thread, bool detached,
  75. const char *name, void *stack_base,
  76. size_t stack_size) {
  77. ENSURE_LSAN_INITED;
  78. EnsureMainThreadIDIsCorrect();
  79. OnCreatedArgs args;
  80. args.stack_begin = reinterpret_cast<uptr>(stack_base);
  81. args.stack_end = args.stack_begin + stack_size;
  82. u32 parent_tid = GetCurrentThread();
  83. u32 tid = ThreadCreate(parent_tid, detached, &args);
  84. return reinterpret_cast<void *>(static_cast<uptr>(tid));
  85. }
  86. // This is called after creating a new thread (in the creating thread),
  87. // with the pointer returned by __sanitizer_before_thread_create_hook (above).
  88. void __sanitizer_thread_create_hook(void *hook, thrd_t thread, int error) {
  89. u32 tid = static_cast<u32>(reinterpret_cast<uptr>(hook));
  90. // On success, there is nothing to do here.
  91. if (error != thrd_success) {
  92. // Clean up the thread registry for the thread creation that didn't happen.
  93. GetLsanThreadRegistryLocked()->FinishThread(tid);
  94. }
  95. }
  96. // This is called in the newly-created thread before it runs anything else,
  97. // with the pointer returned by __sanitizer_before_thread_create_hook (above).
  98. void __sanitizer_thread_start_hook(void *hook, thrd_t self) {
  99. u32 tid = static_cast<u32>(reinterpret_cast<uptr>(hook));
  100. ThreadStart(tid);
  101. }
  102. // Each thread runs this just before it exits,
  103. // with the pointer returned by BeforeThreadCreateHook (above).
  104. // All per-thread destructors have already been called.
  105. void __sanitizer_thread_exit_hook(void *hook, thrd_t self) { ThreadFinish(); }
  106. #endif // SANITIZER_FUCHSIA