Skip to content

UBSan: Fix unaligned load/stores found by Undefined Behaviour Sanitizer. #2522

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
Dec 12, 2019

Conversation

spevans
Copy link
Contributor

@spevans spevans commented Sep 27, 2019

  • Add unaligned_load32() / unaligned_store32() and
    unaligned_load{16,32,64}be() that load/store using byte access. On
    supported architectures clang should convert these into single
    load / store instructions.

  • When allocating buffers on the stack using STACK_BUFFER_DECL() ensure
    the length is > 0, as VLA (Variable Length Arrays) cannot be 0 length.

@spevans
Copy link
Contributor Author

spevans commented Sep 27, 2019

@swift-ci test

@spevans
Copy link
Contributor Author

spevans commented Sep 27, 2019

@swift-ci test macos

@@ -501,7 +501,7 @@ static void _flattenPlist(CFPropertyListRef plist, CFMutableArrayRef objlist, CF
CFDictionaryAddValue(objtable, plist, (const void *)(uintptr_t)refnum);
if (_kCFRuntimeIDCFDictionary == type) {
CFIndex count = CFDictionaryGetCount((CFDictionaryRef)plist);
STACK_BUFFER_DECL(CFPropertyListRef, buffer, count <= 128 ? count * 2 : 1);
STACK_BUFFER_DECL(CFPropertyListRef, buffer, (count > 0 && count <= 128) ? count * 2 : 1);
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this same check need to be repeated on the next line?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It shouldn't be needed because if count == 0 then the stack allocation is used instead of the malloc()'d buffer. 1 byte is wasted on the stack but this is also the case if count > 128 and the heap is used instead of the stack.

#endif

/* Read bytes until the buffer is aligned. */
while( ( (uintptr_t)bytes & align_mask ) && len > 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Style nit; let's make it match the rest of the file

- Add unaligned_load32() / unaligned_store32() and
  unaligned_load{16,32,64}be() that load/store using byte access. On
  supported architectures clang should convert these into single
  load / store instructions.

- When allocating buffers on the stack using STACK_BUFFER_DECL() ensure
  the length is > 0, as VLA (Variable Length Arrays) cannot be 0 length.
@spevans spevans force-pushed the pr_unaligned_access branch from 0230dbe to 73daa3e Compare September 28, 2019 06:52
@spevans
Copy link
Contributor Author

spevans commented Sep 28, 2019

@swift-ci test

1 similar comment
@spevans
Copy link
Contributor Author

spevans commented Sep 29, 2019

@swift-ci test

Copy link
Contributor

@parkera parkera left a comment

Choose a reason for hiding this comment

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

Seems reasonable to me; I'd like to get a second pair of eyes on here (@millenomi perhaps).

@spevans
Copy link
Contributor Author

spevans commented Dec 2, 2019

@millenomi @phausler would you be able to have a look over this if you have some time?

Copy link
Contributor

@parkera parkera left a comment

Choose a reason for hiding this comment

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

Seems reasonable to me

@spevans
Copy link
Contributor Author

spevans commented Dec 12, 2019

@swift-ci test and merge

@swift-ci swift-ci merged commit b45ce94 into swiftlang:master Dec 12, 2019
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