sanitizer_symbolizer_mac.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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_MAC
  15. #include "sanitizer_allocator_internal.h"
  16. #include "sanitizer_mac.h"
  17. #include "sanitizer_symbolizer_mac.h"
  18. #include <dlfcn.h>
  19. #include <errno.h>
  20. #include <mach/mach.h>
  21. #include <stdlib.h>
  22. #include <sys/wait.h>
  23. #include <unistd.h>
  24. #include <util.h>
  25. namespace __sanitizer {
  26. bool DlAddrSymbolizer::SymbolizePC(uptr addr, SymbolizedStack *stack) {
  27. Dl_info info;
  28. int result = dladdr((const void *)addr, &info);
  29. if (!result) return false;
  30. // Compute offset if possible. `dladdr()` doesn't always ensure that `addr >=
  31. // sym_addr` so only compute the offset when this holds. Failure to find the
  32. // function offset is not treated as a failure because it might still be
  33. // possible to get the symbol name.
  34. uptr sym_addr = reinterpret_cast<uptr>(info.dli_saddr);
  35. if (addr >= sym_addr) {
  36. stack->info.function_offset = addr - sym_addr;
  37. }
  38. const char *demangled = DemangleSwiftAndCXX(info.dli_sname);
  39. if (!demangled) return false;
  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. datainfo->name = internal_strdup(demangled);
  49. datainfo->start = (uptr)info.dli_saddr;
  50. return true;
  51. }
  52. #define K_ATOS_ENV_VAR "__check_mach_ports_lookup"
  53. // This cannot live in `AtosSymbolizerProcess` because instances of that object
  54. // are allocated by the internal allocator which under ASan is poisoned with
  55. // kAsanInternalHeapMagic.
  56. static char kAtosMachPortEnvEntry[] = K_ATOS_ENV_VAR "=000000000000000";
  57. class AtosSymbolizerProcess final : public SymbolizerProcess {
  58. public:
  59. explicit AtosSymbolizerProcess(const char *path)
  60. : SymbolizerProcess(path, /*use_posix_spawn*/ true) {
  61. pid_str_[0] = '\0';
  62. }
  63. void LateInitialize() {
  64. if (SANITIZER_IOSSIM) {
  65. // `putenv()` may call malloc/realloc so it is only safe to do this
  66. // during LateInitialize() or later (i.e. we can't do this in the
  67. // constructor). We also can't do this in `StartSymbolizerSubprocess()`
  68. // because in TSan we switch allocators when we're symbolizing.
  69. // We use `putenv()` rather than `setenv()` so that we can later directly
  70. // write into the storage without LibC getting involved to change what the
  71. // variable is set to
  72. int result = putenv(kAtosMachPortEnvEntry);
  73. CHECK_EQ(result, 0);
  74. }
  75. }
  76. private:
  77. bool StartSymbolizerSubprocess() override {
  78. // Configure sandbox before starting atos process.
  79. // Put the string command line argument in the object so that it outlives
  80. // the call to GetArgV.
  81. internal_snprintf(pid_str_, sizeof(pid_str_), "%d", internal_getpid());
  82. if (SANITIZER_IOSSIM) {
  83. // `atos` in the simulator is restricted in its ability to retrieve the
  84. // task port for the target process (us) so we need to do extra work
  85. // to pass our task port to it.
  86. mach_port_t ports[]{mach_task_self()};
  87. kern_return_t ret =
  88. mach_ports_register(mach_task_self(), ports, /*count=*/1);
  89. CHECK_EQ(ret, KERN_SUCCESS);
  90. // Set environment variable that signals to `atos` that it should look
  91. // for our task port. We can't call `setenv()` here because it might call
  92. // malloc/realloc. To avoid that we instead update the
  93. // `mach_port_env_var_entry_` variable with our current PID.
  94. uptr count = internal_snprintf(kAtosMachPortEnvEntry,
  95. sizeof(kAtosMachPortEnvEntry),
  96. K_ATOS_ENV_VAR "=%s", pid_str_);
  97. CHECK_GE(count, sizeof(K_ATOS_ENV_VAR) + internal_strlen(pid_str_));
  98. // Document our assumption but without calling `getenv()` in normal
  99. // builds.
  100. DCHECK(getenv(K_ATOS_ENV_VAR));
  101. DCHECK_EQ(internal_strcmp(getenv(K_ATOS_ENV_VAR), pid_str_), 0);
  102. }
  103. return SymbolizerProcess::StartSymbolizerSubprocess();
  104. }
  105. bool ReachedEndOfOutput(const char *buffer, uptr length) const override {
  106. return (length >= 1 && buffer[length - 1] == '\n');
  107. }
  108. void GetArgV(const char *path_to_binary,
  109. const char *(&argv)[kArgVMax]) const override {
  110. int i = 0;
  111. argv[i++] = path_to_binary;
  112. argv[i++] = "-p";
  113. argv[i++] = &pid_str_[0];
  114. if (GetMacosAlignedVersion() == MacosVersion(10, 9)) {
  115. // On Mavericks atos prints a deprecation warning which we suppress by
  116. // passing -d. The warning isn't present on other OSX versions, even the
  117. // newer ones.
  118. argv[i++] = "-d";
  119. }
  120. argv[i++] = nullptr;
  121. }
  122. char pid_str_[16];
  123. // Space for `\0` in `K_ATOS_ENV_VAR` is reused for `=`.
  124. static_assert(sizeof(kAtosMachPortEnvEntry) ==
  125. (sizeof(K_ATOS_ENV_VAR) + sizeof(pid_str_)),
  126. "sizes should match");
  127. };
  128. #undef K_ATOS_ENV_VAR
  129. static bool ParseCommandOutput(const char *str, uptr addr, char **out_name,
  130. char **out_module, char **out_file, uptr *line,
  131. uptr *start_address) {
  132. // Trim ending newlines.
  133. char *trim;
  134. ExtractTokenUpToDelimiter(str, "\n", &trim);
  135. // The line from `atos` is in one of these formats:
  136. // myfunction (in library.dylib) (sourcefile.c:17)
  137. // myfunction (in library.dylib) + 0x1fe
  138. // myfunction (in library.dylib) + 15
  139. // 0xdeadbeef (in library.dylib) + 0x1fe
  140. // 0xdeadbeef (in library.dylib) + 15
  141. // 0xdeadbeef (in library.dylib)
  142. // 0xdeadbeef
  143. const char *rest = trim;
  144. char *symbol_name;
  145. rest = ExtractTokenUpToDelimiter(rest, " (in ", &symbol_name);
  146. if (rest[0] == '\0') {
  147. InternalFree(symbol_name);
  148. InternalFree(trim);
  149. return false;
  150. }
  151. if (internal_strncmp(symbol_name, "0x", 2) != 0)
  152. *out_name = symbol_name;
  153. else
  154. InternalFree(symbol_name);
  155. rest = ExtractTokenUpToDelimiter(rest, ") ", out_module);
  156. if (rest[0] == '(') {
  157. if (out_file) {
  158. rest++;
  159. rest = ExtractTokenUpToDelimiter(rest, ":", out_file);
  160. char *extracted_line_number;
  161. rest = ExtractTokenUpToDelimiter(rest, ")", &extracted_line_number);
  162. if (line) *line = (uptr)internal_atoll(extracted_line_number);
  163. InternalFree(extracted_line_number);
  164. }
  165. } else if (rest[0] == '+') {
  166. rest += 2;
  167. uptr offset = internal_atoll(rest);
  168. if (start_address) *start_address = addr - offset;
  169. }
  170. InternalFree(trim);
  171. return true;
  172. }
  173. AtosSymbolizer::AtosSymbolizer(const char *path, LowLevelAllocator *allocator)
  174. : process_(new (*allocator) AtosSymbolizerProcess(path)) {}
  175. bool AtosSymbolizer::SymbolizePC(uptr addr, SymbolizedStack *stack) {
  176. if (!process_) return false;
  177. if (addr == 0) return false;
  178. char command[32];
  179. internal_snprintf(command, sizeof(command), "0x%zx\n", addr);
  180. const char *buf = process_->SendCommand(command);
  181. if (!buf) return false;
  182. uptr line;
  183. uptr start_address = AddressInfo::kUnknown;
  184. if (!ParseCommandOutput(buf, addr, &stack->info.function, &stack->info.module,
  185. &stack->info.file, &line, &start_address)) {
  186. process_ = nullptr;
  187. return false;
  188. }
  189. stack->info.line = (int)line;
  190. if (start_address == AddressInfo::kUnknown) {
  191. // Fallback to dladdr() to get function start address if atos doesn't report
  192. // it.
  193. Dl_info info;
  194. int result = dladdr((const void *)addr, &info);
  195. if (result)
  196. start_address = reinterpret_cast<uptr>(info.dli_saddr);
  197. }
  198. // Only assign to `function_offset` if we were able to get the function's
  199. // start address and we got a sensible `start_address` (dladdr doesn't always
  200. // ensure that `addr >= sym_addr`).
  201. if (start_address != AddressInfo::kUnknown && addr >= start_address) {
  202. stack->info.function_offset = addr - start_address;
  203. }
  204. return true;
  205. }
  206. bool AtosSymbolizer::SymbolizeData(uptr addr, DataInfo *info) {
  207. if (!process_) return false;
  208. char command[32];
  209. internal_snprintf(command, sizeof(command), "0x%zx\n", addr);
  210. const char *buf = process_->SendCommand(command);
  211. if (!buf) return false;
  212. if (!ParseCommandOutput(buf, addr, &info->name, &info->module, nullptr,
  213. nullptr, &info->start)) {
  214. process_ = nullptr;
  215. return false;
  216. }
  217. return true;
  218. }
  219. void AtosSymbolizer::LateInitialize() { process_->LateInitialize(); }
  220. } // namespace __sanitizer
  221. #endif // SANITIZER_MAC