Truly independent web browser
LibCore: Recover binary identity from unresolved crash stacks
Andreas Kling2a1792601a40
diff --git a/Libraries/LibCore/CrashHandlerPOSIX.cpp b/Libraries/LibCore/CrashHandlerPOSIX.cppindex 3192159261f..2498a024d58 100644--- a/Libraries/LibCore/CrashHandlerPOSIX.cpp+++ b/Libraries/LibCore/CrashHandlerPOSIX.cpp@@ -141,12 +141,6 @@ static ReportFrame frame_for_address(FlatPtr address)return frame;}-static bool write_frame(FlatPtr address, size_t index)-{- auto frame = frame_for_address(address);- return write_record(&frame, sizeof(frame), sizeof(ReportHeader) + index * sizeof(frame));-}-static void capture_assertion_backtrace(ReadonlySpan<AK::AssertionBacktraceFrame> frames){if (!s_owns_assertion || frames.is_empty())@@ -198,9 +192,12 @@ static u32 capture_native_stack(void* context)#endifu32 frame_count = 0;+ [[maybe_unused]] bool has_binary_identity = false;for (; frame_count < maximum_frames && program_counter;) {- if (!write_frame(program_counter, frame_count))+ auto frame = frame_for_address(program_counter);+ if (!write_record(&frame, sizeof(frame), sizeof(ReportHeader) + frame_count * sizeof(frame)))break;+ has_binary_identity |= frame.binary.size != 0;++frame_count;FlatPtr saved_frame[2];if (!frame_pointer || frame_pointer % alignof(FlatPtr) || !read_stack_frame(frame_pointer, saved_frame))@@ -220,21 +217,23 @@ static u32 capture_native_stack(void* context)}#if defined(AK_OS_LINUX)- // Without frame pointers, retain bounded candidate code addresses from the stack.- if (frame_count < 2 && stack_pointer && stack_pointer % alignof(FlatPtr) == 0) {+ // Without a usable frame chain, retain bounded candidate code addresses from the stack.+ if ((frame_count < 2 || !has_binary_identity) && stack_pointer && stack_pointer % alignof(FlatPtr) == 0) {Array<FlatPtr, 1024> stack_words;auto bytes_read = pread(s_memory_fd, stack_words.data(), sizeof(stack_words), stack_pointer);if (bytes_read >= static_cast<ssize_t>(sizeof(FlatPtr))) {auto words_read = static_cast<size_t>(bytes_read) / sizeof(FlatPtr);for (auto address : stack_words.span().slice(0, words_read)) {- if (frame_count == maximum_frames)- break;auto frame = frame_for_address(address);if (frame.binary.size == 0)continue;- if (!write_record(&frame, sizeof(frame), sizeof(ReportHeader) + frame_count * sizeof(frame)))+ // A full chain of unavailable frames must still leave room for binary identity.+ auto index = min(static_cast<size_t>(frame_count), maximum_frames - 1);+ if (!write_record(&frame, sizeof(frame), sizeof(ReportHeader) + index * sizeof(frame)))+ break;+ frame_count = index + 1;+ if (frame_count == maximum_frames)break;- ++frame_count;}}}diff --git a/Tests/LibWebView/CMakeLists.txt b/Tests/LibWebView/CMakeLists.txtindex f78ff1fe15c..696f4bf7589 100644--- a/Tests/LibWebView/CMakeLists.txt+++ b/Tests/LibWebView/CMakeLists.txt@@ -80,6 +80,13 @@ if (TARGET TestCrashReport AND cpptrace_FOUND AND LADYBIRD_ENABLE_CPPTRACE)target_compile_definitions(TestCrashReport PRIVATE TEST_HAS_CPPTRACE=1)endif()+if (TARGET TestCrashReport AND LINUX AND CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")+ add_library(CrashReportLateLibrary SHARED CrashReportLateLibrary.cpp)+ add_dependencies(TestCrashReport CrashReportLateLibrary)+ target_compile_definitions(TestCrashReport PRIVATE TEST_CRASH_REPORT_LATE_LIBRARY="$<TARGET_FILE:CrashReportLateLibrary>")+ target_link_libraries(TestCrashReport PRIVATE ${CMAKE_DL_LIBS})+endif()+if (APPLE OR LINUX)foreach(panic_strategy abort unwind)set(rust_test_library "${CMAKE_CURRENT_BINARY_DIR}/librust_crash_report_${panic_strategy}.a")diff --git a/Tests/LibWebView/CrashReportLateLibrary.cpp b/Tests/LibWebView/CrashReportLateLibrary.cppnew file mode 100644index 00000000000..af17bb5931c--- /dev/null+++ b/Tests/LibWebView/CrashReportLateLibrary.cpp@@ -0,0 +1,26 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <stddef.h>++extern "C" [[gnu::visibility("default"), gnu::naked, noreturn]] void crash_with_unresolved_frames(size_t);++// Terminate the frame chain inside this library while retaining the caller's+// return address on the stack. Each recursive call adds one unresolved frame.+void crash_with_unresolved_frames(size_t)+{+ asm volatile(+ "endbr64\n"+ "xor %ebp, %ebp\n"+ "1:\n"+ "push %rbp\n"+ "mov %rsp, %rbp\n"+ "sub $1, %rdi\n"+ "jz 2f\n"+ "call 1b\n"+ "2:\n"+ "ud2\n");+}diff --git a/Tests/LibWebView/TestCrashReport.cpp b/Tests/LibWebView/TestCrashReport.cppindex c659767f849..d941cea553e 100644--- a/Tests/LibWebView/TestCrashReport.cpp+++ b/Tests/LibWebView/TestCrashReport.cpp@@ -23,6 +23,10 @@#include <sys/wait.h>#include <unistd.h>+#if defined(TEST_CRASH_REPORT_LATE_LIBRARY)+# include <dlfcn.h>+#endif+#if defined(AK_OS_LINUX)TEST_CASE(crash_scratch_has_fixed_size_and_allows_report_writes){@@ -101,7 +105,8 @@ enum class CrashThread {Worker,};-static int crash_child(WebView::CrashReport& report, int signal, CrashThread thread = CrashThread::Main)+template<typename Crash>+static int crash_child_running(WebView::CrashReport& report, Crash crash){auto pid = fork();VERIFY(pid >= 0);@@ -109,6 +114,17 @@ static int crash_child(WebView::CrashReport& report, int signal, CrashThread thrstruct rlimit limit { 0, 0 };setrlimit(RLIMIT_CORE, &limit);MUST(Core::CrashHandler::initialize(report.fd()));+ crash();+ _exit(0);+ }+ int status;+ VERIFY(waitpid(pid, &status, 0) == pid);+ return status;+}++static int crash_child(WebView::CrashReport& report, int signal, CrashThread thread = CrashThread::Main)+{+ return crash_child_running(report, [&] {if (thread == CrashThread::Worker) {auto crash_worker = [](void* argument) -> void* {raise(*static_cast<int*>(argument));@@ -120,12 +136,39 @@ static int crash_child(WebView::CrashReport& report, int signal, CrashThread thr} else {raise(signal);}- _exit(0);+ });+}++#if defined(TEST_CRASH_REPORT_LATE_LIBRARY)+TEST_CASE(unresolved_frames_do_not_skip_stack_search)+{+ for (size_t frame_count : { 3uz, Core::CrashReportData::maximum_frames }) {+ auto report = MUST(WebView::CrashReport::create(WebView::ProcessType::Compositor));+ auto status = crash_child_running(*report, [&] {+ auto* library = dlopen(TEST_CRASH_REPORT_LATE_LIBRARY, RTLD_NOW | RTLD_LOCAL);+ VERIFY(library);+ auto crash = reinterpret_cast<void (*)(size_t)>(dlsym(library, "crash_with_unresolved_frames"));+ VERIFY(crash);+ crash(frame_count);+ });+ EXPECT(WIFSIGNALED(status));+ EXPECT_EQ(WTERMSIG(status), SIGILL);+ Core::CrashReportData::ReportHeader header;+ EXPECT_EQ(pread(report->fd(), &header, sizeof(header), 0), static_cast<ssize_t>(sizeof(header)));+ EXPECT(header.frame_count >= frame_count);+ EXPECT(header.frame_count <= Core::CrashReportData::maximum_frames);+ bool has_binary_identity = false;+ for (size_t i = 0; i < header.frame_count; ++i) {+ Core::CrashReportData::ReportFrame frame;+ EXPECT_EQ(pread(report->fd(), &frame, sizeof(frame), sizeof(header) + i * sizeof(frame)), static_cast<ssize_t>(sizeof(frame)));+ if (i < min(frame_count, Core::CrashReportData::maximum_frames - 1))+ EXPECT_EQ(frame.binary.size, 0u);+ has_binary_identity |= frame.binary.size != 0;+ }+ EXPECT(has_binary_identity);}- int status;- VERIFY(waitpid(pid, &status, 0) == pid);- return status;}+#endifTEST_CASE(capture_native_crash_without_personal_data){