xref: /freebsd/contrib/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_mac.cpp (revision 04eeddc0aa8e0a417a16eaf9d7d095207f4a8623)
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   // 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) return false;
46   stack->info.function = internal_strdup(demangled);
47   return true;
48 }
49 
50 bool DlAddrSymbolizer::SymbolizeData(uptr addr, DataInfo *datainfo) {
51   Dl_info info;
52   int result = dladdr((const void *)addr, &info);
53   if (!result) return false;
54   const char *demangled = DemangleSwiftAndCXX(info.dli_sname);
55   datainfo->name = internal_strdup(demangled);
56   datainfo->start = (uptr)info.dli_saddr;
57   return true;
58 }
59 
60 class AtosSymbolizerProcess final : public SymbolizerProcess {
61  public:
62   explicit AtosSymbolizerProcess(const char *path)
63       : SymbolizerProcess(path, /*use_posix_spawn*/ true) {
64     pid_str_[0] = '\0';
65   }
66 
67  private:
68   bool StartSymbolizerSubprocess() override {
69     // Put the string command line argument in the object so that it outlives
70     // the call to GetArgV.
71     internal_snprintf(pid_str_, sizeof(pid_str_), "%d", (int)internal_getpid());
72 
73     // Configure sandbox before starting atos process.
74     return SymbolizerProcess::StartSymbolizerSubprocess();
75   }
76 
77   bool ReachedEndOfOutput(const char *buffer, uptr length) const override {
78     return (length >= 1 && buffer[length - 1] == '\n');
79   }
80 
81   void GetArgV(const char *path_to_binary,
82                const char *(&argv)[kArgVMax]) const override {
83     int i = 0;
84     argv[i++] = path_to_binary;
85     argv[i++] = "-p";
86     argv[i++] = &pid_str_[0];
87     if (GetMacosAlignedVersion() == MacosVersion(10, 9)) {
88       // On Mavericks atos prints a deprecation warning which we suppress by
89       // passing -d. The warning isn't present on other OSX versions, even the
90       // newer ones.
91       argv[i++] = "-d";
92     }
93     argv[i++] = nullptr;
94     CHECK_LE(i, kArgVMax);
95   }
96 
97   char pid_str_[16];
98 };
99 
100 #undef K_ATOS_ENV_VAR
101 
102 static bool ParseCommandOutput(const char *str, uptr addr, char **out_name,
103                                char **out_module, char **out_file, uptr *line,
104                                uptr *start_address) {
105   // Trim ending newlines.
106   char *trim;
107   ExtractTokenUpToDelimiter(str, "\n", &trim);
108 
109   // The line from `atos` is in one of these formats:
110   //   myfunction (in library.dylib) (sourcefile.c:17)
111   //   myfunction (in library.dylib) + 0x1fe
112   //   myfunction (in library.dylib) + 15
113   //   0xdeadbeef (in library.dylib) + 0x1fe
114   //   0xdeadbeef (in library.dylib) + 15
115   //   0xdeadbeef (in library.dylib)
116   //   0xdeadbeef
117 
118   const char *rest = trim;
119   char *symbol_name;
120   rest = ExtractTokenUpToDelimiter(rest, " (in ", &symbol_name);
121   if (rest[0] == '\0') {
122     InternalFree(symbol_name);
123     InternalFree(trim);
124     return false;
125   }
126 
127   if (internal_strncmp(symbol_name, "0x", 2) != 0)
128     *out_name = symbol_name;
129   else
130     InternalFree(symbol_name);
131   rest = ExtractTokenUpToDelimiter(rest, ") ", out_module);
132 
133   if (rest[0] == '(') {
134     if (out_file) {
135       rest++;
136       rest = ExtractTokenUpToDelimiter(rest, ":", out_file);
137       char *extracted_line_number;
138       rest = ExtractTokenUpToDelimiter(rest, ")", &extracted_line_number);
139       if (line) *line = (uptr)internal_atoll(extracted_line_number);
140       InternalFree(extracted_line_number);
141     }
142   } else if (rest[0] == '+') {
143     rest += 2;
144     uptr offset = internal_atoll(rest);
145     if (start_address) *start_address = addr - offset;
146   }
147 
148   InternalFree(trim);
149   return true;
150 }
151 
152 AtosSymbolizer::AtosSymbolizer(const char *path, LowLevelAllocator *allocator)
153     : process_(new (*allocator) AtosSymbolizerProcess(path)) {}
154 
155 bool AtosSymbolizer::SymbolizePC(uptr addr, SymbolizedStack *stack) {
156   if (!process_) return false;
157   if (addr == 0) return false;
158   char command[32];
159   internal_snprintf(command, sizeof(command), "0x%zx\n", addr);
160   const char *buf = process_->SendCommand(command);
161   if (!buf) return false;
162   uptr line;
163   uptr start_address = AddressInfo::kUnknown;
164   if (!ParseCommandOutput(buf, addr, &stack->info.function, &stack->info.module,
165                           &stack->info.file, &line, &start_address)) {
166     process_ = nullptr;
167     return false;
168   }
169   stack->info.line = (int)line;
170 
171   if (start_address == AddressInfo::kUnknown) {
172     // Fallback to dladdr() to get function start address if atos doesn't report
173     // it.
174     Dl_info info;
175     int result = dladdr((const void *)addr, &info);
176     if (result)
177       start_address = reinterpret_cast<uptr>(info.dli_saddr);
178   }
179 
180   // Only assign to `function_offset` if we were able to get the function's
181   // start address and we got a sensible `start_address` (dladdr doesn't always
182   // ensure that `addr >= sym_addr`).
183   if (start_address != AddressInfo::kUnknown && addr >= start_address) {
184     stack->info.function_offset = addr - start_address;
185   }
186   return true;
187 }
188 
189 bool AtosSymbolizer::SymbolizeData(uptr addr, DataInfo *info) {
190   if (!process_) return false;
191   char command[32];
192   internal_snprintf(command, sizeof(command), "0x%zx\n", addr);
193   const char *buf = process_->SendCommand(command);
194   if (!buf) return false;
195   if (!ParseCommandOutput(buf, addr, &info->name, &info->module, nullptr,
196                           nullptr, &info->start)) {
197     process_ = nullptr;
198     return false;
199   }
200   return true;
201 }
202 
203 }  // namespace __sanitizer
204 
205 #endif  // SANITIZER_MAC
206