mirror of
https://github.com/google/sandboxed-api.git
synced 2024-03-22 13:11:30 +08:00
Try to demangle c++ symbols when logging the stacktrace
PiperOrigin-RevId: 243612828 Change-Id: I09c748da0c119ba2024b2906802858b5b9bcfeb0
This commit is contained in:
parent
d90b2c6328
commit
af44845246
|
@ -168,7 +168,6 @@ cc_library(
|
|||
deps = [
|
||||
":bpfdisassembler",
|
||||
":comms",
|
||||
":fs",
|
||||
":namespace",
|
||||
":regs",
|
||||
":syscall",
|
||||
|
@ -271,7 +270,6 @@ cc_library(
|
|||
":violation_proto_cc",
|
||||
":forkserver",
|
||||
":forkserver_proto_cc",
|
||||
":fs",
|
||||
":global_forkserver",
|
||||
":ipc",
|
||||
":limits",
|
||||
|
@ -281,7 +279,6 @@ cc_library(
|
|||
":notify",
|
||||
":policy",
|
||||
":regs",
|
||||
":reporter",
|
||||
":result",
|
||||
":syscall",
|
||||
":util",
|
||||
|
|
|
@ -92,6 +92,8 @@ void SymbolizationWorksCommon(
|
|||
|
||||
ASSERT_THAT(result.final_status(), Eq(Result::SIGNALED));
|
||||
ASSERT_THAT(result.GetStackTrace(), HasSubstr("CrashMe"));
|
||||
// Check that demangling works as well.
|
||||
ASSERT_THAT(result.GetStackTrace(), HasSubstr("CrashMe()"));
|
||||
}
|
||||
|
||||
TEST(StackTraceTest, SymbolizationWorksNonSandboxedLibunwind) {
|
||||
|
|
|
@ -14,6 +14,8 @@
|
|||
|
||||
#include "sandboxed_api/sandbox2/unwind/unwind.h"
|
||||
|
||||
#include <cxxabi.h>
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
|
@ -34,6 +36,16 @@
|
|||
namespace sandbox2 {
|
||||
namespace {
|
||||
|
||||
std::string DemangleSymbol(const std::string& maybe_mangled) {
|
||||
int status;
|
||||
std::unique_ptr<char, std::function<void(char*)>> symbol = {
|
||||
abi::__cxa_demangle(maybe_mangled.c_str(), nullptr, nullptr, &status),
|
||||
free};
|
||||
if (symbol && status == 0) {
|
||||
return symbol.get();
|
||||
}
|
||||
return maybe_mangled;
|
||||
}
|
||||
std::string GetSymbolAt(const std::map<uint64_t, std::string>& addr_to_symbol,
|
||||
uint64_t addr) {
|
||||
auto entry_for_next_symbol = addr_to_symbol.lower_bound(addr);
|
||||
|
@ -41,13 +53,14 @@ std::string GetSymbolAt(const std::map<uint64_t, std::string>& addr_to_symbol,
|
|||
entry_for_next_symbol != addr_to_symbol.begin()) {
|
||||
// Matches the addr exactly:
|
||||
if (entry_for_next_symbol->first == addr) {
|
||||
return entry_for_next_symbol->second;
|
||||
return DemangleSymbol(entry_for_next_symbol->second);
|
||||
}
|
||||
|
||||
// Might be inside a function, return symbol+offset;
|
||||
const auto entry_for_previous_symbol = --entry_for_next_symbol;
|
||||
if (!entry_for_previous_symbol->second.empty()) {
|
||||
return absl::StrCat(entry_for_previous_symbol->second, "+0x",
|
||||
return absl::StrCat(DemangleSymbol(entry_for_previous_symbol->second),
|
||||
"+0x",
|
||||
absl::Hex(addr - entry_for_previous_symbol->first));
|
||||
}
|
||||
}
|
||||
|
@ -166,7 +179,7 @@ void RunLibUnwindAndSymbolizer(pid_t pid, std::string* stack_trace_out,
|
|||
addr_to_symbol[entry.end] = "";
|
||||
}
|
||||
|
||||
if (!entry.path.empty() && entry.is_executable) {
|
||||
if (!entry.path.empty() && entry.path != "[vdso]" && entry.is_executable) {
|
||||
auto elf_or = ElfFile::ParseFromFile(entry.path, ElfFile::kLoadSymbols);
|
||||
if (!elf_or.ok()) {
|
||||
SAPI_RAW_LOG(WARNING, "Could not load symbols for %s: %s", entry.path,
|
||||
|
|
|
@ -76,7 +76,6 @@ cc_test(
|
|||
name = "status_test",
|
||||
srcs = ["status_test.cc"],
|
||||
deps = [
|
||||
":status",
|
||||
":status_matchers",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
|
|
Loading…
Reference in New Issue
Block a user