Skip to content

[cxx-interop] Fix a crash with [[no_unique_address]] #80786

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Xazax-hun
Copy link
Contributor

Swift does not support storing fields in the padding of the previous fields just yet, so let's not import fields like that from C++. Represent them as opaque blobs instead.

Fixes #80764

@Xazax-hun Xazax-hun added the c++ interop Feature: Interoperability with C++ label Apr 13, 2025
@Xazax-hun Xazax-hun force-pushed the gaborh/unique-address-padding branch from cf5df84 to 75838d6 Compare April 13, 2025 12:05
@Xazax-hun
Copy link
Contributor Author

@swift-ci please smoke test

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.

I'm wondering whether skipping this field is really the right approach, and whether there are other alternatives. Can you say more about why these overlapping fields are problematic for Swift at the moment?

@@ -24,6 +27,14 @@ struct HasZeroSizedField {
void set_c(short c) { this->c = c; }
};

struct ReuseFieldPadding {
[[no_unique_address]] std::optional<int> a;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add some getter/setter methods to ensure this field is still accessible via those?

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, can you add another test that doesn't really on the layout of std::optional, i.e., using your own padded type?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can you add some getter/setter methods to ensure this field is still accessible via those?

Added a getter.

Also, can you add another test that doesn't really on the layout of std::optional, i.e., using your own padded type?

I tried and failed. Not sure what is unique about std::optional but my couple of attempts to make the compiler do the same optimisation with my own type did not work. Will need to look deeper to figure out what is the difference because I tried to recreate the same layout.

decl->getASTContext().getASTRecordLayout(rd);
if (!decl->isZeroSize(decl->getASTContext()) &&
fieldLayout.getDataSize() != fieldLayout.getSize())
return nullptr;
Copy link
Contributor

Choose a reason for hiding this comment

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

What happens to derived conformances that rely on be presence of this field, eg Sendable or Equatable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. Unfortunately, we would not consider these fields for Sendable. Maybe we could conservatively always say types with unimported fields should always be not Sendable? We already have many fields that we do not import for one reason or another.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think Equatable might already be really problematic for C++ types. We do not generate metadata for private fields, so I think even if we can auto conform a type to that the generated implementation is probably faulty :/

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is not a problem for Equatable. Trying to conform a C++ the type to it gives this error message: "error: extension outside of file declaring struct 'ReuseFieldPadding' prevents automatic synthesis of '==' for protocol 'Equatable'". Unfortunately, I don't think we can do anything about the Sendable problem for now.

@Xazax-hun
Copy link
Contributor Author

I'm wondering whether skipping this field is really the right approach, and whether there are other alternatives. Can you say more about why these overlapping fields are problematic for Swift at the moment?

Absolutely, I would love to support this and that would be the right approach long term. We use FixedTypeInfo to represent the fields, and these classes and all the code using them have the underlying assumption that the sizeof(field) == sizeof(type). This invariant breaks with fields where the padding can be reused. So a significant portion of the code needs to be updated and tested for this to work. Should be doable, but it would be a significantly bigger change than this PR.

Swift does not support storing fields in the padding of the previous
fields just yet, so let's not import fields like that from C++.
Represent them as opaque blobs instead.

Fixes #80764
@Xazax-hun Xazax-hun force-pushed the gaborh/unique-address-padding branch from 75838d6 to 664efc6 Compare April 13, 2025 21:50
@Xazax-hun
Copy link
Contributor Author

@swift-ci please smoke test

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.

[cxx-interop] C++ class with [[no_unique_address]] member crashes IRGen
2 participants