]> git.ipfire.org Git - thirdparty/gcc.git/blame - libsanitizer/sanitizer_common/sanitizer_symbolizer_mac.cpp
Libsanitizer: merge from master.
[thirdparty/gcc.git] / libsanitizer / sanitizer_common / sanitizer_symbolizer_mac.cpp
CommitLineData
b667dd70 1//===-- sanitizer_symbolizer_mac.cpp --------------------------------------===//
696d846a 2//
b667dd70
ML
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
696d846a
MO
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
696d846a
MO
21#include <dlfcn.h>
22#include <errno.h>
3c6331c2 23#include <mach/mach.h>
696d846a
MO
24#include <stdlib.h>
25#include <sys/wait.h>
26#include <unistd.h>
27#include <util.h>
28
10189819
MO
29namespace __sanitizer {
30
696d846a
MO
31bool DlAddrSymbolizer::SymbolizePC(uptr addr, SymbolizedStack *stack) {
32 Dl_info info;
33 int result = dladdr((const void *)addr, &info);
34 if (!result) return false;
3c6331c2
ML
35
36 CHECK(addr >= reinterpret_cast<uptr>(info.dli_saddr));
37 stack->info.function_offset = addr - reinterpret_cast<uptr>(info.dli_saddr);
10189819
MO
38 const char *demangled = DemangleSwiftAndCXX(info.dli_sname);
39 if (!demangled) return false;
696d846a
MO
40 stack->info.function = internal_strdup(demangled);
41 return true;
42}
43
55aea9f5
MO
44bool DlAddrSymbolizer::SymbolizeData(uptr addr, DataInfo *datainfo) {
45 Dl_info info;
46 int result = dladdr((const void *)addr, &info);
47 if (!result) return false;
10189819 48 const char *demangled = DemangleSwiftAndCXX(info.dli_sname);
55aea9f5
MO
49 datainfo->name = internal_strdup(demangled);
50 datainfo->start = (uptr)info.dli_saddr;
51 return true;
696d846a
MO
52}
53
3c6331c2
ML
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
696d846a
MO
61class AtosSymbolizerProcess : public SymbolizerProcess {
62 public:
3c6331c2 63 explicit AtosSymbolizerProcess(const char *path)
3ca75cd5 64 : SymbolizerProcess(path, /*use_posix_spawn*/ true) {
3c6331c2
ML
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 }
696d846a
MO
80 }
81
82 private:
3ca75cd5 83 bool StartSymbolizerSubprocess() override {
b667dd70 84 // Configure sandbox before starting atos process.
3c6331c2
ML
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
b667dd70
ML
113 return SymbolizerProcess::StartSymbolizerSubprocess();
114 }
115
696d846a
MO
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 (GetMacosVersion() == MACOS_VERSION_MAVERICKS) {
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];
3c6331c2
ML
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");
696d846a
MO
140};
141
3c6331c2
ML
142#undef K_ATOS_ENV_VAR
143
55aea9f5
MO
144static bool ParseCommandOutput(const char *str, uptr addr, char **out_name,
145 char **out_module, char **out_file, uptr *line,
146 uptr *start_address) {
696d846a
MO
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
55aea9f5 154 // myfunction (in library.dylib) + 15
696d846a 155 // 0xdeadbeef (in library.dylib) + 0x1fe
55aea9f5 156 // 0xdeadbeef (in library.dylib) + 15
696d846a
MO
157 // 0xdeadbeef (in library.dylib)
158 // 0xdeadbeef
159
10189819
MO
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);
696d846a
MO
165 InternalFree(trim);
166 return false;
167 }
168
55aea9f5
MO
169 if (internal_strncmp(symbol_name, "0x", 2) != 0)
170 *out_name = symbol_name;
696d846a 171 else
55aea9f5
MO
172 InternalFree(symbol_name);
173 rest = ExtractTokenUpToDelimiter(rest, ") ", out_module);
696d846a
MO
174
175 if (rest[0] == '(') {
55aea9f5
MO
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;
696d846a
MO
188 }
189
190 InternalFree(trim);
191 return true;
192}
193
194AtosSymbolizer::AtosSymbolizer(const char *path, LowLevelAllocator *allocator)
3c6331c2 195 : process_(new (*allocator) AtosSymbolizerProcess(path)) {}
696d846a
MO
196
197bool AtosSymbolizer::SymbolizePC(uptr addr, SymbolizedStack *stack) {
198 if (!process_) return false;
10189819 199 if (addr == 0) return false;
696d846a
MO
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;
55aea9f5 204 uptr line;
3c6331c2 205 uptr start_address = AddressInfo::kUnknown;
55aea9f5 206 if (!ParseCommandOutput(buf, addr, &stack->info.function, &stack->info.module,
3c6331c2 207 &stack->info.file, &line, &start_address)) {
696d846a
MO
208 process_ = nullptr;
209 return false;
210 }
55aea9f5 211 stack->info.line = (int)line;
3c6331c2
ML
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 }
696d846a
MO
228 return true;
229}
230
55aea9f5
MO
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}
696d846a 244
3c6331c2
ML
245void AtosSymbolizer::LateInitialize() { process_->LateInitialize(); }
246
696d846a
MO
247} // namespace __sanitizer
248
249#endif // SANITIZER_MAC