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-9744] Swift 4.2.1: runtime crash on swift_dynamicCastMetatypeImpl #52175

Open
swift-ci opened this issue Jan 24, 2019 · 0 comments
Open

[SR-9744] Swift 4.2.1: runtime crash on swift_dynamicCastMetatypeImpl #52175

swift-ci opened this issue Jan 24, 2019 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself crash Bug: A crash, i.e., an abnormal termination of software run-time crash Bug → crash: Swift code crashed during execution standard library Area: Standard library umbrella

Comments

@swift-ci
Copy link
Contributor

Previous ID SR-9744
Radar rdar://problem/47550832
Original Reporter broadway_lamb (JIRA User)
Type Bug
Environment

Ubuntu 16.04

Swift version 4.2.1 (swift-4.2.1-RELEASE)

Target: x86_64-unknown-linux-gnu

Additional Detail from JIRA
Votes 0
Component/s Compiler, Standard Library
Labels Bug, RunTimeCrash
Assignee None
Priority Medium

md5: a45a97a62d8c0db772a41323be90935d

Issue Description:

I'm experiencing a runtime crash on Ubuntu when executing tests for this SPM project https://github.com/broadwaylamb/DependencyFetcher/tree/03e329d5aeeec928546e055a9ea08cd7a4e74c24 on Ubuntu:

