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_APPLE
16
17# include <dlfcn.h>
18# include <errno.h>
19# include <stdlib.h>
20# include <sys/wait.h>
21# include <unistd.h>
22# include <util.h>
23
24# include "sanitizer_allocator_internal.h"
25# include "sanitizer_mac.h"
26# include "sanitizer_symbolizer_mac.h"
27
28namespace __sanitizer {
29
30bool DlAddrSymbolizer::SymbolizePC(uptr addr, SymbolizedStack *stack) {
31 Dl_info info;
32 int result = dladdr((const void *)addr, &info);
33 if (!result || !info.dli_sname) return false;
34
35 // Compute offset if possible. `dladdr()` doesn't always ensure that `addr >=
36 // sym_addr` so only compute the offset when this holds. Failure to find the
37 // function offset is not treated as a failure because it might still be
38 // possible to get the symbol name.
39 uptr sym_addr = reinterpret_cast<uptr>(info.dli_saddr);
40 if (addr >= sym_addr) {
41 stack->info.function_offset = addr - sym_addr;
42 }
43
44 const char *demangled = DemangleSwiftAndCXX(info.dli_sname);
45 if (!demangled)
46 demangled = info.dli_sname;
47 stack->info.function = internal_strdup(demangled);
48 return true;
49}
50
51bool DlAddrSymbolizer::SymbolizeData(uptr addr, DataInfo *datainfo) {
52 Dl_info info;
53 int result = dladdr((const void *)addr, &info);
54 if (!result || !info.dli_sname) return false;
55 const char *demangled = DemangleSwiftAndCXX(info.dli_sname);
56 if (!demangled)
57 demangled = info.dli_sname;
58 datainfo->name = internal_strdup(demangled);
59 datainfo->start = (uptr)info.dli_saddr;
60 return true;
61}
62
63class AtosSymbolizerProcess final : public SymbolizerProcess {
64 public:
65 explicit AtosSymbolizerProcess(const char *path)
66 : SymbolizerProcess(path, /*use_posix_spawn*/ true) {
67 pid_str_[0] = '\0';
68 }
69
70 private:
71 bool StartSymbolizerSubprocess() override {
72 // Put the string command line argument in the object so that it outlives
73 // the call to GetArgV.
74 internal_snprintf(pid_str_, sizeof(pid_str_), "%d", (int)internal_getpid());
75
76 // Configure sandbox before starting atos process.
77 return SymbolizerProcess::StartSymbolizerSubprocess();
78 }
79
80 bool ReachedEndOfOutput(const char *buffer, uptr length) const override {
81 if (common_flags()->symbolize_inline_frames) {
82 // When running with -i, atos sends two newlines at the end of each
83 // address it symbolizes. This indicates the end of the set of frames
84 // for a particular address.
85 return length >= 2 && buffer[length - 1] == '\n' &&
86 buffer[length - 2] == '\n';
87 } else {
88 // When running without -i, atos only sends a single newline at
89 // the end of each address it symbolizes.
90 return length >= 1 && buffer[length - 1] == '\n';
91 }
92 }
93
94 void GetArgV(const char *path_to_binary,
95 const char *(&argv)[kArgVMax]) const override {
96 int i = 0;
97 argv[i++] = path_to_binary;
98 if (common_flags()->symbolize_inline_frames)
99 argv[i++] = "-i";
100 argv[i++] = "-p";
101 argv[i++] = &pid_str_[0];
102 if (GetMacosAlignedVersion() == MacosVersion(10, 9)) {
103 // On Mavericks atos prints a deprecation warning which we suppress by
104 // passing -d. The warning isn't present on other OSX versions, even the
105 // newer ones.
106 argv[i++] = "-d";
107 }
108 argv[i++] = nullptr;
109 CHECK_LE(i, kArgVMax);
110 }
111
112 char pid_str_[16];
113};
114
115#undef K_ATOS_ENV_VAR
116
117// Parses a single frame (one line) from str, and returns the pointer to the
118// next character to parse (i.e. after the newline) if successful. If
119// it fails, returns NULL.
120static const char* ParseCommandOutput(const char* str, uptr addr,
121 char** out_name, char** out_module,
122 char** out_file, uptr* line,
123 uptr* start_address) {
124 // Trim ending newlines.
125 char *trim;
126 str = ExtractTokenUpToDelimiter(str, "\n", &trim);
127
128 // The line from `atos` is in one of these formats:
129 // myfunction (in library.dylib) (sourcefile.c:17)
130 // myfunction (in library.dylib) + 0x1fe
131 // myfunction (in library.dylib) + 15
132 // 0xdeadbeef (in library.dylib) + 0x1fe
133 // 0xdeadbeef (in library.dylib) + 15
134 // 0xdeadbeef (in library.dylib)
135 // 0xdeadbeef
136
137 const char *rest = trim;
138 char *symbol_name;
139 rest = ExtractTokenUpToDelimiter(rest, " (in ", &symbol_name);
140 if (rest[0] == '\0') {
141 InternalFree(symbol_name);
142 InternalFree(trim);
143 return NULL;
144 }
145
146 if (internal_strncmp(symbol_name, "0x", 2) != 0)
147 *out_name = symbol_name;
148 else
149 InternalFree(symbol_name);
150 rest = ExtractTokenUpToDelimiter(rest, ") ", out_module);
151
152 if (rest[0] == '(') {
153 if (out_file) {
154 rest++;
155 rest = ExtractTokenUpToDelimiter(rest, ":", out_file);
156 char *extracted_line_number;
157 rest = ExtractTokenUpToDelimiter(rest, ")", &extracted_line_number);
158 if (line) *line = (uptr)internal_atoll(extracted_line_number);
159 InternalFree(extracted_line_number);
160 }
161 } else if (rest[0] == '+') {
162 rest += 2;
163 uptr offset = internal_atoll(rest);
164 if (start_address) *start_address = addr - offset;
165 }
166
167 InternalFree(trim);
168 return str;
169}
170
171AtosSymbolizer::AtosSymbolizer(const char *path, LowLevelAllocator *allocator)
172 : process_(new (*allocator) AtosSymbolizerProcess(path)) {}
173
174bool AtosSymbolizer::SymbolizePC(uptr addr, SymbolizedStack *stack) {
175 if (!process_) return false;
176 if (addr == 0) return false;
177 char command[32];
178 internal_snprintf(command, sizeof(command), "0x%zx\n", addr);
179 const char *buf = process_->SendCommand(command);
180 if (!buf)
181 return false;
182
183 SymbolizedStack* last = stack;
184 bool top_frame = true;
185
186 // Parse one line of input (i.e. one frame).
187 //
188 // When symbolize_inline_frames=true, an empty line
189 // (i.e. \n at the beginning of a line) indicates that the last
190 // frame has been sent.
191 //
192 // When symbolize_inline_frames=false, the symbolizer will send only
193 // one frame (without a empty line), so loop runs exactly once
194 // and hits an early `break`.
195 while (*buf != '\n') {
196 uptr line;
197 uptr start_address = AddressInfo::kUnknown;
198
199 SymbolizedStack* cur;
200 if (top_frame) {
201 cur = stack;
202 } else {
203 cur = SymbolizedStack::New(stack->info.address);
204 cur->info.FillModuleInfo(stack->info.module, stack->info.module_offset,
205 stack->info.module_arch);
206 last->next = cur;
207 last = cur;
208 }
209
210 // Parse one line of input (i.e. one frame)
211 // If this succeeds, buf will be updated to point to the first character
212 // after the newline.
213 buf = ParseCommandOutput(buf, addr, &cur->info.function, &cur->info.module,
214 &cur->info.file, &line, &start_address);
215
216 // Upon failure, ParseCommandOutput returns NULL.
217 if (!buf) {
218 Report("WARNING: atos failed to symbolize address \"0x%zx\"\n", addr);
219 return false;
220 }
221 cur->info.line = (int)line;
222
223 if (top_frame && start_address == AddressInfo::kUnknown) {
224 // Fallback to dladdr() to get function start address if atos doesn't
225 // report it.
226 Dl_info info;
227 int result = dladdr((const void*)addr, &info);
228 if (result)
229 start_address = reinterpret_cast<uptr>(info.dli_saddr);
230 }
231
232 // Only assign to `function_offset` if we were able to get the function's
233 // start address and we got a sensible `start_address` (dladdr doesn't
234 // always ensure that `addr >= sym_addr`).
235 if (start_address != AddressInfo::kUnknown && addr >= start_address) {
236 cur->info.function_offset = addr - start_address;
237 }
238
239 // atos only sends one line when inline frames are off
240 if (!common_flags()->symbolize_inline_frames)
241 break;
242
243 top_frame = false;
244 }
245
246 return true;
247}
248
249bool AtosSymbolizer::SymbolizeData(uptr addr, DataInfo *info) {
250 if (!process_) return false;
251 char command[32];
252 internal_snprintf(command, sizeof(command), "0x%zx\n", addr);
253 const char *buf = process_->SendCommand(command);
254 if (!buf) return false;
255 if (!ParseCommandOutput(buf, addr, &info->name, &info->module, nullptr,
256 nullptr, &info->start)) {
257 process_ = nullptr;
258 return false;
259 }
260 return true;
261}
262
263} // namespace __sanitizer
264
265#endif // SANITIZER_APPLE
266