sanitizer_symbolizer_mac.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //===-- sanitizer_symbolizer_mac.cpp --------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file is shared between various sanitizers' runtime libraries.
  10. //
  11. // Implementation of Mac-specific "atos" symbolizer.
  12. //===----------------------------------------------------------------------===//
  13. #include "sanitizer_platform.h"
  14. #if SANITIZER_APPLE
  15. # include <dlfcn.h>
  16. # include <errno.h>
  17. # include <stdlib.h>
  18. # include <sys/wait.h>
  19. # include <unistd.h>
  20. # include <util.h>
  21. # include "sanitizer_allocator_internal.h"
  22. # include "sanitizer_mac.h"
  23. # include "sanitizer_symbolizer_mac.h"
  24. namespace __sanitizer {
  25. bool DlAddrSymbolizer::SymbolizePC(uptr addr, SymbolizedStack *stack) {
  26. Dl_info info;
  27. int result = dladdr((const void *)addr, &info);
  28. if (!result) return false;
  29. // Compute offset if possible. `dladdr()` doesn't always ensure that `addr >=
  30. // sym_addr` so only compute the offset when this holds. Failure to find the
  31. // function offset is not treated as a failure because it might still be
  32. // possible to get the symbol name.
  33. uptr sym_addr = reinterpret_cast<uptr>(info.dli_saddr);
  34. if (addr >= sym_addr) {
  35. stack->info.function_offset = addr - sym_addr;
  36. }
  37. const char *demangled = DemangleSwiftAndCXX(info.dli_sname);
  38. if (!demangled)
  39. demangled = info.dli_sname;
  40. stack->info.function = internal_strdup(demangled);
  41. return true;
  42. }
  43. bool DlAddrSymbolizer::SymbolizeData(uptr addr, DataInfo *datainfo) {
  44. Dl_info info;
  45. int result = dladdr((const void *)addr, &info);
  46. if (!result) return false;
  47. const char *demangled = DemangleSwiftAndCXX(info.dli_sname);
  48. if (!demangled)
  49. demangled = info.dli_sname;
  50. datainfo->name = internal_strdup(demangled);
  51. datainfo->start = (uptr)info.dli_saddr;
  52. return true;
  53. }
  54. class AtosSymbolizerProcess final : public SymbolizerProcess {
  55. public:
  56. explicit AtosSymbolizerProcess(const char *path)
  57. : SymbolizerProcess(path, /*use_posix_spawn*/ true) {
  58. pid_str_[0] = '\0';
  59. }
  60. private:
  61. bool StartSymbolizerSubprocess() override {
  62. // Put the string command line argument in the object so that it outlives
  63. // the call to GetArgV.
  64. internal_snprintf(pid_str_, sizeof(pid_str_), "%d", (int)internal_getpid());
  65. // Configure sandbox before starting atos process.
  66. return SymbolizerProcess::StartSymbolizerSubprocess();
  67. }
  68. bool ReachedEndOfOutput(const char *buffer, uptr length) const override {
  69. return (length >= 1 && buffer[length - 1] == '\n');
  70. }
  71. void GetArgV(const char *path_to_binary,
  72. const char *(&argv)[kArgVMax]) const override {
  73. int i = 0;
  74. argv[i++] = path_to_binary;
  75. argv[i++] = "-p";
  76. argv[i++] = &pid_str_[0];
  77. if (GetMacosAlignedVersion() == MacosVersion(10, 9)) {
  78. // On Mavericks atos prints a deprecation warning which we suppress by
  79. // passing -d. The warning isn't present on other OSX versions, even the
  80. // newer ones.
  81. argv[i++] = "-d";
  82. }
  83. argv[i++] = nullptr;
  84. CHECK_LE(i, kArgVMax);
  85. }
  86. char pid_str_[16];
  87. };
  88. #undef K_ATOS_ENV_VAR
  89. static bool ParseCommandOutput(const char *str, uptr addr, char **out_name,
  90. char **out_module, char **out_file, uptr *line,
  91. uptr *start_address) {
  92. // Trim ending newlines.
  93. char *trim;
  94. ExtractTokenUpToDelimiter(str, "\n", &trim);
  95. // The line from `atos` is in one of these formats:
  96. // myfunction (in library.dylib) (sourcefile.c:17)
  97. // myfunction (in library.dylib) + 0x1fe
  98. // myfunction (in library.dylib) + 15
  99. // 0xdeadbeef (in library.dylib) + 0x1fe
  100. // 0xdeadbeef (in library.dylib) + 15
  101. // 0xdeadbeef (in library.dylib)
  102. // 0xdeadbeef
  103. const char *rest = trim;
  104. char *symbol_name;
  105. rest = ExtractTokenUpToDelimiter(rest, " (in ", &symbol_name);
  106. if (rest[0] == '\0') {
  107. InternalFree(symbol_name);
  108. InternalFree(trim);
  109. return false;
  110. }
  111. if (internal_strncmp(symbol_name, "0x", 2) != 0)
  112. *out_name = symbol_name;
  113. else
  114. InternalFree(symbol_name);
  115. rest = ExtractTokenUpToDelimiter(rest, ") ", out_module);
  116. if (rest[0] == '(') {
  117. if (out_file) {
  118. rest++;
  119. rest = ExtractTokenUpToDelimiter(rest, ":", out_file);
  120. char *extracted_line_number;
  121. rest = ExtractTokenUpToDelimiter(rest, ")", &extracted_line_number);
  122. if (line) *line = (uptr)internal_atoll(extracted_line_number);
  123. InternalFree(extracted_line_number);
  124. }
  125. } else if (rest[0] == '+') {
  126. rest += 2;
  127. uptr offset = internal_atoll(rest);
  128. if (start_address) *start_address = addr - offset;
  129. }
  130. InternalFree(trim);
  131. return true;
  132. }
  133. AtosSymbolizer::AtosSymbolizer(const char *path, LowLevelAllocator *allocator)
  134. : process_(new (*allocator) AtosSymbolizerProcess(path)) {}
  135. bool AtosSymbolizer::SymbolizePC(uptr addr, SymbolizedStack *stack) {
  136. if (!process_) return false;
  137. if (addr == 0) return false;
  138. char command[32];
  139. internal_snprintf(command, sizeof(command), "0x%zx\n", addr);
  140. const char *buf = process_->SendCommand(command);
  141. if (!buf) return false;
  142. uptr line;
  143. uptr start_address = AddressInfo::kUnknown;
  144. if (!ParseCommandOutput(buf, addr, &stack->info.function, &stack->info.module,
  145. &stack->info.file, &line, &start_address)) {
  146. Report("WARNING: atos failed to symbolize address \"0x%zx\"\n", addr);
  147. return false;
  148. }
  149. stack->info.line = (int)line;
  150. if (start_address == AddressInfo::kUnknown) {
  151. // Fallback to dladdr() to get function start address if atos doesn't report
  152. // it.
  153. Dl_info info;
  154. int result = dladdr((const void *)addr, &info);
  155. if (result)
  156. start_address = reinterpret_cast<uptr>(info.dli_saddr);
  157. }
  158. // Only assign to `function_offset` if we were able to get the function's
  159. // start address and we got a sensible `start_address` (dladdr doesn't always
  160. // ensure that `addr >= sym_addr`).
  161. if (start_address != AddressInfo::kUnknown && addr >= start_address) {
  162. stack->info.function_offset = addr - start_address;
  163. }
  164. return true;
  165. }
  166. bool AtosSymbolizer::SymbolizeData(uptr addr, DataInfo *info) {
  167. if (!process_) return false;
  168. char command[32];
  169. internal_snprintf(command, sizeof(command), "0x%zx\n", addr);
  170. const char *buf = process_->SendCommand(command);
  171. if (!buf) return false;
  172. if (!ParseCommandOutput(buf, addr, &info->name, &info->module, nullptr,
  173. nullptr, &info->start)) {
  174. process_ = nullptr;
  175. return false;
  176. }
  177. return true;
  178. }
  179. } // namespace __sanitizer
  180. #endif // SANITIZER_APPLE