[Support] Flush LLVM profiles on fatal exit (#9438)

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 (<cstdint>,
<iostream>, <memory>, and "support/bits.h") and add the missing #include
<cstdint> to src/support/string.h.
diff --git a/src/support/string.h b/src/support/string.h
index 5d9da0e..9c066f0 100644
--- a/src/support/string.h
+++ b/src/support/string.h
@@ -24,6 +24,7 @@
 #include "support/utilities.h"
 #include <algorithm>
 #include <cctype>
+#include <cstdint>
 #include <ostream>
 #include <string>
 #include <vector>
diff --git a/src/support/utilities.cpp b/src/support/utilities.cpp
index f051a18..884e072 100644
--- a/src/support/utilities.cpp
+++ b/src/support/utilities.cpp
@@ -19,11 +19,39 @@
 #include <cassert>
 #include <cstdlib>
 #include <iostream>
+#include <mutex>
 
 #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<std::mutex> 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 9397f03..08c6ee5 100644
--- a/src/support/utilities.h
+++ b/src/support/utilities.h
@@ -20,14 +20,13 @@
 #include "compiler-support.h"
 
 #include <cassert>
-#include <cstdint>
 #include <cstring>
-#include <iostream>
-#include <memory>
 #include <sstream>
 #include <type_traits>
 
-#include "support/bits.h"
+#ifdef __LLVM_INSTR_PROFILE_GENERATE
+extern "C" int __llvm_profile_write_file();
+#endif
 
 namespace wasm {
 
@@ -52,6 +51,18 @@
 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<typename T> Fatal& operator<<(T&& arg) {
@@ -60,11 +71,11 @@
   }
 #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