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

Crash introduced in 0.26.0 #2276

Closed
2 tasks done
johnhammerlund opened this issue Jul 2, 2018 · 17 comments
Closed
2 tasks done

Crash introduced in 0.26.0 #2276

johnhammerlund opened this issue Jul 2, 2018 · 17 comments
Labels

Comments

@johnhammerlund
Copy link

New Issue Checklist

There is a similar issue (#2039) but the stack traces seem unrelated

Bug Report

Complete output when running SwiftLint, including the stack trace and command used
$ swiftlint lint
Loading configuration from '.swiftlint.yml'
Linting Swift files in current working directory
Linting 'DarwinNotificationCenter.swift' (1/96)
Linting 'OAuth2Error.swift' (2/96)
Linting 'OAuth2TokenRefreshCoordinator.swift' (3/96)
Linting 'OAuth2TokenDiskStore.swift' (4/96)
...
Linting 'URLSessionClient.swift' (55/96)
Linting 'HTTPRequestBuilder.swift' (56/96)
[1]    53616 illegal hardware instruction  swiftlint

Environment

  • SwiftLint version (run swiftlint version to be sure)?
    0.26.0

  • Installation method used (Homebrew, CocoaPods, building from source, etc)?
    Homebrew

  • Paste your configuration file:

included:
  - Sources
  - Tests

excluded:
  - Sources/Conduit/Auth/Vendor

line_length: 160
statement_position:
  statement_mode: uncuddled_else
type_name:
  min_length: 3
  max_length: 60
identifier_name:
  min_length: 3
  max_length: 60

opt_in_rules:
  - attributes
  - closure_end_indentation
  - closure_spacing 
  - conditional_returns_on_newline
  - empty_count
  - fatal_error_message
  - first_where
  - force_unwrapping
  - joined_default_parameter
  - let_var_whitespace
  - nimble_operator
  - no_extension_access_modifier
  - number_separator
  - object_literal
  - operator_usage_whitespace
  - overridden_super_call
  - pattern_matching_keywords
  - private_outlet
  - prohibited_super_call
  - quick_discouraged_call 
  - redundant_nil_coalescing
  - single_test_class
  - strict_fileprivate
  - switch_case_on_newline
  - trailing_closure
  - unneeded_parentheses_in_closure_argument
  - vertical_parameter_alignment_on_call

Note: This crash still occurs when running swiftlint --enable-all-rules

  • Are you using nested configurations?
    If so, paste their relative paths and respective contents.
    No

  • Which Xcode version are you using (check xcode-select -p)?
    Xcode 9.4.1, active developer directory is set to /Applications/Xcode.app/Contents/Developer

  • Do you have a sample that shows the issue?
    Not yet sure about the exact source of the crash, but this exact failure can be observed here: https://travis-ci.org/mindbody/Conduit

The issue specifically started happening once SwiftLint was updated from 0.25.1 to 0.26.0.

Crash log:

Process:               swiftlint [53483]
Path:                  /usr/local/Cellar/swiftlint/0.26.0/bin/swiftlint
Identifier:            swiftlint
Version:               0
Code Type:             X86-64 (Native)
Parent Process:        zsh [50825]
Responsible:           swiftlint [53483]
User ID:               566097933

Date/Time:             2018-07-02 15:55:20.807 -0700
OS Version:            Mac OS X 10.13.4 (17E202)
Report Version:        12
Anonymous UUID:        77753AA3-F9B6-66B2-C051-8544C46F5487

Sleep/Wake UUID:       FB630A13-637E-437D-A2BE-DDCC8D1AF37B

Time Awake Since Boot: 170000 seconds
Time Since Wake:       25000 seconds

System Integrity Protection: enabled

Crashed Thread:        7  Dispatch queue: com.apple.root.user-initiated-qos

Exception Type:        EXC_BAD_INSTRUCTION (SIGILL)
Exception Codes:       0x0000000000000001, 0x0000000000000000
Exception Note:        EXC_CORPSE_NOTIFY

Termination Signal:    Illegal instruction: 4
Termination Reason:    Namespace SIGNAL, Code 0x4
Terminating Process:   exc handler [0]

Thread 7 Crashed:: Dispatch queue: com.apple.root.user-initiated-qos
0   swiftlint                     	0x00000001052c4704 NSString.CacheContainer.location(fromByteOffset:) + 468
1   swiftlint                     	0x00000001052c3dcd NSString.byteRangeToNSRange(start:length:) + 109
2   swiftlint                     	0x000000010546603f specialized NoFallthroughOnlyRule.validate(file:kind:dictionary:) + 143
3   swiftlint                     	0x000000010536b251 _T018SwiftLintFramework7ASTRulePAASS8KindType_8RawValueRTzrlE8validateSayAA14StyleViolationVG012SourceKittenC04FileC4file_s10DictionaryVySSAK0L16KitRepresentable_pG10dictionarytFAjRcfU_Tf4gnn_nAA12ForWhereRuleV_Tg5Tm + 433
4   swiftlint                     	0x000000010536b2f7 _T018SwiftLintFramework7ASTRulePAASS8KindType_8RawValueRTzrlE8validateSayAA14StyleViolationVG012SourceKittenC04FileC4file_s10DictionaryVySSAK0L16KitRepresentable_pG10dictionarytFAjRcfU_Tf4gnn_nAA21NoFallthroughOnlyRuleV_Tg5 + 23
5   swiftlint                     	0x000000010536b167 _T018SwiftLintFramework7ASTRulePAASS8KindType_8RawValueRTzrlE8validateSayAA14StyleViolationVG012SourceKittenC04FileC4file_s10DictionaryVySSAK0L16KitRepresentable_pG10dictionarytFAjRcfU_Tf4gnn_nAA12ForWhereRuleV_Tg5Tm + 199
6   swiftlint                     	0x000000010536b2f7 _T018SwiftLintFramework7ASTRulePAASS8KindType_8RawValueRTzrlE8validateSayAA14StyleViolationVG012SourceKittenC04FileC4file_s10DictionaryVySSAK0L16KitRepresentable_pG10dictionarytFAjRcfU_Tf4gnn_nAA21NoFallthroughOnlyRuleV_Tg5 + 23
7   swiftlint                     	0x000000010536b167 _T018SwiftLintFramework7ASTRulePAASS8KindType_8RawValueRTzrlE8validateSayAA14StyleViolationVG012SourceKittenC04FileC4file_s10DictionaryVySSAK0L16KitRepresentable_pG10dictionarytFAjRcfU_Tf4gnn_nAA12ForWhereRuleV_Tg5Tm + 199
8   swiftlint                     	0x000000010536b2f7 _T018SwiftLintFramework7ASTRulePAASS8KindType_8RawValueRTzrlE8validateSayAA14StyleViolationVG012SourceKittenC04FileC4file_s10DictionaryVySSAK0L16KitRepresentable_pG10dictionarytFAjRcfU_Tf4gnn_nAA21NoFallthroughOnlyRuleV_Tg5 + 23
9   swiftlint                     	0x000000010536b167 _T018SwiftLintFramework7ASTRulePAASS8KindType_8RawValueRTzrlE8validateSayAA14StyleViolationVG012SourceKittenC04FileC4file_s10DictionaryVySSAK0L16KitRepresentable_pG10dictionarytFAjRcfU_Tf4gnn_nAA12ForWhereRuleV_Tg5Tm + 199
10  swiftlint                     	0x000000010536b2f7 _T018SwiftLintFramework7ASTRulePAASS8KindType_8RawValueRTzrlE8validateSayAA14StyleViolationVG012SourceKittenC04FileC4file_s10DictionaryVySSAK0L16KitRepresentable_pG10dictionarytFAjRcfU_Tf4gnn_nAA21NoFallthroughOnlyRuleV_Tg5 + 23
11  swiftlint                     	0x000000010536dcdb _T018SwiftLintFramework7ASTRulePAASS8KindType_8RawValueRTzrlE8validateSayAA14StyleViolationVG012SourceKittenC04FileC4file_tFTf4gn_nAA12ForWhereRuleV_Tg5Tm + 171
12  swiftlint                     	0x0000000105368536 protocol witness for Rule.validate(file:) in conformance NoFallthroughOnlyRule + 22
13  swiftlint                     	0x0000000105341522 specialized Rule.lint(file:regions:benchmark:superfluousDisableCommandRule:) + 626
14  swiftlint                     	0x0000000105348790 partial apply for closure #2 in Linter.getStyleViolations(benchmark:) + 128
15  swiftlint                     	0x0000000105348811 _T018SwiftLintFramework4Rule_pAA0B6Result33_5A5F37FCDEE0B4D78BAE05A90BA8A062LLVSgIegio_AaB_pAFIegir_TRTA + 33
16  swiftlint                     	0x00000001052e081d specialized closure #1 in closure #1 in Array.parallelMap<A>(transform:) + 93
17  swiftlint                     	0x00000001052e7ae9 partial apply for specialized  + 25
18  swiftlint                     	0x0000000105609bf7 thunk for @callee_guaranteed (@unowned Int) -> () + 39
19  libdispatch.dylib             	0x00007fff6f832221 _dispatch_client_callout2 + 8
20  libdispatch.dylib             	0x00007fff6f835d71 _dispatch_apply_serial + 111
21  libdispatch.dylib             	0x00007fff6f827e08 _dispatch_client_callout + 8
22  libdispatch.dylib             	0x00007fff6f83e019 _dispatch_sync_function_invoke + 54
23  libdispatch.dylib             	0x00007fff6f832070 dispatch_apply_f + 947
24  swiftlint                     	0x0000000105609b99 static DispatchQueue.concurrentPerform(iterations:execute:) + 217
25  swiftlint                     	0x00000001052e05c5 specialized closure #1 in Array.parallelMap<A>(transform:) + 197
26  swiftlint                     	0x00000001052e5c88 specialized Array.parallelFlatMap<A>(transform:) + 232
27  swiftlint                     	0x000000010533966d Linter.getStyleViolations(benchmark:) + 1485
28  swiftlint                     	0x000000010533907d _T018SwiftLintFramework6LinterV15styleViolationsSayAA14StyleViolationVGvg + 13
29  swiftlint                     	0x00000001055cdd85 specialized closure #1 in LintCommand.run(_:) + 69
30  swiftlint                     	0x00000001055d22fc partial apply for closure #1 in LintCommand.run(_:) + 60
31  swiftlint                     	0x00000001055e0ed8 partial apply for closure #2 in closure #2 in closure #2 in Configuration.visitLintableFiles(path:action:useSTDIN:quiet:useScriptInputFiles:forceExclude:cache:parallel:visitorBlock:) + 104
32  swiftlint                     	0x00000001055e1599 closure #2 in closure #2 in closure #2 in Configuration.visitLintableFiles(path:action:useSTDIN:quiet:useScriptInputFiles:forceExclude:cache:parallel:visitorBlock:)partial apply + 9
33  swiftlint                     	0x00000001055e0f01 partial apply for thunk for @callee_guaranteed () -> (@error @owned Error) + 17
34  swiftlint                     	0x00000001055e1569 thunk for @callee_guaranteed () -> (@error @owned Error)partial apply + 9
35  swiftlint                     	0x00000001055ea57a autoreleasepool<A>(invoking:) + 58
36  swiftlint                     	0x00000001055e12fe specialized closure #2 in closure #2 in Configuration.visitLintableFiles(path:action:useSTDIN:quiet:useScriptInputFiles:forceExclude:cache:parallel:visitorBlock:) + 670
37  swiftlint                     	0x00000001055e0c63 partial apply for closure #2 in closure #2 in Configuration.visitLintableFiles(path:action:useSTDIN:quiet:useScriptInputFiles:forceExclude:cache:parallel:visitorBlock:) + 67
38  swiftlint                     	0x00000001055d94de closure #4 in closure #2 in Configuration.visitLintableFiles(path:action:useSTDIN:quiet:useScriptInputFiles:forceExclude:cache:parallel:visitorBlock:) + 126
39  swiftlint                     	0x00000001055e0f45 partial apply for closure #4 in closure #2 in Configuration.visitLintableFiles(path:action:useSTDIN:quiet:useScriptInputFiles:forceExclude:cache:parallel:visitorBlock:) + 21
40  swiftlint                     	0x0000000105609bf7 thunk for @callee_guaranteed (@unowned Int) -> () + 39
41  libdispatch.dylib             	0x00007fff6f832221 _dispatch_client_callout2 + 8
42  libdispatch.dylib             	0x00007fff6f83215d _dispatch_apply_invoke + 159
43  libdispatch.dylib             	0x00007fff6f827e08 _dispatch_client_callout + 8
44  libdispatch.dylib             	0x00007fff6f829b7c _dispatch_root_queue_drain + 902
45  libdispatch.dylib             	0x00007fff6f8297a5 _dispatch_worker_thread3 + 101
46  libsystem_pthread.dylib       	0x00007fff6fb79169 _pthread_wqthread + 1387
47  libsystem_pthread.dylib       	0x00007fff6fb78be9 start_wqthread + 13
@realm-probot realm-probot bot added the O:User label Jul 2, 2018
@eneko
Copy link

eneko commented Jul 2, 2018

The problem seems to reside in the SourceKitten framework, ironically caused by a force-unwrap.

screenshot 2018-07-02 16 27 03

SwiftLint 0.26.0 resolves to SourceKitten 0.21.0. Checking the SourceKitten repo, seems like a crash related to Swift 4.2 was fixed on the 0.21.1 release.

SourceKitten 0.21.1 does not seem to be available for SwiftLint (yet), since Yams is locked to 0.20.0.

error: dependency graph is unresolvable; found these conflicting requirements:

Dependencies: 
    https://github.com/jpsim/Yams.git @ 0.7.0..<1.0.0

@jpsim is there an easy way to remove that dependency block to see if 0.21.1 solves this issue?

@eneko
Copy link

eneko commented Jul 2, 2018

The file from our project being processed where SwiftLint crashes is this one (has not been updated in the last 7 months):
https://github.com/mindbody/Conduit/blob/master/Sources/Conduit/Networking/URLSessionClientLogging.swift

That file contains a few emoji used for logging purposes, which might be related to the encoding issues causing the crash in SourceKitten.

From the call-stack in Xcode, I believe the rule being validated is "no_fallthrough_only". Within that rule, the crash happens in NoFallthroughOnlyRule.swift, line 170:

public func validate(file: File,
                     kind: StatementKind,
                     dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] {

    guard kind == .case,
        let length = dictionary.length,
        let offset = dictionary.offset,
        case let nsstring = file.contents.bridge(),
        let range = nsstring.byteRangeToNSRange(start: offset, length: length),  // <-- 💥 crash
        let colonLocation = findCaseColon(text: nsstring, range: range)
    else {
        return []
    }
 
   ...

@SDGGiesbrecht
Copy link
Contributor

It is unlikely that it has anything to do with SourceKitten.

The crash is ultimately due to invalid string index conversions (scalar vs cluster vs byte vs UTF‐16 element).

Far more likely, something in the logic of the no_fallthrough_only rule is mixing them up and then asking SourceKitten to do something out of bounds. (Emoji have some of the largest indexing differences and no_fallthrough_only was added between 0.25.1 and 0.26.0.)

The relevant logic is here, but I could not spot the problem with a quick perusal.

@SDGGiesbrecht
Copy link
Contributor

...but I only see one direct call to byteRangeToNSRange and it uses an unaltered value from SourceKitten, so maybe I’m wrong.

Is the stack trace you provided from an build that was optimized? Are there possibly inlined functions not showing?

@marcelofabri
Copy link
Collaborator

I was able to reproduce the problem with this example:

switch code {
    case 200..<300:
        return "\(code)"
    default:
        break

Weirdly, it doesn't happen if I remove the string interpolation.

Also, it looks like the first byteRangeToNSRange call is the one causing the crash, but everything on the rule looks fine up to that point.

@marcelofabri
Copy link
Collaborator

I think this is the same issue as jpsim/SourceKitten#444 which was reported on SourceKit as well: https://bugs.swift.org/browse/SR-6443

@SDGGiesbrecht
Copy link
Contributor

Yes, you are right, @marcelofabri.

I did not think of it at first, but now I remember being confused by SourceKit reporting bizarre numbers around interpolation cases—and I was using it directly, not through SourceKitten.

@johnhammerlund, in the meantime disabling no_fallthrough_only ought to avoid triggering the crash.

@johnhammerlund
Copy link
Author

Great to know, thanks all for jumping on this so quickly. I'd like to keep that rule enabled, so I'll instead refactor the switch body as a workaround to SR-6443.

johnhammerlund added a commit to mindbody/Conduit that referenced this issue Jul 3, 2018
As discussed in realm/SwiftLint#2276, there is a SourceKit bug involving switch case annotations when string interpolation is involved, which throws off an underlying unicode conversion when running the new no_fallthrough_only rule and causes a crash. As a workaround, this moves the string interpolation outside of the switch body.
johnhammerlund added a commit to mindbody/Conduit that referenced this issue Jul 3, 2018
As discussed in realm/SwiftLint#2276, there is a SourceKit bug involving switch case annotations when string interpolation is involved, which throws off an underlying unicode conversion when running the new no_fallthrough_only rule and causes a crash. As a workaround, this moves the string interpolation outside of the switch body.
@4ndrey
Copy link

4ndrey commented Oct 18, 2018

same for me, and this show-stopper is still not fixed?

@barrault01
Copy link

@johnhammerlund, in the meantime disabling no_fallthrough_only ought to avoid triggering the crash.

This solution is really working ☝️ thanks @SDGGiesbrecht

@dudarenko-io
Copy link

dudarenko-io commented Nov 27, 2018

Reproduced this bug with this code

enum Foo {
    case one
    case two
}

func bar(foo: Foo) -> String {
    let some = "ru_RU"
    switch foo {
    case .one:
        return "\(some) один"
    default:
        return "\(some) два"
    }
}

disabling no_fallthrough_only rule helps to prevent this crash.

I noticed that removing spaces in return statements will not trigger the crash
return "\(some)два"

PS: reproduced on SwiftLint v 0.28.2 and 0.27

@jpsim
Copy link
Collaborator

jpsim commented Nov 27, 2018

Thanks for sharing @Statusgman, I was able to reproduce the crash with that example. Fix in jpsim/SourceKitten#570

@jpsim
Copy link
Collaborator

jpsim commented Nov 28, 2018

Fixed in #2487.

@jpsim jpsim closed this as completed Nov 28, 2018
@vigneshnarayanasamy
Copy link

This also happens when CommandLines tools for not set in PATH.

@eroth
Copy link

eroth commented Nov 13, 2020

This also happens when CommandLines tools for not set in PATH.

Or when xcode-select hasn't been used to select a version of Xcode and CommandLineTools is a command lines instance (#2789 (comment)).

@urajio
Copy link

urajio commented Jun 8, 2021

I have same problem with swiftlint 0.43.1

swiftlint
SourceKittenFramework/library_wrapper.swift:31: Fatal error: Loading sourcekitd.framework/Versions/A/sourcekitd failed
[1]    14984 illegal hardware instruction  swiftlint

Apple Swift version 5.4 (swiftlang-1205.0.26.9 clang-1205.0.19.55)

@jpsim
Copy link
Collaborator

jpsim commented Jun 8, 2021

@urajio please open a new issue taking care to fill out the template so we can help.

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

No branches or pull requests