Sergey Matveev | e96c62a | 2013-05-20 11:04:43 +0000 | [diff] [blame] | 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" |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 18 | #include "sanitizer_common/sanitizer_flag_parser.h" |
Sergey Matveev | e96c62a | 2013-05-20 11:04:43 +0000 | [diff] [blame] | 19 | #include "sanitizer_common/sanitizer_stacktrace.h" |
| 20 | #include "lsan_allocator.h" |
| 21 | #include "lsan_common.h" |
| 22 | #include "lsan_thread.h" |
| 23 | |
Sergey Matveev | 74c8879 | 2013-11-25 17:39:36 +0000 | [diff] [blame] | 24 | bool lsan_inited; |
| 25 | bool lsan_init_is_running; |
| 26 | |
Sergey Matveev | e96c62a | 2013-05-20 11:04:43 +0000 | [diff] [blame] | 27 | namespace __lsan { |
| 28 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 29 | ///// Interface to the common LSan module. ///// |
| 30 | bool WordIsPoisoned(uptr addr) { |
| 31 | return false; |
Sergey Matveev | e96c62a | 2013-05-20 11:04:43 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Sergey Matveev | 74c8879 | 2013-11-25 17:39:36 +0000 | [diff] [blame] | 34 | } // namespace __lsan |
| 35 | |
| 36 | using namespace __lsan; // NOLINT |
| 37 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 38 | static void InitializeFlags() { |
| 39 | // Set all the default values. |
| 40 | SetCommonFlagsDefaults(); |
| 41 | { |
| 42 | CommonFlags cf; |
| 43 | cf.CopyFrom(*common_flags()); |
| 44 | cf.external_symbolizer_path = GetEnv("LSAN_SYMBOLIZER_PATH"); |
| 45 | cf.malloc_context_size = 30; |
| 46 | cf.detect_leaks = true; |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 47 | cf.exitcode = 23; |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 48 | OverrideCommonFlags(cf); |
| 49 | } |
| 50 | |
| 51 | Flags *f = flags(); |
| 52 | f->SetDefaults(); |
| 53 | |
| 54 | FlagParser parser; |
| 55 | RegisterLsanFlags(&parser, f); |
| 56 | RegisterCommonFlags(&parser); |
| 57 | |
| 58 | parser.ParseString(GetEnv("LSAN_OPTIONS")); |
| 59 | |
| 60 | SetVerbosity(common_flags()->verbosity); |
| 61 | |
| 62 | if (Verbosity()) ReportUnrecognizedFlags(); |
| 63 | |
| 64 | if (common_flags()->help) parser.PrintFlagDescriptions(); |
| 65 | } |
| 66 | |
Sergey Matveev | 74c8879 | 2013-11-25 17:39:36 +0000 | [diff] [blame] | 67 | extern "C" void __lsan_init() { |
| 68 | CHECK(!lsan_init_is_running); |
| 69 | if (lsan_inited) |
Sergey Matveev | e96c62a | 2013-05-20 11:04:43 +0000 | [diff] [blame] | 70 | return; |
Sergey Matveev | 74c8879 | 2013-11-25 17:39:36 +0000 | [diff] [blame] | 71 | lsan_init_is_running = true; |
Sergey Matveev | e96c62a | 2013-05-20 11:04:43 +0000 | [diff] [blame] | 72 | SanitizerToolName = "LeakSanitizer"; |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 73 | CacheBinaryName(); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 74 | InitializeFlags(); |
| 75 | InitCommonLsan(); |
Sergey Matveev | e96c62a | 2013-05-20 11:04:43 +0000 | [diff] [blame] | 76 | InitializeAllocator(); |
Sergey Matveev | e96c62a | 2013-05-20 11:04:43 +0000 | [diff] [blame] | 77 | InitTlsSize(); |
| 78 | InitializeInterceptors(); |
| 79 | InitializeThreadRegistry(); |
| 80 | u32 tid = ThreadCreate(0, 0, true); |
| 81 | CHECK_EQ(tid, 0); |
| 82 | ThreadStart(tid, GetTid()); |
Sergey Matveev | 02d7f32 | 2013-06-18 14:44:45 +0000 | [diff] [blame] | 83 | SetCurrentThread(tid); |
Sergey Matveev | e96c62a | 2013-05-20 11:04:43 +0000 | [diff] [blame] | 84 | |
Sergey Matveev | 41f1540 | 2013-08-01 14:57:07 +0000 | [diff] [blame] | 85 | if (common_flags()->detect_leaks && common_flags()->leak_check_at_exit) |
| 86 | Atexit(DoLeakCheck); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 87 | |
| 88 | InitializeCoverage(common_flags()->coverage, common_flags()->coverage_dir); |
| 89 | |
Sergey Matveev | 74c8879 | 2013-11-25 17:39:36 +0000 | [diff] [blame] | 90 | lsan_inited = true; |
| 91 | lsan_init_is_running = false; |
Sergey Matveev | e96c62a | 2013-05-20 11:04:43 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 94 | extern "C" SANITIZER_INTERFACE_ATTRIBUTE |
| 95 | void __sanitizer_print_stack_trace() { |
| 96 | GET_STACK_TRACE_FATAL; |
| 97 | stack.Print(); |
| 98 | } |