1 //===-- sanitizer_symbolizer_libcdep.cc -----------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is shared between AddressSanitizer and ThreadSanitizer
11 // run-time libraries.
12 //===----------------------------------------------------------------------===//
14 #include "sanitizer_allocator_internal.h"
15 #include "sanitizer_internal_defs.h"
16 #include "sanitizer_symbolizer_internal.h"
18 namespace __sanitizer {
20 Symbolizer *Symbolizer::GetOrInit() {
21 SpinMutexLock l(&init_mu_);
24 symbolizer_ = PlatformInit();
29 // See sanitizer_symbolizer_markup.cc.
30 #if !SANITIZER_SYMBOLIZER_MARKUP
32 const char *ExtractToken(const char *str, const char *delims, char **result) {
33 uptr prefix_len = internal_strcspn(str, delims);
34 *result = (char*)InternalAlloc(prefix_len + 1);
35 internal_memcpy(*result, str, prefix_len);
36 (*result)[prefix_len] = '\0';
37 const char *prefix_end = str + prefix_len;
38 if (*prefix_end != '\0') prefix_end++;
42 const char *ExtractInt(const char *str, const char *delims, int *result) {
44 const char *ret = ExtractToken(str, delims, &buff);
46 *result = (int)internal_atoll(buff);
52 const char *ExtractUptr(const char *str, const char *delims, uptr *result) {
54 const char *ret = ExtractToken(str, delims, &buff);
56 *result = (uptr)internal_atoll(buff);
62 const char *ExtractTokenUpToDelimiter(const char *str, const char *delimiter,
64 const char *found_delimiter = internal_strstr(str, delimiter);
66 found_delimiter ? found_delimiter - str : internal_strlen(str);
67 *result = (char *)InternalAlloc(prefix_len + 1);
68 internal_memcpy(*result, str, prefix_len);
69 (*result)[prefix_len] = '\0';
70 const char *prefix_end = str + prefix_len;
71 if (*prefix_end != '\0') prefix_end += internal_strlen(delimiter);
75 SymbolizedStack *Symbolizer::SymbolizePC(uptr addr) {
76 BlockingMutexLock l(&mu_);
77 const char *module_name;
80 SymbolizedStack *res = SymbolizedStack::New(addr);
81 if (!FindModuleNameAndOffsetForAddress(addr, &module_name, &module_offset,
84 // Always fill data about module name and offset.
85 res->info.FillModuleInfo(module_name, module_offset, arch);
86 for (auto &tool : tools_) {
87 SymbolizerScope sym_scope(this);
88 if (tool.SymbolizePC(addr, res)) {
95 bool Symbolizer::SymbolizeData(uptr addr, DataInfo *info) {
96 BlockingMutexLock l(&mu_);
97 const char *module_name;
100 if (!FindModuleNameAndOffsetForAddress(addr, &module_name, &module_offset,
104 info->module = internal_strdup(module_name);
105 info->module_offset = module_offset;
106 info->module_arch = arch;
107 for (auto &tool : tools_) {
108 SymbolizerScope sym_scope(this);
109 if (tool.SymbolizeData(addr, info)) {
116 bool Symbolizer::GetModuleNameAndOffsetForPC(uptr pc, const char **module_name,
117 uptr *module_address) {
118 BlockingMutexLock l(&mu_);
119 const char *internal_module_name = nullptr;
121 if (!FindModuleNameAndOffsetForAddress(pc, &internal_module_name,
122 module_address, &arch))
126 *module_name = module_names_.GetOwnedCopy(internal_module_name);
130 void Symbolizer::Flush() {
131 BlockingMutexLock l(&mu_);
132 for (auto &tool : tools_) {
133 SymbolizerScope sym_scope(this);
138 const char *Symbolizer::Demangle(const char *name) {
139 BlockingMutexLock l(&mu_);
140 for (auto &tool : tools_) {
141 SymbolizerScope sym_scope(this);
142 if (const char *demangled = tool.Demangle(name))
145 return PlatformDemangle(name);
148 bool Symbolizer::FindModuleNameAndOffsetForAddress(uptr address,
149 const char **module_name,
151 ModuleArch *module_arch) {
152 const LoadedModule *module = FindModuleForAddress(address);
153 if (module == nullptr)
155 *module_name = module->full_name();
156 *module_offset = address - module->base_address();
157 *module_arch = module->arch();
161 void Symbolizer::RefreshModules() {
163 fallback_modules_.fallbackInit();
164 RAW_CHECK(modules_.size() > 0);
165 modules_fresh_ = true;
168 static const LoadedModule *SearchForModule(const ListOfModules &modules,
170 for (uptr i = 0; i < modules.size(); i++) {
171 if (modules[i].containsAddress(address)) {
178 const LoadedModule *Symbolizer::FindModuleForAddress(uptr address) {
179 bool modules_were_reloaded = false;
180 if (!modules_fresh_) {
182 modules_were_reloaded = true;
184 const LoadedModule *module = SearchForModule(modules_, address);
185 if (module) return module;
187 // dlopen/dlclose interceptors invalidate the module list, but when
188 // interception is disabled, we need to retry if the lookup fails in
189 // case the module list changed.
190 #if !SANITIZER_INTERCEPT_DLOPEN_DLCLOSE
191 if (!modules_were_reloaded) {
193 module = SearchForModule(modules_, address);
194 if (module) return module;
198 if (fallback_modules_.size()) {
199 module = SearchForModule(fallback_modules_, address);
204 // For now we assume the following protocol:
205 // For each request of the form
206 // <module_name> <module_offset>
207 // passed to STDIN, external symbolizer prints to STDOUT response:
209 // <file_name>:<line_number>:<column_number>
211 // <file_name>:<line_number>:<column_number>
214 class LLVMSymbolizerProcess : public SymbolizerProcess {
216 explicit LLVMSymbolizerProcess(const char *path) : SymbolizerProcess(path) {}
219 bool ReachedEndOfOutput(const char *buffer, uptr length) const override {
220 // Empty line marks the end of llvm-symbolizer output.
221 return length >= 2 && buffer[length - 1] == '\n' &&
222 buffer[length - 2] == '\n';
225 // When adding a new architecture, don't forget to also update
226 // script/asan_symbolize.py and sanitizer_common.h.
227 void GetArgV(const char *path_to_binary,
228 const char *(&argv)[kArgVMax]) const override {
229 #if defined(__x86_64h__)
230 const char* const kSymbolizerArch = "--default-arch=x86_64h";
231 #elif defined(__x86_64__)
232 const char* const kSymbolizerArch = "--default-arch=x86_64";
233 #elif defined(__i386__)
234 const char* const kSymbolizerArch = "--default-arch=i386";
235 #elif defined(__aarch64__)
236 const char* const kSymbolizerArch = "--default-arch=arm64";
237 #elif defined(__arm__)
238 const char* const kSymbolizerArch = "--default-arch=arm";
239 #elif defined(__powerpc64__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
240 const char* const kSymbolizerArch = "--default-arch=powerpc64";
241 #elif defined(__powerpc64__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
242 const char* const kSymbolizerArch = "--default-arch=powerpc64le";
243 #elif defined(__s390x__)
244 const char* const kSymbolizerArch = "--default-arch=s390x";
245 #elif defined(__s390__)
246 const char* const kSymbolizerArch = "--default-arch=s390";
248 const char* const kSymbolizerArch = "--default-arch=unknown";
251 const char *const inline_flag = common_flags()->symbolize_inline_frames
253 : "--inlining=false";
255 argv[i++] = path_to_binary;
256 argv[i++] = inline_flag;
257 argv[i++] = kSymbolizerArch;
262 LLVMSymbolizer::LLVMSymbolizer(const char *path, LowLevelAllocator *allocator)
263 : symbolizer_process_(new(*allocator) LLVMSymbolizerProcess(path)) {}
265 // Parse a <file>:<line>[:<column>] buffer. The file path may contain colons on
266 // Windows, so extract tokens from the right hand side first. The column info is
268 static const char *ParseFileLineInfo(AddressInfo *info, const char *str) {
269 char *file_line_info = 0;
270 str = ExtractToken(str, "\n", &file_line_info);
271 CHECK(file_line_info);
273 if (uptr size = internal_strlen(file_line_info)) {
274 char *back = file_line_info + size - 1;
275 for (int i = 0; i < 2; ++i) {
276 while (back > file_line_info && IsDigit(*back)) --back;
277 if (*back != ':' || !IsDigit(back[1])) break;
278 info->column = info->line;
279 info->line = internal_atoll(back + 1);
280 // Truncate the string at the colon to keep only filename.
284 ExtractToken(file_line_info, "", &info->file);
287 InternalFree(file_line_info);
291 // Parses one or more two-line strings in the following format:
293 // <file_name>:<line_number>[:<column_number>]
294 // Used by LLVMSymbolizer, Addr2LinePool and InternalSymbolizer, since all of
295 // them use the same output format.
296 void ParseSymbolizePCOutput(const char *str, SymbolizedStack *res) {
297 bool top_frame = true;
298 SymbolizedStack *last = res;
300 char *function_name = 0;
301 str = ExtractToken(str, "\n", &function_name);
302 CHECK(function_name);
303 if (function_name[0] == '\0') {
304 // There are no more frames.
305 InternalFree(function_name);
308 SymbolizedStack *cur;
313 cur = SymbolizedStack::New(res->info.address);
314 cur->info.FillModuleInfo(res->info.module, res->info.module_offset,
315 res->info.module_arch);
320 AddressInfo *info = &cur->info;
321 info->function = function_name;
322 str = ParseFileLineInfo(info, str);
324 // Functions and filenames can be "??", in which case we write 0
325 // to address info to mark that names are unknown.
326 if (0 == internal_strcmp(info->function, "??")) {
327 InternalFree(info->function);
330 if (0 == internal_strcmp(info->file, "??")) {
331 InternalFree(info->file);
337 // Parses a two-line string in the following format:
339 // <start_address> <size>
340 // Used by LLVMSymbolizer and InternalSymbolizer.
341 void ParseSymbolizeDataOutput(const char *str, DataInfo *info) {
342 str = ExtractToken(str, "\n", &info->name);
343 str = ExtractUptr(str, " ", &info->start);
344 str = ExtractUptr(str, "\n", &info->size);
347 bool LLVMSymbolizer::SymbolizePC(uptr addr, SymbolizedStack *stack) {
348 AddressInfo *info = &stack->info;
349 const char *buf = FormatAndSendCommand(
350 /*is_data*/ false, info->module, info->module_offset, info->module_arch);
352 ParseSymbolizePCOutput(buf, stack);
358 bool LLVMSymbolizer::SymbolizeData(uptr addr, DataInfo *info) {
359 const char *buf = FormatAndSendCommand(
360 /*is_data*/ true, info->module, info->module_offset, info->module_arch);
362 ParseSymbolizeDataOutput(buf, info);
363 info->start += (addr - info->module_offset); // Add the base address.
369 const char *LLVMSymbolizer::FormatAndSendCommand(bool is_data,
370 const char *module_name,
374 const char *is_data_str = is_data ? "DATA " : "";
375 if (arch == kModuleArchUnknown) {
376 if (internal_snprintf(buffer_, kBufferSize, "%s\"%s\" 0x%zx\n", is_data_str,
378 module_offset) >= static_cast<int>(kBufferSize)) {
379 Report("WARNING: Command buffer too small");
383 if (internal_snprintf(buffer_, kBufferSize, "%s\"%s:%s\" 0x%zx\n",
384 is_data_str, module_name, ModuleArchToString(arch),
385 module_offset) >= static_cast<int>(kBufferSize)) {
386 Report("WARNING: Command buffer too small");
390 return symbolizer_process_->SendCommand(buffer_);
393 SymbolizerProcess::SymbolizerProcess(const char *path, bool use_forkpty)
395 input_fd_(kInvalidFd),
396 output_fd_(kInvalidFd),
398 failed_to_start_(false),
399 reported_invalid_path_(false),
400 use_forkpty_(use_forkpty) {
402 CHECK_NE(path_[0], '\0');
405 static bool IsSameModule(const char* path) {
406 if (const char* ProcessName = GetProcessName()) {
407 if (const char* SymbolizerName = StripModuleName(path)) {
408 return !internal_strcmp(ProcessName, SymbolizerName);
414 const char *SymbolizerProcess::SendCommand(const char *command) {
415 if (failed_to_start_)
417 if (IsSameModule(path_)) {
418 Report("WARNING: Symbolizer was blocked from starting itself!\n");
419 failed_to_start_ = true;
422 for (; times_restarted_ < kMaxTimesRestarted; times_restarted_++) {
423 // Start or restart symbolizer if we failed to send command to it.
424 if (const char *res = SendCommandImpl(command))
428 if (!failed_to_start_) {
429 Report("WARNING: Failed to use and restart external symbolizer!\n");
430 failed_to_start_ = true;
435 const char *SymbolizerProcess::SendCommandImpl(const char *command) {
436 if (input_fd_ == kInvalidFd || output_fd_ == kInvalidFd)
438 if (!WriteToSymbolizer(command, internal_strlen(command)))
440 if (!ReadFromSymbolizer(buffer_, kBufferSize))
445 bool SymbolizerProcess::Restart() {
446 if (input_fd_ != kInvalidFd)
447 CloseFile(input_fd_);
448 if (output_fd_ != kInvalidFd)
449 CloseFile(output_fd_);
450 return StartSymbolizerSubprocess();
453 bool SymbolizerProcess::ReadFromSymbolizer(char *buffer, uptr max_length) {
459 bool success = ReadFromFile(input_fd_, buffer + read_len,
460 max_length - read_len - 1, &just_read);
461 // We can't read 0 bytes, as we don't expect external symbolizer to close
463 if (!success || just_read == 0) {
464 Report("WARNING: Can't read from symbolizer at fd %d\n", input_fd_);
467 read_len += just_read;
468 if (ReachedEndOfOutput(buffer, read_len))
470 if (read_len + 1 == max_length) {
471 Report("WARNING: Symbolizer buffer too small\n");
476 buffer[read_len] = '\0';
480 bool SymbolizerProcess::WriteToSymbolizer(const char *buffer, uptr length) {
484 bool success = WriteToFile(output_fd_, buffer, length, &write_len);
485 if (!success || write_len != length) {
486 Report("WARNING: Can't write to symbolizer at fd %d\n", output_fd_);
492 #endif // !SANITIZER_SYMBOLIZER_MARKUP
494 } // namespace __sanitizer