* thread #​1, name = 'DependencyFetch', stop reason = signal SIGSEGV: invalid address (fault address: 0x30)
  * frame #&#8203;0: 0x00007ffff765b164 libswiftCore.so`swift_dynamicCastMetatypeImpl(swift::TargetMetadata<swift::InProcess> const*, swift::TargetMetadata<swift::InProcess> const*) + 4
    frame #&#8203;1: 0x00007ffff740acaa libswiftCore.so`swift_swiftValueConformsTo + 74
    frame #&#8203;2: 0x00007ffff765bc25 libswiftCore.so`_dynamicCastToExistential(swift::OpaqueValue*, swift::OpaqueValue*, swift::TargetMetadata<swift::InProcess> const*, swift::TargetExistentialTypeMetadata<swift::InProcess> const*, swift::DynamicCastFlags) + 565
    frame #&#8203;3: 0x00007ffff765b43d libswiftCore.so`checkDynamicCastFromOptional(swift::OpaqueValue*, swift::OpaqueValue*, swift::TargetMetadata<swift::InProcess> const*, swift::TargetMetadata<swift::InProcess> const*, swift::DynamicCastFlags) + 93
    frame #&#8203;4: 0x00007ffff765a00f libswiftCore.so`swift_dynamicCastImpl(swift::OpaqueValue*, swift::OpaqueValue*, swift::TargetMetadata<swift::InProcess> const*, swift::TargetMetadata<swift::InProcess> const*, swift::DynamicCastFlags) + 31
    frame #&#8203;5: 0x00005555555610b2 DependencyFetcherPackageTests.xctest`closure #&#8203;1 in Fetcher.fetch<A>(self=0x0000604000002610, serviceID=(_value = 0x0000603000002e30 -> 0x00007ffff77a48f8 libswiftCore.so`InitialAllocationPool + 15712), service=(makeService = 0x007ffff77a48f872)) at Fetcher.swift:95
    frame #&#8203;6: 0x0000555555561c65 DependencyFetcherPackageTests.xctest`partial apply for closure #&#8203;1 in Fetcher.fetch<A>(_:) at <compiler-generated>:0
    frame #&#8203;7: 0x00007ffff7fa2ca1 libdispatch.so`partial apply forwarder for reabstraction thunk helper <A> from @callee_guaranteed () -> (@out A, @error @owned Swift.Error) to @escaping @callee_guaranteed () -> (@out A, @error @owned Swift.Error) + 17
    frame #&#8203;8: 0x00007ffff7f9b122 libdispatch.so`closure #&#8203;1 () -> () in closure #&#8203;1 (() throws -> A) -> () in Dispatch.DispatchQueue.(_syncHelper in _F417D752D2C4E9330E1C700411CE0C6A)<A>(fn: (() -> ()) -> (), execute: () throws -> A, rescue: (Swift.Error) throws -> A) throws -> A + 82
    frame #&#8203;9: 0x00007ffff7fa2d0d libdispatch.so`merged partial apply forwarder for closure #&#8203;1 () -> () in Dispatch.DispatchQueue.(_syncHelper in _F417D752D2C4E9330E1C700411CE0C6A)<A>(fn: (Dispatch.DispatchWorkItem) -> (), flags: Dispatch.DispatchWorkItemFlags, execute: () throws -> A, rescue: (Swift.Error) throws -> A) throws -> A + 29
    frame #&#8203;10: 0x00007ffff7fa2ce0 libdispatch.so`partial apply forwarder for closure #&#8203;1 () -> () in closure #&#8203;1 (() throws -> A) -> () in Dispatch.DispatchQueue.(_syncHelper in _F417D752D2C4E9330E1C700411CE0C6A)<A>(fn: (() -> ()) -> (), execute: () throws -> A, rescue: (Swift.Error) throws -> A) throws -> A + 16
    frame #&#8203;11: 0x00007ffff7fa0131 libdispatch.so`partial apply forwarder for reabstraction thunk helper from @callee_guaranteed () -> () to @escaping @callee_guaranteed () -> () + 17
    frame #&#8203;12: 0x00007ffff7f93c0e libdispatch.so`reabstraction thunk helper from @escaping @callee_guaranteed () -> () to @callee_unowned @convention(block) () -> () + 14
    frame #&#8203;13: 0x00007ffff7fb206f libdispatch.so`_dispatch_queue_barrier_sync_invoke_and_complete + 95
    frame #&#8203;14: 0x00007ffff7f9d53e libdispatch.so`Dispatch.DispatchQueue.sync(execute: () -> ()) -> () + 158
    frame #&#8203;15: 0x00007ffff7f9fa81 libdispatch.so`partial apply forwarder + 17
    frame #&#8203;16: 0x00007ffff7f9afcb libdispatch.so`Dispatch.DispatchQueue.(_syncHelper in _F417D752D2C4E9330E1C700411CE0C6A)<A>(fn: (() -> ()) -> (), execute: () throws -> A, rescue: (Swift.Error) throws -> A) throws -> A + 267
    frame #&#8203;17: 0x00007ffff7f9b48e libdispatch.so`Dispatch.DispatchQueue.sync<A>(execute: () throws -> A) throws -> A + 174
    frame #&#8203;18: 0x00005555555604fe DependencyFetcherPackageTests.xctest`Fetcher.fetch<A>(service=(makeService = 0x007ffff77a48f872), self=0x0000604000002610) at Fetcher.swift:94
    frame #&#8203;19: 0x00005555555653d4 DependencyFetcherPackageTests.xctest`FetcherTests.testDefaultFetcher(self=0x00006070000003a0) at FetcherTests.swift:24
    frame #&#8203;20: 0x0000555555568bb9 DependencyFetcherPackageTests.xctest`partial apply for FetcherTests.testDefaultFetcher() at <compiler-generated>:0
    frame #&#8203;21: 0x0000555555565bdc DependencyFetcherPackageTests.xctest`thunk for @escaping @callee_guaranteed () -> () at <compiler-generated>:0
    frame #&#8203;22: 0x0000555555568ba1 DependencyFetcherPackageTests.xctest`partial apply for thunk for @escaping @callee_guaranteed () -> () at <compiler-generated>:0
    frame #&#8203;23: 0x00007ffff7efce71 libXCTest.so`partial apply forwarder for reabstraction thunk helper from @escaping @callee_guaranteed (@in_guaranteed ()) -> (@out ()) to @escaping @callee_guaranteed () -> () + 17
    frame #&#8203;24: 0x00007ffff7efcecc libXCTest.so`partial apply forwarder for closure #&#8203;1 (XCTest.XCTestCase) throws -> () in XCTest.(test in _3BE257A46ADB477C7BF2D39968B39F9D)<A where A: XCTest.XCTestCase>((A) -> () throws -> ()) -> (XCTest.XCTestCase) throws -> () + 76
    frame #&#8203;25: 0x00007ffff7efce54 libXCTest.so`partial apply forwarder for reabstraction thunk helper from @escaping @callee_guaranteed (@guaranteed XCTest.XCTestCase) -> (@error @owned Swift.Error) to @escaping @callee_guaranteed (@in_guaranteed XCTest.XCTestCase) -> (@out (), @error @owned Swift.Error) + 20
    frame #&#8203;26: 0x00007ffff7efd377 libXCTest.so`partial apply forwarder for reabstraction thunk helper from @escaping @callee_guaranteed (@in_guaranteed XCTest.XCTestCase) -> (@out (), @error @owned Swift.Error) to @escaping @callee_guaranteed (@guaranteed XCTest.XCTestCase) -> (@error @owned Swift.Error) + 39
    frame #&#8203;27: 0x00007ffff7ee9c88 libXCTest.so`XCTest.XCTestCase.invokeTest() -> () + 72
    frame #&#8203;28: 0x00007ffff7ee990a libXCTest.so`XCTest.XCTestCase.perform(XCTest.XCTestRun) -> () + 314
    frame #&#8203;29: 0x00007ffff7ee1930 libXCTest.so`XCTest.XCTest.run() -> () + 640
    frame #&#8203;30: 0x00007ffff7ee0cee libXCTest.so`XCTest.XCTestSuite.perform(XCTest.XCTestRun) -> () + 654
    frame #&#8203;31: 0x00007ffff7ee1210 libXCTest.so`XCTest.XCTestSuite.perform(XCTest.XCTestRun) -> () + 1968
    frame #&#8203;32: 0x00007ffff7ee1016 libXCTest.so`XCTest.XCTestSuite.perform(XCTest.XCTestRun) -> () + 1462
    frame #&#8203;33: 0x00007ffff7ee8c18 libXCTest.so`XCTest.XCTMain(Swift.Array<(testCaseClass: XCTest.XCTestCase.Type, allTests: Swift.Array<(Swift.String, (XCTest.XCTestCase) throws -> ())>)>) -> Swift.Never + 3960
    frame #&#8203;34: 0x00005555555648e1 DependencyFetcherPackageTests.xctest`main at LinuxMain.swift:17
    frame #&#8203;35: 0x00007ffff5b85830 libc.so.6`__libc_start_main(main=(DependencyFetcherPackageTests.xctest`main at LinuxMain.swift), argc=1, argv=0x00007fffffffe3b8, init=<unavailable>, fini=<unavailable>, rtld_fini=<unavailable>, stack_end=0x00007fffffffe3a8) at libc-start.c:291

