xref: /netbsd-src/sys/external/bsd/compiler_rt/dist/lib/lsan/lsan.cc (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 //=-- lsan.cc -------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is a part of LeakSanitizer.
11 // Standalone LSan RTL.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "lsan.h"
16 
17 #include "sanitizer_common/sanitizer_flags.h"
18 #include "sanitizer_common/sanitizer_stacktrace.h"
19 #include "lsan_allocator.h"
20 #include "lsan_common.h"
21 #include "lsan_thread.h"
22 
23 namespace __lsan {
24 
25 static void InitializeCommonFlags() {
26   CommonFlags *cf = common_flags();
27   cf->external_symbolizer_path = GetEnv("LSAN_SYMBOLIZER_PATH");
28   cf->symbolize = (cf->external_symbolizer_path &&
29       cf->external_symbolizer_path[0]);
30   cf->strip_path_prefix = "";
31   cf->fast_unwind_on_malloc = true;
32   cf->malloc_context_size = 30;
33 
34   ParseCommonFlagsFromString(GetEnv("LSAN_OPTIONS"));
35 }
36 
37 void Init() {
38   static bool inited;
39   if (inited)
40     return;
41   inited = true;
42   SanitizerToolName = "LeakSanitizer";
43   InitializeCommonFlags();
44   InitializeAllocator();
45   InitTlsSize();
46   InitializeInterceptors();
47   InitializeThreadRegistry();
48   u32 tid = ThreadCreate(0, 0, true);
49   CHECK_EQ(tid, 0);
50   ThreadStart(tid, GetTid());
51   SetCurrentThread(tid);
52 
53   // Start symbolizer process if necessary.
54   const char* external_symbolizer = common_flags()->external_symbolizer_path;
55   if (common_flags()->symbolize && external_symbolizer &&
56       external_symbolizer[0]) {
57     InitializeExternalSymbolizer(external_symbolizer);
58   }
59 
60   InitCommonLsan();
61   Atexit(DoLeakCheck);
62 }
63 
64 }  // namespace __lsan
65