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

SyntaxArena: thread safety #807

Merged
merged 2 commits into from
Sep 21, 2022
Merged

SyntaxArena: thread safety #807

merged 2 commits into from
Sep 21, 2022

Conversation

rintaro
Copy link
Member

@rintaro rintaro commented Sep 16, 2022

Use mutex to guard concurrent mutation to the arena.
But make a "single thread" mode so that we don't regress performance too much when we know the arena is not used by other threads.

(non Darwin/Glibc environment e.g. Windows implementation is currently a dummy)

#785
rdar://99812330

@rintaro
Copy link
Member Author

rintaro commented Sep 16, 2022

@swift-ci Please test

Use mutex to guard concurrent mutation to the arena.

swiftlang#785
rdar://99812330
@rintaro
Copy link
Member Author

rintaro commented Sep 16, 2022

@swift-ci Please test

@rintaro rintaro marked this pull request as ready for review September 16, 2022 23:32
@rintaro rintaro requested a review from CodaFi September 16, 2022 23:33
jpsim added a commit to realm/SwiftLint that referenced this pull request Sep 19, 2022
@jpsim
Copy link
Contributor

jpsim commented Sep 19, 2022

Thank you, this certainly helps, although TSan now reports an access race in the guard code:


WARNING: ThreadSanitizer: Swift access race (pid=42514)
  Modifying access of Swift variable at 0x0001148c22b0 by thread T25:
    #0 ScopeGuard.withGuard<A>(body:) <null>:82026728 (SwiftLintFrameworkTests:arm64+0x126a334)
    #1 SyntaxArena.withGuard<A>(body:) <null>:82026728 (SwiftLintFrameworkTests:arm64+0x126b504)
    #2 SyntaxArena.allocateRawSyntaxBuffer(count:) <null>:82026728 (SwiftLintFrameworkTests:arm64+0x126c2fc)
    #3 static RawSyntax.makeLayout(kind:uninitializedCount:arena:initializingWith:) <null>:82026728 (SwiftLintFrameworkTests:arm64+0xffcd14)
    #4 RawSyntaxLayoutView.replacingChild(at:with:arena:) <null>:82026728 (SwiftLintFrameworkTests:arm64+0x10036c8)
    #5 SyntaxData.replacingChild(_:at:) <null>:82026728 (SwiftLintFrameworkTests:arm64+0x1285864)

Full report: https://buildkite.com/swiftlint/swiftlint/builds/3411#018354b5-95eb-4c14-ab21-f7d59113f241

/// Copies a source buffer in to the memory this arena manages, and returns
/// the interned buffer.
///
/// The interned buffer is guaranteed to be null-terminated.
/// `contains(address _:)` is faster if the address is inside the memory
/// range this function returned.
public func internSourceBuffer(_ buffer: UnsafeBufferPointer<UInt8>) -> UnsafeBufferPointer<UInt8> {
let allocated = lock.withGuard {
Copy link
Member

Choose a reason for hiding this comment

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

Should this be self.withGuard?

@@ -114,7 +189,9 @@ public class SyntaxArena {
/// Copies a `RawSyntaxData` to the memory this arena manages, and retuns the
/// pointer to the destination.
func intern(_ value: RawSyntaxData) -> UnsafePointer<RawSyntaxData> {
let allocated = allocator.allocate(RawSyntaxData.self, count: 1).baseAddress!
let allocated = lock.withGuard {
Copy link
Member

Choose a reason for hiding this comment

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

Same here, should this be self.withGuard?

Copy link
Contributor

@CodaFi CodaFi left a comment

Choose a reason for hiding this comment

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

These ScopeGuards are not correct. We cannot use the materialization operator here despite the fact that we're indexing through a heap allocation for the member access - it's still a logical data race even if it's not always a physical data race. These locks need to be allocated out-of-line and the resulting address passed.

Passing `os_unfair_lock` with inout expression is considered "mutation"
by thread sanitizer. Instead, keep the lock object with
`let UnsafeMutablePointer` form, and pass it.
@rintaro
Copy link
Member Author

rintaro commented Sep 20, 2022

@jpsim I confirmed the failure locally. And the modified implementation resolves it. Could you check the new implementation?

@rintaro
Copy link
Member Author

rintaro commented Sep 20, 2022

@swift-ci Please test

Copy link
Contributor

@jpsim jpsim left a comment

Choose a reason for hiding this comment

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

There are no more TSan failures in my testing. Thank you!

jpsim added a commit to realm/SwiftLint that referenced this pull request Sep 21, 2022
jpsim added a commit to realm/SwiftLint that referenced this pull request Sep 21, 2022
@rintaro rintaro merged commit 6ba87a8 into swiftlang:main Sep 21, 2022
@rintaro rintaro deleted the arena-lock branch September 21, 2022 03:25
jpsim added a commit to realm/SwiftLint that referenced this pull request Sep 21, 2022
jpsim added a commit to realm/SwiftLint that referenced this pull request Sep 25, 2022
jpsim added a commit to realm/SwiftLint that referenced this pull request Sep 27, 2022
jpsim added a commit to realm/SwiftLint that referenced this pull request Sep 28, 2022
jpsim added a commit to realm/SwiftLint that referenced this pull request Sep 28, 2022
jpsim added a commit to realm/SwiftLint that referenced this pull request Sep 29, 2022
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.

4 participants