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 14 #include "sanitizer_platform.h" 15 #if SANITIZER_MAC 16 17 #include "sanitizer_allocator_internal.h" 18 #include "sanitizer_mac.h" 19 #include "sanitizer_symbolizer_mac.h" 20 21 #include <dlfcn.h> 22 #include <errno.h> 23 #include <stdlib.h> 24 #include <sys/wait.h> 25 #include <unistd.h> 26 #include <util.h> 27 28 namespace __sanitizer { 29 30 bool DlAddrSymbolizer::SymbolizePC(uptr addr, SymbolizedStack *stack) { 31 Dl_info info; 32 int result = dladdr((const void *)addr, &info); 33 if (!result) return false; 34 35 CHECK(addr >= reinterpret_cast<uptr>(info.dli_saddr)); 36 stack->info.function_offset = addr - reinterpret_cast<uptr>(info.dli_saddr); 37 const char *demangled = DemangleSwiftAndCXX(info.dli_sname); 38 if (!demangled) return false; 39 stack->info.function = internal_strdup(demangled); 40 return true; 41 } 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 53 class AtosSymbolizerProcess : public SymbolizerProcess { 54 public: 55 explicit AtosSymbolizerProcess(const char *path, pid_t parent_pid) 56 : SymbolizerProcess(path, /*use_posix_spawn*/ true) { 57 // Put the string command line argument in the object so that it outlives 58 // the call to GetArgV. 59 internal_snprintf(pid_str_, sizeof(pid_str_), "%d", parent_pid); 60 } 61 62 private: 63 bool StartSymbolizerSubprocess() override { 64 // Configure sandbox before starting atos process. 65 return SymbolizerProcess::StartSymbolizerSubprocess(); 66 } 67 68 bool ReachedEndOfOutput(const char *buffer, uptr length) const override { 69 return (length >= 1 && buffer[length - 1] == '\n'); 70 } 71 72 void GetArgV(const char *path_to_binary, 73 const char *(&argv)[kArgVMax]) const override { 74 int i = 0; 75 argv[i++] = path_to_binary; 76 argv[i++] = "-p"; 77 argv[i++] = &pid_str_[0]; 78 if (GetMacosVersion() == MACOS_VERSION_MAVERICKS) { 79 // On Mavericks atos prints a deprecation warning which we suppress by 80 // passing -d. The warning isn't present on other OSX versions, even the 81 // newer ones. 82 argv[i++] = "-d"; 83 } 84 argv[i++] = nullptr; 85 } 86 87 char pid_str_[16]; 88 }; 89 90 static bool ParseCommandOutput(const char *str, uptr addr, char **out_name, 91 char **out_module, char **out_file, uptr *line, 92 uptr *start_address) { 93 // Trim ending newlines. 94 char *trim; 95 ExtractTokenUpToDelimiter(str, "\n", &trim); 96 97 // The line from `atos` is in one of these formats: 98 // myfunction (in library.dylib) (sourcefile.c:17) 99 // myfunction (in library.dylib) + 0x1fe 100 // myfunction (in library.dylib) + 15 101 // 0xdeadbeef (in library.dylib) + 0x1fe 102 // 0xdeadbeef (in library.dylib) + 15 103 // 0xdeadbeef (in library.dylib) 104 // 0xdeadbeef 105 106 const char *rest = trim; 107 char *symbol_name; 108 rest = ExtractTokenUpToDelimiter(rest, " (in ", &symbol_name); 109 if (rest[0] == '\0') { 110 InternalFree(symbol_name); 111 InternalFree(trim); 112 return false; 113 } 114 115 if (internal_strncmp(symbol_name, "0x", 2) != 0) 116 *out_name = symbol_name; 117 else 118 InternalFree(symbol_name); 119 rest = ExtractTokenUpToDelimiter(rest, ") ", out_module); 120 121 if (rest[0] == '(') { 122 if (out_file) { 123 rest++; 124 rest = ExtractTokenUpToDelimiter(rest, ":", out_file); 125 char *extracted_line_number; 126 rest = ExtractTokenUpToDelimiter(rest, ")", &extracted_line_number); 127 if (line) *line = (uptr)internal_atoll(extracted_line_number); 128 InternalFree(extracted_line_number); 129 } 130 } else if (rest[0] == '+') { 131 rest += 2; 132 uptr offset = internal_atoll(rest); 133 if (start_address) *start_address = addr - offset; 134 } 135 136 InternalFree(trim); 137 return true; 138 } 139 140 AtosSymbolizer::AtosSymbolizer(const char *path, LowLevelAllocator *allocator) 141 : process_(new(*allocator) AtosSymbolizerProcess(path, getpid())) {} 142 143 bool AtosSymbolizer::SymbolizePC(uptr addr, SymbolizedStack *stack) { 144 if (!process_) return false; 145 if (addr == 0) return false; 146 char command[32]; 147 internal_snprintf(command, sizeof(command), "0x%zx\n", addr); 148 const char *buf = process_->SendCommand(command); 149 if (!buf) return false; 150 uptr line; 151 uptr start_address = AddressInfo::kUnknown; 152 if (!ParseCommandOutput(buf, addr, &stack->info.function, &stack->info.module, 153 &stack->info.file, &line, &start_address)) { 154 process_ = nullptr; 155 return false; 156 } 157 stack->info.line = (int)line; 158 159 if (start_address == AddressInfo::kUnknown) { 160 // Fallback to dladdr() to get function start address if atos doesn't report 161 // it. 162 Dl_info info; 163 int result = dladdr((const void *)addr, &info); 164 if (result) 165 start_address = reinterpret_cast<uptr>(info.dli_saddr); 166 } 167 168 // Only assig to `function_offset` if we were able to get the function's 169 // start address. 170 if (start_address != AddressInfo::kUnknown) { 171 CHECK(addr >= start_address); 172 stack->info.function_offset = addr - start_address; 173 } 174 return true; 175 } 176 177 bool AtosSymbolizer::SymbolizeData(uptr addr, DataInfo *info) { 178 if (!process_) return false; 179 char command[32]; 180 internal_snprintf(command, sizeof(command), "0x%zx\n", addr); 181 const char *buf = process_->SendCommand(command); 182 if (!buf) return false; 183 if (!ParseCommandOutput(buf, addr, &info->name, &info->module, nullptr, 184 nullptr, &info->start)) { 185 process_ = nullptr; 186 return false; 187 } 188 return true; 189 } 190 191 } // namespace __sanitizer 192 193 #endif // SANITIZER_MAC 194