lsan.cpp 3.1 KB

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