-
Notifications
You must be signed in to change notification settings - Fork 4
/
llvm_symbolizer.h
281 lines (239 loc) · 7.08 KB
/
llvm_symbolizer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// This file contains modified parts of the LLVM compiler-rt source code
// (c) Michael Hept 2022. See LICENSE.txt
#pragma once
#include "llvm_stacktrace_defs.h"
#include <cstring>
#include <vector>
namespace llvm_stacktrace {
enum ModuleArch {
kModuleArchUnknown,
kModuleArchI386,
kModuleArchX86_64,
kModuleArchX86_64H,
kModuleArchARMV6,
kModuleArchARMV7,
kModuleArchARMV7S,
kModuleArchARMV7K,
kModuleArchARM64,
kModuleArchRISCV64,
kModuleArchHexagon
};
// When adding a new architecture, don't forget to also update
// script/asan_symbolize.py and sanitizer_symbolizer_libcdep.cpp.
inline const char *ModuleArchToString(ModuleArch arch) {
switch (arch) {
case kModuleArchUnknown:
return "";
case kModuleArchI386:
return "i386";
case kModuleArchX86_64:
return "x86_64";
case kModuleArchX86_64H:
return "x86_64h";
case kModuleArchARMV6:
return "armv6";
case kModuleArchARMV7:
return "armv7";
case kModuleArchARMV7S:
return "armv7s";
case kModuleArchARMV7K:
return "armv7k";
case kModuleArchARM64:
return "arm64";
case kModuleArchRISCV64:
return "riscv64";
case kModuleArchHexagon:
return "hexagon";
}
CHECK(0 && "Invalid module arch");
return "";
}
const uptr kModuleUUIDSize = 32;
const uptr kMaxSegName = 16;
// Represents a binary loaded into virtual memory (e.g. this can be an
// executable or a shared object).
class LoadedModule {
public:
LoadedModule()
: full_name_(nullptr),
base_address_(0),
max_executable_address_(0),
arch_(kModuleArchUnknown),
uuid_size_(0),
instrumented_(false) {
std::memset(uuid_, 0, kModuleUUIDSize);
ranges_.clear();
}
void set(const char *module_name, uptr base_address);
void set(const char *module_name, uptr base_address, ModuleArch arch,
u8 uuid[kModuleUUIDSize], bool instrumented);
void setUuid(const char *uuid, uptr size);
void clear();
void addAddressRange(uptr beg, uptr end, bool executable, bool writable,
const char *name = nullptr);
bool containsAddress(uptr address) const;
const char *full_name() const { return full_name_; }
uptr base_address() const { return base_address_; }
uptr max_executable_address() const { return max_executable_address_; }
ModuleArch arch() const { return arch_; }
const u8 *uuid() const { return uuid_; }
uptr uuid_size() const { return uuid_size_; }
bool instrumented() const { return instrumented_; }
struct AddressRange {
AddressRange *next;
uptr beg;
uptr end;
bool executable;
bool writable;
char name[kMaxSegName];
AddressRange(uptr beg, uptr end, bool executable, bool writable,
const char *name)
: next(nullptr),
beg(beg),
end(end),
executable(executable),
writable(writable) {
std::strncpy(this->name, (name ? name : ""), ARRAY_SIZE(this->name));
}
};
const std::vector<AddressRange> &ranges() const { return ranges_; }
private:
char *full_name_; // Owned.
uptr base_address_;
uptr max_executable_address_;
ModuleArch arch_;
uptr uuid_size_;
u8 uuid_[kModuleUUIDSize];
bool instrumented_;
std::vector<AddressRange> ranges_;
};
// List of LoadedModules. OS-dependent implementation is responsible for
// filling this information.
class ListOfModules {
public:
ListOfModules() : initialized(false) {}
~ListOfModules() { clear(); }
void init();
uptr size() const { return modules_.size(); }
const LoadedModule &operator[](uptr i) const {
CHECK_LT(i, modules_.size());
return modules_[i];
}
const std::vector<LoadedModule>& getList() const {
return modules_;
}
std::vector<LoadedModule>& getList() {
return modules_;
}
private:
void clear() {
for (auto &module : modules_) module.clear();
modules_.clear();
}
void clearOrInit() {
initialized ? clear() : modules_.reserve(kInitialCapacity);
initialized = true;
}
std::vector<LoadedModule> modules_;
// We rarely have more than 16K loaded modules.
static const uptr kInitialCapacity = 1 << 14;
bool initialized;
};
struct AddressInfo {
// Owns all the string members. Storage for them is
// (de)allocated using sanitizer internal allocator.
uptr address;
char *module;
uptr module_offset;
ModuleArch module_arch;
u8 uuid[kModuleUUIDSize];
uptr uuid_size;
static const uptr kUnknown = ~(uptr)0;
char *function;
uptr function_offset;
char *file;
int line;
int column;
AddressInfo();
// Deletes all strings and resets all fields.
void Clear();
void FillModuleInfo(const char *mod_name, uptr mod_offset, ModuleArch arch);
void FillModuleInfo(const LoadedModule &mod);
uptr module_base() const { return address - module_offset; }
};
// Linked list of symbolized frames (each frame is described by AddressInfo).
struct SymbolizedStack {
SymbolizedStack *next;
AddressInfo info;
static SymbolizedStack *New(uptr addr);
// Deletes current, and all subsequent frames in the linked list.
// The object cannot be accessed after the call to this function.
void ClearAll();
private:
SymbolizedStack();
};
// For now, DataInfo is used to describe global variable.
struct DataInfo {
// Owns all the string members. Storage for them is
// (de)allocated using sanitizer internal allocator.
char *module;
uptr module_offset;
ModuleArch module_arch;
char *file;
uptr line;
char *name;
uptr start;
uptr size;
DataInfo();
void Clear();
};
struct LocalInfo {
char *function_name = nullptr;
char *name = nullptr;
char *decl_file = nullptr;
unsigned decl_line = 0;
bool has_frame_offset = false;
bool has_size = false;
bool has_tag_offset = false;
sptr frame_offset;
uptr size;
uptr tag_offset;
void Clear();
};
struct FrameInfo {
char *module;
uptr module_offset;
ModuleArch module_arch;
std::vector<LocalInfo> locals;
void Clear();
};
class SymbolizerTool;
class Symbolizer final {
public:
explicit Symbolizer(std::vector<SymbolizerTool*> tools);
~Symbolizer();
/// Initialize and return platform-specific implementation of symbolizer
/// (if it wasn't already initialized).
static Symbolizer *GetOrInit();
static void LateInitialize();
// Returns a list of symbolized frames for a given address (containing
// all inlined functions, if necessary).
SymbolizedStack *SymbolizePC(uptr address);
bool SymbolizeData(uptr address, DataInfo *info);
bool SymbolizeFrame(uptr address, FrameInfo *info);
const LoadedModule *FindModuleForAddress(uptr address);
void InvalidateModuleList();
private:
/// Platform-specific function for creating a Symbolizer object.
static Symbolizer *PlatformInit();
bool FindModuleNameAndOffsetForAddress(uptr address, const char **module_name,
uptr *module_offset,
ModuleArch *module_arch);
ListOfModules modules_;
static Symbolizer *symbolizer_;
std::vector<SymbolizerTool*> tools_;
};
#ifdef SANITIZER_WINDOWS
void InitializeDbgHelpIfNeeded();
#endif
} // namespace __sanitizer