Truly independent web browser
AK: Name backtrace frames in images whose debug info is unreadable
Jelle Raaijmakersf12511fe6ffd
diff --git a/AK/Assertions.cpp b/AK/Assertions.cppindex f9461d0c259..d6bd0f8c480 100644--- a/AK/Assertions.cpp+++ b/AK/Assertions.cpp@@ -115,10 +115,18 @@ void dump_backtrace(unsigned frames_to_skip, unsigned max_depth)// We should be using cpptrace for everything but android.auto stacktrace = cpptrace::generate_trace(frames_to_skip, max_depth);- // cpptrace only demangles C++ names.for (auto& frame : stacktrace.frames) {- if (auto demangled = demangle_rust_symbol({ frame.symbol.data(), frame.symbol.size() }); demangled.has_value())+ if (frame.symbol.empty()) {+ // A sandboxed process can't read the debug info that cpptrace resolves symbols from, but dladdr still names+ // what the loaded image exports or keeps in its symbol table.+ if (!frame.is_inline) {+ if (auto symbol = symbol_at_address(frame.raw_address); symbol.has_value())+ frame.symbol.assign(symbol->name.characters(), symbol->name.length());+ }+ } else if (auto demangled = demangle_rust_symbol({ frame.symbol.data(), frame.symbol.size() }); demangled.has_value()) {+ // cpptrace only demangles C++ names.frame.symbol.assign(demangled->characters(), demangled->length());+ }}if (auto callback = __atomic_load_n(&s_assertion_backtrace_callback, __ATOMIC_ACQUIRE)) {diff --git a/AK/Demangle.cpp b/AK/Demangle.cppindex 123556ceb77..c656038ca91 100644--- a/AK/Demangle.cpp+++ b/AK/Demangle.cpp@@ -10,6 +10,7 @@#ifndef AK_OS_WINDOWS# include <cxxabi.h>+# include <dlfcn.h># include <stdlib.h># include <string.h>#endif@@ -76,6 +77,22 @@ ByteString demangle(StringView name)free(demangled_name);return string;}++Optional<SymbolAtAddress> symbol_at_address(FlatPtr address)+{+ Dl_info info {};+ if (!dladdr(reinterpret_cast<void const*>(address), &info) || !info.dli_sname)+ return {};+ return SymbolAtAddress { demangle({ info.dli_sname, strlen(info.dli_sname) }), address - reinterpret_cast<FlatPtr>(info.dli_saddr) };+}++#else++Optional<SymbolAtAddress> symbol_at_address(FlatPtr)+{+ return {};+}+#endif}diff --git a/AK/Demangle.h b/AK/Demangle.hindex bea577d2e18..e2c8baecb65 100644--- a/AK/Demangle.h+++ b/AK/Demangle.h@@ -16,6 +16,14 @@ ByteString demangle(StringView name);Optional<ByteString> demangle_rust_symbol(StringView symbol);ByteString demangle_backtrace_symbols_line(StringView line);+struct SymbolAtAddress {+ ByteString name;+ FlatPtr offset;+};++// Names the function that a loaded image exports or keeps in its symbol table, without reading any file.+Optional<SymbolAtAddress> symbol_at_address(FlatPtr address);+using RustSymbolDemangler = bool (*)(char const* name, size_t name_length, void (*append)(void* context, char const*, size_t), void* context);}@@ -26,4 +34,6 @@ extern "C" void ladybird_set_rust_symbol_demangler(AK::RustSymbolDemangler);using AK::demangle;using AK::demangle_backtrace_symbols_line;using AK::demangle_rust_symbol;+using AK::symbol_at_address;+using AK::SymbolAtAddress;#endifdiff --git a/Libraries/LibWebView/CrashReport.cpp b/Libraries/LibWebView/CrashReport.cppindex e46c8e7ee49..c94d7adf901 100644--- a/Libraries/LibWebView/CrashReport.cpp+++ b/Libraries/LibWebView/CrashReport.cpp@@ -205,6 +205,8 @@ ErrorOr<void> CrashReport::save(int wait_status, ByteString const& path, Optionabuilder.appendff("#{} unavailable\n", frame_count);continue;}+ if (!frame.description_length && frame.binary.size)+ symbolicate_frame(frame);if (frame.binary.size)builder.appendff("#{} {} {:#x}", frame_count, encode_hex(frame.binary.bytes.span().trim(frame.binary.size)), frame.address);else@@ -213,10 +215,6 @@ ErrorOr<void> CrashReport::save(int wait_status, ByteString const& path, Optionabuilder.append(' ');for (u32 i = 0; i < frame.description_length; ++i)builder.append(is_ascii_printable(frame.description[i]) ? frame.description[i] : '?');- } else if (frame.binary.size) {- auto symbol = symbolicate_frame(frame);- if (!symbol.is_empty())- builder.appendff(" {}", symbol);}builder.append('\n');}@@ -245,9 +243,8 @@ ErrorOr<void> CrashReport::record_build_description(StringView) { return {}; }#endif#if !defined(AK_OS_MACOS) && !defined(AK_OS_LINUX)-ByteString CrashReport::symbolicate_frame(ReportFrame const&)+void CrashReport::symbolicate_frame(ReportFrame&){- return {};}bool CrashReport::is_supported()diff --git a/Libraries/LibWebView/CrashReport.h b/Libraries/LibWebView/CrashReport.hindex 1b533bcd5be..6e8138d98be 100644--- a/Libraries/LibWebView/CrashReport.h+++ b/Libraries/LibWebView/CrashReport.h@@ -50,7 +50,7 @@ public:}private:- static ByteString symbolicate_frame(Core::CrashReportData::ReportFrame const&);+ static void symbolicate_frame(Core::CrashReportData::ReportFrame&);NonnullOwnPtr<Core::File> m_file;[[maybe_unused]] ProcessType m_process_type;diff --git a/Libraries/LibWebView/CrashReportPOSIX.cpp b/Libraries/LibWebView/CrashReportPOSIX.cppindex 251f0327faf..30fc5549039 100644--- a/Libraries/LibWebView/CrashReportPOSIX.cpp+++ b/Libraries/LibWebView/CrashReportPOSIX.cpp@@ -11,52 +11,41 @@# include <LibCore/CrashReportDataLinux.h>#endif#include <LibWebView/CrashReport.h>-#include <dlfcn.h>namespace WebView {using namespace Core::CrashReportData;-static ByteString symbolicate_image(ReportFrame const& frame, Image const& image)+static Optional<SymbolAtAddress> symbolicate_image(ReportFrame const& frame, Image const& image){// Match binary identities before translating between processes' ASLR slides.if (frame.binary.size != image.binary.size || frame.binary.bytes != image.binary.bytes)return {};if (frame.address < image.start - image.relocation || frame.address >= image.end - image.relocation)return {};- auto address = frame.address + image.relocation;-- Dl_info info_for_address {};- if (!dladdr(reinterpret_cast<void const*>(address), &info_for_address) || !info_for_address.dli_sname)- return {};- auto symbol = demangle(StringView { info_for_address.dli_sname, strlen(info_for_address.dli_sname) });- if (sanitize_backtrace_symbol(symbol).is_empty())- return {};- return ByteString::formatted("{} + {:#x}", symbol, address - reinterpret_cast<FlatPtr>(info_for_address.dli_saddr));+ return symbol_at_address(frame.address + image.relocation);}-ByteString CrashReport::symbolicate_frame(ReportFrame const& frame)+void CrashReport::symbolicate_frame(ReportFrame& frame){+ Optional<SymbolAtAddress> symbol;#if defined(AK_OS_MACOS)- for (u32 i = 0; i < _dyld_image_count(); ++i) {- auto symbol = symbolicate_image(frame, image_at_index(i));- if (!symbol.is_empty())- return symbol;- }- return {};+ for (u32 i = 0; i < _dyld_image_count() && !symbol.has_value(); ++i)+ symbol = symbolicate_image(frame, image_at_index(i));#elsestruct Context {ReportFrame const& frame;- ByteString symbol;- } context { frame, {} };+ Optional<SymbolAtAddress>& symbol;+ } context { frame, symbol };dl_iterate_phdr([](dl_phdr_info* info, size_t, void* opaque_context) {auto& context = *static_cast<Context*>(opaque_context);context.symbol = symbolicate_image(context.frame, image_from_phdr_info(*info));- return context.symbol.is_empty() ? 0 : 1;+ return context.symbol.has_value() ? 1 : 0;},&context);- return context.symbol;#endif+ if (symbol.has_value())+ describe_backtrace_frame(frame, { 0, ByteString::formatted("{} + {:#x}", symbol->name, symbol->offset), {}, 0, 0, false });}bool CrashReport::is_supported()