lsan.cc revision 74c88796a4024922144660ed1ade519af5008fe2
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
23bool lsan_inited;
24bool lsan_init_is_running;
25
26namespace __lsan {
27
28static void InitializeCommonFlags() {
29  CommonFlags *cf = common_flags();
30  SetCommonFlagDefaults();
31  cf->external_symbolizer_path = GetEnv("LSAN_SYMBOLIZER_PATH");
32  cf->malloc_context_size = 30;
33  cf->detect_leaks = true;
34
35  ParseCommonFlagsFromString(GetEnv("LSAN_OPTIONS"));
36}
37
38}  // namespace __lsan
39
40using namespace __lsan;  // NOLINT
41
42extern "C" void __lsan_init() {
43  CHECK(!lsan_init_is_running);
44  if (lsan_inited)
45    return;
46  lsan_init_is_running = true;
47  SanitizerToolName = "LeakSanitizer";
48  InitializeCommonFlags();
49  InitializeAllocator();
50  InitTlsSize();
51  InitializeInterceptors();
52  InitializeThreadRegistry();
53  u32 tid = ThreadCreate(0, 0, true);
54  CHECK_EQ(tid, 0);
55  ThreadStart(tid, GetTid());
56  SetCurrentThread(tid);
57
58  // Start symbolizer process if necessary.
59  if (common_flags()->symbolize) {
60    Symbolizer::Init(common_flags()->external_symbolizer_path);
61  } else {
62    Symbolizer::Disable();
63  }
64
65  InitCommonLsan();
66  if (common_flags()->detect_leaks && common_flags()->leak_check_at_exit)
67    Atexit(DoLeakCheck);
68  lsan_inited = true;
69  lsan_init_is_running = false;
70}
71
72