Skip to content

Commit 2bc24b8

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` > 1 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 3e71357 commit 2bc24b8

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@
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
26-
2727
// Require at least Windows 7 API.
2828
#define _WIN32_WINNT 0x0601
29+
#endif
30+
2931
#define WIN32_LEAN_AND_MEAN
3032
#ifndef NOMINMAX
3133
#define NOMINMAX

llvm/lib/Support/Windows/Path.inc

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -466,21 +466,41 @@ 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;
470-
RenameInfo.RootDirectory = 0;
471-
RenameInfo.FileNameLength = ToWide.size() * sizeof(wchar_t);
472-
std::copy(ToWide.begin(), ToWide.end(), &RenameInfo.FileName[0]);
473-
474-
SetLastError(ERROR_SUCCESS);
475-
if (!SetFileInformationByHandle(FromHandle, FileRenameInfo, &RenameInfo,
476-
RenameInfoBuf.size())) {
477-
unsigned Error = GetLastError();
478-
if (Error == ERROR_SUCCESS)
479-
Error = ERROR_CALL_NOT_IMPLEMENTED; // Wine doesn't always set error code.
480-
return mapWindowsError(Error);
469+
470+
static const struct RenameStyle {
471+
DWORD dwFlag;
472+
FILE_INFO_BY_HANDLE_CLASS Class;
473+
} kRenameStyles[] = {
474+
{ FILE_RENAME_FLAG_POSIX_SEMANTICS, FileRenameInfoEx },
475+
{ 0, FileRenameInfo },
476+
};
477+
478+
DWORD dwError = ERROR_SUCCESS;
479+
for (const auto &style : kRenameStyles) {
480+
RenameInfo.Flags = style.dwFlag |
481+
(ReplaceIfExists ? FILE_RENAME_FLAG_REPLACE_IF_EXISTS
482+
: 0);
483+
RenameInfo.RootDirectory = 0;
484+
RenameInfo.FileNameLength = ToWide.size() * sizeof(wchar_t);
485+
std::copy(ToWide.begin(), ToWide.end(), &RenameInfo.FileName[0]);
486+
487+
SetLastError(ERROR_SUCCESS);
488+
if (!SetFileInformationByHandle(FromHandle, FileRenameInfoEx, &RenameInfo,
489+
RenameInfoBuf.size())) {
490+
dwError = GetLastError();
491+
if (dwError == ERROR_SUCCESS)
492+
dwError = ERROR_CALL_NOT_IMPLEMENTED; // Wine doesn't always set error code.
493+
// Retry with a different style if we do not support this style.
494+
if (dwError == ERROR_INVALID_PARAMETER ||
495+
dwError == ERROR_CALL_NOT_IMPLEMENTED)
496+
continue;
497+
return mapWindowsError(Error);
498+
}
481499
}
482500

483-
return std::error_code();
501+
if (dwError == ERROR_SUCCESS)
502+
return std::error_code();
503+
return mapWindowsError(Error);
484504
}
485505

486506
static std::error_code rename_handle(HANDLE FromHandle, const Twine &To) {

0 commit comments

Comments
 (0)