Skip to content

[cxx-interop] Fix memory layout for structs with out-of-order base types #78166

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 17, 2024

Conversation

egorzhdan
Copy link
Contributor

@egorzhdan egorzhdan commented Dec 13, 2024

In C++, a primary base class that is placed in the beginning of the type's memory layout isn't always the type that is the first in the list of bases – the base types might be laid out in memory in a different order.

This makes sure that IRGen handles base types of C++ structs in the correct order.

This fixes an assertion in asserts-enabled compilers, and an out-of-memory error in asserts-disabled compilers. The issue was happening for both value types and foreign reference types. This change also includes a small refactoring to reuse the logic between the two code paths.

rdar://140848603

@egorzhdan egorzhdan added the c++ interop Feature: Interoperability with C++ label Dec 13, 2024
@egorzhdan egorzhdan requested a review from j-hui December 13, 2024 17:01
@egorzhdan
Copy link
Contributor Author

@swift-ci please test

@fahadnayyar fahadnayyar self-requested a review December 13, 2024 17:23
@egorzhdan egorzhdan force-pushed the egorzhdan/derived-out-of-order branch from 0237526 to 729f6de Compare December 16, 2024 13:42
In C++, a primary base class that is placed in the beginning of the type's memory layout isn't always the type that is the first in the list of bases – the base types might be laid out in memory in a different order.

This makes sure that IRGen handles base types of C++ structs in the correct order.

This fixes an assertion in asserts-enabled compilers, and an out-of-memory error in asserts-disabled compilers. The issue was happening for both value types and foreign reference types. This change also includes a small refactoring to reuse the logic between the two code paths.

rdar://140848603
@egorzhdan egorzhdan force-pushed the egorzhdan/derived-out-of-order branch from 729f6de to 57c7ecd Compare December 16, 2024 15:54
@egorzhdan egorzhdan changed the title [cxx-interop] Fix IRGen for foreign reference types with multiple bases [cxx-interop] Fix memory layout for structs with out-of-order base types Dec 16, 2024
@egorzhdan
Copy link
Contributor Author

@swift-ci please test

@egorzhdan egorzhdan marked this pull request as ready for review December 16, 2024 15:56
Copy link
Contributor

@Xazax-hun Xazax-hun left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link
Contributor

@j-hui j-hui left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link
Contributor

@rjmccall rjmccall left a comment

Choose a reason for hiding this comment

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

LGTM

@egorzhdan egorzhdan merged commit 907ce4c into main Dec 17, 2024
5 checks passed
@egorzhdan egorzhdan deleted the egorzhdan/derived-out-of-order branch December 17, 2024 12:46
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.

4 participants