Skip to content

Ensure all Datas are properly aligned#1883

Merged
jmschonfeld merged 2 commits intoswiftlang:mainfrom
jmschonfeld:empty-data-alignment
Apr 8, 2026
Merged

Ensure all Datas are properly aligned#1883
jmschonfeld merged 2 commits intoswiftlang:mainfrom
jmschonfeld:empty-data-alignment

Conversation

@jmschonfeld
Copy link
Copy Markdown
Contributor

Ensures Data pointers are always aligned to the largest integer size like malloc

Motivation:

It's reasonable to expect that a pointer provided by a Data is aligned to a sufficient value for storing any integer (this is the behavior of malloc). 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 by Data().withUnsafeBytes (which is provided due to historical compatibility reasons) but it is possible for a client to use it via withMemoryRebound without first checking count which will trap for alignment mismatches.

Modifications:

Ensures the stack pointers provided by withUnsafeBytes/withUnsafeMutableBytes are always 8-byte aligned

Result:

Pointers from Data can always be assumed to be 8-byte aligned

Resolves rdar://174244163

Testing:

Two additional unit tests added for the alignment of empty and inline data pointers (those pointers not created by malloc)

@jmschonfeld jmschonfeld requested a review from glessard April 7, 2026 20:56
@jmschonfeld jmschonfeld requested a review from a team as a code owner April 7, 2026 20:56
@jmschonfeld
Copy link
Copy Markdown
Contributor Author

@swift-ci please test

@jmschonfeld jmschonfeld force-pushed the empty-data-alignment branch from 5d25a41 to 8897d59 Compare April 7, 2026 23:16
@jmschonfeld
Copy link
Copy Markdown
Contributor Author

@swift-ci please test

Copy link
Copy Markdown
Contributor

@glessard glessard left a comment

Choose a reason for hiding this comment

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

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.

@jmschonfeld jmschonfeld merged commit 503dc5a into swiftlang:main Apr 8, 2026
22 checks passed
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.

3 participants