[PR c++/839888] Baselink tsubst ICE
[official-gcc.git] / libsanitizer / lsan / lsan.cc
blob7540aeb327b46ee8789cf98c0d4f043f6554ff86
1 //=-- lsan.cc -------------------------------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is a part of LeakSanitizer.
9 // Standalone LSan RTL.
11 //===----------------------------------------------------------------------===//
13 #include "lsan.h"
15 #include "sanitizer_common/sanitizer_flags.h"
16 #include "sanitizer_common/sanitizer_flag_parser.h"
17 #include "sanitizer_common/sanitizer_stacktrace.h"
18 #include "lsan_allocator.h"
19 #include "lsan_common.h"
20 #include "lsan_thread.h"
22 bool lsan_inited;
23 bool lsan_init_is_running;
25 namespace __lsan {
27 ///// Interface to the common LSan module. /////
28 bool WordIsPoisoned(uptr addr) {
29 return false;
32 } // namespace __lsan
34 using namespace __lsan; // NOLINT
36 static void InitializeFlags() {
37 // Set all the default values.
38 SetCommonFlagsDefaults();
40 CommonFlags cf;
41 cf.CopyFrom(*common_flags());
42 cf.external_symbolizer_path = GetEnv("LSAN_SYMBOLIZER_PATH");
43 cf.malloc_context_size = 30;
44 cf.intercept_tls_get_addr = true;
45 cf.detect_leaks = true;
46 cf.exitcode = 23;
47 OverrideCommonFlags(cf);
50 Flags *f = flags();
51 f->SetDefaults();
53 FlagParser parser;
54 RegisterLsanFlags(&parser, f);
55 RegisterCommonFlags(&parser);
57 // Override from user-specified string.
58 const char *lsan_default_options = MaybeCallLsanDefaultOptions();
59 parser.ParseString(lsan_default_options);
60 parser.ParseString(GetEnv("LSAN_OPTIONS"));
62 SetVerbosity(common_flags()->verbosity);
64 if (Verbosity()) ReportUnrecognizedFlags();
66 if (common_flags()->help) parser.PrintFlagDescriptions();
69 static void OnStackUnwind(const SignalContext &sig, const void *,
70 BufferedStackTrace *stack) {
71 GetStackTraceWithPcBpAndContext(stack, kStackTraceMax, sig.pc, sig.bp,
72 sig.context,
73 common_flags()->fast_unwind_on_fatal);
76 void LsanOnDeadlySignal(int signo, void *siginfo, void *context) {
77 HandleDeadlySignal(siginfo, context, GetCurrentThread(), &OnStackUnwind,
78 nullptr);
81 extern "C" void __lsan_init() {
82 CHECK(!lsan_init_is_running);
83 if (lsan_inited)
84 return;
85 lsan_init_is_running = true;
86 SanitizerToolName = "LeakSanitizer";
87 CacheBinaryName();
88 AvoidCVE_2016_2143();
89 InitializeFlags();
90 InitCommonLsan();
91 InitializeAllocator();
92 ReplaceSystemMalloc();
93 InitTlsSize();
94 InitializeInterceptors();
95 InitializeThreadRegistry();
96 InstallDeadlySignalHandlers(LsanOnDeadlySignal);
97 u32 tid = ThreadCreate(0, 0, true);
98 CHECK_EQ(tid, 0);
99 ThreadStart(tid, GetTid());
100 SetCurrentThread(tid);
102 if (common_flags()->detect_leaks && common_flags()->leak_check_at_exit)
103 Atexit(DoLeakCheck);
105 InitializeCoverage(common_flags()->coverage, common_flags()->coverage_dir);
107 lsan_inited = true;
108 lsan_init_is_running = false;
111 extern "C" SANITIZER_INTERFACE_ATTRIBUTE
112 void __sanitizer_print_stack_trace() {
113 GET_STACK_TRACE_FATAL;
114 stack.Print();