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-14163] Unexpected twice deallocation when constructing enum cases #56542

Closed
swift-ci opened this issue Feb 7, 2021 · 0 comments
Closed
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Feb 7, 2021

Previous ID SR-14163
Radar None
Original Reporter gettoset (JIRA User)
Type Bug
Status Resolved
Resolution Duplicate
Environment

Xcode Version 12.4 (12D4e)

> swift --version
Apple Swift version 5.3.2 (swiftlang-1200.0.45 clang-1200.0.32.28) Target: x86_64-apple-darwin20.3.0
Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Bug
Assignee None
Priority Medium

md5: d2112017708c38057866d77d3e22e56d

duplicates:

  • SR-14041 Protocol allows to create a nested enum without explicit indirect keyword resulting in a runtime crash

Issue Description:

I ran into strange crashes when accessing model objects when I'm developing an iOS app. I spent some time diving deeper into these problems, I found that constructing enum cases in such a way like the following code will result twice deallocation of same object, which in turn crashes the whole app.

I believe its a bug of Swift language itself but I had little knowledge about its internals. Here is the self-contained code to reproduce this problem:

// MARK: - ViewModel

protocol ViewModel: class {
  associatedtype Action
}

protocol DomainAction {
  static func event(_: Event) -> Self
}

protocol DomainViewModel: ViewModel where Action: DomainAction { }

final class ConcreteViewModel: DomainViewModel {
  enum Action: DomainAction {
    case event(_: Event)
  }
}

// MARK: - Event

struct EventPayload {
  let foo: String
  let bar: String
}

protocol Event {
  var payload: EventPayload { get }
}

struct ConcreteEvent: Event {
  let payload: EventPayload
}

// MARK: - View

final class View<ViewModel: DomainViewModel> {
  init() {
    _ = ViewModel.Action.event(ConcreteEvent(payload: EventPayload(foo: "foo", bar: "bar")))
  }
}

_ = View<ConcreteViewModel>()

and here is the stacktrace

Crashed Thread:        0  Dispatch queue: com.apple.main-thread

Exception Type:        EXC_CRASH (SIGABRT)
Exception Codes:       0x0000000000000000, 0x0000000000000000
Exception Note:        EXC_CORPSE_NOTIFY

Application Specific Information:
abort() called
TestAppCrash(307,0x1101e9e00) malloc: Double free of object 0x7fb216805f20
 

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   libsystem_kernel.dylib          0x00007fff20392462 __pthread_kill + 10
1   libsystem_pthread.dylib         0x00007fff203c0610 pthread_kill + 263
2   libsystem_c.dylib               0x00007fff20313720 abort + 120
3   libsystem_malloc.dylib          0x00007fff201f4430 malloc_vreport + 548
4   libsystem_malloc.dylib          0x00007fff20208702 malloc_zone_error + 183
5   TestAppCrash                    0x000000010f6f4f05 0x10f6f0000 + 20229
6   libswiftCore.dylib              0x00007fff2cac7820 _swift_release_dealloc + 16
7   TestAppCrash                    0x000000010f6f3e1d __swift_destroy_boxed_opaque_existential_1 + 77
8   TestAppCrash                    0x000000010f6f3f41 destroy for ConcreteViewModel.Action + 17
9   TestAppCrash                    0x000000010f6f3b84 closure #&#8203;1 in View.init() + 452 (main.swift:48)
10  TestAppCrash                    0x000000010f6f3c1c thunk for @escaping @callee_guaranteed (@guaranteed ConcreteEvent) -> () + 60
11  TestAppCrash                    0x000000010f7c5bd3 closure #&#8203;1 in ObservableType.subscribe(onNext:onError:onCompleted:onDisposed:) + 739
12  TestAppCrash                    0x000000010f7c5f73 partial apply for closure #&#8203;1 in ObservableType.subscribe(onNext:onError:onCompleted:onDisposed:) + 115
13  TestAppCrash                    0x000000010f6fc7cf AnonymousObserver.onCore(_:) + 95
14  TestAppCrash                    0x000000010f7cd8a1 ObserverBase.on(_:) + 273
15  TestAppCrash                    0x000000010f7cdb67 protocol witness for ObserverType.on(_:) in conformance ObserverBase<A> + 23
16  TestAppCrash                    0x000000010f7a35cf Just.subscribe<A>(_:) + 319
17  TestAppCrash                    0x000000010f7c56d6 ObservableType.subscribe(onNext:onError:onCompleted:onDisposed:) + 1702
18  TestAppCrash                    0x000000010f6f3750 View.init() + 768 (main.swift:47)
19  TestAppCrash                    0x000000010f6f3441 View.__allocating_init() + 33
20  TestAppCrash                    0x000000010f6f3038 main + 40 (main.swift:55)
21  libdyld.dylib                   0x00007fff203db621 start + 1

Thread 0 crashed with X86 Thread State (64-bit):
  rax: 0x0000000000000000  rbx: 0x00000001101e9e00  rcx: 0x00007ffee050e6f8  rdx: 0x0000000000000000
  rdi: 0x0000000000000307  rsi: 0x0000000000000006  rbp: 0x00007ffee050e720  rsp: 0x00007ffee050e6f8
   r8: 0x0000000000000000   r9: 0x0000000000000000  r10: 0x00000001101e9e00  r11: 0x0000000000000246
  r12: 0x0000000000000307  r13: 0x0000000000000043  r14: 0x0000000000000006  r15: 0x0000000000000016
  rip: 0x00007fff20392462  rfl: 0x0000000000000246  cr2: 0x000000010fcce000
  
Logical CPU:     0
Error Code:      0x02000148
Trap Number:     133

I hope the community can help in further investigation of this problem, thanks.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
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. standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

1 participant