Path: blob/main/contrib/llvm-project/compiler-rt/lib/lsan/lsan.cpp
35233 views
//=-- lsan.cpp ------------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file is a part of LeakSanitizer.9// Standalone LSan RTL.10//11//===----------------------------------------------------------------------===//1213#include "lsan.h"1415#include "lsan_allocator.h"16#include "lsan_common.h"17#include "lsan_thread.h"18#include "sanitizer_common/sanitizer_flag_parser.h"19#include "sanitizer_common/sanitizer_flags.h"20#include "sanitizer_common/sanitizer_interface_internal.h"2122bool lsan_inited;23bool lsan_init_is_running;2425namespace __lsan {2627///// Interface to the common LSan module. /////28bool WordIsPoisoned(uptr addr) {29return false;30}3132} // namespace __lsan3334void __sanitizer::BufferedStackTrace::UnwindImpl(35uptr pc, uptr bp, void *context, bool request_fast, u32 max_depth) {36using namespace __lsan;37uptr stack_top = 0, stack_bottom = 0;38if (ThreadContextLsanBase *t = GetCurrentThread()) {39stack_top = t->stack_end();40stack_bottom = t->stack_begin();41}42if (SANITIZER_MIPS && !IsValidFrame(bp, stack_top, stack_bottom))43return;44bool fast = StackTrace::WillUseFastUnwind(request_fast);45Unwind(max_depth, pc, bp, context, stack_top, stack_bottom, fast);46}4748using namespace __lsan;4950static void InitializeFlags() {51// Set all the default values.52SetCommonFlagsDefaults();53{54CommonFlags cf;55cf.CopyFrom(*common_flags());56cf.external_symbolizer_path = GetEnv("LSAN_SYMBOLIZER_PATH");57cf.malloc_context_size = 30;58cf.intercept_tls_get_addr = true;59cf.detect_leaks = true;60cf.exitcode = 23;61OverrideCommonFlags(cf);62}6364Flags *f = flags();65f->SetDefaults();6667FlagParser parser;68RegisterLsanFlags(&parser, f);69RegisterCommonFlags(&parser);7071// Override from user-specified string.72const char *lsan_default_options = __lsan_default_options();73parser.ParseString(lsan_default_options);74parser.ParseStringFromEnv("LSAN_OPTIONS");7576InitializeCommonFlags();7778if (Verbosity()) ReportUnrecognizedFlags();7980if (common_flags()->help) parser.PrintFlagDescriptions();8182__sanitizer_set_report_path(common_flags()->log_path);83}8485extern "C" void __lsan_init() {86CHECK(!lsan_init_is_running);87if (lsan_inited)88return;89lsan_init_is_running = true;90SanitizerToolName = "LeakSanitizer";91CacheBinaryName();92AvoidCVE_2016_2143();93InitializeFlags();94InitCommonLsan();95InitializeAllocator();96ReplaceSystemMalloc();97InitTlsSize();98InitializeInterceptors();99InitializeThreads();100InstallDeadlySignalHandlers(LsanOnDeadlySignal);101InitializeMainThread();102InstallAtExitCheckLeaks();103InstallAtForkHandler();104105InitializeCoverage(common_flags()->coverage, common_flags()->coverage_dir);106107lsan_inited = true;108lsan_init_is_running = false;109}110111extern "C" SANITIZER_INTERFACE_ATTRIBUTE112void __sanitizer_print_stack_trace() {113GET_STACK_TRACE_FATAL;114stack.Print();115}116117118