1 //===-- sanitizer_symbolize.cpp ---------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // Implementation of weak hooks from sanitizer_symbolizer_posix_libcdep.cpp.
11 //===----------------------------------------------------------------------===//
18 #include "llvm/DebugInfo/Symbolize/DIPrinter.h"
19 #include "llvm/DebugInfo/Symbolize/Symbolize.h"
20 #include "llvm/Demangle/Demangle.h"
22 static llvm::symbolize::LLVMSymbolizer
*Symbolizer
= nullptr;
23 static bool Demangle
= true;
24 static bool InlineFrames
= true;
26 static llvm::symbolize::LLVMSymbolizer
*getDefaultSymbolizer() {
29 llvm::symbolize::LLVMSymbolizer::Options Opts
;
30 Opts
.Demangle
= Demangle
;
31 Opts
.UntagAddresses
= true;
32 Symbolizer
= new llvm::symbolize::LLVMSymbolizer(Opts
);
36 static llvm::symbolize::PrinterConfig
getDefaultPrinterConfig() {
37 llvm::symbolize::PrinterConfig Config
;
38 Config
.Pretty
= false;
39 Config
.Verbose
= false;
40 Config
.PrintFunctions
= true;
41 Config
.PrintAddress
= false;
42 Config
.SourceContextLines
= 0;
46 static llvm::symbolize::ErrorHandler
symbolize_error_handler(
47 llvm::raw_string_ostream
&OS
) {
49 [&](const llvm::ErrorInfoBase
&ErrorInfo
, llvm::StringRef ErrorBanner
) {
56 namespace __sanitizer
{
57 int internal_snprintf(char *buffer
, uintptr_t length
, const char *format
, ...);
58 } // namespace __sanitizer
64 bool __sanitizer_symbolize_code(const char *ModuleName
, uint64_t ModuleOffset
,
65 char *Buffer
, int MaxLength
) {
68 llvm::raw_string_ostream
OS(Result
);
69 llvm::symbolize::PrinterConfig Config
= getDefaultPrinterConfig();
70 llvm::symbolize::Request Request
{ModuleName
, ModuleOffset
};
71 auto Printer
= std::make_unique
<llvm::symbolize::LLVMPrinter
>(
72 OS
, symbolize_error_handler(OS
), Config
);
74 // TODO: it is neccessary to set proper SectionIndex here.
75 // object::SectionedAddress::UndefSection works for only absolute addresses.
77 auto ResOrErr
= getDefaultSymbolizer()->symbolizeInlinedCode(
79 {ModuleOffset
, llvm::object::SectionedAddress::UndefSection
});
82 Printer
->print(Request
, ResOrErr
.get());
84 auto ResOrErr
= getDefaultSymbolizer()->symbolizeCode(
86 {ModuleOffset
, llvm::object::SectionedAddress::UndefSection
});
89 Printer
->print(Request
, ResOrErr
.get());
92 return __sanitizer::internal_snprintf(Buffer
, MaxLength
, "%s",
93 Result
.c_str()) < MaxLength
;
96 bool __sanitizer_symbolize_data(const char *ModuleName
, uint64_t ModuleOffset
,
97 char *Buffer
, int MaxLength
) {
100 llvm::symbolize::PrinterConfig Config
= getDefaultPrinterConfig();
101 llvm::raw_string_ostream
OS(Result
);
102 llvm::symbolize::Request Request
{ModuleName
, ModuleOffset
};
103 auto Printer
= std::make_unique
<llvm::symbolize::LLVMPrinter
>(
104 OS
, symbolize_error_handler(OS
), Config
);
106 // TODO: it is neccessary to set proper SectionIndex here.
107 // object::SectionedAddress::UndefSection works for only absolute addresses.
108 auto ResOrErr
= getDefaultSymbolizer()->symbolizeData(
110 {ModuleOffset
, llvm::object::SectionedAddress::UndefSection
});
113 Printer
->print(Request
, ResOrErr
.get());
115 return __sanitizer::internal_snprintf(Buffer
, MaxLength
, "%s",
116 Result
.c_str()) < MaxLength
;
119 bool __sanitizer_symbolize_frame(const char *ModuleName
, uint64_t ModuleOffset
,
120 char *Buffer
, int MaxLength
) {
123 llvm::symbolize::PrinterConfig Config
= getDefaultPrinterConfig();
124 llvm::raw_string_ostream
OS(Result
);
125 llvm::symbolize::Request Request
{ModuleName
, ModuleOffset
};
126 auto Printer
= std::make_unique
<llvm::symbolize::LLVMPrinter
>(
127 OS
, symbolize_error_handler(OS
), Config
);
129 // TODO: it is neccessary to set proper SectionIndex here.
130 // object::SectionedAddress::UndefSection works for only absolute addresses.
131 auto ResOrErr
= getDefaultSymbolizer()->symbolizeFrame(
133 {ModuleOffset
, llvm::object::SectionedAddress::UndefSection
});
136 Printer
->print(Request
, ResOrErr
.get());
138 return __sanitizer::internal_snprintf(Buffer
, MaxLength
, "%s",
139 Result
.c_str()) < MaxLength
;
142 void __sanitizer_symbolize_flush() {
147 bool __sanitizer_symbolize_demangle(const char *Name
, char *Buffer
,
150 if (!llvm::nonMicrosoftDemangle(Name
, Result
))
152 return __sanitizer::internal_snprintf(Buffer
, MaxLength
, "%s",
153 Result
.c_str()) < MaxLength
;
156 bool __sanitizer_symbolize_set_demangle(bool Value
) {
157 // Must be called before LLVMSymbolizer created.
164 bool __sanitizer_symbolize_set_inline_frames(bool Value
) {
165 InlineFrames
= Value
;
169 // Override __cxa_atexit and ignore callbacks.
170 // This prevents crashes in a configuration when the symbolizer
171 // is built into sanitizer runtime and consequently into the test process.
172 // LLVM libraries have some global objects destroyed during exit,
173 // so if the test process triggers any bugs after that, the symbolizer crashes.
174 // An example stack trace of such crash:
177 // #2 std::__u::__throw_system_error
178 // #3 std::__u::recursive_mutex::lock
179 // #4 __sanitizer_llvm::ManagedStaticBase::RegisterManagedStatic
180 // #5 __sanitizer_llvm::errorToErrorCode
181 // #6 __sanitizer_llvm::getFileAux
182 // #7 __sanitizer_llvm::MemoryBuffer::getFileOrSTDIN
183 // #10 __sanitizer_llvm::symbolize::LLVMSymbolizer::getOrCreateModuleInfo
184 // #13 __sanitizer::Symbolizer::SymbolizeData
185 // #14 __tsan::SymbolizeData
186 // #16 __tsan::ReportRace
188 // #19 race() () at test/tsan/atexit4.cpp
189 // #20 cxa_at_exit_wrapper
190 // #21 __cxa_finalize
193 // For the standalone llvm-symbolizer this does not hurt,
194 // we just don't destroy few global objects on exit.
195 int __cxa_atexit(void (*f
)(void *a
), void *arg
, void *dso
) { return 0; }