Skip to content

[llvm][cas] Fix thread safety issues with MappedFileRegionBumpPtr #8621

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

Merged
merged 1 commit into from
Apr 22, 2024

Conversation

benlangmuir
Copy link

llvm::sys::fs::lockFile and friends, which are implemented with fcntl F_SETLK, are only safe to use across processes and not across threads. We were attempting to workaround that using createShared, which tries to unique the mapped file per process, but (a) that only works if you have a single copy of llvm in your process, which may not always be true, and (b) we had a race in the implementation between closing one CAS and opening a new one at the same path. Replace these with an flock-based implementation. Since this is not available on all platforms LLVM supports we use a separate implementation for the on-disk CAS rather than change llvm::sys::fs::lockFile.

rdar://126882843

@benlangmuir
Copy link
Author

@swift-ci please test llvm

Copy link

@cachemeifyoucan cachemeifyoucan left a comment

Choose a reason for hiding this comment

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

Looks fine in general as long as this doesn't regress linux.

#include <thread>

#if __has_include(<sys/file.h>)
#include <sys/file.h>

Choose a reason for hiding this comment

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

Is this a common unix header or is this macOS only?

Copy link
Author

Choose a reason for hiding this comment

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

@@ -52,12 +52,14 @@
//===----------------------------------------------------------------------===//

#include "llvm/CAS/MappedFileRegionBumpPtr.h"
#include "llvm/ADT/ScopeExit.h"

Choose a reason for hiding this comment

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

don't need empty line around this include?

Copy link
Author

Choose a reason for hiding this comment

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

Hmm, I see a mix of styles for including lib-directory headers. Some have empty lines around it, some only have empty line above it, some have no empty lines. Not clear to me what it should be

Copy link
Author

Choose a reason for hiding this comment

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

No spaces seems more common, going with that.

llvm::sys::fs::lockFile and friends, which are implemented with fcntl
F_SETLK, are only safe to use across processes and not across threads.
We were attempting to workaround that using createShared, which tries to
unique the mapped file per process, but (a) that only works if you have
a single copy of llvm in your process, which may not always be true, and
(b) we had a race in the implementation between closing one CAS and
opening a new one at the same path.  Replace these with an flock-based
implementation. Since this is not available on all platforms LLVM
supports we use a separate implementation for the on-disk CAS rather
than change llvm::sys::fs::lockFile.

rdar://126882843
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants