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