xref: /aosp_15_r20/external/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer.cc (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot //===-- sanitizer_symbolizer.cc -------------------------------------------===//
2*7c3d14c8STreehugger Robot //
3*7c3d14c8STreehugger Robot //                     The LLVM Compiler Infrastructure
4*7c3d14c8STreehugger Robot //
5*7c3d14c8STreehugger Robot // This file is distributed under the University of Illinois Open Source
6*7c3d14c8STreehugger Robot // License. See LICENSE.TXT for details.
7*7c3d14c8STreehugger Robot //
8*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
9*7c3d14c8STreehugger Robot //
10*7c3d14c8STreehugger Robot // This file is shared between AddressSanitizer and ThreadSanitizer
11*7c3d14c8STreehugger Robot // run-time libraries.
12*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
13*7c3d14c8STreehugger Robot 
14*7c3d14c8STreehugger Robot #include "sanitizer_allocator_internal.h"
15*7c3d14c8STreehugger Robot #include "sanitizer_platform.h"
16*7c3d14c8STreehugger Robot #include "sanitizer_internal_defs.h"
17*7c3d14c8STreehugger Robot #include "sanitizer_libc.h"
18*7c3d14c8STreehugger Robot #include "sanitizer_placement_new.h"
19*7c3d14c8STreehugger Robot #include "sanitizer_symbolizer_internal.h"
20*7c3d14c8STreehugger Robot 
21*7c3d14c8STreehugger Robot namespace __sanitizer {
22*7c3d14c8STreehugger Robot 
AddressInfo()23*7c3d14c8STreehugger Robot AddressInfo::AddressInfo() {
24*7c3d14c8STreehugger Robot   internal_memset(this, 0, sizeof(AddressInfo));
25*7c3d14c8STreehugger Robot   function_offset = kUnknown;
26*7c3d14c8STreehugger Robot }
27*7c3d14c8STreehugger Robot 
Clear()28*7c3d14c8STreehugger Robot void AddressInfo::Clear() {
29*7c3d14c8STreehugger Robot   InternalFree(module);
30*7c3d14c8STreehugger Robot   InternalFree(function);
31*7c3d14c8STreehugger Robot   InternalFree(file);
32*7c3d14c8STreehugger Robot   internal_memset(this, 0, sizeof(AddressInfo));
33*7c3d14c8STreehugger Robot   function_offset = kUnknown;
34*7c3d14c8STreehugger Robot }
35*7c3d14c8STreehugger Robot 
FillModuleInfo(const char * mod_name,uptr mod_offset)36*7c3d14c8STreehugger Robot void AddressInfo::FillModuleInfo(const char *mod_name, uptr mod_offset) {
37*7c3d14c8STreehugger Robot   module = internal_strdup(mod_name);
38*7c3d14c8STreehugger Robot   module_offset = mod_offset;
39*7c3d14c8STreehugger Robot }
40*7c3d14c8STreehugger Robot 
SymbolizedStack()41*7c3d14c8STreehugger Robot SymbolizedStack::SymbolizedStack() : next(nullptr), info() {}
42*7c3d14c8STreehugger Robot 
New(uptr addr)43*7c3d14c8STreehugger Robot SymbolizedStack *SymbolizedStack::New(uptr addr) {
44*7c3d14c8STreehugger Robot   void *mem = InternalAlloc(sizeof(SymbolizedStack));
45*7c3d14c8STreehugger Robot   SymbolizedStack *res = new(mem) SymbolizedStack();
46*7c3d14c8STreehugger Robot   res->info.address = addr;
47*7c3d14c8STreehugger Robot   return res;
48*7c3d14c8STreehugger Robot }
49*7c3d14c8STreehugger Robot 
ClearAll()50*7c3d14c8STreehugger Robot void SymbolizedStack::ClearAll() {
51*7c3d14c8STreehugger Robot   info.Clear();
52*7c3d14c8STreehugger Robot   if (next)
53*7c3d14c8STreehugger Robot     next->ClearAll();
54*7c3d14c8STreehugger Robot   InternalFree(this);
55*7c3d14c8STreehugger Robot }
56*7c3d14c8STreehugger Robot 
DataInfo()57*7c3d14c8STreehugger Robot DataInfo::DataInfo() {
58*7c3d14c8STreehugger Robot   internal_memset(this, 0, sizeof(DataInfo));
59*7c3d14c8STreehugger Robot }
60*7c3d14c8STreehugger Robot 
Clear()61*7c3d14c8STreehugger Robot void DataInfo::Clear() {
62*7c3d14c8STreehugger Robot   InternalFree(module);
63*7c3d14c8STreehugger Robot   InternalFree(file);
64*7c3d14c8STreehugger Robot   InternalFree(name);
65*7c3d14c8STreehugger Robot   internal_memset(this, 0, sizeof(DataInfo));
66*7c3d14c8STreehugger Robot }
67*7c3d14c8STreehugger Robot 
68*7c3d14c8STreehugger Robot Symbolizer *Symbolizer::symbolizer_;
69*7c3d14c8STreehugger Robot StaticSpinMutex Symbolizer::init_mu_;
70*7c3d14c8STreehugger Robot LowLevelAllocator Symbolizer::symbolizer_allocator_;
71*7c3d14c8STreehugger Robot 
AddHooks(Symbolizer::StartSymbolizationHook start_hook,Symbolizer::EndSymbolizationHook end_hook)72*7c3d14c8STreehugger Robot void Symbolizer::AddHooks(Symbolizer::StartSymbolizationHook start_hook,
73*7c3d14c8STreehugger Robot                           Symbolizer::EndSymbolizationHook end_hook) {
74*7c3d14c8STreehugger Robot   CHECK(start_hook_ == 0 && end_hook_ == 0);
75*7c3d14c8STreehugger Robot   start_hook_ = start_hook;
76*7c3d14c8STreehugger Robot   end_hook_ = end_hook;
77*7c3d14c8STreehugger Robot }
78*7c3d14c8STreehugger Robot 
GetOwnedCopy(const char * str)79*7c3d14c8STreehugger Robot const char *Symbolizer::ModuleNameOwner::GetOwnedCopy(const char *str) {
80*7c3d14c8STreehugger Robot   mu_->CheckLocked();
81*7c3d14c8STreehugger Robot 
82*7c3d14c8STreehugger Robot   // 'str' will be the same string multiple times in a row, optimize this case.
83*7c3d14c8STreehugger Robot   if (last_match_ && !internal_strcmp(last_match_, str))
84*7c3d14c8STreehugger Robot     return last_match_;
85*7c3d14c8STreehugger Robot 
86*7c3d14c8STreehugger Robot   // FIXME: this is linear search.
87*7c3d14c8STreehugger Robot   // We should optimize this further if this turns out to be a bottleneck later.
88*7c3d14c8STreehugger Robot   for (uptr i = 0; i < storage_.size(); ++i) {
89*7c3d14c8STreehugger Robot     if (!internal_strcmp(storage_[i], str)) {
90*7c3d14c8STreehugger Robot       last_match_ = storage_[i];
91*7c3d14c8STreehugger Robot       return last_match_;
92*7c3d14c8STreehugger Robot     }
93*7c3d14c8STreehugger Robot   }
94*7c3d14c8STreehugger Robot   last_match_ = internal_strdup(str);
95*7c3d14c8STreehugger Robot   storage_.push_back(last_match_);
96*7c3d14c8STreehugger Robot   return last_match_;
97*7c3d14c8STreehugger Robot }
98*7c3d14c8STreehugger Robot 
Symbolizer(IntrusiveList<SymbolizerTool> tools)99*7c3d14c8STreehugger Robot Symbolizer::Symbolizer(IntrusiveList<SymbolizerTool> tools)
100*7c3d14c8STreehugger Robot     : module_names_(&mu_), modules_(), modules_fresh_(false), tools_(tools),
101*7c3d14c8STreehugger Robot       start_hook_(0), end_hook_(0) {}
102*7c3d14c8STreehugger Robot 
SymbolizerScope(const Symbolizer * sym)103*7c3d14c8STreehugger Robot Symbolizer::SymbolizerScope::SymbolizerScope(const Symbolizer *sym)
104*7c3d14c8STreehugger Robot     : sym_(sym) {
105*7c3d14c8STreehugger Robot   if (sym_->start_hook_)
106*7c3d14c8STreehugger Robot     sym_->start_hook_();
107*7c3d14c8STreehugger Robot }
108*7c3d14c8STreehugger Robot 
~SymbolizerScope()109*7c3d14c8STreehugger Robot Symbolizer::SymbolizerScope::~SymbolizerScope() {
110*7c3d14c8STreehugger Robot   if (sym_->end_hook_)
111*7c3d14c8STreehugger Robot     sym_->end_hook_();
112*7c3d14c8STreehugger Robot }
113*7c3d14c8STreehugger Robot 
114*7c3d14c8STreehugger Robot }  // namespace __sanitizer
115