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-14041] Protocol allows to create a nested enum without explicit indirect keyword resulting in a runtime crash #56432

Open
swift-ci opened this issue Jan 13, 2021 · 3 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-14041
Radar rdar://problem/73158441
Original Reporter siejkowski (JIRA User)
Type Bug
Environment

Xcode 12.3

Apple Swift version 5.3.2 (swiftlang-1200.0.45 clang-1200.0.32.28)

Target: x86_64-apple-darwin20.2.0

Additional Detail from JIRA
Votes 2
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: 4ea4587179ea7c27cb24f95ce9490b74

is duplicated by:

  • SR-14071 Enum case as protocol witness uses incorrect ARC semantics for payload
  • SR-14163 Unexpected twice deallocation when constructing enum cases
  • SR-14171 Double free when using enum case as protocol witness

Issue Description:

It's possible to create a nested enum without explicit `indirect` keyword by taking advantage of the fact that enum cases are protocol witnesses.

Trying to access the inner value in such case results in a runtime crash.

The minimal sample code reproducing the problem:

protocol IndirectEnumType {
  static func inner(_: IndirectEnumType?) -> Self
}

enum IndirectEnum: IndirectEnumType {
  case inner(IndirectEnumType?)
}

func createIndirectEnumWithInner<E: IndirectEnumType>(type: E.Type) -> E {
  E.inner(E.inner(nil))
}

let value = createIndirectEnumWithInner(type: IndirectEnum.self)
if case let .inner(innerValue?) = value {
  print(innerValue) // crashing on access to inner value
}

Even if the `indirect` keyword is provided, the crash on access to inner value still occurs:

protocol IndirectEnumType {
  static func inner(_: IndirectEnumType?) -> Self
}

indirect enum IndirectEnum: IndirectEnumType {
  case inner(IndirectEnumType?)
}

func createIndirectEnumWithInner<E: IndirectEnumType>(type: E.Type) -> E {
  E.inner(E.inner(nil))
}

let value = createIndirectEnumWithInner(type: IndirectEnum.self)
if case let .inner(innerValue?) = value {
  print(innerValue) // crashing on access to inner value
}
@typesanitizer
Copy link

@swift-ci create

@jckarter
Copy link
Member

There's nothing formally wrong with this, since the existential box for the `IndirectEnumType` existential type introduces indirection already. The crash is because of a calling convention mismatch between the protocol witness and the case constructor, which we can fix.

@nate-chandler
Copy link
Contributor

It looks like this was fixed on release/5.4 (#35853 and main (#35838 Can you verify with a recent toolchain or Xcode12.5 Beta 3?

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 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 in itself
Projects
None yet
Development

No branches or pull requests

4 participants