-
Notifications
You must be signed in to change notification settings - Fork 10.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Return errc::no_such_file_or_directory
in fs::access
if GetFileAttributesW
fails
#83495
Return errc::no_such_file_or_directory
in fs::access
if GetFileAttributesW
fails
#83495
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write If you have received no comments on your PR for a week, you can request a review If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-llvm-support @llvm/pr-subscribers-platform-windows Author: Jeremy Day (z2oh) ChangesFixes #83046 There is a race condition when calling I looked for alternate APIs to avoid bringing in Full diff: https://github.com/llvm/llvm-project/pull/83495.diff 2 Files Affected:
diff --git a/llvm/lib/Support/CMakeLists.txt b/llvm/lib/Support/CMakeLists.txt
index 1f2d82427552f7..83889535d88f1b 100644
--- a/llvm/lib/Support/CMakeLists.txt
+++ b/llvm/lib/Support/CMakeLists.txt
@@ -40,7 +40,7 @@ endif()
if( MSVC OR MINGW )
# libuuid required for FOLDERID_Profile usage in lib/Support/Windows/Path.inc.
# advapi32 required for CryptAcquireContextW in lib/Support/Windows/Path.inc.
- set(system_libs ${system_libs} psapi shell32 ole32 uuid advapi32 ws2_32)
+ set(system_libs ${system_libs} psapi shell32 shlwapi ole32 uuid advapi32 ws2_32)
elseif( CMAKE_HOST_UNIX )
if( HAVE_LIBRT )
set(system_libs ${system_libs} rt)
diff --git a/llvm/lib/Support/Windows/Path.inc b/llvm/lib/Support/Windows/Path.inc
index 3e4c1f74161c6f..03befdd5412628 100644
--- a/llvm/lib/Support/Windows/Path.inc
+++ b/llvm/lib/Support/Windows/Path.inc
@@ -27,6 +27,7 @@
#include "llvm/Support/Windows/WindowsSupport.h"
#include <shellapi.h>
#include <shlobj.h>
+#include <shlwapi.h>
#undef max
@@ -620,6 +621,14 @@ std::error_code access(const Twine &Path, AccessMode Mode) {
if (std::error_code EC = widenPath(Path, PathUtf16))
return EC;
+ if (Mode == AccessMode::Exist) {
+ if (::PathFileExistsW(PathUtf16.begin())) {
+ return std::error_code();
+ } else {
+ return errc::no_such_file_or_directory;
+ }
+ }
+
DWORD Attributes = ::GetFileAttributesW(PathUtf16.begin());
if (Attributes == INVALID_FILE_ATTRIBUTES) {
|
Can you elaborate a bit more about how this race condition occurs, and how the suggested solution avoids the issue? (I guess it's explained somewhere in the linked issues, but in order to evaluate the suggested fix, it would be good to have that analysis spelled out here as well.) |
Unfortunately I haven't been able to pin down the exact source of the race here, only that sometimes there is a call to I've evaluated the veracity of this patch experimentally, but after seeing Wine's implementation of |
Regardless I think it would still be valuable if we returned
|
645f01f
to
905f9f6
Compare
GetFileAttributesW
in Windows' fs::access
when checking for existenceerrc::no_such_file_or_directory
in fs::access
if GetFileAttributesW
fails
905f9f6
to
10c747f
Compare
Okay, that makes sense. I've updated the PR. Thanks for your insight and reviews @aganea! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
10c747f
to
61b89f3
Compare
61b89f3
to
8c97bd8
Compare
After a rebase, CI is passing. I believe this is ready to merge! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@z2oh Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested Please check whether problems have been caused by your change specifically, as How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
[cherrypick stable/20240123] Return `errc::no_such_file_or_directory` in `fs::access` if `GetFileAttributesW` fails (llvm#83495)
…ess-20230725 [cherrypick stable/20230725] Return `errc::no_such_file_or_directory` in `fs::access` if `GetFileAttributesW` fails (llvm#83495)
Fixes #83046
There is a race condition when calling
GetFileAttributesW
that can cause it to returnERROR_ACCESS_DENIED
on a path which exists, which is unexpected for callers using this function to check for file existence by passingAccessMode::Exist
. This was manifesting as a compiler crash on Windows downstream in the Swift compiler when using the-index-store-path
flag (more information in apple#8224).I looked for alternate APIs to avoid bringing in
shlwapi.h
, but didn't see any good candidates. I'm not tied at all to this solution, any feedback and alternative approaches are more than welcome.