Skip to content

🍒[cxx-interop] Fix runtime crash when casting from an existential to a foreign reference type #78238

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

Merged
merged 1 commit into from
Dec 18, 2024

Conversation

egorzhdan
Copy link
Contributor

@egorzhdan egorzhdan commented Dec 17, 2024

Explanation: When a C++ foreign reference type is conformed to a Swift protocol via a Swift extension, trying to cast any MyProtocol to the foreign reference type crashes the runtime. This was because selectCasterForDest wasn't handling C++ foreign reference types, and we were hitting swift_unreachable.
Scope: This changes the dynamic casting behavior for foreign reference types in the Swift runtime.
Risk: Low, this only changes a code path that is specific to foreign reference types.
Testing: Added compiler tests.
Issue: rdar://141227849
Reviewer: @mikeash @tbkka

Original PR: #78223

… foreign reference type

When a C++ foreign reference type is conformed to a Swift protocol via a Swift extension, trying to cast `any MyProtocol` to the foreign reference type crashes the runtime.

This was because `selectCasterForDest` wasn't handling C++ foreign reference types, and we were hitting `swift_unreachable`.

This change makes sure the runtime doesn't crash for such casts.

Notably, Swift doesn't have enough metadata to determine if the conditional cast actually succeeded. This is also a problem for CF types. Casting CF types in a similar fashion triggers a typechecker diagnostic. That diagnostic will be amended in a follow-up patch to also trigger for foreign reference types.

rdar://141227849
(cherry picked from commit 84ae5fb)
@egorzhdan egorzhdan added the c++ interop Feature: Interoperability with C++ label Dec 17, 2024
@egorzhdan
Copy link
Contributor Author

@swift-ci please test

@egorzhdan egorzhdan changed the title [cxx-interop] Fix runtime crash when casting from an existential to a foreign reference type 🍒[cxx-interop] Fix runtime crash when casting from an existential to a foreign reference type Dec 17, 2024
@egorzhdan egorzhdan marked this pull request as ready for review December 17, 2024 13:12
@egorzhdan egorzhdan requested a review from a team as a code owner December 17, 2024 13:12
@egorzhdan egorzhdan merged commit b4ca11e into release/6.1 Dec 18, 2024
5 checks passed
@egorzhdan egorzhdan deleted the egorzhdan/6.1-frt-as-existential-crash branch December 18, 2024 12:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ interop Feature: Interoperability with C++
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants