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