[Windows] Use TerminateProcess to exit without running destructors

If exiting using _Exit or ExitProcess, DLLs are still unloaded
cleanly before exiting, running destructors and other cleanup in those
DLLs. When the caller expects to exit without cleanup, running
destructors in some loaded DLLs (which can be either libLLVM.dll or
e.g. libc++.dll) can cause deadlocks occasionally.

This is an alternative to D102684.

Differential Revision: https://reviews.llvm.org/D102944
This commit is contained in:
Martin Storsjö 2021-05-21 23:50:01 +03:00
parent c5638a71d8
commit b4fd512c36
4 changed files with 14 additions and 1 deletions

View File

@ -216,6 +216,10 @@ public:
/// Use \arg NoCleanup for calling _exit() instead of exit().
LLVM_ATTRIBUTE_NORETURN
static void Exit(int RetCode, bool NoCleanup = false);
private:
LLVM_ATTRIBUTE_NORETURN
static void ExitNoCleanup(int RetCode);
};
}

View File

@ -98,7 +98,7 @@ void Process::Exit(int RetCode, bool NoCleanup) {
CRC->HandleExit(RetCode);
if (NoCleanup)
_Exit(RetCode);
ExitNoCleanup(RetCode);
else
::exit(RetCode);
}

View File

@ -460,3 +460,6 @@ unsigned llvm::sys::Process::GetRandomNumber() {
return ::rand();
#endif
}
LLVM_ATTRIBUTE_NORETURN
void Process::ExitNoCleanup(int RetCode) { _Exit(RetCode); }

View File

@ -503,3 +503,9 @@ bool llvm::RunningWindows8OrGreater() {
// Windows 8 is version 6.2, service pack 0.
return GetWindowsOSVersion() >= llvm::VersionTuple(6, 2, 0, 0);
}
LLVM_ATTRIBUTE_NORETURN
void Process::ExitNoCleanup(int RetCode) {
TerminateProcess(GetCurrentProcess(), RetCode);
llvm_unreachable("TerminateProcess doesn't return");
}