1 //=-- lsan.cpp ------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file is a part of LeakSanitizer.
10 // Standalone LSan RTL.
12 //===----------------------------------------------------------------------===//
16 #include "sanitizer_common/sanitizer_flags.h"
17 #include "sanitizer_common/sanitizer_flag_parser.h"
18 #include "lsan_allocator.h"
19 #include "lsan_common.h"
20 #include "lsan_thread.h"
23 bool lsan_init_is_running
;
27 ///// Interface to the common LSan module. /////
28 bool WordIsPoisoned(uptr addr
) {
34 void __sanitizer::BufferedStackTrace::UnwindImpl(
35 uptr pc
, uptr bp
, void *context
, bool request_fast
, u32 max_depth
) {
36 using namespace __lsan
;
37 uptr stack_top
= 0, stack_bottom
= 0;
39 if (StackTrace::WillUseFastUnwind(request_fast
) &&
40 (t
= CurrentThreadContext())) {
41 stack_top
= t
->stack_end();
42 stack_bottom
= t
->stack_begin();
44 if (!SANITIZER_MIPS
|| IsValidFrame(bp
, stack_top
, stack_bottom
)) {
45 if (StackTrace::WillUseFastUnwind(request_fast
))
46 Unwind(max_depth
, pc
, bp
, nullptr, stack_top
, stack_bottom
, true);
48 Unwind(max_depth
, pc
, 0, context
, 0, 0, false);
52 using namespace __lsan
;
54 static void InitializeFlags() {
55 // Set all the default values.
56 SetCommonFlagsDefaults();
59 cf
.CopyFrom(*common_flags());
60 cf
.external_symbolizer_path
= GetEnv("LSAN_SYMBOLIZER_PATH");
61 cf
.malloc_context_size
= 30;
62 cf
.intercept_tls_get_addr
= true;
63 cf
.detect_leaks
= true;
65 OverrideCommonFlags(cf
);
72 RegisterLsanFlags(&parser
, f
);
73 RegisterCommonFlags(&parser
);
75 // Override from user-specified string.
76 const char *lsan_default_options
= MaybeCallLsanDefaultOptions();
77 parser
.ParseString(lsan_default_options
);
78 parser
.ParseStringFromEnv("LSAN_OPTIONS");
80 SetVerbosity(common_flags()->verbosity
);
82 if (Verbosity()) ReportUnrecognizedFlags();
84 if (common_flags()->help
) parser
.PrintFlagDescriptions();
86 __sanitizer_set_report_path(common_flags()->log_path
);
89 extern "C" void __lsan_init() {
90 CHECK(!lsan_init_is_running
);
93 lsan_init_is_running
= true;
94 SanitizerToolName
= "LeakSanitizer";
99 InitializeAllocator();
100 ReplaceSystemMalloc();
102 InitializeInterceptors();
103 InitializeThreadRegistry();
104 InstallDeadlySignalHandlers(LsanOnDeadlySignal
);
105 InitializeMainThread();
107 if (common_flags()->detect_leaks
&& common_flags()->leak_check_at_exit
)
110 InitializeCoverage(common_flags()->coverage
, common_flags()->coverage_dir
);
113 lsan_init_is_running
= false;
116 extern "C" SANITIZER_INTERFACE_ATTRIBUTE
117 void __sanitizer_print_stack_trace() {
118 GET_STACK_TRACE_FATAL
;