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