From 553271c3ede569a42394ecd203035812b0740cfe Mon Sep 17 00:00:00 2001 From: DianQK Date: Thu, 7 Mar 2024 22:51:40 +0800 Subject: [PATCH] Add a global lock to `FatalErrorHandler` to avoid re-entry --- compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp index d9262a9278271..85073ee35ba1c 100644 --- a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp +++ b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp @@ -60,12 +60,17 @@ static AtomicOrdering fromRust(LLVMAtomicOrdering Ordering) { static LLVM_THREAD_LOCAL char *LastError; +static std::mutex FatalErrorHandlerMutex; +static std::unique_lock FatalErrorHandlerLock(FatalErrorHandlerMutex, std::defer_lock); // Custom error handler for fatal LLVM errors. // // Notably it exits the process with code 101, unlike LLVM's default of 1. static void FatalErrorHandler(void *UserData, const char* Reason, bool GenCrashDiag) { + // Add a global lock to avoid re-entry here. When encountering an error, + // we only need and can only enter once. + FatalErrorHandlerLock.lock(); // Once upon a time we emitted "LLVM ERROR:" specifically to mimic LLVM. Then, // we developed crater and other tools which only expose logs, not error codes. // Use a more greppable prefix that will still match the "LLVM ERROR:" prefix.