Ensure all Datas are properly aligned#1883
Merged
jmschonfeld merged 2 commits intoswiftlang:mainfrom Apr 8, 2026
Merged
Conversation
Contributor
Author
|
@swift-ci please test |
kperryua
approved these changes
Apr 7, 2026
glessard
reviewed
Apr 7, 2026
5d25a41 to
8897d59
Compare
Contributor
Author
|
@swift-ci please test |
glessard
approved these changes
Apr 7, 2026
Contributor
glessard
left a comment
There was a problem hiding this comment.
There's a slight possibility that the instance we get from case .inline could in the future stop having the same alignment as the Representation enum, but that doesn't seem likely. The new test would let us know if that were to happen.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Ensures
Datapointers are always aligned to the largest integer size likemallocMotivation:
It's reasonable to expect that a pointer provided by a
Datais aligned to a sufficient value for storing any integer (this is the behavior ofmalloc). In an edge case for the empty data, we don't actually guarantee this today. You can't actually access the values at the pointer provided byData().withUnsafeBytes(which is provided due to historical compatibility reasons) but it is possible for a client to use it viawithMemoryReboundwithout first checking count which will trap for alignment mismatches.Modifications:
Ensures the stack pointers provided by
withUnsafeBytes/withUnsafeMutableBytesare always 8-byte alignedResult:
Pointers from
Datacan always be assumed to be 8-byte alignedResolves rdar://174244163
Testing:
Two additional unit tests added for the alignment of empty and inline data pointers (those pointers not created by
malloc)