Skip to content

Fixed misalignment issue in SmallVector - #115

Open
chrhaase wants to merge 1 commit into
Tracktion:mainfrom
chrhaase:smallvector-alignment
Open

chrhaase wants to merge 1 commit into
Tracktion:mainfrom
chrhaase:smallvector-alignment

Conversation

@chrhaase

Copy link
Copy Markdown

Hey, I ran into some weird crashes that only happened on my old Intel Mac. After some digging found the issue in the choc::SmallVector.
This PR adds a simple fix and minimal test for verifying.

The internal storage is aligned to 8 bytes and sitting at 24 byte offset in the class, so a 16-byte aligned item type is likely to be misaligned.
As far as I know, the max default alignment on arm64 is 8 bytes, so usually not an issue there, but on x86_64 some types need 16 byte-alignment - hence the crashes.

Aligning the internal storage to the alignment of ElementType if it's greater than the ditto for uint64_t fixes it.
I have verified by running the tests on Intel. And on silicon the test triggers a ubsan violation without the fix.

Unfortunately, when using SmallVector with a 16-byte aligned type, MSVC warns that the alignment specifier results in padding, so I had to disable that warning. Not ideal, but I couldn't see a way around it.

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.

1 participant