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

[SR-936] NSMutableData's hash() causes a segmentation fault on Linux #4577

Closed
swift-ci opened this issue Mar 14, 2016 · 5 comments
Closed

[SR-936] NSMutableData's hash() causes a segmentation fault on Linux #4577

swift-ci opened this issue Mar 14, 2016 · 5 comments

Comments

@swift-ci
Copy link
Contributor

Previous ID SR-936
Radar None
Original Reporter inket (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment

Ubuntu 14.04, Swift version 3.0-dev (LLVM b361b0fc05, Clang 11493b0f62, Swift 24a0c3de75)

Reproduced on Ubuntu 16.04, Swift version 4.0.1-dev (LLVM 2dedb62a0b, Clang ab7472e733, Swift 7efb2d13a2)

Additional Detail from JIRA
Votes 3
Component/s Foundation
Labels Bug, Linux
Assignee @spevans
Priority Medium

md5: bf8cffe9642151374ef9e452574daaaf

relates to:

  • SR-1547 NSData causes segmentation fault

Issue Description:

The current implementation of NSData's hash() causes a segmentation fault on Linux.

Using the REPL on Mac:

Welcome to Apple Swift version 3.0-dev (LLVM b361b0fc05, Clang 11493b0f62, Swift 24a0c3de75). Type :help for assistance.
  1> import Foundation
  2> "foobar".dataUsingEncoding(NSUTF8StringEncoding)?.hash
$R0: Int? = 114710658

on Linux:

Welcome to Swift version 3.0-dev (LLVM b361b0fc05, Clang 11493b0f62, Swift 24a0c3de75). Type :help for assistance.
  1> import Foundation
  2> "foobar".dataUsingEncoding(NSUTF8StringEncoding)?.hash
Execution interrupted. Enter code to recover and continue.
@swift-ci
Copy link
Contributor Author

Comment by Christian Rishøj (JIRA)

A stack overflow appears to occur with NSMutableData, but not with NSData.

Reproducible with the following minimal test case (using Swift 2.2 on Ubuntu 14.04):

Welcome to Swift version 2.2-dev (LLVM 524cfeb800, Clang f66c5bb67b, Swift 870696170f). Type :help for assistance.
  1> import Foundation
  2> NSData().hash
$R0: Int = 0
  3> NSMutableData().hash
Execution interrupted. Enter code to recover and continue.
Enter LLDB commands to investigate (type :help for assistance.)

Backtrace snippet:

  4> :bt 10
* thread #​1: tid = 18075, 0x00007ffff429dd7c libFoundation.so`static Foundation.=== infix (Swift.AnyObject.Type, Swift.AnyObject.Type) -> Swift.Bool + 12, name = 'repl_swift', stop reason = signal SIGSEGV: invalid address (fault address: 0x7fffff7feff8)
  * frame #​0: 0x00007ffff429dd7c libFoundation.so`static Foundation.=== infix (Swift.AnyObject.Type, Swift.AnyObject.Type) -> Swift.Bool + 12
    frame #​1: 0x00007ffff4119c47 libFoundation.so`Foundation.NSData._cfObject.getter : __ObjC.CFData + 39
    frame #​2: 0x00007ffff411a051 libFoundation.so`Foundation.NSData.hash.getter : Swift.Int + 17
    frame #​3: 0x00007ffff429b03a libFoundation.so`Foundation._CFSwiftGetHash (Swift.AnyObject) -> Swift.UInt + 58
    frame #​4: 0x00007ffff429c50a libFoundation.so`@objc Foundation._CFSwiftGetHash (Swift.AnyObject) -> Swift.UInt + 26
    frame #​5: 0x00007ffff411a05d libFoundation.so`Foundation.NSData.hash.getter : Swift.Int + 29
    frame #​6: 0x00007ffff429b03a libFoundation.so`Foundation._CFSwiftGetHash (Swift.AnyObject) -> Swift.UInt + 58
    frame #​7: 0x00007ffff429c50a libFoundation.so`@objc Foundation._CFSwiftGetHash (Swift.AnyObject) -> Swift.UInt + 26
    frame #​8: 0x00007ffff411a05d libFoundation.so`Foundation.NSData.hash.getter : Swift.Int + 29
    frame #​9: 0x00007ffff429b03a libFoundation.so`Foundation._CFSwiftGetHash (Swift.AnyObject) -> Swift.UInt + 58

@tbkka
Copy link
Contributor

tbkka commented Jan 10, 2017

I don't think this is related to SR-1547, as there's no error objects here. The key symptom of this bug is the endless mutual recursion between `_CFSwiftGetHash` and `NSData.hash.getter`.

I've seen that same symptom in a larger program that used Foundation.Data, but only on Linux, not on macOS. The workaround in that case was to first check if the data object was zero-length and not request the hashValue unless it had at least one byte.

@alblue
Copy link
Contributor

alblue commented Oct 5, 2017

The original issue seems to be fixed in 4.0.1-dev:

{{$ swift -I /usr/lib/swift/clang/include
Welcome to Swift version 4.0.1-dev (LLVM 2dedb62a0b, Clang ab7472e733, Swift 7efb2d13a2). Type :help for assistance.
1> import Foundation
2> "foobar".data(using:.utf8)?.hashValue
$R0: Int? = 114710658
}}

The second problem, that there is a recursive call on NSMutableData's hash, still seems to exist:

{{ 3> NSData().hash
$R1: Int = 0
4> NSMutableData().hash
Execution interrupted. Enter code to recover and continue.
Enter LLDB commands to investigate (type :help for assistance.)
Process 149 stopped

  • thread Fixing whitespace formatting #1, name = 'repl_swift', stop reason = signal SIGSEGV: invalid address (fault address: 0x7fffff7feff8)
    frame #0: 0x00007ffff3bbf294 libFoundation.so`merged Foundation.NSData.length.getter : Swift.Int + 4
    libFoundation.so`merged Foundation.NSData.length.getter : Swift.Int:
    -> 0x7ffff3bbf294 <+4>: pushq %r15
    0x7ffff3bbf296 <+6>: pushq %r14
    0x7ffff3bbf298 <+8>: pushq %rbx
    0x7ffff3bbf299 <+9>: pushq %rax
    Target 0: (repl_swift) stopped.
    }}

@spevans
Copy link
Collaborator

spevans commented Aug 26, 2018

Possible fix: #1669

@spevans
Copy link
Collaborator

spevans commented Aug 27, 2018

Merged to `master` branch.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 6, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants