]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_posix_libcdep.cc
Upgrade Unbound to 1.6.0. More to follow.
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / sanitizer_common / sanitizer_symbolizer_posix_libcdep.cc
1 //===-- sanitizer_symbolizer_posix_libcdep.cc -----------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is shared between AddressSanitizer and ThreadSanitizer
11 // run-time libraries.
12 // POSIX-specific implementation of symbolizer parts.
13 //===----------------------------------------------------------------------===//
14
15 #include "sanitizer_platform.h"
16 #if SANITIZER_POSIX
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"
29
30 #include <dlfcn.h>   // for dlsym()
31 #include <errno.h>
32 #include <stdint.h>
33 #include <stdlib.h>
34 #include <sys/wait.h>
35 #include <unistd.h>
36
37 #if SANITIZER_MAC
38 #include <util.h>  // for forkpty()
39 #endif  // SANITIZER_MAC
40
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);
48 }
49
50 namespace __sanitizer {
51
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;
63
64   return name;
65 }
66
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
69 // linked.
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;
74
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
77 // symbolication.
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
81 }
82
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;
87
88   // Check if we are dealing with a Swift mangled name first.
89   if (name[0] != '_' || name[1] != 'T') {
90     return nullptr;
91   }
92
93   if (swift_demangle_f)
94     return swift_demangle_f(name, internal_strlen(name), 0, 0, 0);
95
96   return nullptr;
97 }
98
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);
104 }
105
106 static bool CreateTwoHighNumberedPipes(int *infd_, int *outfd_) {
107   int *infd = NULL;
108   int *outfd = NULL;
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).
115   int sock_pair[5][2];
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]);
121       }
122       return false;
123     } else if (sock_pair[i][0] > 2 && sock_pair[i][1] > 2) {
124       if (infd == NULL) {
125         infd = sock_pair[i];
126       } else {
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]);
132         }
133         break;
134       }
135     }
136   }
137   CHECK(infd);
138   CHECK(outfd);
139   infd_[0] = infd[0];
140   infd_[1] = infd[1];
141   outfd_[0] = outfd[0];
142   outfd_[1] = outfd[1];
143   return true;
144 }
145
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;
151     }
152     return false;
153   }
154
155   int pid = -1;
156
157   int infd[2];
158   internal_memset(&infd, 0, sizeof(infd));
159   int outfd[2];
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);
164     return false;
165   }
166
167   if (use_forkpty_) {
168 #if SANITIZER_MAC
169     fd_t fd = kInvalidFd;
170
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);
176
177     // We only need one pipe, for stdin of the child.
178     close(outfd[0]);
179     close(outfd[1]);
180
181     // Use forkpty to disable buffering in the new terminal.
182     pid = internal_forkpty(&fd);
183     if (pid == -1) {
184       // forkpty() failed.
185       Report("WARNING: failed to fork external symbolizer (errno: %d)\n",
186              errno);
187       return false;
188     } else if (pid == 0) {
189       // Child subprocess.
190
191       // infd[0] is the child's reading end.
192       close(infd[1]);
193
194       // Set up stdin to read from the pipe.
195       CHECK_GE(dup2(infd[0], STDIN_FILENO), 0);
196       close(infd[0]);
197
198       // Restore stderr.
199       CHECK_GE(dup2(saved_stderr, STDERR_FILENO), 0);
200       close(saved_stderr);
201
202       const char *argv[kArgVMax];
203       GetArgV(path_, argv);
204       execv(path_, const_cast<char **>(&argv[0]));
205       internal__exit(1);
206     }
207
208     // Input for the child, infd[1] is our writing end.
209     output_fd_ = infd[1];
210     close(infd[0]);
211
212     // Continue execution in parent process.
213     input_fd_ = fd;
214
215     close(saved_stderr);
216
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
224     UNIMPLEMENTED();
225 #endif  // SANITIZER_MAC
226   } else {
227     const char *argv[kArgVMax];
228     GetArgV(path_, argv);
229     pid = StartSubprocess(path_, argv, /* stdin */ outfd[0],
230                           /* stdout */ infd[1]);
231     if (pid < 0) {
232       internal_close(infd[0]);
233       internal_close(outfd[1]);
234       return false;
235     }
236
237     input_fd_ = infd[0];
238     output_fd_ = outfd[1];
239   }
240
241   CHECK_GT(pid, 0);
242
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");
248     return false;
249   }
250
251   return true;
252 }
253
254 class Addr2LineProcess : public SymbolizerProcess {
255  public:
256   Addr2LineProcess(const char *path, const char *module_name)
257       : SymbolizerProcess(path), module_name_(internal_strdup(module_name)) {}
258
259   const char *module_name() const { return module_name_; }
260
261  private:
262   void GetArgV(const char *path_to_binary,
263                const char *(&argv)[kArgVMax]) const override {
264     int i = 0;
265     argv[i++] = path_to_binary;
266     argv[i++] = "-iCfe";
267     argv[i++] = module_name_;
268     argv[i++] = nullptr;
269   }
270
271   bool ReachedEndOfOutput(const char *buffer, uptr length) const override;
272
273   bool ReadFromSymbolizer(char *buffer, uptr max_length) override {
274     if (!SymbolizerProcess::ReadFromSymbolizer(buffer, max_length))
275       return false;
276     // The returned buffer is empty when output is valid, but exceeds
277     // max_length.
278     if (*buffer == '\0')
279       return true;
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_.
288     CHECK(garbage);
289     // Trim the buffer.
290     garbage[0] = '\0';
291     return true;
292   }
293
294   const char *module_name_;  // Owned, leaked.
295   static const char output_terminator_[];
296 };
297
298 const char Addr2LineProcess::output_terminator_[] = "??\n??:0\n";
299
300 bool Addr2LineProcess::ReachedEndOfOutput(const char *buffer,
301                                           uptr length) const {
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);
312 }
313
314 class Addr2LinePool : public SymbolizerTool {
315  public:
316   explicit Addr2LinePool(const char *addr2line_path,
317                          LowLevelAllocator *allocator)
318       : addr2line_path_(addr2line_path), allocator_(allocator),
319         addr2line_pool_(16) {}
320
321   bool SymbolizePC(uptr addr, SymbolizedStack *stack) override {
322     if (const char *buf =
323             SendCommand(stack->info.module, stack->info.module_offset)) {
324       ParseSymbolizePCOutput(buf, stack);
325       return true;
326     }
327     return false;
328   }
329
330   bool SymbolizeData(uptr addr, DataInfo *info) override {
331     return false;
332   }
333
334  private:
335   const char *SendCommand(const char *module_name, uptr module_offset) {
336     Addr2LineProcess *addr2line = 0;
337     for (uptr i = 0; i < addr2line_pool_.size(); ++i) {
338       if (0 ==
339           internal_strcmp(module_name, addr2line_pool_[i]->module_name())) {
340         addr2line = addr2line_pool_[i];
341         break;
342       }
343     }
344     if (!addr2line) {
345       addr2line =
346           new(*allocator_) Addr2LineProcess(addr2line_path_, module_name);
347       addr2line_pool_.push_back(addr2line);
348     }
349     CHECK_EQ(0, internal_strcmp(module_name, addr2line->module_name()));
350     char buffer[kBufferSize];
351     internal_snprintf(buffer, kBufferSize, "0x%zx\n0x%zx\n",
352                       module_offset, dummy_address_);
353     return addr2line->SendCommand(buffer);
354   }
355
356   static const uptr kBufferSize = 64;
357   const char *addr2line_path_;
358   LowLevelAllocator *allocator_;
359   InternalMmapVector<Addr2LineProcess*> addr2line_pool_;
360   static const uptr dummy_address_ =
361       FIRST_32_SECOND_64(UINT32_MAX, UINT64_MAX);
362 };
363
364 #if SANITIZER_SUPPORTS_WEAK_HOOKS
365 extern "C" {
366 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
367 bool __sanitizer_symbolize_code(const char *ModuleName, u64 ModuleOffset,
368                                 char *Buffer, int MaxLength);
369 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
370 bool __sanitizer_symbolize_data(const char *ModuleName, u64 ModuleOffset,
371                                 char *Buffer, int MaxLength);
372 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
373 void __sanitizer_symbolize_flush();
374 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
375 int __sanitizer_symbolize_demangle(const char *Name, char *Buffer,
376                                    int MaxLength);
377 }  // extern "C"
378
379 class InternalSymbolizer : public SymbolizerTool {
380  public:
381   static InternalSymbolizer *get(LowLevelAllocator *alloc) {
382     if (__sanitizer_symbolize_code != 0 &&
383         __sanitizer_symbolize_data != 0) {
384       return new(*alloc) InternalSymbolizer();
385     }
386     return 0;
387   }
388
389   bool SymbolizePC(uptr addr, SymbolizedStack *stack) override {
390     bool result = __sanitizer_symbolize_code(
391         stack->info.module, stack->info.module_offset, buffer_, kBufferSize);
392     if (result) ParseSymbolizePCOutput(buffer_, stack);
393     return result;
394   }
395
396   bool SymbolizeData(uptr addr, DataInfo *info) override {
397     bool result = __sanitizer_symbolize_data(info->module, info->module_offset,
398                                              buffer_, kBufferSize);
399     if (result) {
400       ParseSymbolizeDataOutput(buffer_, info);
401       info->start += (addr - info->module_offset);  // Add the base address.
402     }
403     return result;
404   }
405
406   void Flush() override {
407     if (__sanitizer_symbolize_flush)
408       __sanitizer_symbolize_flush();
409   }
410
411   const char *Demangle(const char *name) override {
412     if (__sanitizer_symbolize_demangle) {
413       for (uptr res_length = 1024;
414            res_length <= InternalSizeClassMap::kMaxSize;) {
415         char *res_buff = static_cast<char*>(InternalAlloc(res_length));
416         uptr req_length =
417             __sanitizer_symbolize_demangle(name, res_buff, res_length);
418         if (req_length > res_length) {
419           res_length = req_length + 1;
420           InternalFree(res_buff);
421           continue;
422         }
423         return res_buff;
424       }
425     }
426     return name;
427   }
428
429  private:
430   InternalSymbolizer() { }
431
432   static const int kBufferSize = 16 * 1024;
433   char buffer_[kBufferSize];
434 };
435 #else  // SANITIZER_SUPPORTS_WEAK_HOOKS
436
437 class InternalSymbolizer : public SymbolizerTool {
438  public:
439   static InternalSymbolizer *get(LowLevelAllocator *alloc) { return 0; }
440 };
441
442 #endif  // SANITIZER_SUPPORTS_WEAK_HOOKS
443
444 const char *Symbolizer::PlatformDemangle(const char *name) {
445   return DemangleSwiftAndCXX(name);
446 }
447
448 void Symbolizer::PlatformPrepareForSandboxing() {}
449
450 static SymbolizerTool *ChooseExternalSymbolizer(LowLevelAllocator *allocator) {
451   const char *path = common_flags()->external_symbolizer_path;
452   const char *binary_name = path ? StripModuleName(path) : "";
453   if (path && path[0] == '\0') {
454     VReport(2, "External symbolizer is explicitly disabled.\n");
455     return nullptr;
456   } else if (!internal_strcmp(binary_name, "llvm-symbolizer")) {
457     VReport(2, "Using llvm-symbolizer at user-specified path: %s\n", path);
458     return new(*allocator) LLVMSymbolizer(path, allocator);
459   } else if (!internal_strcmp(binary_name, "atos")) {
460 #if SANITIZER_MAC
461     VReport(2, "Using atos at user-specified path: %s\n", path);
462     return new(*allocator) AtosSymbolizer(path, allocator);
463 #else  // SANITIZER_MAC
464     Report("ERROR: Using `atos` is only supported on Darwin.\n");
465     Die();
466 #endif  // SANITIZER_MAC
467   } else if (!internal_strcmp(binary_name, "addr2line")) {
468     VReport(2, "Using addr2line at user-specified path: %s\n", path);
469     return new(*allocator) Addr2LinePool(path, allocator);
470   } else if (path) {
471     Report("ERROR: External symbolizer path is set to '%s' which isn't "
472            "a known symbolizer. Please set the path to the llvm-symbolizer "
473            "binary or other known tool.\n", path);
474     Die();
475   }
476
477   // Otherwise symbolizer program is unknown, let's search $PATH
478   CHECK(path == nullptr);
479 #if SANITIZER_MAC
480   if (const char *found_path = FindPathToBinary("atos")) {
481     VReport(2, "Using atos found at: %s\n", found_path);
482     return new(*allocator) AtosSymbolizer(found_path, allocator);
483   }
484 #endif  // SANITIZER_MAC
485   if (const char *found_path = FindPathToBinary("llvm-symbolizer")) {
486     VReport(2, "Using llvm-symbolizer found at: %s\n", found_path);
487     return new(*allocator) LLVMSymbolizer(found_path, allocator);
488   }
489   if (common_flags()->allow_addr2line) {
490     if (const char *found_path = FindPathToBinary("addr2line")) {
491       VReport(2, "Using addr2line found at: %s\n", found_path);
492       return new(*allocator) Addr2LinePool(found_path, allocator);
493     }
494   }
495   return nullptr;
496 }
497
498 static void ChooseSymbolizerTools(IntrusiveList<SymbolizerTool> *list,
499                                   LowLevelAllocator *allocator) {
500   if (!common_flags()->symbolize) {
501     VReport(2, "Symbolizer is disabled.\n");
502     return;
503   }
504   if (IsAllocatorOutOfMemory()) {
505     VReport(2, "Cannot use internal symbolizer: out of memory\n");
506   } else if (SymbolizerTool *tool = InternalSymbolizer::get(allocator)) {
507     VReport(2, "Using internal symbolizer.\n");
508     list->push_back(tool);
509     return;
510   }
511   if (SymbolizerTool *tool = LibbacktraceSymbolizer::get(allocator)) {
512     VReport(2, "Using libbacktrace symbolizer.\n");
513     list->push_back(tool);
514     return;
515   }
516
517   if (SymbolizerTool *tool = ChooseExternalSymbolizer(allocator)) {
518     list->push_back(tool);
519   }
520
521 #if SANITIZER_MAC
522   VReport(2, "Using dladdr symbolizer.\n");
523   list->push_back(new(*allocator) DlAddrSymbolizer());
524 #endif  // SANITIZER_MAC
525 }
526
527 Symbolizer *Symbolizer::PlatformInit() {
528   IntrusiveList<SymbolizerTool> list;
529   list.clear();
530   ChooseSymbolizerTools(&list, &symbolizer_allocator_);
531   return new(symbolizer_allocator_) Symbolizer(list);
532 }
533
534 void Symbolizer::LateInitialize() {
535   Symbolizer::GetOrInit();
536   InitializeSwiftDemangler();
537 }
538
539 }  // namespace __sanitizer
540
541 #endif  // SANITIZER_POSIX