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

🍒[cxx-interop][Runtime] Initialize metadata of a Swift array of C++ references correctly #73671

Merged
merged 1 commit into from
May 17, 2024

Conversation

egorzhdan
Copy link
Contributor

@egorzhdan egorzhdan commented May 16, 2024

Explanation: This fixes a crash at runtime when destroying a Swift array of values of a C++ foreign reference type. Swift optimizes the amount of metadata emitted for _ContiguousArrayStorage<Element> by reusing _ContiguousArrayStorage<AnyObject> whenever possible (see getContiguousArrayStorageType). However, C++ foreign reference types are not AnyObjects, since they have custom retain/release operations. This change disables the _ContiguousArrayStorage metadata optimization for C++ reference types, which makes sure that swift_arrayDestroy will call the correct release operation for elements of [MyCxxRefType].
Scope: Changes the runtime handling of C++ foreign reference types – they will no longer be treated as AnyObjects by the runtime. Also changes the SIL logic that handles the _ContiguousArrayStorage<AnyObject> optimization.
Risk: Low, only affects C++ foreign reference types.
Testing: Added a compile test.
Issue: rdar://127154770 / #72827
Reviewer: @mikeash @aschwaighofer @zoecarver

Original PR: #73615

…ferences correctly

This fixes a crash at runtime when destroying a Swift array of values of a C++ foreign reference type.

Swift optimizes the amount of metadata emitted for `_ContiguousArrayStorage<Element>` by reusing `_ContiguousArrayStorage<AnyObject>` whenever possible (see `getContiguousArrayStorageType`). However, C++ foreign reference types are not `AnyObject`s, since they have custom retain/release operations.

This change disables the `_ContiguousArrayStorage` metadata optimization for C++ reference types, which makes sure that `swift_arrayDestroy` will call the correct release operation for elements of `[MyCxxRefType]`.

rdar://127154770
(cherry picked from commit fd04cc3)
@egorzhdan egorzhdan added the c++ interop Feature: Interoperability with C++ label May 16, 2024
@egorzhdan
Copy link
Contributor Author

@swift-ci please test

@egorzhdan egorzhdan marked this pull request as ready for review May 16, 2024 16:05
@egorzhdan egorzhdan requested a review from a team as a code owner May 16, 2024 16:05
@@ -1,5 +1,6 @@
// RUN: %target-swift-emit-irgen %s -I %S/Inputs -cxx-interoperability-mode=default -Xcc -fignore-exceptions -disable-availability-checking | %FileCheck %s
// XFAIL: OS=linux-android, OS=linux-androideabi
// XFAIL: OS=windows-msvc
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? If there are really platform-specific tests here, can we put them in a separate file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Foreign reference types don't yet work on Windows, since we don't have the lazy metadata initialization logic implemented: https://github.com/apple/swift/blob/d380bf9ae361520f0c1f16519a199afe46ea99dc/lib/IRGen/GenMeta.cpp#L6578-L6581

Previously this test wasn't going through this code path on Windows, but now it does and it hits llvm_unreachable.

@egorzhdan egorzhdan merged commit 83a35a0 into release/6.0 May 17, 2024
5 checks passed
@egorzhdan egorzhdan deleted the egorzhdan/6.0-frt-array-metadata branch May 17, 2024 10:00
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.

None yet

2 participants