Here's the code where the crash occurs:

  public func fetch<T>(_ service: Service<T>) -> T {
    let serviceID = ObjectIdentifier(service)
    return _syncQueue.sync {
      if let service = _instances[serviceID] as? T { // CRASH
        return service
      } else {
        let instance = _overrides[serviceID]?() as? T ?? service.makeService() // CRASH
        _instances[serviceID] = instance
        return instance
      }
    }
  }

The crash occurs only when running `swift test` for the package. When the package is added as a dependency for an executable with the same code as in the testing method, everything is fine.

This code works fine though:

  public func fetch<T>(_ service: Service<T>) -> T {
    let serviceID = ObjectIdentifier(service)
    return _syncQueue.sync {
      let instance = _instances[serviceID]
      if instance != nil, let instance = instance as? T {
        return instance
      } else {
        let overridenMakeService = _overrides[serviceID]
        let result: T
        if overridenMakeService != nil,
          let inst = overridenMakeService!() as? T {
          result = inst
        } else {
          result = service.makeService()
        }
        _instances[serviceID] = result
        return result
      }
    }
  }

Unfortunately, I wasn't able to reduce the test case.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added the crash Bug: A crash, i.e., an abnormal termination of software label Dec 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself crash Bug: A crash, i.e., an abnormal termination of software run-time crash Bug → crash: Swift code crashed during execution standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

2 participants