Skip to content

stdlib: fix the problem swept under the rug in 58a97f1603 #78895

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
Feb 4, 2025

Conversation

compnerd
Copy link
Member

The -Winvalid-offsetof warning is valid in this case. offsetof is being applied to types with a non-standard layout. The layout of this type is undefined by the specification. There is no guarantee that the type layout is uniform across all ABIs. It is not possible to portably compute the offset statically, especially efficiently.

@compnerd
Copy link
Member Author

@swift-ci please test

@al45tair
Copy link
Contributor

I'm not sure I'm sold on this; I appreciate that the code you're replacing here is UB, but I don't think we necessarily want to replace it with a runtime test in the constructor that's allegedly inefficient in some cases.

If we're going to replace it with a runtime test, then IMO that test belongs in the test suite somewhere; we don't want to end up potentially paying a runtime cost every time we construct one of these objects.

@compnerd
Copy link
Member Author

I'm fine with this going to a test. However, I think that I would rather that we do this once with a global constructor. I'll be following up with that soon.

@al45tair
Copy link
Contributor

However, I think that I would rather that we do this once with a global constructor.

We don't really like global constructors — they cost us start-up time, so we generally try to avoid them. I know there are some, and I also know I added some of them myself, but I'd be strongly against using one to check a type at runtime that we could test in a unit test instead.

The right place for this kind of check if we can't do it statically at compile time is in the test suite.

@compnerd
Copy link
Member Author

I suppose that I am unsure how to do that in a unit test. I don't have a problem with this being sunk outside of the runtime.

@compnerd
Copy link
Member Author

@swift-ci please test

@compnerd
Copy link
Member Author

@swift-ci please test Windows platform

@compnerd
Copy link
Member Author

@swift-ci please test

Copy link
Contributor

@al45tair al45tair left a comment

Choose a reason for hiding this comment

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

LGTM.

Comment on lines +82 to +83
// error: 'Value' is a private member of
// 'swift::VariadicMetadataCacheEntryBase<swift::GenericCacheEntry>'
Copy link
Contributor

Choose a reason for hiding this comment

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

Could probably use a friend class to work around that (e.g. add friend class MetadataCacheEntryTesting to VariadicMetadataCacheEntryBase then define the class in unittests/runtime/TypeLayoutChecks.cpp and have that contain a function that does the ASSERT_EQ or maybe just the offset_of calculations.

Copy link
Member Author

Choose a reason for hiding this comment

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

The inline function definition sounds interesting - I think that doing that as a follow up makes sense.

The `-Winvalid-offsetof` warning is valid in this case. `offsetof` is
being applied to types with a non-standard layout. The layout of this
type is undefined by the specification. There is no guarantee that the
type layout is uniform across all ABIs. It is not possible to portably
compute the offset statically, especially efficiently.

Sink this check into a unit test to avoid performing this test at
runtime. In order to do this in the standard library, we would need to
do this check through a global constructor.
@compnerd
Copy link
Member Author

compnerd commented Feb 3, 2025

@swift-ci please smoke test

@compnerd compnerd enabled auto-merge February 3, 2025 17:25
@compnerd
Copy link
Member Author

compnerd commented Feb 3, 2025

@swift-ci please smoke test macOS platform

@compnerd compnerd merged commit d450bda into swiftlang:main Feb 4, 2025
3 checks passed
@compnerd compnerd deleted the dynamic-offsets branch February 4, 2025 00:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants