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