Skip to content

[SourceKit] Add defensive guard for invalid offset #14803

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
Feb 28, 2018

Conversation

rintaro
Copy link
Member

@rintaro rintaro commented Feb 23, 2018

Invalid (too high) offset used to cause a inifinite loop in Lexer. This could happens if:

  1. Client send a request to SourceKit
  2. User somehow truncate the file
  3. SourceKit handle the request asynchronously

This is a quick fix until we fix desynchronization problem in SourceKit.
Making a test case is not easy at this point because we currently don't have a tool to test this scenario.
rdar://problem/30346106

@rintaro
Copy link
Member Author

rintaro commented Feb 23, 2018

@swift-ci Please smoke test

@rintaro rintaro requested a review from akyrtzi February 23, 2018 16:38
Copy link
Contributor

@akyrtzi akyrtzi left a comment

Choose a reason for hiding this comment

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

Is it not possible to create a unit-test for this ? See unittests/SourceKit/SwiftLang/CursorInfoTest.cpp

@rintaro
Copy link
Member Author

rintaro commented Feb 24, 2018

Ah, I forgot about unittests, I will.

@rintaro
Copy link
Member Author

rintaro commented Feb 24, 2018

@akyrtzi Sorry, it was not so easy. This private getSourceToken() function is only called from
https://github.com/rintaro/swift/blob/4fe5ead3/tools/SourceKit/lib/SwiftLang/SwiftSourceDocInfo.cpp#L1167
And I haven't found the code path to reach here. Specifically, do you know how to pass this Snap->getStamp() == InputSnap->getStamp() condition?
My scenario in the description seems wrong.

@akyrtzi
Copy link
Contributor

akyrtzi commented Feb 26, 2018

I think you'll go pass that condition if you make a cursor info call, make an edit, then make another cursor call. But this may depend on timing, not sure.

If you can't get it to trigger via a unit test as is, I'd be fine with exposing the getSourceToken() function somewhere in a header and testing it directly.

Invalid offset used to cause a inifinite loop in Lexer in some race
condition.

This is a quick fix until we fix underlying problem in SourceKit.
@rintaro rintaro force-pushed the sourcekit-offset-guard branch from 4fe5ead to 3f232e7 Compare February 27, 2018 09:16
@rintaro
Copy link
Member Author

rintaro commented Feb 27, 2018

I managed to reproduce the crash (assertion failure) with sourcekitd-test.
Added test case. Thanks!

@rintaro
Copy link
Member Author

rintaro commented Feb 27, 2018

@swift-ci Please smoke test

@rintaro rintaro merged commit cbeacfd into swiftlang:master Feb 28, 2018
@rintaro rintaro deleted the sourcekit-offset-guard branch February 28, 2018 01:21
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