lsan_fuchsia.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. GetThreadRegistryLocked()->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. } // namespace __lsan
  65. // These are declared (in extern "C") by <zircon/sanitizer.h>.
  66. // The system runtime will call our definitions directly.
  67. // This is called before each thread creation is attempted. So, in
  68. // its first call, the calling thread is the initial and sole thread.
  69. void *__sanitizer_before_thread_create_hook(thrd_t thread, bool detached,
  70. const char *name, void *stack_base,
  71. size_t stack_size) {
  72. ENSURE_LSAN_INITED;
  73. EnsureMainThreadIDIsCorrect();
  74. OnCreatedArgs args;
  75. args.stack_begin = reinterpret_cast<uptr>(stack_base);
  76. args.stack_end = args.stack_begin + stack_size;
  77. u32 parent_tid = GetCurrentThread();
  78. u32 tid = ThreadCreate(parent_tid, detached, &args);
  79. return reinterpret_cast<void *>(static_cast<uptr>(tid));
  80. }
  81. // This is called after creating a new thread (in the creating thread),
  82. // with the pointer returned by __sanitizer_before_thread_create_hook (above).
  83. void __sanitizer_thread_create_hook(void *hook, thrd_t thread, int error) {
  84. u32 tid = static_cast<u32>(reinterpret_cast<uptr>(hook));
  85. // On success, there is nothing to do here.
  86. if (error != thrd_success) {
  87. // Clean up the thread registry for the thread creation that didn't happen.
  88. GetThreadRegistryLocked()->FinishThread(tid);
  89. }
  90. }
  91. // This is called in the newly-created thread before it runs anything else,
  92. // with the pointer returned by __sanitizer_before_thread_create_hook (above).
  93. void __sanitizer_thread_start_hook(void *hook, thrd_t self) {
  94. u32 tid = static_cast<u32>(reinterpret_cast<uptr>(hook));
  95. ThreadStart(tid);
  96. }
  97. // Each thread runs this just before it exits,
  98. // with the pointer returned by BeforeThreadCreateHook (above).
  99. // All per-thread destructors have already been called.
  100. void __sanitizer_thread_exit_hook(void *hook, thrd_t self) { ThreadFinish(); }
  101. #endif // SANITIZER_FUCHSIA