Skip to content
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

Merged

Conversation

z2oh
Copy link
Contributor

@z2oh z2oh commented Feb 29, 2024

Fixes #83046

There is a race condition when calling GetFileAttributesW that can cause it to return ERROR_ACCESS_DENIED on a path which exists, which is unexpected for callers using this function to check for file existence by passing AccessMode::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.

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

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
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

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.

@llvmbot
Copy link
Collaborator

llvmbot commented Feb 29, 2024

@llvm/pr-subscribers-llvm-support

@llvm/pr-subscribers-platform-windows

Author: Jeremy Day (z2oh)

Changes

Fixes #83046

There is a race condition when calling GetFileAttributesW that can cause it to return ERROR_ACCESS_DENIED on a path which exists, which is unexpected for callers using this function to check for file existence by passing AccessMode::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.


Full diff: https://github.com/llvm/llvm-project/pull/83495.diff

2 Files Affected:

  • (modified) llvm/lib/Support/CMakeLists.txt (+1-1)
  • (modified) llvm/lib/Support/Windows/Path.inc (+9)
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) {

@mstorsjo
Copy link
Member

mstorsjo commented Mar 1, 2024

There is a race condition when calling GetFileAttributesW that can cause it to return ERROR_ACCESS_DENIED on a path which exists

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.)

@z2oh
Copy link
Contributor Author

z2oh commented Mar 1, 2024

Unfortunately I haven't been able to pin down the exact source of the race here, only that sometimes there is a call to fs::access(_, AccessMode::Exist) that unexpectedly fails with ERROR_ACCESS_DENIED. We have been able to reproduce this call failing in this way in isolation, where GetFileAttributesW fails with ERROR_ACCESS_DENIED called on an open handle that has been marked for deletion.

I've evaluated the veracity of this patch experimentally, but after seeing Wine's implementation of PathFileExistsW linked above I'm thinking it works by simply treating any failure of GetFileAttributesW as the evidence of the path not existing. This actually seems to be the semantics of semantics of the fs::exists function, so I may be able to just use this instead in downstream code. Thanks for the pointers everyone! I'll report back after verifying this.

@aganea
Copy link
Member

aganea commented Mar 12, 2024

Regardless I think it would still be valuable if we returned errc::no_such_file_or_directory on failure, when calling access with AccessMode::Exists, and not some other Windows error code, ie:

if (Attributes == INVALID_FILE_ATTRIBUTES) {
  if (Mode == AccessMode::Exists)
    return errc::no_such_file_or_directory;
...

@z2oh z2oh force-pushed the avoid-GetFileAttributesW-for-existence-check branch from 645f01f to 905f9f6 Compare March 14, 2024 17:54
@z2oh z2oh changed the title Avoid calling GetFileAttributesW in Windows' fs::access when checking for existence Return errc::no_such_file_or_directory in fs::access if GetFileAttributesW fails Mar 14, 2024
@z2oh z2oh force-pushed the avoid-GetFileAttributesW-for-existence-check branch from 905f9f6 to 10c747f Compare March 14, 2024 17:56
@z2oh
Copy link
Contributor Author

z2oh commented Mar 14, 2024

Okay, that makes sense. I've updated the PR. Thanks for your insight and reviews @aganea!

Copy link
Member

@aganea aganea left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@z2oh z2oh force-pushed the avoid-GetFileAttributesW-for-existence-check branch from 10c747f to 61b89f3 Compare March 19, 2024 17:54
@z2oh z2oh force-pushed the avoid-GetFileAttributesW-for-existence-check branch from 61b89f3 to 8c97bd8 Compare March 21, 2024 23:01
@z2oh
Copy link
Contributor Author

z2oh commented Mar 25, 2024

After a rebase, CI is passing. I believe this is ready to merge!

@aganea aganea requested a review from mstorsjo March 25, 2024 16:50
Copy link
Member

@mstorsjo mstorsjo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@compnerd compnerd merged commit 9961c03 into llvm:main Mar 27, 2024
4 checks passed
Copy link

@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
by our build bots. If there is a problem with a build, you may recieve a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as
the builds can include changes from many authors. It is not uncommon for your
change to be included in a build that fails due to someone else's changes, or
infrastructure issues.

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.
This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

compnerd added a commit to apple/llvm-project that referenced this pull request Apr 2, 2024
[cherrypick stable/20240123] Return `errc::no_such_file_or_directory` in `fs::access` if `GetFileAttributesW` fails (llvm#83495)
compnerd added a commit to apple/llvm-project that referenced this pull request Apr 9, 2024
…ess-20230725

[cherrypick stable/20230725] Return `errc::no_such_file_or_directory` in `fs::access` if `GetFileAttributesW` fails (llvm#83495)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

llvm::sys::fs::access can spuriously fail on Windows when called with AccessMode::Exist
5 participants