Skip to content

Commit b65bd93

Browse files
committed
Support: unlock Windows API support, switch to Windows 10 RS1+ APIs
This addresses a small window of in-atomicity in the rename support for Windows. If the rename API is stressed sufficiently, it can run afoul of the small window of opportunity where the destination file will not exist. In Swift, this causes the `LockFileManager` to improperly handle file locking for module compilation resulting in spurious failures as the file file is failed to be read after writing due to multiple processes overwriting the file and exposing the window constantly to the scheduled process when building with `-num-threads` > for the Swift driver. The implementation of `llvm::sys::fs::rename` was changed in SVN r315079 (80e31f1) which even explicitly identifies the issue: ~~~ This implementation is still not fully POSIX. Specifically in the case where the destination file is open at the point when rename is called, there will be a short interval of time during which the destination file will not exist. It isn't clear whether it is possible to avoid this using the Windows API. ~~~ Special thanks to Ben Barham for the discussions and help with analyzing logging to track down this issue! This API was introduced with Windows 10 RS1, and although there are LTSC contracts that allow older versions to be supported still, the mainstream support system has declared this release to be obsoleted. As such, assuming that the OS is at least as new as this is reasonable.
1 parent 6b47772 commit b65bd93

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

llvm/include/llvm/Support/Windows/WindowsSupport.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@
2121
#ifndef LLVM_SUPPORT_WINDOWSSUPPORT_H
2222
#define LLVM_SUPPORT_WINDOWSSUPPORT_H
2323

24+
#if defined(__MINGW32__)
2425
// mingw-w64 tends to define it as 0x0502 in its headers.
2526
#undef _WIN32_WINNT
2627
#undef _WIN32_IE
2728

2829
// Require at least Windows 7 API.
2930
#define _WIN32_WINNT 0x0601
3031
#define _WIN32_IE 0x0800 // MinGW at it again. FIXME: verify if still needed.
32+
#endif
33+
3134
#define WIN32_LEAN_AND_MEAN
3235
#ifndef NOMINMAX
3336
#define NOMINMAX

llvm/lib/Support/Windows/Path.inc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,13 +466,14 @@ static std::error_code rename_internal(HANDLE FromHandle, const Twine &To,
466466
(ToWide.size() * sizeof(wchar_t)));
467467
FILE_RENAME_INFO &RenameInfo =
468468
*reinterpret_cast<FILE_RENAME_INFO *>(RenameInfoBuf.data());
469-
RenameInfo.ReplaceIfExists = ReplaceIfExists;
469+
RenameInfo.Flags = FILE_RENAME_FLAG_POSIX_SEMANTICS
470+
| (ReplaceIfExists ? FILE_RENAME_FLAG_REPLACE_IF_EXISTS : 0);
470471
RenameInfo.RootDirectory = 0;
471472
RenameInfo.FileNameLength = ToWide.size() * sizeof(wchar_t);
472473
std::copy(ToWide.begin(), ToWide.end(), &RenameInfo.FileName[0]);
473474

474475
SetLastError(ERROR_SUCCESS);
475-
if (!SetFileInformationByHandle(FromHandle, FileRenameInfo, &RenameInfo,
476+
if (!SetFileInformationByHandle(FromHandle, FileRenameInfoEx, &RenameInfo,
476477
RenameInfoBuf.size())) {
477478
unsigned Error = GetLastError();
478479
if (Error == ERROR_SUCCESS)

0 commit comments

Comments
 (0)