1 //===-- sanitizer_symbolizer_posix_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 // POSIX-specific implementation of symbolizer parts.
13 //===----------------------------------------------------------------------===//
15 #include "sanitizer_platform.h"
17 #include "sanitizer_allocator_internal.h"
18 #include "sanitizer_common.h"
19 #include "sanitizer_file.h"
20 #include "sanitizer_flags.h"
21 #include "sanitizer_internal_defs.h"
22 #include "sanitizer_linux.h"
23 #include "sanitizer_placement_new.h"
24 #include "sanitizer_posix.h"
25 #include "sanitizer_procmaps.h"
26 #include "sanitizer_symbolizer_internal.h"
27 #include "sanitizer_symbolizer_libbacktrace.h"
28 #include "sanitizer_symbolizer_mac.h"
30 #include <dlfcn.h> // for dlsym()
38 #include <util.h> // for forkpty()
39 #endif // SANITIZER_MAC
41 // C++ demangling function, as required by Itanium C++ ABI. This is weak,
42 // because we do not require a C++ ABI library to be linked to a program
43 // using sanitizers; if it's not present, we'll just use the mangled name.
44 namespace __cxxabiv1 {
45 extern "C" SANITIZER_WEAK_ATTRIBUTE
46 char *__cxa_demangle(const char *mangled, char *buffer,
47 size_t *length, int *status);
50 namespace __sanitizer {
52 // Attempts to demangle the name via __cxa_demangle from __cxxabiv1.
53 const char *DemangleCXXABI(const char *name) {
54 // FIXME: __cxa_demangle aggressively insists on allocating memory.
55 // There's not much we can do about that, short of providing our
56 // own demangler (libc++abi's implementation could be adapted so that
57 // it does not allocate). For now, we just call it anyway, and we leak
58 // the returned value.
59 if (&__cxxabiv1::__cxa_demangle)
60 if (const char *demangled_name =
61 __cxxabiv1::__cxa_demangle(name, 0, 0, 0))
62 return demangled_name;
67 // As of now, there are no headers for the Swift runtime. Once they are
68 // present, we will weakly link since we do not require Swift runtime to be
70 typedef char *(*swift_demangle_ft)(const char *mangledName,
71 size_t mangledNameLength, char *outputBuffer,
72 size_t *outputBufferSize, uint32_t flags);
73 static swift_demangle_ft swift_demangle_f;
75 // This must not happen lazily at symbolication time, because dlsym uses
76 // malloc and thread-local storage, which is not a good thing to do during
78 static void InitializeSwiftDemangler() {
79 swift_demangle_f = (swift_demangle_ft)dlsym(RTLD_DEFAULT, "swift_demangle");
80 (void)dlerror(); // Cleanup error message in case of failure
83 // Attempts to demangle a Swift name. The demangler will return nullptr if a
84 // non-Swift name is passed in.
85 const char *DemangleSwift(const char *name) {
86 if (!name) return nullptr;
88 // Check if we are dealing with a Swift mangled name first.
89 if (name[0] != '_' || name[1] != 'T') {
94 return swift_demangle_f(name, internal_strlen(name), 0, 0, 0);
99 const char *DemangleSwiftAndCXX(const char *name) {
100 if (!name) return nullptr;
101 if (const char *swift_demangled_name = DemangleSwift(name))
102 return swift_demangled_name;
103 return DemangleCXXABI(name);
106 static bool CreateTwoHighNumberedPipes(int *infd_, int *outfd_) {
109 // The client program may close its stdin and/or stdout and/or stderr
110 // thus allowing socketpair to reuse file descriptors 0, 1 or 2.
111 // In this case the communication between the forked processes may be
112 // broken if either the parent or the child tries to close or duplicate
113 // these descriptors. The loop below produces two pairs of file
114 // descriptors, each greater than 2 (stderr).
116 for (int i = 0; i < 5; i++) {
117 if (pipe(sock_pair[i]) == -1) {
118 for (int j = 0; j < i; j++) {
119 internal_close(sock_pair[j][0]);
120 internal_close(sock_pair[j][1]);
123 } else if (sock_pair[i][0] > 2 && sock_pair[i][1] > 2) {
127 outfd = sock_pair[i];
128 for (int j = 0; j < i; j++) {
129 if (sock_pair[j] == infd) continue;
130 internal_close(sock_pair[j][0]);
131 internal_close(sock_pair[j][1]);
141 outfd_[0] = outfd[0];
142 outfd_[1] = outfd[1];
146 bool SymbolizerProcess::StartSymbolizerSubprocess() {
147 if (!FileExists(path_)) {
148 if (!reported_invalid_path_) {
149 Report("WARNING: invalid path to external symbolizer!\n");
150 reported_invalid_path_ = true;
158 internal_memset(&infd, 0, sizeof(infd));
160 internal_memset(&outfd, 0, sizeof(outfd));
161 if (!CreateTwoHighNumberedPipes(infd, outfd)) {
162 Report("WARNING: Can't create a socket pair to start "
163 "external symbolizer (errno: %d)\n", errno);
169 fd_t fd = kInvalidFd;
171 // forkpty redirects stdout and stderr into a single stream, so we would
172 // receive error messages as standard replies. To avoid that, let's dup
173 // stderr and restore it in the child.
174 int saved_stderr = dup(STDERR_FILENO);
175 CHECK_GE(saved_stderr, 0);
177 // We only need one pipe, for stdin of the child.
181 // Use forkpty to disable buffering in the new terminal.
182 pid = internal_forkpty(&fd);
185 Report("WARNING: failed to fork external symbolizer (errno: %d)\n",
188 } else if (pid == 0) {
191 // infd[0] is the child's reading end.
194 // Set up stdin to read from the pipe.
195 CHECK_GE(dup2(infd[0], STDIN_FILENO), 0);
199 CHECK_GE(dup2(saved_stderr, STDERR_FILENO), 0);
202 const char *argv[kArgVMax];
203 GetArgV(path_, argv);
204 execv(path_, const_cast<char **>(&argv[0]));
208 // Input for the child, infd[1] is our writing end.
209 output_fd_ = infd[1];
212 // Continue execution in parent process.
217 // Disable echo in the new terminal, disable CR.
218 struct termios termflags;
219 tcgetattr(fd, &termflags);
220 termflags.c_oflag &= ~ONLCR;
221 termflags.c_lflag &= ~ECHO;
222 tcsetattr(fd, TCSANOW, &termflags);
223 #else // SANITIZER_MAC
225 #endif // SANITIZER_MAC
227 const char *argv[kArgVMax];
228 GetArgV(path_, argv);
229 pid = StartSubprocess(path_, argv, /* stdin */ outfd[0],
230 /* stdout */ infd[1]);
232 internal_close(infd[0]);
233 internal_close(outfd[1]);
238 output_fd_ = outfd[1];
243 // Check that symbolizer subprocess started successfully.
244 SleepForMillis(kSymbolizerStartupTimeMillis);
245 if (!IsProcessRunning(pid)) {
246 // Either waitpid failed, or child has already exited.
247 Report("WARNING: external symbolizer didn't start up correctly!\n");
254 class Addr2LineProcess : public SymbolizerProcess {
256 Addr2LineProcess(const char *path, const char *module_name)
257 : SymbolizerProcess(path), module_name_(internal_strdup(module_name)) {}
259 const char *module_name() const { return module_name_; }
262 void GetArgV(const char *path_to_binary,
263 const char *(&argv)[kArgVMax]) const override {
265 argv[i++] = path_to_binary;
267 argv[i++] = module_name_;
271 bool ReachedEndOfOutput(const char *buffer, uptr length) const override;
273 bool ReadFromSymbolizer(char *buffer, uptr max_length) override {
274 if (!SymbolizerProcess::ReadFromSymbolizer(buffer, max_length))
276 // The returned buffer is empty when output is valid, but exceeds
280 // We should cut out output_terminator_ at the end of given buffer,
281 // appended by addr2line to mark the end of its meaningful output.
282 // We cannot scan buffer from it's beginning, because it is legal for it
283 // to start with output_terminator_ in case given offset is invalid. So,
284 // scanning from second character.
285 char *garbage = internal_strstr(buffer + 1, output_terminator_);
286 // This should never be NULL since buffer must end up with
287 // output_terminator_.
294 const char *module_name_; // Owned, leaked.
295 static const char output_terminator_[];
298 const char Addr2LineProcess::output_terminator_[] = "??\n??:0\n";
300 bool Addr2LineProcess::ReachedEndOfOutput(const char *buffer,
302 const size_t kTerminatorLen = sizeof(output_terminator_) - 1;
303 // Skip, if we read just kTerminatorLen bytes, because Addr2Line output
304 // should consist at least of two pairs of lines:
305 // 1. First one, corresponding to given offset to be symbolized
306 // (may be equal to output_terminator_, if offset is not valid).
307 // 2. Second one for output_terminator_, itself to mark the end of output.
308 if (length <= kTerminatorLen) return false;
309 // Addr2Line output should end up with output_terminator_.
310 return !internal_memcmp(buffer + length - kTerminatorLen,
311 output_terminator_, kTerminatorLen);
314 class Addr2LinePool : public SymbolizerTool {
316 explicit Addr2LinePool(const char *addr2line_path,
317 LowLevelAllocator *allocator)
318 : addr2line_path_(addr2line_path), allocator_(allocator) {
319 addr2line_pool_.reserve(16);
322 bool SymbolizePC(uptr addr, SymbolizedStack *stack) override {
323 if (const char *buf =
324 SendCommand(stack->info.module, stack->info.module_offset)) {
325 ParseSymbolizePCOutput(buf, stack);
331 bool SymbolizeData(uptr addr, DataInfo *info) override {
336 const char *SendCommand(const char *module_name, uptr module_offset) {
337 Addr2LineProcess *addr2line = 0;
338 for (uptr i = 0; i < addr2line_pool_.size(); ++i) {
340 internal_strcmp(module_name, addr2line_pool_[i]->module_name())) {
341 addr2line = addr2line_pool_[i];
347 new(*allocator_) Addr2LineProcess(addr2line_path_, module_name);
348 addr2line_pool_.push_back(addr2line);
350 CHECK_EQ(0, internal_strcmp(module_name, addr2line->module_name()));
351 char buffer[kBufferSize];
352 internal_snprintf(buffer, kBufferSize, "0x%zx\n0x%zx\n",
353 module_offset, dummy_address_);
354 return addr2line->SendCommand(buffer);
357 static const uptr kBufferSize = 64;
358 const char *addr2line_path_;
359 LowLevelAllocator *allocator_;
360 InternalMmapVector<Addr2LineProcess*> addr2line_pool_;
361 static const uptr dummy_address_ =
362 FIRST_32_SECOND_64(UINT32_MAX, UINT64_MAX);
365 #if SANITIZER_SUPPORTS_WEAK_HOOKS
367 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
368 bool __sanitizer_symbolize_code(const char *ModuleName, u64 ModuleOffset,
369 char *Buffer, int MaxLength);
370 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
371 bool __sanitizer_symbolize_data(const char *ModuleName, u64 ModuleOffset,
372 char *Buffer, int MaxLength);
373 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
374 void __sanitizer_symbolize_flush();
375 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
376 int __sanitizer_symbolize_demangle(const char *Name, char *Buffer,
380 class InternalSymbolizer : public SymbolizerTool {
382 static InternalSymbolizer *get(LowLevelAllocator *alloc) {
383 if (__sanitizer_symbolize_code != 0 &&
384 __sanitizer_symbolize_data != 0) {
385 return new(*alloc) InternalSymbolizer();
390 bool SymbolizePC(uptr addr, SymbolizedStack *stack) override {
391 bool result = __sanitizer_symbolize_code(
392 stack->info.module, stack->info.module_offset, buffer_, kBufferSize);
393 if (result) ParseSymbolizePCOutput(buffer_, stack);
397 bool SymbolizeData(uptr addr, DataInfo *info) override {
398 bool result = __sanitizer_symbolize_data(info->module, info->module_offset,
399 buffer_, kBufferSize);
401 ParseSymbolizeDataOutput(buffer_, info);
402 info->start += (addr - info->module_offset); // Add the base address.
407 void Flush() override {
408 if (__sanitizer_symbolize_flush)
409 __sanitizer_symbolize_flush();
412 const char *Demangle(const char *name) override {
413 if (__sanitizer_symbolize_demangle) {
414 for (uptr res_length = 1024;
415 res_length <= InternalSizeClassMap::kMaxSize;) {
416 char *res_buff = static_cast<char*>(InternalAlloc(res_length));
418 __sanitizer_symbolize_demangle(name, res_buff, res_length);
419 if (req_length > res_length) {
420 res_length = req_length + 1;
421 InternalFree(res_buff);
431 InternalSymbolizer() { }
433 static const int kBufferSize = 16 * 1024;
434 char buffer_[kBufferSize];
436 #else // SANITIZER_SUPPORTS_WEAK_HOOKS
438 class InternalSymbolizer : public SymbolizerTool {
440 static InternalSymbolizer *get(LowLevelAllocator *alloc) { return 0; }
443 #endif // SANITIZER_SUPPORTS_WEAK_HOOKS
445 const char *Symbolizer::PlatformDemangle(const char *name) {
446 return DemangleSwiftAndCXX(name);
449 static SymbolizerTool *ChooseExternalSymbolizer(LowLevelAllocator *allocator) {
450 const char *path = common_flags()->external_symbolizer_path;
451 const char *binary_name = path ? StripModuleName(path) : "";
452 if (path && path[0] == '\0') {
453 VReport(2, "External symbolizer is explicitly disabled.\n");
455 } else if (!internal_strcmp(binary_name, "llvm-symbolizer")) {
456 VReport(2, "Using llvm-symbolizer at user-specified path: %s\n", path);
457 return new(*allocator) LLVMSymbolizer(path, allocator);
458 } else if (!internal_strcmp(binary_name, "atos")) {
460 VReport(2, "Using atos at user-specified path: %s\n", path);
461 return new(*allocator) AtosSymbolizer(path, allocator);
462 #else // SANITIZER_MAC
463 Report("ERROR: Using `atos` is only supported on Darwin.\n");
465 #endif // SANITIZER_MAC
466 } else if (!internal_strcmp(binary_name, "addr2line")) {
467 VReport(2, "Using addr2line at user-specified path: %s\n", path);
468 return new(*allocator) Addr2LinePool(path, allocator);
470 Report("ERROR: External symbolizer path is set to '%s' which isn't "
471 "a known symbolizer. Please set the path to the llvm-symbolizer "
472 "binary or other known tool.\n", path);
476 // Otherwise symbolizer program is unknown, let's search $PATH
477 CHECK(path == nullptr);
479 if (const char *found_path = FindPathToBinary("atos")) {
480 VReport(2, "Using atos found at: %s\n", found_path);
481 return new(*allocator) AtosSymbolizer(found_path, allocator);
483 #endif // SANITIZER_MAC
484 if (const char *found_path = FindPathToBinary("llvm-symbolizer")) {
485 VReport(2, "Using llvm-symbolizer found at: %s\n", found_path);
486 return new(*allocator) LLVMSymbolizer(found_path, allocator);
488 if (common_flags()->allow_addr2line) {
489 if (const char *found_path = FindPathToBinary("addr2line")) {
490 VReport(2, "Using addr2line found at: %s\n", found_path);
491 return new(*allocator) Addr2LinePool(found_path, allocator);
497 static void ChooseSymbolizerTools(IntrusiveList<SymbolizerTool> *list,
498 LowLevelAllocator *allocator) {
499 if (!common_flags()->symbolize) {
500 VReport(2, "Symbolizer is disabled.\n");
503 if (IsAllocatorOutOfMemory()) {
504 VReport(2, "Cannot use internal symbolizer: out of memory\n");
505 } else if (SymbolizerTool *tool = InternalSymbolizer::get(allocator)) {
506 VReport(2, "Using internal symbolizer.\n");
507 list->push_back(tool);
510 if (SymbolizerTool *tool = LibbacktraceSymbolizer::get(allocator)) {
511 VReport(2, "Using libbacktrace symbolizer.\n");
512 list->push_back(tool);
516 if (SymbolizerTool *tool = ChooseExternalSymbolizer(allocator)) {
517 list->push_back(tool);
521 VReport(2, "Using dladdr symbolizer.\n");
522 list->push_back(new(*allocator) DlAddrSymbolizer());
523 #endif // SANITIZER_MAC
526 Symbolizer *Symbolizer::PlatformInit() {
527 IntrusiveList<SymbolizerTool> list;
529 ChooseSymbolizerTools(&list, &symbolizer_allocator_);
530 return new(symbolizer_allocator_) Symbolizer(list);
533 void Symbolizer::LateInitialize() {
534 Symbolizer::GetOrInit();
535 InitializeSwiftDemangler();
538 } // namespace __sanitizer
540 #endif // SANITIZER_POSIX