From 5ebd86b871e10022e3dedc3657642a484758ebe3 Mon Sep 17 00:00:00 2001 From: Thomas Lively Date: Mon, 30 Nov 2026 15:25:29 -0800 Subject: [PATCH] [Support] Flush LLVM profiles on fatal exit Fatal::~Fatal terminates via _Exit to avoid running static destructors, which bypasses the atexit handlers registered by LLVM's instrumentation profile runtime and drops profile data on fatal error paths. Move the exit path into a mutex-serialized Fatal::exit helper in src/support/utilities.cpp. When __LLVM_INSTR_PROFILE_GENERATE is defined, Fatal::~Fatal passes the caller's __llvm_profile_write_file function pointer to Fatal::exit, which flushes the library's profile runtime and, if the function pointer is distinct, the caller's profile runtime as well. This flushes both runtimes in shared-library builds where the library and executables each link a separate hidden copy of the profile runtime, while avoiding duplicate writes when both resolve to the same copy. Also remove unused includes from src/support/utilities.h (, , , and "support/bits.h") and add the missing #include to src/support/string.h. --- src/support/string.h | 1 + src/support/utilities.cpp | 28 ++++++++++++++++++++++++++++ src/support/utilities.h | 29 ++++++++++++++++++++--------- 3 files changed, 49 insertions(+), 9 deletions(-) diff --git a/src/support/string.h b/src/support/string.h index 5d9da0e0cf0..9c066f0cde4 100644 --- a/src/support/string.h +++ b/src/support/string.h @@ -24,6 +24,7 @@ #include "support/utilities.h" #include #include +#include #include #include #include diff --git a/src/support/utilities.cpp b/src/support/utilities.cpp index f051a1871eb..884e0723f75 100644 --- a/src/support/utilities.cpp +++ b/src/support/utilities.cpp @@ -19,11 +19,39 @@ #include #include #include +#include #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__) #include "sanitizer/common_interface_defs.h" #endif +#ifndef THROW_ON_FATAL +#ifdef __LLVM_INSTR_PROFILE_GENERATE +[[noreturn]] void wasm::Fatal::exit(const std::string& msg, + int (*flushCallerProfile)()) { +#else +[[noreturn]] void wasm::Fatal::exit(const std::string& msg) { +#endif + // Serialize fatal exits so concurrent worker threads do not interleave + // output or race while flushing instrumentation profiles before _Exit. + static std::mutex fatalMutex; + std::lock_guard lock(fatalMutex); + std::cerr << msg << std::endl; +#ifdef __LLVM_INSTR_PROFILE_GENERATE + // Flush this library's profile runtime and, if distinct, the caller's profile + // runtime before _Exit bypasses exit handlers. + __llvm_profile_write_file(); + if (flushCallerProfile != __llvm_profile_write_file) { + flushCallerProfile(); + } +#endif + // Use _Exit here to avoid calling static destructors. This avoids deadlocks + // in (for example) the thread worker pool, where workers hold a lock while + // performing their work. + _Exit(EXIT_FAILURE); +} +#endif + [[noreturn]] void wasm::handle_unreachable(const char* msg, const char* file, unsigned line) { #ifndef NDEBUG diff --git a/src/support/utilities.h b/src/support/utilities.h index 9397f033504..08c6ee5a900 100644 --- a/src/support/utilities.h +++ b/src/support/utilities.h @@ -20,14 +20,13 @@ #include "compiler-support.h" #include -#include #include -#include -#include #include #include -#include "support/bits.h" +#ifdef __LLVM_INSTR_PROFILE_GENERATE +extern "C" int __llvm_profile_write_file(); +#endif namespace wasm { @@ -52,6 +51,18 @@ class Fatal { private: std::stringstream buffer; +#ifndef THROW_ON_FATAL +#ifdef __LLVM_INSTR_PROFILE_GENERATE + // Pass the caller's profile flush function because shared-library builds link + // separate hidden copies of the profile runtime into the library and each + // executable. + [[noreturn]] static void exit(const std::string& msg, + int (*flushCallerProfile)()); +#else + [[noreturn]] static void exit(const std::string& msg); +#endif +#endif + public: Fatal() { buffer << "Fatal: "; } template Fatal& operator<<(T&& arg) { @@ -60,11 +71,11 @@ class Fatal { } #ifndef THROW_ON_FATAL [[noreturn]] ~Fatal() { - std::cerr << buffer.str() << std::endl; - // Use _Exit here to avoid calling static destructors. This avoids deadlocks - // in (for example) the thread worker pool, where workers hold a lock while - // performing their work. - _Exit(EXIT_FAILURE); +#ifdef __LLVM_INSTR_PROFILE_GENERATE + exit(buffer.str(), __llvm_profile_write_file); +#else + exit(buffer.str()); +#endif } #else // This variation is a best-effort attempt to make fatal errors recoverable