lsan.cpp 3.1 KB

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