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

Hotfix alloc crash, when no read data avail. #24

Merged

Conversation

furby-tm
Copy link
Contributor

@furby-tm furby-tm commented May 5, 2024

Note

This fixes a Swift memory allocation crash, when attempting to allocate an internally managed buffer with a capacity of 0 (zero), if there's no read data available for the data copy we early out and return nil.

Backtrace

Thread 1 Crashed::  Dispatch queue: CosmoEditor.TreeSitter.EditQueue
0   libswiftCore.dylib                       0x1ad1eeb74 swift_slowAlloc.cold.1 + 16
1   libswiftCore.dylib                       0x1ad12d500 swift_slowAlloc + 88
2   libswiftCore.dylib                       0x1ad06da1c static UnsafeMutableBufferPointer.allocate(capacity:) + 124
3   Kraken                                   0x109a4d098 readFunction(payload:byteIndex:position:bytesRead:) + 596 (Input.swift:60)
4   Kraken                                   0x109a4cd74 @objc readFunction(payload:byteIndex:position:bytesRead:) + 40
5   Kraken                                   0x10af42f64 ts_lexer__get_chunk + 76 (lexer.c:59)
6   Kraken                                   0x10af5cf18 ts_lexer__do_advance + 548 (lexer.c:195)
7   Kraken                                   0x10af42518 ts_lexer__advance + 508 (lexer.c:217)
8   Kraken                                   0x10b201e24 ts_lex + 60
9   Kraken                                   0x10af5e3bc ts_parser__lex + 1224 (parser.c:485)
10  Kraken                                   0x10af4823c ts_parser__advance + 340 (parser.c:1441)
11  Kraken                                   0x10af477bc ts_parser_parse + 1384 (parser.c:1933)
12  Kraken                                   0x109a58028 Parser.parse(tree:encoding:readBlock:) + 368 (Parser.swift:116)
13  Kraken                                   0x100939ebc TreeSitterState.parseDocument(readCallback:readBlock:) + 484 (TreeSitterState.swift:94)
14  Kraken                                   0x100939504 TreeSitterState.init(codeLanguage:readCallback:readBlock:) + 296 (TreeSitterState.swift:52)
15  Kraken                                   0x1009393cc TreeSitterState.__allocating_init(codeLanguage:readCallback:readBlock:) + 88
16  Kraken                                   0x100935c3c closure #1 in TreeSitterClient.setState(language:readCallback:readBlock:) + 364 (TreeSitterClient.swift:145)
17  Kraken                                   0x100936430 closure #1 in TreeSitterClient.performAsync(_:) + 996 

LLDB Breakpoint

Process 88310 stopped
* thread #3, queue = 'CosmoEditor.TreeSitter.EditQueue', stop reason = EXC_BREAKPOINT (code=1, subcode=0x1ad1eeb74)
    frame #0: 0x00000001ad1eeb74 libswiftCore.dylib`swift_slowAlloc.cold.1 + 16
libswiftCore.dylib`swift_slowAlloc.cold.1:
->  0x1ad1eeb74 <+16>: brk    #0x1

libswiftCore.dylib`swift_allocBox.cold.1:
    0x1ad1eeb78 <+0>:  adrp   x8, 186
    0x1ad1eeb7c <+4>:  add    x8, x8, #0x8f0            ; "Could not allocate memory."
    0x1ad1eeb80 <+8>:  adrp   x9, 359303

* This fixes a Swift allocation crash, when attempting to
  allocate an internally managed buffer with a capacity of
  0 (zero), if there's no read data available for the data
  copy - we early out and return nil.
@mattmassicotte
Copy link
Contributor

Wow! Thank you!

Do you have a situation that can easily reproduce this? I’d love to capture it in a test.

@mattmassicotte mattmassicotte merged commit 96ad58b into tree-sitter:main May 5, 2024
6 checks passed
@furby-tm
Copy link
Contributor Author

furby-tm commented May 5, 2024

Wow! Thank you!

Do you have a situation that can easily reproduce this? I’d love to capture it in a test.

It's a weird one for sure! I was able to reproduce it from this project here, simply adjust the following line, by shifting branch: "hotfix-read-empty-noalloc" to revision: "b0e00f8", and it should summon the error immediately upon launching the application (or at least upon opening any file within that program that enables syntax highlighting; such as opening a *.swift file from the File > Open dialog) -- after building and running the app via swift run.

However; I'm also building all of Pixar's Universal Scene Description with that Swift package 😅; so I would advise a much smaller reproducible project than having to deal with all of that.

If I find some time I may be able to create you a small SwiftPM test project repo, copying over my same TreeSitters over to that project repo along with a super basic little "hello world" executable app to spool it up if that's something that may be of use to you.

mattmassicotte added a commit that referenced this pull request Jul 3, 2024
@mattmassicotte
Copy link
Contributor

It turned out to be easy to make a test that goes down this code path: f46c79e

However, bizarrely, it does not crash! I wonder if this is something that is ok in debug mode? Anyways, I think that's suficeint for now. We tried. Thanks again for your help here.

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