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