1 //===-- sanitizer_symbolizer_posix_libcdep.cpp ----------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file is shared between AddressSanitizer and ThreadSanitizer
10 // run-time libraries.
11 // POSIX-specific implementation of symbolizer parts.
12 //===----------------------------------------------------------------------===//
14 #include "sanitizer_platform.h"
15 #include "sanitizer_symbolizer_markup.h"
17 # include <dlfcn.h> // for dlsym()
21 # include <sys/wait.h>
24 # include "sanitizer_allocator_internal.h"
25 # include "sanitizer_common.h"
26 # include "sanitizer_file.h"
27 # include "sanitizer_flags.h"
28 # include "sanitizer_internal_defs.h"
29 # include "sanitizer_linux.h"
30 # include "sanitizer_placement_new.h"
31 # include "sanitizer_posix.h"
32 # include "sanitizer_procmaps.h"
33 # include "sanitizer_symbolizer_internal.h"
34 # include "sanitizer_symbolizer_libbacktrace.h"
35 # include "sanitizer_symbolizer_mac.h"
37 // C++ demangling function, as required by Itanium C++ ABI. This is weak,
38 // because we do not require a C++ ABI library to be linked to a program
39 // using sanitizers; if it's not present, we'll just use the mangled name.
40 namespace __cxxabiv1
{
41 extern "C" SANITIZER_WEAK_ATTRIBUTE
42 char *__cxa_demangle(const char *mangled
, char *buffer
,
43 size_t *length
, int *status
);
46 namespace __sanitizer
{
48 // Attempts to demangle the name via __cxa_demangle from __cxxabiv1.
49 const char *DemangleCXXABI(const char *name
) {
50 // FIXME: __cxa_demangle aggressively insists on allocating memory.
51 // There's not much we can do about that, short of providing our
52 // own demangler (libc++abi's implementation could be adapted so that
53 // it does not allocate). For now, we just call it anyway, and we leak
54 // the returned value.
55 if (&__cxxabiv1::__cxa_demangle
)
56 if (const char *demangled_name
=
57 __cxxabiv1::__cxa_demangle(name
, 0, 0, 0))
58 return demangled_name
;
63 // As of now, there are no headers for the Swift runtime. Once they are
64 // present, we will weakly link since we do not require Swift runtime to be
66 typedef char *(*swift_demangle_ft
)(const char *mangledName
,
67 size_t mangledNameLength
, char *outputBuffer
,
68 size_t *outputBufferSize
, uint32_t flags
);
69 static swift_demangle_ft swift_demangle_f
;
71 // This must not happen lazily at symbolication time, because dlsym uses
72 // malloc and thread-local storage, which is not a good thing to do during
74 static void InitializeSwiftDemangler() {
75 swift_demangle_f
= (swift_demangle_ft
)dlsym(RTLD_DEFAULT
, "swift_demangle");
78 // Attempts to demangle a Swift name. The demangler will return nullptr if a
79 // non-Swift name is passed in.
80 const char *DemangleSwift(const char *name
) {
82 return swift_demangle_f(name
, internal_strlen(name
), 0, 0, 0);
87 const char *DemangleSwiftAndCXX(const char *name
) {
88 if (!name
) return nullptr;
89 if (const char *swift_demangled_name
= DemangleSwift(name
))
90 return swift_demangled_name
;
91 return DemangleCXXABI(name
);
94 static bool CreateTwoHighNumberedPipes(int *infd_
, int *outfd_
) {
97 // The client program may close its stdin and/or stdout and/or stderr
98 // thus allowing socketpair to reuse file descriptors 0, 1 or 2.
99 // In this case the communication between the forked processes may be
100 // broken if either the parent or the child tries to close or duplicate
101 // these descriptors. The loop below produces two pairs of file
102 // descriptors, each greater than 2 (stderr).
104 for (int i
= 0; i
< 5; i
++) {
105 if (pipe(sock_pair
[i
]) == -1) {
106 for (int j
= 0; j
< i
; j
++) {
107 internal_close(sock_pair
[j
][0]);
108 internal_close(sock_pair
[j
][1]);
111 } else if (sock_pair
[i
][0] > 2 && sock_pair
[i
][1] > 2) {
115 outfd
= sock_pair
[i
];
116 for (int j
= 0; j
< i
; j
++) {
117 if (sock_pair
[j
] == infd
) continue;
118 internal_close(sock_pair
[j
][0]);
119 internal_close(sock_pair
[j
][1]);
129 outfd_
[0] = outfd
[0];
130 outfd_
[1] = outfd
[1];
134 bool SymbolizerProcess::StartSymbolizerSubprocess() {
135 if (!FileExists(path_
)) {
136 if (!reported_invalid_path_
) {
137 Report("WARNING: invalid path to external symbolizer!\n");
138 reported_invalid_path_
= true;
143 const char *argv
[kArgVMax
];
144 GetArgV(path_
, argv
);
147 // Report how symbolizer is being launched for debugging purposes.
148 if (Verbosity() >= 3) {
149 // Only use `Report` for first line so subsequent prints don't get prefixed
151 Report("Launching Symbolizer process: ");
152 for (unsigned index
= 0; index
< kArgVMax
&& argv
[index
]; ++index
)
153 Printf("%s ", argv
[index
]);
157 if (use_posix_spawn_
) {
159 fd_t fd
= internal_spawn(argv
, const_cast<const char **>(GetEnvP()), &pid
);
160 if (fd
== kInvalidFd
) {
161 Report("WARNING: failed to spawn external symbolizer (errno: %d)\n",
168 #else // SANITIZER_APPLE
170 #endif // SANITIZER_APPLE
172 fd_t infd
[2] = {}, outfd
[2] = {};
173 if (!CreateTwoHighNumberedPipes(infd
, outfd
)) {
174 Report("WARNING: Can't create a socket pair to start "
175 "external symbolizer (errno: %d)\n", errno
);
179 pid
= StartSubprocess(path_
, argv
, GetEnvP(), /* stdin */ outfd
[0],
180 /* stdout */ infd
[1]);
182 internal_close(infd
[0]);
183 internal_close(outfd
[1]);
188 output_fd_
= outfd
[1];
193 // Check that symbolizer subprocess started successfully.
194 SleepForMillis(kSymbolizerStartupTimeMillis
);
195 if (!IsProcessRunning(pid
)) {
196 // Either waitpid failed, or child has already exited.
197 Report("WARNING: external symbolizer didn't start up correctly!\n");
204 class Addr2LineProcess final
: public SymbolizerProcess
{
206 Addr2LineProcess(const char *path
, const char *module_name
)
207 : SymbolizerProcess(path
), module_name_(internal_strdup(module_name
)) {}
209 const char *module_name() const { return module_name_
; }
212 void GetArgV(const char *path_to_binary
,
213 const char *(&argv
)[kArgVMax
]) const override
{
215 argv
[i
++] = path_to_binary
;
216 if (common_flags()->demangle
)
218 if (common_flags()->symbolize_inline_frames
)
221 argv
[i
++] = module_name_
;
223 CHECK_LE(i
, kArgVMax
);
226 bool ReachedEndOfOutput(const char *buffer
, uptr length
) const override
;
228 bool ReadFromSymbolizer() override
{
229 if (!SymbolizerProcess::ReadFromSymbolizer())
231 auto &buff
= GetBuff();
232 // We should cut out output_terminator_ at the end of given buffer,
233 // appended by addr2line to mark the end of its meaningful output.
234 // We cannot scan buffer from it's beginning, because it is legal for it
235 // to start with output_terminator_ in case given offset is invalid. So,
236 // scanning from second character.
237 char *garbage
= internal_strstr(buff
.data() + 1, output_terminator_
);
238 // This should never be NULL since buffer must end up with
239 // output_terminator_.
243 uintptr_t new_size
= garbage
- buff
.data();
244 GetBuff().resize(new_size
);
245 GetBuff().push_back('\0');
249 const char *module_name_
; // Owned, leaked.
250 static const char output_terminator_
[];
253 const char Addr2LineProcess::output_terminator_
[] = "??\n??:0\n";
255 bool Addr2LineProcess::ReachedEndOfOutput(const char *buffer
,
257 const size_t kTerminatorLen
= sizeof(output_terminator_
) - 1;
258 // Skip, if we read just kTerminatorLen bytes, because Addr2Line output
259 // should consist at least of two pairs of lines:
260 // 1. First one, corresponding to given offset to be symbolized
261 // (may be equal to output_terminator_, if offset is not valid).
262 // 2. Second one for output_terminator_, itself to mark the end of output.
263 if (length
<= kTerminatorLen
) return false;
264 // Addr2Line output should end up with output_terminator_.
265 return !internal_memcmp(buffer
+ length
- kTerminatorLen
,
266 output_terminator_
, kTerminatorLen
);
269 class Addr2LinePool final
: public SymbolizerTool
{
271 explicit Addr2LinePool(const char *addr2line_path
,
272 LowLevelAllocator
*allocator
)
273 : addr2line_path_(addr2line_path
), allocator_(allocator
) {
274 addr2line_pool_
.reserve(16);
277 bool SymbolizePC(uptr addr
, SymbolizedStack
*stack
) override
{
278 if (const char *buf
=
279 SendCommand(stack
->info
.module
, stack
->info
.module_offset
)) {
280 ParseSymbolizePCOutput(buf
, stack
);
286 bool SymbolizeData(uptr addr
, DataInfo
*info
) override
{
291 const char *SendCommand(const char *module_name
, uptr module_offset
) {
292 Addr2LineProcess
*addr2line
= 0;
293 for (uptr i
= 0; i
< addr2line_pool_
.size(); ++i
) {
295 internal_strcmp(module_name
, addr2line_pool_
[i
]->module_name())) {
296 addr2line
= addr2line_pool_
[i
];
302 new(*allocator_
) Addr2LineProcess(addr2line_path_
, module_name
);
303 addr2line_pool_
.push_back(addr2line
);
305 CHECK_EQ(0, internal_strcmp(module_name
, addr2line
->module_name()));
306 char buffer
[kBufferSize
];
307 internal_snprintf(buffer
, kBufferSize
, "0x%zx\n0x%zx\n",
308 module_offset
, dummy_address_
);
309 return addr2line
->SendCommand(buffer
);
312 static const uptr kBufferSize
= 64;
313 const char *addr2line_path_
;
314 LowLevelAllocator
*allocator_
;
315 InternalMmapVector
<Addr2LineProcess
*> addr2line_pool_
;
316 static const uptr dummy_address_
=
317 FIRST_32_SECOND_64(UINT32_MAX
, UINT64_MAX
);
320 # if SANITIZER_SUPPORTS_WEAK_HOOKS
322 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
bool
323 __sanitizer_symbolize_code(const char *ModuleName
, u64 ModuleOffset
,
324 char *Buffer
, int MaxLength
);
325 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
bool
326 __sanitizer_symbolize_data(const char *ModuleName
, u64 ModuleOffset
,
327 char *Buffer
, int MaxLength
);
328 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
bool
329 __sanitizer_symbolize_frame(const char *ModuleName
, u64 ModuleOffset
,
330 char *Buffer
, int MaxLength
);
331 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
void
332 __sanitizer_symbolize_flush();
333 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
bool
334 __sanitizer_symbolize_demangle(const char *Name
, char *Buffer
, int MaxLength
);
335 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
bool
336 __sanitizer_symbolize_set_demangle(bool Demangle
);
337 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
bool
338 __sanitizer_symbolize_set_inline_frames(bool InlineFrames
);
341 class InternalSymbolizer final
: public SymbolizerTool
{
343 static InternalSymbolizer
*get(LowLevelAllocator
*alloc
) {
344 // These one is the most used one, so we will use it to detect a presence of
345 // internal symbolizer.
346 if (&__sanitizer_symbolize_code
== nullptr)
348 CHECK(__sanitizer_symbolize_set_demangle(common_flags()->demangle
));
349 CHECK(__sanitizer_symbolize_set_inline_frames(
350 common_flags()->symbolize_inline_frames
));
351 return new (*alloc
) InternalSymbolizer();
354 bool SymbolizePC(uptr addr
, SymbolizedStack
*stack
) override
{
355 bool result
= __sanitizer_symbolize_code(
356 stack
->info
.module
, stack
->info
.module_offset
, buffer_
, sizeof(buffer_
));
358 ParseSymbolizePCOutput(buffer_
, stack
);
362 bool SymbolizeData(uptr addr
, DataInfo
*info
) override
{
363 bool result
= __sanitizer_symbolize_data(info
->module
, info
->module_offset
,
364 buffer_
, sizeof(buffer_
));
366 ParseSymbolizeDataOutput(buffer_
, info
);
367 info
->start
+= (addr
- info
->module_offset
); // Add the base address.
372 bool SymbolizeFrame(uptr addr
, FrameInfo
*info
) override
{
373 bool result
= __sanitizer_symbolize_frame(info
->module
, info
->module_offset
,
374 buffer_
, sizeof(buffer_
));
376 ParseSymbolizeFrameOutput(buffer_
, &info
->locals
);
380 void Flush() override
{ __sanitizer_symbolize_flush(); }
382 const char *Demangle(const char *name
) override
{
383 if (__sanitizer_symbolize_demangle(name
, buffer_
, sizeof(buffer_
))) {
384 char *res_buff
= nullptr;
385 ExtractToken(buffer_
, "", &res_buff
);
392 InternalSymbolizer() {}
394 char buffer_
[16 * 1024];
396 # else // SANITIZER_SUPPORTS_WEAK_HOOKS
398 class InternalSymbolizer final
: public SymbolizerTool
{
400 static InternalSymbolizer
*get(LowLevelAllocator
*alloc
) { return 0; }
403 # endif // SANITIZER_SUPPORTS_WEAK_HOOKS
405 const char *Symbolizer::PlatformDemangle(const char *name
) {
406 return DemangleSwiftAndCXX(name
);
409 static SymbolizerTool
*ChooseExternalSymbolizer(LowLevelAllocator
*allocator
) {
410 const char *path
= common_flags()->external_symbolizer_path
;
412 if (path
&& internal_strchr(path
, '%')) {
413 char *new_path
= (char *)InternalAlloc(kMaxPathLength
);
414 SubstituteForFlagValue(path
, new_path
, kMaxPathLength
);
418 const char *binary_name
= path
? StripModuleName(path
) : "";
419 static const char kLLVMSymbolizerPrefix
[] = "llvm-symbolizer";
420 if (path
&& path
[0] == '\0') {
421 VReport(2, "External symbolizer is explicitly disabled.\n");
423 } else if (!internal_strncmp(binary_name
, kLLVMSymbolizerPrefix
,
424 internal_strlen(kLLVMSymbolizerPrefix
))) {
425 VReport(2, "Using llvm-symbolizer at user-specified path: %s\n", path
);
426 return new(*allocator
) LLVMSymbolizer(path
, allocator
);
427 } else if (!internal_strcmp(binary_name
, "atos")) {
429 VReport(2, "Using atos at user-specified path: %s\n", path
);
430 return new(*allocator
) AtosSymbolizer(path
, allocator
);
431 #else // SANITIZER_APPLE
432 Report("ERROR: Using `atos` is only supported on Darwin.\n");
434 #endif // SANITIZER_APPLE
435 } else if (!internal_strcmp(binary_name
, "addr2line")) {
436 VReport(2, "Using addr2line at user-specified path: %s\n", path
);
437 return new(*allocator
) Addr2LinePool(path
, allocator
);
439 Report("ERROR: External symbolizer path is set to '%s' which isn't "
440 "a known symbolizer. Please set the path to the llvm-symbolizer "
441 "binary or other known tool.\n", path
);
445 // Otherwise symbolizer program is unknown, let's search $PATH
446 CHECK(path
== nullptr);
448 if (const char *found_path
= FindPathToBinary("atos")) {
449 VReport(2, "Using atos found at: %s\n", found_path
);
450 return new(*allocator
) AtosSymbolizer(found_path
, allocator
);
452 #endif // SANITIZER_APPLE
453 if (const char *found_path
= FindPathToBinary("llvm-symbolizer")) {
454 VReport(2, "Using llvm-symbolizer found at: %s\n", found_path
);
455 return new(*allocator
) LLVMSymbolizer(found_path
, allocator
);
457 if (common_flags()->allow_addr2line
) {
458 if (const char *found_path
= FindPathToBinary("addr2line")) {
459 VReport(2, "Using addr2line found at: %s\n", found_path
);
460 return new(*allocator
) Addr2LinePool(found_path
, allocator
);
466 static void ChooseSymbolizerTools(IntrusiveList
<SymbolizerTool
> *list
,
467 LowLevelAllocator
*allocator
) {
468 if (!common_flags()->symbolize
) {
469 VReport(2, "Symbolizer is disabled.\n");
472 if (common_flags()->enable_symbolizer_markup
) {
473 VReport(2, "Using symbolizer markup");
474 SymbolizerTool
*tool
= new (*allocator
) MarkupSymbolizerTool();
476 list
->push_back(tool
);
478 if (IsAllocatorOutOfMemory()) {
479 VReport(2, "Cannot use internal symbolizer: out of memory\n");
480 } else if (SymbolizerTool
*tool
= InternalSymbolizer::get(allocator
)) {
481 VReport(2, "Using internal symbolizer.\n");
482 list
->push_back(tool
);
485 if (SymbolizerTool
*tool
= LibbacktraceSymbolizer::get(allocator
)) {
486 VReport(2, "Using libbacktrace symbolizer.\n");
487 list
->push_back(tool
);
491 if (SymbolizerTool
*tool
= ChooseExternalSymbolizer(allocator
)) {
492 list
->push_back(tool
);
496 VReport(2, "Using dladdr symbolizer.\n");
497 list
->push_back(new(*allocator
) DlAddrSymbolizer());
498 #endif // SANITIZER_APPLE
501 Symbolizer
*Symbolizer::PlatformInit() {
502 IntrusiveList
<SymbolizerTool
> list
;
504 ChooseSymbolizerTools(&list
, &symbolizer_allocator_
);
505 return new(symbolizer_allocator_
) Symbolizer(list
);
508 void Symbolizer::LateInitialize() {
509 Symbolizer::GetOrInit();
510 InitializeSwiftDemangler();
513 } // namespace __sanitizer
515 #endif // SANITIZER_POSIX