Skip to content

[cxx-interop] Do not crash when emitting layout of std::string #81844

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
May 30, 2025

Conversation

egorzhdan
Copy link
Contributor

If a [[no_unique_address]] field has zero size according to Clang, and field has a type that isn't representable in Swift, Swift would previously try to add an opaque field of size 1 for it.

This is wrong and was causing crashes for std::string while emitting a padding field:

_LIBCPP_NO_UNIQUE_ADDRESS ::std::__compressed_pair_padding<T1> _LIBCPP_CONCAT3(__padding1_, __LINE__, _);

rdar://151941799

@egorzhdan egorzhdan requested a review from j-hui May 29, 2025 19:20
@egorzhdan egorzhdan added the c++ interop Feature: Interoperability with C++ label May 29, 2025
@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

@egorzhdan egorzhdan force-pushed the egorzhdan/zero-sized-no-unique-address branch from 69f7dba to c7eb1f7 Compare May 30, 2025 13:34
@egorzhdan egorzhdan marked this pull request as ready for review May 30, 2025 13:36
@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

auto fieldTypeSize = ClangContext.getTypeSizeInChars(clangField->getType());
auto fieldSize = isZeroSized
? clang::CharUnits::Zero()
: dataSize.value_or(Size(fieldTypeSize.getQuantity()));
Copy link
Contributor

Choose a reason for hiding this comment

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

What are the types involved here? Do we need to wrap fieldTypeSize.getQuantity() in Size?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Size is a type from the Swift codebase, and fieldTypeSize is coming from Clang, so these are different types.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, what confuses me is that the other branch of the expression is clang::CharUnits::Zero(). So what type does fieldSize end up having?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, I see what you mean. Yeah, let me simplify the types here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fieldSize is clang::CharUnits, which swift::Size implicitly converts to.

If a `[[no_unique_address]]` field has zero size according to Clang, and field has a type that isn't representable in Swift, Swift would previously try to add an opaque field of size 1 for it.

This is wrong and was causing crashes for `std::string` while emitting a padding field:
```
_LIBCPP_NO_UNIQUE_ADDRESS ::std::__compressed_pair_padding<T1> _LIBCPP_CONCAT3(__padding1_, __LINE__, _);
```

rdar://151941799
@egorzhdan egorzhdan force-pushed the egorzhdan/zero-sized-no-unique-address branch from c7eb1f7 to a7c1744 Compare May 30, 2025 16:32
@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

Copy link
Contributor

@hnrklssn hnrklssn 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.

Looks great, nice work on finding a small reproducer!

@egorzhdan egorzhdan enabled auto-merge May 30, 2025 18:30
@egorzhdan egorzhdan merged commit 284c371 into main May 30, 2025
3 checks passed
@egorzhdan egorzhdan deleted the egorzhdan/zero-sized-no-unique-address branch May 30, 2025 21:14
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