From 9c0d705c9a16a9ecff53c2ceac167db477276205 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Thu, 28 Nov 2024 10:52:20 -0500 Subject: [PATCH] AK+LibMain: Perform LSAN checks ahead of process shutdown From the documentation of __lsan_do_leak_check: By calling this function early during process shutdown, you can instruct LSan to ignore shutdown-only leaks which happen later on. We currently have tens of thousands of lines of LSAN output at the end of each LibWeb test run on CI. May of these are expected leaks; e.g. we load a bunch of fonts at WebContent process start and just hold on to them statically, until process exit. By calling this LSAN method just before we exit from LibMain's main(), we get a much shorter report of what appear to be more legit leaks. --- AK/LsanSuppressions.h | 16 ++++++++++++++-- Libraries/LibMain/Main.cpp | 6 ++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/AK/LsanSuppressions.h b/AK/LsanSuppressions.h index 4049f7d45424c..a4f5b53c65f54 100644 --- a/AK/LsanSuppressions.h +++ b/AK/LsanSuppressions.h @@ -8,9 +8,10 @@ #include -#ifdef HAS_ADDRESS_SANITIZER +#if defined(HAS_ADDRESS_SANITIZER) +# include + extern "C" { -char const* __lsan_default_suppressions(); char const* __lsan_default_suppressions() { // Both Skia and Chromium suppress false positive FontConfig leaks @@ -20,3 +21,14 @@ char const* __lsan_default_suppressions() } } #endif + +namespace AK { + +inline void perform_leak_sanitizer_checks() +{ +#if defined(HAS_ADDRESS_SANITIZER) + __lsan_do_leak_check(); +#endif +} + +} diff --git a/Libraries/LibMain/Main.cpp b/Libraries/LibMain/Main.cpp index 270b88e7e7ba9..15ffa97fa178e 100644 --- a/Libraries/LibMain/Main.cpp +++ b/Libraries/LibMain/Main.cpp @@ -5,6 +5,8 @@ */ #include +#include +#include #include #include #include @@ -41,10 +43,14 @@ int main(int argc, char** argv) .argv = argv, .strings = arguments.span(), }); + + ScopeGuard guard { []() { AK::perform_leak_sanitizer_checks(); } }; + if (result.is_error()) { auto error = result.release_error(); warnln("\033[31;1mRuntime error\033[0m: {}", error); return Main::return_code_for_errors(); } + return result.value(); }