-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[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
Conversation
@swift-ci please smoke test |
69f7dba
to
c7eb1f7
Compare
@swift-ci please smoke test |
lib/IRGen/GenStruct.cpp
Outdated
auto fieldTypeSize = ClangContext.getTypeSizeInChars(clangField->getType()); | ||
auto fieldSize = isZeroSized | ||
? clang::CharUnits::Zero() | ||
: dataSize.value_or(Size(fieldTypeSize.getQuantity())); |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
c7eb1f7
to
a7c1744
Compare
@swift-ci please smoke test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
There was a problem hiding this 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!
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:rdar://151941799