Truly independent web browser
AK+LibCore: Keep caught Rust panics out of crash reports
Jelle Raaijmakers897c72fd16ef
diff --git a/AK/AssertionFailure.h b/AK/AssertionFailure.hindex 096cba21b99..75b296ab2ef 100644--- a/AK/AssertionFailure.h+++ b/AK/AssertionFailure.h@@ -47,4 +47,6 @@ char const* current_rust_panic_message();}extern "C" void ladybird_rust_panic(char const*, size_t, char const*, size_t, u32, u32, bool (*)());+extern "C" void ladybird_rust_panic_hook_returned();+extern "C" void ladybird_rust_panic_may_abort();extern "C" void ladybird_rust_panic_will_abort();diff --git a/AK/Assertions.cpp b/AK/Assertions.cppindex f9631d54666..f7079b0dd46 100644--- a/AK/Assertions.cpp+++ b/AK/Assertions.cpp@@ -47,9 +47,15 @@ static AK::AssertionBacktraceCallback s_assertion_backtrace_callback;static thread_local Array<char, 8193> s_rust_panic_message;static thread_local bool (*s_rust_is_panicking)();+static thread_local u32 s_rust_panic_hook_depth;+static thread_local bool s_rust_panic_backtrace_captured;+#if defined(AK_HAS_CPPTRACE)+static thread_local bool s_capturing_backtrace_quietly;+#endifvoid ladybird_rust_panic(char const* message, size_t message_length, char const* filename, size_t filename_length, u32 line, u32 column, bool (*is_panicking)()){+ ++s_rust_panic_hook_depth;__atomic_store_n(&s_rust_is_panicking, nullptr, __ATOMIC_RELEASE);size_t length = 0;auto append = [&](StringView text) {@@ -88,15 +94,30 @@ char const* AK::current_rust_panic_message()return nullptr;}-void ladybird_rust_panic_will_abort()+void ladybird_rust_panic_hook_returned(){- // The panic hooks of the process's Rust crates chain into each other, and each of them that knows the panic is- // fatal asks for the backtrace.- static thread_local bool s_backtrace_captured;- if (s_backtrace_captured)+ if (s_rust_panic_hook_depth > 0 && --s_rust_panic_hook_depth == 0)+ s_rust_panic_backtrace_captured = false;+}++void ladybird_rust_panic_may_abort()+{+ // The panic hooks of the process's Rust crates chain into each other, so capture the backtrace once per run of that+ // chain. Code built with unwinding may still catch the panic, so only hand the backtrace to the crash handler,+ // which uses it if the thread is still panicking when it crashes.+ if (s_rust_panic_backtrace_captured)return;- s_backtrace_captured = true;+ s_rust_panic_backtrace_captured = true;+#if defined(AK_HAS_CPPTRACE)+ s_capturing_backtrace_quietly = true;+ dump_backtrace(1, 100);+ s_capturing_backtrace_quietly = false;+#endif+}++void ladybird_rust_panic_will_abort()+{if (__atomic_load_n(&s_rust_is_panicking, __ATOMIC_ACQUIRE)) {if (auto callback = __atomic_load_n(&s_assertion_failure_callback, __ATOMIC_ACQUIRE))callback(AK::AssertionFailureKind::RustPanic, s_rust_panic_message.data());@@ -156,6 +177,8 @@ void dump_backtrace(unsigned frames_to_skip, unsigned max_depth)}callback(frames.span().trim(count));}+ if (s_capturing_backtrace_quietly)+ return;auto* var = getenv("LADYBIRD_BACKTRACE_SNIPPETS");bool print_snippets = var && strnlen(var, 1) > 0;static NeverDestroyed<cpptrace::formatter> formatter { cpptrace::formatter {}.snippets(print_snippets) };diff --git a/Libraries/LibCore/CrashHandlerPOSIX.cpp b/Libraries/LibCore/CrashHandlerPOSIX.cppindex 3545ab6681b..073bac31117 100644--- a/Libraries/LibCore/CrashHandlerPOSIX.cpp+++ b/Libraries/LibCore/CrashHandlerPOSIX.cpp@@ -38,7 +38,10 @@ static int s_assertion_state;static Assertion s_assertion;static int s_backtrace_state;static u32 s_backtrace_frame_count;+static u32 s_backtrace_generation;static thread_local bool s_owns_assertion;+static thread_local u32 s_captured_backtrace_generation;+static thread_local bool s_writing_backtrace;alignas(16) static Array<u8, 64 * 1024> s_signal_stack;#if defined(AK_OS_MACOS)@@ -143,23 +146,40 @@ static ReportFrame frame_for_address(FlatPtr address)static void capture_assertion_backtrace(ReadonlySpan<AK::AssertionBacktraceFrame> frames){- if (!s_owns_assertion || frames.is_empty())+ // Without an assertion of its own, a thread captures the backtrace of a Rust panic that may still turn out to be+ // caught. That backtrace stays provisional (state 3) and gives way to any later one.+ bool provisional = !s_owns_assertion;+ if (frames.is_empty() || (provisional && !AK::current_rust_panic_message()))return;int expected = 0;- if (!__atomic_compare_exchange_n(&s_backtrace_state, &expected, 1, false, __ATOMIC_RELAXED, __ATOMIC_RELAXED))- return;+ if (!__atomic_compare_exchange_n(&s_backtrace_state, &expected, 1, false, __ATOMIC_RELAXED, __ATOMIC_RELAXED)) {+ expected = 3;+ if (!__atomic_compare_exchange_n(&s_backtrace_state, &expected, 1, false, __ATOMIC_RELAXED, __ATOMIC_RELAXED))+ return;+ }+ s_writing_backtrace = true;auto count = min(frames.size(), maximum_frames);for (size_t i = 0; i < count; ++i) {auto frame = frame_for_address(frames[i].address);if (frame.binary.size)describe_backtrace_frame(frame, frames[i]);- if (!write_record(&frame, sizeof(frame), sizeof(ReportHeader) + i * sizeof(frame)))+ if (!write_record(&frame, sizeof(frame), sizeof(ReportHeader) + i * sizeof(frame))) {+ __atomic_store_n(&s_backtrace_state, 0, __ATOMIC_RELEASE);+ s_writing_backtrace = false;return;+ }}s_backtrace_frame_count = count;+ if (provisional) {+ s_captured_backtrace_generation = __atomic_add_fetch(&s_backtrace_generation, 1, __ATOMIC_RELAXED);+ __atomic_store_n(&s_backtrace_state, 3, __ATOMIC_RELEASE);+ s_writing_backtrace = false;+ return;+ }write_record(&s_backtrace_frame_count, sizeof(s_backtrace_frame_count), offsetof(ReportHeader, frame_count));// Only publish a complete trace. Signal capture remains available if resolution fails.__atomic_store_n(&s_backtrace_state, 2, __ATOMIC_RELEASE);+ s_writing_backtrace = false;}static u32 capture_native_stack(void* context)@@ -263,9 +283,23 @@ void CrashHandler::handle_fatal_signal(int signal, siginfo_t* info, void* contex// Persist the reason first, even if unwinding is impossible or interrupted.write_record(&header, sizeof(header), 0);- auto frame_count = __atomic_load_n(&s_backtrace_state, __ATOMIC_ACQUIRE) == 2- ? s_backtrace_frame_count- : capture_native_stack(context);+ // Claim the backtrace, so that no capture on another thread replaces its frames while this handler reads them or+ // writes its own. A provisional backtrace only describes this crash if this thread captured it and is still+ // panicking. While another thread writes a backtrace, leave the frames out instead of writing over them; if this+ // thread crashed while writing one, nothing else writes, so capture the native stack.+ auto backtrace_state = __atomic_load_n(&s_backtrace_state, __ATOMIC_ACQUIRE);+ while (backtrace_state == 0 || backtrace_state == 3) {+ if (__atomic_compare_exchange_n(&s_backtrace_state, &backtrace_state, 2, false, __ATOMIC_ACQ_REL,+ __ATOMIC_ACQUIRE))+ break;+ }+ auto owns_provisional_backtrace = backtrace_state == 3 && AK::current_rust_panic_message()+ && s_captured_backtrace_generation == __atomic_load_n(&s_backtrace_generation, __ATOMIC_RELAXED);+ u32 frame_count = 0;+ if (backtrace_state == 2 || owns_provisional_backtrace)+ frame_count = s_backtrace_frame_count;+ else if (backtrace_state != 1 || s_writing_backtrace)+ frame_count = capture_native_stack(context);write_record(&frame_count, sizeof(frame_count), offsetof(ReportHeader, frame_count));// Preserve signal termination and the operating system's crash handling.diff --git a/Libraries/RustPanic.rs b/Libraries/RustPanic.rsindex 9b3f76e9fb5..501b97afad9 100644--- a/Libraries/RustPanic.rs+++ b/Libraries/RustPanic.rs@@ -15,6 +15,9 @@ unsafe extern "C" {column: u32,is_panicking: extern "C" fn() -> bool,);+ fn ladybird_rust_panic_hook_returned();+ #[cfg(panic = "abort")]+ fn ladybird_rust_panic_may_abort();fn ladybird_rust_panic_will_abort();}@@ -52,13 +55,18 @@ pub(crate) extern "C" fn initialize() {is_panicking,);}- // Nothing can catch this panic, so no later code gets to capture the backtrace.+ // Nothing in this crate can catch the panic, but the hook also sees the panics of other crates that share+ // its standard library, which code built with unwinding may catch.#[cfg(panic = "abort")]// SAFETY: The bridge only reads the message stored above.unsafe {- ladybird_rust_panic_will_abort();+ ladybird_rust_panic_may_abort();}previous(info);+ // SAFETY: The bridge only updates its own per-thread state.+ unsafe {+ ladybird_rust_panic_hook_returned();+ }}));});}