Skip to content

Eliminate a race condition in NSError bridging #3975

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

Conversation

DougGregor
Copy link
Member

What's in this pull request?

This PR eliminates a race condition in NSError bridging, where we could end up observing uninitialized the userInfo dictionary in an _SwiftNativeNSError. This race was quite rare---we've seen it only a handful of times on the buildbots, and I've only managed to trigger it once locally---but the issue and fix have been verified by TSan. Big thanks to @gottesmm for getting a TSan build up and running so we could use it here.

Resolved bug number: (rdar://problem/27541751)


Before merging this pull request to apple/swift repository:

  • Test pull request on Swift continuous integration.

Triggering Swift CI

The swift-ci is triggered by writing a comment on this PR addressed to the GitHub user @swift-ci. Different tests will run depending on the specific comment that you use. The currently available comments are:

Smoke Testing

Platform Comment
All supported platforms @swift-ci Please smoke test
All supported platforms @swift-ci Please smoke test and merge
OS X platform @swift-ci Please smoke test OS X platform
Linux platform @swift-ci Please smoke test Linux platform

A smoke test on macOS does the following:

  1. Builds the compiler incrementally.
  2. Builds the standard library only for macOS. Simulator standard libraries and
    device standard libraries are not built.
  3. lldb is not built.
  4. The test and validation-test targets are run only for macOS. The optimized
    version of these tests are not run.

A smoke test on Linux does the following:

  1. Builds the compiler incrementally.
  2. Builds the standard library incrementally.
  3. lldb is built incrementally.
  4. The swift test and validation-test targets are run. The optimized version of these
    tests are not run.
  5. lldb is tested.

Validation Testing

Platform Comment
All supported platforms @swift-ci Please test
All supported platforms @swift-ci Please test and merge
OS X platform @swift-ci Please test OS X platform
OS X platform @swift-ci Please benchmark
Linux platform @swift-ci Please test Linux platform

Lint Testing

Language Comment
Python @swift-ci Please Python lint

Note: Only members of the Apple organization can trigger swift-ci.

Huge thanks to John for noting that 'consume' didn't provide the
guarantees we wanted, and to Michael G. for getting a TSan build up
and running to identify/verify this race.

It's possible that this is overlay strict, and that we only need to
look at the domain to ensure that the code and userInfo are
visible. However, TSan seems to prefix the form in this patch, so
we'll be more conservative for now.

Fixes rdar://problem/27541751.

(cherry picked from commit e07e887)
@DougGregor
Copy link
Member Author

@swift-ci please test

@@ -334,6 +332,10 @@ static id _swift_bridgeErrorToNSError_(SwiftError *errorObject) {
NSDictionary *userInfo =
swift_stdlib_getErrorUserInfoNSDictionary(value, type, witness);

// Never produce an empty userInfo dictionary.
if (!userInfo)
userInfo = SWIFT_LAZY_CONSTANT(@{});
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we already do this somewhere else? We might want to factor this out into a static helper so there's only one global initializer for all empty dictionaries.

Copy link
Member Author

Choose a reason for hiding this comment

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

I eliminated the other place I know about, but I do want to revisit this because on modern platforms we don't need the dispatch_once at all for empty collection literals.

@jckarter
Copy link
Contributor

jckarter commented Aug 3, 2016

Looks good as a conservative fix.

@DougGregor DougGregor added this to the Swift 3.0 milestone Aug 3, 2016
@DougGregor DougGregor merged commit fc5b4fe into swiftlang:swift-3.0-branch Aug 4, 2016
@DougGregor DougGregor deleted the rdar-27541751-error-bridging-race branch August 4, 2016 05:35
kateinoigakukun pushed a commit that referenced this pull request Aug 31, 2022
[pull] swiftwasm from main
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.

3 participants