llvm-project
116 строк · 3.1 Кб
1//=-- lsan.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.
11//
12//===----------------------------------------------------------------------===//
13
14#include "lsan.h"
15
16#include "lsan_allocator.h"
17#include "lsan_common.h"
18#include "lsan_thread.h"
19#include "sanitizer_common/sanitizer_flag_parser.h"
20#include "sanitizer_common/sanitizer_flags.h"
21#include "sanitizer_common/sanitizer_interface_internal.h"
22
23bool lsan_inited;
24bool lsan_init_is_running;
25
26namespace __lsan {
27
28///// Interface to the common LSan module. /////
29bool WordIsPoisoned(uptr addr) {
30return false;
31}
32
33} // namespace __lsan
34
35void __sanitizer::BufferedStackTrace::UnwindImpl(
36uptr pc, uptr bp, void *context, bool request_fast, u32 max_depth) {
37using namespace __lsan;
38uptr stack_top = 0, stack_bottom = 0;
39if (ThreadContextLsanBase *t = GetCurrentThread()) {
40stack_top = t->stack_end();
41stack_bottom = t->stack_begin();
42}
43if (SANITIZER_MIPS && !IsValidFrame(bp, stack_top, stack_bottom))
44return;
45bool fast = StackTrace::WillUseFastUnwind(request_fast);
46Unwind(max_depth, pc, bp, context, stack_top, stack_bottom, fast);
47}
48
49using namespace __lsan;
50
51static void InitializeFlags() {
52// Set all the default values.
53SetCommonFlagsDefaults();
54{
55CommonFlags cf;
56cf.CopyFrom(*common_flags());
57cf.external_symbolizer_path = GetEnv("LSAN_SYMBOLIZER_PATH");
58cf.malloc_context_size = 30;
59cf.intercept_tls_get_addr = true;
60cf.detect_leaks = true;
61cf.exitcode = 23;
62OverrideCommonFlags(cf);
63}
64
65Flags *f = flags();
66f->SetDefaults();
67
68FlagParser parser;
69RegisterLsanFlags(&parser, f);
70RegisterCommonFlags(&parser);
71
72// Override from user-specified string.
73const char *lsan_default_options = __lsan_default_options();
74parser.ParseString(lsan_default_options);
75parser.ParseStringFromEnv("LSAN_OPTIONS");
76
77InitializeCommonFlags();
78
79if (Verbosity()) ReportUnrecognizedFlags();
80
81if (common_flags()->help) parser.PrintFlagDescriptions();
82
83__sanitizer_set_report_path(common_flags()->log_path);
84}
85
86extern "C" void __lsan_init() {
87CHECK(!lsan_init_is_running);
88if (lsan_inited)
89return;
90lsan_init_is_running = true;
91SanitizerToolName = "LeakSanitizer";
92CacheBinaryName();
93AvoidCVE_2016_2143();
94InitializeFlags();
95InitCommonLsan();
96InitializeAllocator();
97ReplaceSystemMalloc();
98InitTlsSize();
99InitializeInterceptors();
100InitializeThreads();
101InstallDeadlySignalHandlers(LsanOnDeadlySignal);
102InitializeMainThread();
103InstallAtExitCheckLeaks();
104InstallAtForkHandler();
105
106InitializeCoverage(common_flags()->coverage, common_flags()->coverage_dir);
107
108lsan_inited = true;
109lsan_init_is_running = false;
110}
111
112extern "C" SANITIZER_INTERFACE_ATTRIBUTE
113void __sanitizer_print_stack_trace() {
114GET_STACK_TRACE_FATAL;
115stack.Print();
116}
117