Skip to content

Commit

Permalink
[Windows] Use TerminateProcess to exit without running destructors
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mstorsjo committed May 22, 2021
1 parent c5638a7 commit b4fd512
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 1 deletion.
4 changes: 4 additions & 0 deletions llvm/include/llvm/Support/Process.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ class Process {
/// 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);
};

}
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Support/Process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void Process::Exit(int RetCode, bool NoCleanup) {
CRC->HandleExit(RetCode);

if (NoCleanup)
_Exit(RetCode);
ExitNoCleanup(RetCode);
else
::exit(RetCode);
}
Expand Down
3 changes: 3 additions & 0 deletions llvm/lib/Support/Unix/Process.inc
Original file line number Diff line number Diff line change
Expand Up @@ -460,3 +460,6 @@ unsigned llvm::sys::Process::GetRandomNumber() {
return ::rand();
#endif
}

LLVM_ATTRIBUTE_NORETURN
void Process::ExitNoCleanup(int RetCode) { _Exit(RetCode); }
6 changes: 6 additions & 0 deletions llvm/lib/Support/Windows/Process.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

0 comments on commit b4fd512

Please sign in to comment.