Skip to content

Commit a48395f

Browse files
compnerdmstorsjo
authored andcommitted
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 174e262 commit a48395f

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,18 @@
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
27+
#undef NTDDI_VERSION
28+
29+
// Expose APIs from Windows 10 RS1. We only require Windows 7 at runtime,
30+
// so make sure that we only use such features optionally, with fallbacks
31+
// where relevant.
32+
#define _WIN32_WINNT 0x0A00
33+
#define NTDDI_VERSION 0x0A000002 // Expose APIs from WIN10_RS1
34+
#endif
2635

27-
// Require at least Windows 7 API.
28-
#define _WIN32_WINNT 0x0601
2936
#define WIN32_LEAN_AND_MEAN
3037
#ifndef NOMINMAX
3138
#define NOMINMAX

llvm/lib/Support/Windows/Path.inc

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -466,18 +466,39 @@ 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+
470+
RenameInfo.Flags = FILE_RENAME_FLAG_POSIX_SEMANTICS
471+
| (ReplaceIfExists ? FILE_RENAME_FLAG_REPLACE_IF_EXISTS : 0);
470472
RenameInfo.RootDirectory = 0;
471473
RenameInfo.FileNameLength = ToWide.size() * sizeof(wchar_t);
472474
std::copy(ToWide.begin(), ToWide.end(), &RenameInfo.FileName[0]);
473475

474476
SetLastError(ERROR_SUCCESS);
475-
if (!SetFileInformationByHandle(FromHandle, FileRenameInfo, &RenameInfo,
477+
// FileRenameInfoEx requires Windows 10 RS1
478+
if (!SetFileInformationByHandle(FromHandle, FileRenameInfoEx, &RenameInfo,
476479
RenameInfoBuf.size())) {
477480
unsigned Error = GetLastError();
478481
if (Error == ERROR_SUCCESS)
479482
Error = ERROR_CALL_NOT_IMPLEMENTED; // Wine doesn't always set error code.
480-
return mapWindowsError(Error);
483+
if (Error == ERROR_INVALID_PARAMETER || Error == ERROR_CALL_NOT_IMPLEMENTED) {
484+
// If running on an older version of Windows, fall back on FileRenameInfo.
485+
RenameInfo.ReplaceIfExists = ReplaceIfExists;
486+
RenameInfo.RootDirectory = 0;
487+
RenameInfo.FileNameLength = ToWide.size() * sizeof(wchar_t);
488+
std::copy(ToWide.begin(), ToWide.end(), &RenameInfo.FileName[0]);
489+
490+
SetLastError(ERROR_SUCCESS);
491+
if (!SetFileInformationByHandle(FromHandle, FileRenameInfo, &RenameInfo,
492+
RenameInfoBuf.size())) {
493+
unsigned Error = GetLastError();
494+
if (Error == ERROR_SUCCESS)
495+
Error = ERROR_CALL_NOT_IMPLEMENTED; // Wine doesn't always set error code.
496+
return mapWindowsError(Error);
497+
}
498+
} else {
499+
return mapWindowsError(Error);
500+
}
501+
481502
}
482503

483504
return std::error_code();

0 commit comments

Comments
 (0)